Mercurial > gftp.yaz
annotate lib/rfc2068.c @ 478:97fd5eb34be0
2004-6-6 Brian Masney <masneyb@gftp.org>
* acinclude.m4 configure.in - added AC_TYPE_INTPTR_T that will define
intptr_t if it does not exist on the system
* lib/gftp.h lib/protocols.c lib/rfc959.c
src/uicommon/gftpuicallbacks.c - added specify site argument to
rfc959_site(). If this is set, then SITE will be prepended to the
command
author | masneyb |
---|---|
date | Sun, 06 Jun 2004 15:12:35 +0000 |
parents | 075f89b4395c |
children | 7334a74db6f6 |
rev | line source |
---|---|
1 | 1 /*****************************************************************************/ |
2 /* rfc2068.c - General purpose routines for the HTTP protocol */ | |
122 | 3 /* Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org> */ |
1 | 4 /* */ |
5 /* This program is free software; you can redistribute it and/or modify */ | |
6 /* it under the terms of the GNU General Public License as published by */ | |
7 /* the Free Software Foundation; either version 2 of the License, or */ | |
8 /* (at your option) any later version. */ | |
9 /* */ | |
10 /* This program is distributed in the hope that it will be useful, */ | |
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ | |
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */ | |
13 /* GNU General Public License for more details. */ | |
14 /* */ | |
15 /* You should have received a copy of the GNU General Public License */ | |
16 /* along with this program; if not, write to the Free Software */ | |
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */ | |
18 /*****************************************************************************/ | |
19 | |
20 #include "gftp.h" | |
168 | 21 #include "httpcommon.h" |
22 | |
33 | 23 static const char cvsid[] = "$Id$"; |
1 | 24 |
122 | 25 static gftp_config_vars config_vars[] = |
26 { | |
227 | 27 {"", N_("HTTP"), gftp_option_type_notebook, NULL, NULL, |
28 GFTP_CVARS_FLAGS_SHOW_BOOKMARK, NULL, GFTP_PORT_GTK, NULL}, | |
122 | 29 |
30 {"http_proxy_host", N_("Proxy hostname:"), | |
31 gftp_option_type_text, "", NULL, 0, | |
32 N_("Firewall hostname"), GFTP_PORT_ALL, 0}, | |
33 {"http_proxy_port", N_("Proxy port:"), | |
34 gftp_option_type_int, GINT_TO_POINTER(80), NULL, 0, | |
35 N_("Port to connect to on the firewall"), GFTP_PORT_ALL, NULL}, | |
36 {"http_proxy_username", N_("Proxy username:"), | |
37 gftp_option_type_text, "", NULL, 0, | |
38 N_("Your firewall username"), GFTP_PORT_ALL, NULL}, | |
39 {"http_proxy_password", N_("Proxy password:"), | |
40 gftp_option_type_hidetext, "", NULL, 0, | |
41 N_("Your firewall password"), GFTP_PORT_ALL, NULL}, | |
42 | |
43 {"use_http11", N_("Use HTTP/1.1"), | |
227 | 44 gftp_option_type_checkbox, GINT_TO_POINTER(1), NULL, |
45 GFTP_CVARS_FLAGS_SHOW_BOOKMARK, | |
122 | 46 N_("Do you want to use HTTP/1.1 or HTTP/1.0"), GFTP_PORT_ALL, NULL}, |
227 | 47 |
122 | 48 {NULL, NULL, 0, NULL, NULL, 0, NULL, 0, NULL} |
49 }; | |
50 | |
48 | 51 |
219 | 52 static int |
53 rfc2068_connect (gftp_request * request) | |
54 { | |
55 char *proxy_hostname, *proxy_config; | |
56 rfc2068_params * params; | |
325 | 57 intptr_t proxy_port; |
58 int ret; | |
219 | 59 |
60 g_return_val_if_fail (request != NULL, GFTP_EFATAL); | |
61 g_return_val_if_fail (request->hostname != NULL, GFTP_EFATAL); | |
62 | |
63 params = request->protocol_data; | |
64 | |
65 if (request->datafd > 0) | |
66 return (0); | |
67 | |
68 gftp_lookup_request_option (request, "proxy_config", &proxy_config); | |
69 gftp_lookup_request_option (request, "http_proxy_host", &proxy_hostname); | |
70 gftp_lookup_request_option (request, "http_proxy_port", &proxy_port); | |
71 | |
72 if (proxy_config != NULL && strcmp (proxy_config, "ftp") == 0) | |
73 { | |
74 g_free (request->url_prefix); | |
75 request->url_prefix = g_strdup ("ftp"); | |
76 } | |
77 | |
78 if ((ret = gftp_connect_server (request, request->url_prefix, proxy_hostname, | |
79 proxy_port)) < 0) | |
80 return (ret); | |
81 | |
82 if (request->directory && *request->directory == '\0') | |
83 { | |
84 g_free (request->directory); | |
85 request->directory = NULL; | |
86 } | |
87 | |
88 if (!request->directory) | |
89 request->directory = g_strdup ("/"); | |
90 | |
91 return (0); | |
92 } | |
93 | |
94 | |
58 | 95 static off_t |
48 | 96 rfc2068_read_response (gftp_request * request) |
97 { | |
98 rfc2068_params * params; | |
207 | 99 char tempstr[8192]; |
168 | 100 int ret, chunked; |
48 | 101 |
102 params = request->protocol_data; | |
209 | 103 *tempstr = '\0'; |
168 | 104 chunked = 0; |
105 params->chunk_size = 0; | |
106 params->content_length = 0; | |
209 | 107 params->eof = 0; |
207 | 108 |
105 | 109 if (request->last_ftp_response) |
110 { | |
111 g_free (request->last_ftp_response); | |
112 request->last_ftp_response = NULL; | |
113 } | |
48 | 114 |
207 | 115 if (params->extra_read_buffer != NULL) |
116 { | |
117 g_free (params->extra_read_buffer); | |
118 params->extra_read_buffer = NULL; | |
209 | 119 params->extra_read_buffer_len = 0; |
207 | 120 } |
121 | |
105 | 122 do |
123 { | |
168 | 124 if ((ret = gftp_get_line (request, ¶ms->rbuf, tempstr, |
169 | 125 sizeof (tempstr), request->datafd)) < 0) |
105 | 126 return (ret); |
48 | 127 |
105 | 128 if (request->last_ftp_response == NULL) |
129 request->last_ftp_response = g_strdup (tempstr); | |
130 | |
131 if (*tempstr != '\0') | |
132 { | |
186 | 133 request->logging_function (gftp_logging_recv, request, "%s\n", |
105 | 134 tempstr); |
48 | 135 |
105 | 136 if (strncmp (tempstr, "Content-Length:", 15) == 0) |
244 | 137 params->content_length = gftp_parse_file_size (tempstr + 16); |
168 | 138 else if (strcmp (tempstr, "Transfer-Encoding: chunked") == 0) |
139 chunked = 1; | |
105 | 140 } |
48 | 141 } |
105 | 142 while (*tempstr != '\0'); |
48 | 143 |
168 | 144 if (chunked) |
145 { | |
146 if ((ret = gftp_get_line (request, ¶ms->rbuf, tempstr, | |
169 | 147 sizeof (tempstr), request->datafd)) < 0) |
168 | 148 return (ret); |
149 | |
150 if (sscanf ((char *) tempstr, "%lx", ¶ms->chunk_size) != 1) | |
151 { | |
186 | 152 request->logging_function (gftp_logging_recv, request, |
209 | 153 _("Received wrong response from server, disconnecting\nInvalid chunk size '%s' returned by the remote server\n"), |
154 tempstr); | |
168 | 155 gftp_disconnect (request); |
156 return (GFTP_EFATAL); | |
157 } | |
158 | |
159 if (params->chunk_size == 0) | |
160 return (0); | |
161 | |
162 params->chunk_size -= params->rbuf->cur_bufsize; | |
163 if (params->chunk_size < 0) | |
164 { | |
207 | 165 params->extra_read_buffer_len = params->chunk_size * -1; |
209 | 166 params->chunk_size = 0; |
207 | 167 params->extra_read_buffer = g_malloc (params->extra_read_buffer_len + 1); |
168 memcpy (params->extra_read_buffer, params->rbuf->curpos + (params->rbuf->cur_bufsize - params->extra_read_buffer_len), params->extra_read_buffer_len); | |
169 params->extra_read_buffer[params->extra_read_buffer_len] = '\0'; | |
170 params->rbuf->cur_bufsize -= params->extra_read_buffer_len; | |
209 | 171 params->rbuf->curpos[params->rbuf->cur_bufsize] = '\0'; |
168 | 172 } |
173 } | |
174 | |
175 params->chunked_transfer = chunked; | |
176 return (params->content_length > 0 ? params->content_length : params->chunk_size); | |
48 | 177 } |
178 | |
179 | |
58 | 180 static off_t |
168 | 181 rfc2068_send_command (gftp_request * request, const void *command, size_t len) |
48 | 182 { |
122 | 183 char *tempstr, *str, *proxy_hostname, *proxy_username, *proxy_password; |
325 | 184 intptr_t proxy_port; |
185 int conn_ret; | |
58 | 186 ssize_t ret; |
48 | 187 |
84 | 188 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
189 g_return_val_if_fail (command != NULL, GFTP_EFATAL); | |
48 | 190 |
219 | 191 if (request->datafd < 0 && (conn_ret = rfc2068_connect (request)) != 0) |
192 return (conn_ret); | |
193 | |
168 | 194 tempstr = g_strdup_printf ("%sUser-Agent: %s\nHost: %s\n", (char *) command, |
122 | 195 gftp_version, request->hostname); |
48 | 196 |
186 | 197 request->logging_function (gftp_logging_send, request, |
58 | 198 "%s", tempstr); |
199 | |
168 | 200 ret = request->write_function (request, tempstr, strlen (tempstr), |
169 | 201 request->datafd); |
58 | 202 g_free (tempstr); |
203 | |
204 if (ret < 0) | |
84 | 205 return (ret); |
48 | 206 |
122 | 207 gftp_lookup_request_option (request, "http_proxy_host", &proxy_hostname); |
208 gftp_lookup_request_option (request, "http_proxy_port", &proxy_port); | |
209 gftp_lookup_request_option (request, "http_proxy_username", &proxy_username); | |
210 gftp_lookup_request_option (request, "http_proxy_password", &proxy_password); | |
211 | |
212 if (request->use_proxy && proxy_username != NULL && *proxy_username != '\0') | |
48 | 213 { |
122 | 214 tempstr = g_strconcat (proxy_username, ":", proxy_password, NULL); |
48 | 215 str = base64_encode (tempstr); |
216 g_free (tempstr); | |
217 | |
186 | 218 request->logging_function (gftp_logging_send, request, |
48 | 219 "Proxy-authorization: Basic xxxx:xxxx\n"); |
169 | 220 ret = gftp_writefmt (request, request->datafd, |
58 | 221 "Proxy-authorization: Basic %s\n", str); |
48 | 222 g_free (str); |
58 | 223 if (ret < 0) |
168 | 224 return (ret); |
48 | 225 } |
226 | |
227 if (request->username != NULL && *request->username != '\0') | |
228 { | |
229 tempstr = g_strconcat (request->username, ":", request->password, NULL); | |
230 str = base64_encode (tempstr); | |
231 g_free (tempstr); | |
232 | |
186 | 233 request->logging_function (gftp_logging_send, request, |
48 | 234 "Authorization: Basic xxxx\n"); |
169 | 235 ret = gftp_writefmt (request, request->datafd, |
58 | 236 "Authorization: Basic %s\n", str); |
48 | 237 g_free (str); |
58 | 238 if (ret < 0) |
84 | 239 return (ret); |
48 | 240 } |
241 | |
169 | 242 if ((ret = request->write_function (request, "\n", 1, request->datafd)) < 0) |
84 | 243 return (ret); |
58 | 244 |
48 | 245 return (rfc2068_read_response (request)); |
1 | 246 } |
247 | |
248 | |
249 static void | |
250 rfc2068_disconnect (gftp_request * request) | |
251 { | |
252 g_return_if_fail (request != NULL); | |
253 | |
169 | 254 if (request->datafd > 0) |
1 | 255 { |
186 | 256 request->logging_function (gftp_logging_misc, request, |
1 | 257 _("Disconnecting from site %s\n"), |
258 request->hostname); | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
259 |
169 | 260 if (close (request->datafd) < 0) |
186 | 261 request->logging_function (gftp_logging_error, request, |
58 | 262 _("Error closing file descriptor: %s\n"), |
263 g_strerror (errno)); | |
264 | |
169 | 265 request->datafd = -1; |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
266 } |
1 | 267 } |
268 | |
269 | |
58 | 270 static off_t |
271 rfc2068_get_file (gftp_request * request, const char *filename, int fd, | |
1 | 272 off_t startsize) |
273 { | |
303 | 274 char *tempstr, *oldstr, *hf; |
460 | 275 rfc2068_params * params; |
325 | 276 intptr_t use_http11; |
460 | 277 int restarted; |
278 size_t len; | |
1 | 279 off_t size; |
280 | |
84 | 281 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
282 g_return_val_if_fail (filename != NULL, GFTP_EFATAL); | |
1 | 283 |
122 | 284 params = request->protocol_data; |
285 | |
286 gftp_lookup_request_option (request, "use_http11", &use_http11); | |
287 | |
58 | 288 if (fd > 0) |
169 | 289 request->datafd = fd; |
1 | 290 |
303 | 291 hf = gftp_build_path (request->hostname, filename, NULL); |
292 | |
7 | 293 if (request->username == NULL || *request->username == '\0') |
303 | 294 tempstr = g_strconcat ("GET ", request->url_prefix, "://", hf, |
1 | 295 use_http11 ? " HTTP/1.1\n" : " HTTP/1.0\n", NULL); |
296 else | |
122 | 297 tempstr = g_strconcat ("GET ", request->url_prefix, "://", |
303 | 298 request->username, "@", hf, |
1 | 299 use_http11 ? " HTTP/1.1\n" : " HTTP/1.0\n", NULL); |
300 | |
303 | 301 g_free (hf); |
1 | 302 |
168 | 303 if (use_http11 && startsize > 0) |
1 | 304 { |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
305 #if defined (_LARGEFILE_SOURCE) |
186 | 306 request->logging_function (gftp_logging_misc, request, |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
307 _("Starting the file transfer at offset %lld\n"), |
372 | 308 (long long) startsize); |
168 | 309 |
310 oldstr = tempstr; | |
311 tempstr = g_strdup_printf ("%sRange: bytes=%lld-\n", tempstr, startsize); | |
312 g_free (oldstr); | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
313 #else |
186 | 314 request->logging_function (gftp_logging_misc, request, |
1 | 315 _("Starting the file transfer at offset %ld\n"), |
316 startsize); | |
168 | 317 |
318 oldstr = tempstr; | |
319 tempstr = g_strdup_printf ("%sRange: bytes=%ld-\n", tempstr, startsize); | |
320 g_free (oldstr); | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
321 #endif |
1 | 322 } |
323 | |
168 | 324 size = rfc2068_send_command (request, tempstr, strlen (tempstr)); |
1 | 325 g_free (tempstr); |
58 | 326 if (size < 0) |
84 | 327 return (size); |
1 | 328 |
329 restarted = 0; | |
460 | 330 len = strlen (request->last_ftp_response); |
331 if (len > 9 && strncmp (request->last_ftp_response + 9, "206", 3) == 0) | |
1 | 332 restarted = 1; |
460 | 333 else if (len < 9 || strncmp (request->last_ftp_response + 9, "200", 3) != 0) |
1 | 334 { |
186 | 335 request->logging_function (gftp_logging_error, request, |
1 | 336 _("Cannot retrieve file %s\n"), filename); |
84 | 337 return (GFTP_ERETRYABLE); |
1 | 338 } |
339 | |
168 | 340 params->read_bytes = 0; |
341 | |
1 | 342 return (restarted ? size + startsize : size); |
343 } | |
344 | |
345 | |
58 | 346 static ssize_t |
1 | 347 rfc2068_get_next_file_chunk (gftp_request * request, char *buf, size_t size) |
348 { | |
349 rfc2068_params * params; | |
350 size_t len; | |
351 | |
84 | 352 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 353 |
354 params = request->protocol_data; | |
304 | 355 if (params->rbuf != NULL && params->rbuf->curpos != NULL) |
356 { | |
357 len = params->rbuf->cur_bufsize > size ? size : params->rbuf->cur_bufsize; | |
358 memcpy (buf, params->rbuf->curpos, len); | |
1 | 359 |
304 | 360 if (len == params->rbuf->cur_bufsize) |
361 gftp_free_getline_buffer (¶ms->rbuf); | |
362 else | |
363 { | |
364 params->rbuf->curpos += len; | |
365 params->rbuf->cur_bufsize -= len; | |
366 } | |
1 | 367 |
304 | 368 return (len); |
369 } | |
370 | |
371 return (request->read_function (request, buf, size, request->datafd)); | |
1 | 372 } |
373 | |
374 | |
375 static int | |
376 rfc2068_end_transfer (gftp_request * request) | |
377 { | |
378 rfc2068_params * params; | |
379 | |
84 | 380 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 381 |
169 | 382 if (request->datafd < 0) |
84 | 383 return (GFTP_EFATAL); |
1 | 384 |
207 | 385 gftp_disconnect (request); |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
386 |
1 | 387 params = request->protocol_data; |
168 | 388 params->content_length = 0; |
389 params->chunked_transfer = 0; | |
390 params->chunk_size = 0; | |
209 | 391 params->eof = 0; |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
392 |
1 | 393 return (0); |
394 } | |
395 | |
396 | |
397 static int | |
398 rfc2068_list_files (gftp_request * request) | |
399 { | |
400 rfc2068_params *params; | |
303 | 401 char *tempstr, *hd; |
325 | 402 intptr_t use_http11; |
58 | 403 off_t ret; |
1 | 404 |
84 | 405 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 406 |
407 params = request->protocol_data; | |
122 | 408 gftp_lookup_request_option (request, "use_http11", &use_http11); |
409 | |
369 | 410 if (strncmp (request->directory, "/", strlen (request->directory)) == 0) |
411 hd = g_strdup (request->hostname); | |
412 else | |
413 hd = gftp_build_path (request->hostname, request->directory, NULL); | |
303 | 414 |
7 | 415 if (request->username == NULL || *request->username == '\0') |
303 | 416 tempstr = g_strconcat ("GET ", request->url_prefix, "://", hd, |
1 | 417 use_http11 ? "/ HTTP/1.1\n" : "/ HTTP/1.0\n", NULL); |
418 else | |
122 | 419 tempstr = g_strconcat ("GET ", request->url_prefix, "://", |
303 | 420 request->username, "@", hd, |
1 | 421 use_http11 ? "/ HTTP/1.1\n" : "/ HTTP/1.0\n", NULL); |
422 | |
303 | 423 g_free (hd); |
1 | 424 |
168 | 425 ret = rfc2068_send_command (request, tempstr, strlen (tempstr)); |
1 | 426 g_free (tempstr); |
58 | 427 if (ret < 0) |
84 | 428 return ((int) ret); |
1 | 429 |
430 params->read_bytes = 0; | |
431 if (strlen (request->last_ftp_response) > 9 && | |
432 strncmp (request->last_ftp_response + 9, "200", 3) == 0) | |
433 { | |
186 | 434 request->logging_function (gftp_logging_misc, request, |
1 | 435 _("Retrieving directory listing...\n")); |
436 return (0); | |
437 } | |
168 | 438 |
207 | 439 gftp_end_transfer (request); |
440 | |
84 | 441 return (GFTP_ERETRYABLE); |
1 | 442 } |
443 | |
444 | |
445 static off_t | |
446 rfc2068_get_file_size (gftp_request * request, const char *filename) | |
447 { | |
122 | 448 rfc2068_params *params; |
303 | 449 char *tempstr, *hf; |
325 | 450 intptr_t use_http11; |
58 | 451 off_t size; |
1 | 452 |
84 | 453 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
454 g_return_val_if_fail (filename != NULL, GFTP_EFATAL); | |
1 | 455 |
122 | 456 params = request->protocol_data; |
457 gftp_lookup_request_option (request, "use_http11", &use_http11); | |
458 | |
303 | 459 hf = gftp_build_path (request->hostname, filename, NULL); |
460 | |
7 | 461 if (request->username == NULL || *request->username == '\0') |
303 | 462 tempstr = g_strconcat ("HEAD ", request->url_prefix, "://", hf, |
1 | 463 use_http11 ? " HTTP/1.1\n" : " HTTP/1.0\n", NULL); |
464 else | |
219 | 465 tempstr = g_strconcat ("HEAD ", request->url_prefix, "://", |
303 | 466 request->username, "@", hf, |
1 | 467 use_http11 ? " HTTP/1.1\n" : " HTTP/1.0\n", NULL); |
468 | |
303 | 469 g_free (hf); |
1 | 470 |
168 | 471 size = rfc2068_send_command (request, tempstr, strlen (tempstr)); |
1 | 472 g_free (tempstr); |
473 return (size); | |
474 } | |
475 | |
476 | |
477 static int | |
478 parse_html_line (char *tempstr, gftp_file * fle) | |
479 { | |
207 | 480 char *stpos, *kpos, *mpos, *pos; |
1 | 481 long units; |
482 | |
483 memset (fle, 0, sizeof (*fle)); | |
484 | |
485 if ((pos = strstr (tempstr, "<A HREF=")) == NULL && | |
486 (pos = strstr (tempstr, "<a href=")) == NULL) | |
487 return (0); | |
488 | |
489 /* Find the filename */ | |
490 while (*pos != '"' && *pos != '\0') | |
491 pos++; | |
492 if (*pos == '\0') | |
493 return (0); | |
494 pos++; | |
495 | |
496 for (stpos = pos; *pos != '"' && *pos != '\0'; pos++); | |
497 if (*pos == '\0') | |
498 return (0); | |
499 *pos = '\0'; | |
500 | |
501 /* Copy file attributes. Just about the only thing we can get is whether it | |
502 is a directory or not */ | |
503 if (*(pos - 1) == '/') | |
504 { | |
336 | 505 fle->attribs = g_strdup ("drwxr-xr-x"); |
1 | 506 *(pos - 1) = '\0'; |
507 fle->isdir = 1; | |
508 } | |
336 | 509 else |
510 fle->attribs = g_strdup ("-rw-r--r--"); | |
1 | 511 |
512 /* Copy filename */ | |
513 if (strchr (stpos, '/') != NULL || strncmp (stpos, "mailto:", 7) == 0 || | |
514 *stpos == '\0' || *stpos == '?') | |
515 return (0); | |
516 | |
105 | 517 fle->file = g_strdup (stpos); |
1 | 518 |
519 if (*(pos - 1) == '\0') | |
520 *(pos - 1) = '/'; | |
521 *pos = '"'; | |
522 pos++; | |
523 | |
524 /* Skip whitespace and html tags after file and before date */ | |
525 stpos = pos; | |
526 if ((pos = strstr (stpos, "</A>")) == NULL && | |
527 (pos = strstr (stpos, "</a>")) == NULL) | |
528 return (0); | |
529 | |
530 pos += 4; | |
531 | |
105 | 532 while (*pos == ' ' || *pos == '\t' || *pos == '.' || *pos == '<') |
1 | 533 { |
534 if (*pos == '<') | |
535 { | |
536 if (strncmp (pos, "<A ", 3) == 0 || strncmp (pos, "<a ", 3) == 0) | |
537 { | |
538 stpos = pos; | |
539 if ((pos = strstr (stpos, "</A>")) == NULL | |
540 && (pos = strstr (stpos, "</a>")) == NULL) | |
541 return (0); | |
542 pos += 4; | |
543 } | |
544 else | |
545 { | |
546 while (*pos != '>' && *pos != '\0') | |
547 pos++; | |
548 if (*pos == '\0') | |
549 return (0); | |
550 } | |
551 } | |
552 pos++; | |
553 } | |
554 | |
102 | 555 if (*pos == '[') |
556 pos++; | |
1 | 557 |
102 | 558 fle->datetime = parse_time (pos, &pos); |
1 | 559 |
168 | 560 fle->user = g_strdup (_("<unknown>")); |
561 fle->group = g_strdup (_("<unknown>")); | |
562 | |
105 | 563 if (pos == NULL) |
564 return (1); | |
565 | |
1 | 566 while (*pos == ' ' || *pos == ']') |
567 pos++; | |
568 | |
569 /* Get the size */ | |
207 | 570 |
571 kpos = strchr (pos, 'k'); | |
572 mpos = strchr (pos, 'M'); | |
427 | 573 |
574 if (kpos != NULL && (mpos == NULL || (kpos < mpos))) | |
207 | 575 stpos = kpos; |
576 else | |
577 stpos = mpos; | |
578 | |
1 | 579 if (stpos == NULL || !isdigit (*(stpos - 1))) |
580 return (1); /* Return successfully | |
581 since we got the file */ | |
207 | 582 |
1 | 583 if (*stpos == 'k') |
584 units = 1024; | |
585 else | |
586 units = 1048576; | |
587 | |
588 fle->size = 0; | |
589 | |
590 while (*(stpos - 1) != ' ' && *(stpos - 1) != '\t' && stpos > tempstr) | |
591 { | |
592 stpos--; | |
207 | 593 if ((*stpos == '.') && isdigit (*(stpos + 1))) |
1 | 594 { /* found decimal point */ |
244 | 595 fle->size = units * gftp_parse_file_size (stpos + 1) / 10;; |
1 | 596 } |
597 } | |
598 | |
244 | 599 fle->size += units * gftp_parse_file_size (stpos); |
168 | 600 |
1 | 601 return (1); |
602 } | |
603 | |
604 | |
168 | 605 int |
58 | 606 rfc2068_get_next_file (gftp_request * request, gftp_file * fle, int fd) |
1 | 607 { |
48 | 608 rfc2068_params * params; |
219 | 609 char tempstr[8192]; |
168 | 610 ssize_t len; |
84 | 611 int ret; |
1 | 612 |
84 | 613 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
614 g_return_val_if_fail (fle != NULL, GFTP_EFATAL); | |
48 | 615 |
616 params = request->protocol_data; | |
617 if (request->last_dir_entry) | |
618 { | |
619 g_free (request->last_dir_entry); | |
620 request->last_dir_entry = NULL; | |
621 } | |
622 | |
105 | 623 if (fd < 0) |
169 | 624 fd = request->datafd; |
105 | 625 |
48 | 626 while (1) |
627 { | |
168 | 628 if ((ret = gftp_get_line (request, ¶ms->rbuf, tempstr, sizeof (tempstr), fd)) <= 0) |
84 | 629 return (ret); |
1 | 630 |
48 | 631 if (parse_html_line (tempstr, fle) == 0 || fle->file == NULL) |
632 gftp_file_destroy (fle); | |
633 else | |
634 break; | |
635 } | |
636 | |
637 if (fle->file == NULL) | |
1 | 638 { |
48 | 639 gftp_file_destroy (fle); |
105 | 640 return (0); |
1 | 641 } |
642 | |
48 | 643 len = strlen (tempstr); |
644 if (!request->cached) | |
645 { | |
60 | 646 request->last_dir_entry = g_strdup_printf ("%s\n", tempstr); |
647 request->last_dir_entry_len = len + 1; | |
48 | 648 } |
219 | 649 |
58 | 650 return (len); |
48 | 651 } |
1 | 652 |
653 | |
48 | 654 static int |
655 rfc2068_chdir (gftp_request * request, const char *directory) | |
656 { | |
204 | 657 char *tempstr, *olddir; |
658 | |
84 | 659 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
660 g_return_val_if_fail (directory != NULL, GFTP_EFATAL); | |
48 | 661 |
662 if (request->directory != directory) | |
663 { | |
204 | 664 olddir = request->directory; |
665 | |
219 | 666 if (*directory != '/' && request->directory != NULL) |
204 | 667 { |
668 tempstr = g_strconcat (request->directory, "/", directory, NULL); | |
669 request->directory = expand_path (tempstr); | |
670 g_free (tempstr); | |
671 } | |
672 else | |
673 request->directory = expand_path (directory); | |
674 | |
675 if (olddir != NULL) | |
676 g_free (olddir); | |
48 | 677 } |
678 return (0); | |
679 } | |
680 | |
681 | |
177 | 682 static int |
58 | 683 rfc2068_set_config_options (gftp_request * request) |
684 { | |
177 | 685 return (0); |
58 | 686 } |
687 | |
688 | |
87 | 689 static void |
690 rfc2068_destroy (gftp_request * request) | |
691 { | |
207 | 692 rfc2068_params * params; |
693 | |
694 params = request->protocol_data; | |
695 | |
87 | 696 if (request->url_prefix) |
697 { | |
698 g_free (request->url_prefix); | |
699 request->url_prefix = NULL; | |
700 } | |
207 | 701 |
702 if (params->rbuf != NULL) | |
703 gftp_free_getline_buffer (¶ms->rbuf); | |
704 | |
705 if (params->extra_read_buffer != NULL) | |
706 { | |
707 g_free (params->extra_read_buffer); | |
708 params->extra_read_buffer = NULL; | |
209 | 709 params->extra_read_buffer_len = 0; |
207 | 710 } |
87 | 711 } |
712 | |
713 | |
168 | 714 static ssize_t |
715 rfc2068_chunked_read (gftp_request * request, void *ptr, size_t size, int fd) | |
716 { | |
303 | 717 size_t read_size, begin_ptr_len, current_size, crlfsize; |
718 rfc2068_params * params; | |
215 | 719 char *stpos, *crlfpos; |
209 | 720 void *read_ptr_pos; |
168 | 721 ssize_t retval; |
722 | |
723 params = request->protocol_data; | |
303 | 724 params->read_ref_cnt++; |
168 | 725 |
207 | 726 if (params->extra_read_buffer != NULL) |
168 | 727 { |
209 | 728 g_return_val_if_fail (params->extra_read_buffer_len <= size, GFTP_EFATAL); |
729 | |
730 memcpy (ptr, params->extra_read_buffer, params->extra_read_buffer_len); | |
731 | |
732 begin_ptr_len = params->extra_read_buffer_len; | |
733 read_ptr_pos = (char *) ptr + begin_ptr_len; | |
734 | |
735 /* Check for end of chunk */ | |
736 if (begin_ptr_len > 5 && strncmp ("\r\n0\r\n", (char *) ptr, 5) == 0) | |
737 read_size = 0; | |
738 else | |
739 read_size = size - begin_ptr_len; | |
740 | |
741 g_free (params->extra_read_buffer); | |
742 params->extra_read_buffer = NULL; | |
743 params->extra_read_buffer_len = 0; | |
207 | 744 } |
745 else | |
746 { | |
209 | 747 begin_ptr_len = 0; |
748 read_ptr_pos = ptr; | |
749 | |
207 | 750 read_size = size; |
751 if (params->content_length > 0) | |
752 { | |
753 if (params->content_length == params->read_bytes) | |
303 | 754 { |
755 params->read_ref_cnt--; | |
756 return (0); | |
757 } | |
168 | 758 |
207 | 759 if (read_size + params->read_bytes > params->content_length) |
760 read_size = params->content_length - params->read_bytes; | |
761 } | |
762 else if (params->chunked_transfer && params->chunk_size > 0 && | |
763 params->chunk_size < read_size) | |
764 read_size = params->chunk_size; | |
168 | 765 } |
766 | |
209 | 767 if (read_size > 0 && !params->eof) |
768 { | |
293 | 769 if (size == read_size) |
770 read_size--; /* decrement by one so that we can put the NUL character | |
771 in the buffer */ | |
772 | |
303 | 773 retval = params->real_read_function (request, read_ptr_pos, read_size, |
774 fd); | |
209 | 775 |
303 | 776 if (retval <= 0) |
777 { | |
778 params->read_ref_cnt--; | |
779 return (retval); | |
780 } | |
781 else if (retval > 0) | |
209 | 782 params->read_bytes += retval; |
783 | |
303 | 784 if (params->chunk_size > 0) |
209 | 785 { |
786 params->chunk_size -= retval; | |
303 | 787 params->read_ref_cnt--; |
209 | 788 return (retval); |
789 } | |
790 | |
791 retval += begin_ptr_len; | |
792 } | |
793 else | |
794 retval = begin_ptr_len; | |
168 | 795 |
215 | 796 ((char *) ptr)[retval] = '\0'; |
797 | |
303 | 798 if (!params->chunked_transfer) |
799 { | |
800 params->read_ref_cnt--; | |
801 return (retval); | |
802 } | |
168 | 803 |
209 | 804 stpos = (char *) ptr; |
805 while (params->chunk_size == 0) | |
168 | 806 { |
215 | 807 current_size = retval - (stpos - (char *) ptr); |
303 | 808 if (current_size == 0) |
809 break; | |
810 | |
811 if (*stpos == '\r' && *(stpos + 1) == '\n') | |
812 crlfpos = strstr (stpos + 2, "\r\n"); | |
813 else | |
814 crlfpos = NULL; | |
815 | |
816 if (crlfpos == NULL) | |
215 | 817 { |
818 /* The current chunk size is split between multiple packets. | |
819 Save this chunk and read the next */ | |
820 | |
821 params->extra_read_buffer = g_malloc (current_size + 1); | |
822 memcpy (params->extra_read_buffer, stpos, current_size); | |
823 params->extra_read_buffer[current_size] = '\0'; | |
824 params->extra_read_buffer_len = current_size; | |
825 retval -= current_size; | |
826 | |
827 if (retval == 0) | |
303 | 828 { |
829 /* Don't let a hostile web server send us in an infinite recursive | |
830 loop */ | |
215 | 831 |
303 | 832 if (params->read_ref_cnt > 2) |
833 { | |
834 request->logging_function (gftp_logging_error, request, _("Received wrong response from server, disconnecting\n")); | |
835 gftp_disconnect (request); | |
836 params->read_ref_cnt--; | |
837 return (GFTP_EFATAL); | |
838 } | |
209 | 839 |
303 | 840 retval = rfc2068_chunked_read (request, ptr, size, fd); |
841 } | |
842 | |
843 params->read_ref_cnt--; | |
844 return (retval); | |
209 | 845 } |
168 | 846 |
215 | 847 *crlfpos = '\0'; |
848 crlfpos++; /* advance to line feed */ | |
209 | 849 |
850 if (sscanf (stpos + 2, "%lx", ¶ms->chunk_size) != 1) | |
851 { | |
852 request->logging_function (gftp_logging_recv, request, | |
853 _("Received wrong response from server, disconnecting\nInvalid chunk size '%s' returned by the remote server\n"), | |
854 stpos + 2); | |
855 gftp_disconnect (request); | |
303 | 856 params->read_ref_cnt--; |
209 | 857 return (GFTP_EFATAL); |
858 } | |
859 | |
303 | 860 crlfsize = crlfpos - (char *) stpos + 1; |
861 retval -= crlfsize; | |
862 current_size -= crlfsize; | |
215 | 863 |
209 | 864 if (params->chunk_size == 0) |
865 { | |
866 if (params->eof) | |
303 | 867 { |
868 params->read_ref_cnt--; | |
869 return (0); | |
870 } | |
209 | 871 |
872 params->eof = 1; | |
303 | 873 params->read_ref_cnt--; |
209 | 874 return (retval); |
875 } | |
876 | |
303 | 877 memmove (stpos, crlfpos + 1, current_size + 1); |
209 | 878 |
303 | 879 if (params->chunk_size < current_size) |
209 | 880 { |
303 | 881 stpos += params->chunk_size; |
209 | 882 params->chunk_size = 0; |
168 | 883 } |
303 | 884 else |
885 params->chunk_size -= current_size; | |
168 | 886 } |
887 | |
303 | 888 params->read_ref_cnt--; |
168 | 889 return (retval); |
890 } | |
891 | |
892 | |
122 | 893 void |
894 rfc2068_register_module (void) | |
895 { | |
896 gftp_register_config_vars (config_vars); | |
897 } | |
898 | |
899 | |
173 | 900 int |
48 | 901 rfc2068_init (gftp_request * request) |
902 { | |
168 | 903 rfc2068_params * params; |
904 | |
173 | 905 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 906 |
48 | 907 request->protonum = GFTP_HTTP_NUM; |
908 request->init = rfc2068_init; | |
309 | 909 request->copy_param_options = NULL; |
168 | 910 request->read_function = rfc2068_chunked_read; |
911 request->write_function = gftp_fd_write; | |
87 | 912 request->destroy = rfc2068_destroy; |
48 | 913 request->connect = rfc2068_connect; |
168 | 914 request->post_connect = NULL; |
48 | 915 request->disconnect = rfc2068_disconnect; |
916 request->get_file = rfc2068_get_file; | |
917 request->put_file = NULL; | |
918 request->transfer_file = NULL; | |
919 request->get_next_file_chunk = rfc2068_get_next_file_chunk; | |
920 request->put_next_file_chunk = NULL; | |
921 request->end_transfer = rfc2068_end_transfer; | |
922 request->abort_transfer = rfc2068_end_transfer; /* NOTE: uses end_transfer */ | |
923 request->list_files = rfc2068_list_files; | |
924 request->get_next_file = rfc2068_get_next_file; | |
925 request->get_file_size = rfc2068_get_file_size; | |
926 request->chdir = rfc2068_chdir; | |
927 request->rmdir = NULL; | |
928 request->rmfile = NULL; | |
929 request->mkdir = NULL; | |
930 request->rename = NULL; | |
931 request->chmod = NULL; | |
932 request->site = NULL; | |
58 | 933 request->parse_url = NULL; |
63 | 934 request->swap_socks = NULL; |
58 | 935 request->set_config_options = rfc2068_set_config_options; |
87 | 936 request->url_prefix = g_strdup ("http"); |
48 | 937 request->need_hostport = 1; |
938 request->need_userpass = 0; | |
939 request->use_cache = 1; | |
105 | 940 request->always_connected = 1; |
168 | 941 |
48 | 942 request->protocol_data = g_malloc0 (sizeof (rfc2068_params)); |
168 | 943 params = request->protocol_data; |
944 params->real_read_function = gftp_fd_read; | |
945 | |
177 | 946 return (gftp_set_config_options (request)); |
48 | 947 } |
1 | 948 |