Mercurial > gftp.yaz
annotate lib/rfc2068.c @ 516:fbb5a02beddb
2004-7-26 Brian Masney <masneyb@gftp.org>
* lib/config_file.c lib/local.c lib/ftps.c lib/gftp.h lib/httpcommon.h
lib/https.c lib/misc.c lib/protocols.c lib/pty.c lib/rfc2068.c
lib/rfc959.c lib/sshv2.c - various fixes found through a static code
analysis checker. (some signed/unsigned fixes, removed unneeded casts,
removed unneeded variables, more consistency, largefile fixes). Thanks
to Derek M Jones for running the static code analysis for me
* lib/cache.c - s/remove/del_entry/g
* lib/bookmark.c lib/protocols.c (gftp_parse_bookmark) - allow
the third parameter of this function to be a NULL pointer
author | masneyb |
---|---|
date | Tue, 27 Jul 2004 02:46:54 +0000 |
parents | ba50a7085d93 |
children | 648bf2825ea9 |
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 | |
516 | 150 #ifdef _LARGEFILE_SOURCE |
151 ret = sscanf (tempstr, "%llx", ¶ms->chunk_size); | |
152 #else | |
153 ret = sscanf (tempstr, "%lx", ¶ms->chunk_size); | |
154 #endif | |
155 | |
156 if (ret != 1) | |
168 | 157 { |
186 | 158 request->logging_function (gftp_logging_recv, request, |
209 | 159 _("Received wrong response from server, disconnecting\nInvalid chunk size '%s' returned by the remote server\n"), |
160 tempstr); | |
168 | 161 gftp_disconnect (request); |
162 return (GFTP_EFATAL); | |
163 } | |
164 | |
165 if (params->chunk_size == 0) | |
166 return (0); | |
167 | |
168 params->chunk_size -= params->rbuf->cur_bufsize; | |
169 if (params->chunk_size < 0) | |
170 { | |
207 | 171 params->extra_read_buffer_len = params->chunk_size * -1; |
209 | 172 params->chunk_size = 0; |
207 | 173 params->extra_read_buffer = g_malloc (params->extra_read_buffer_len + 1); |
174 memcpy (params->extra_read_buffer, params->rbuf->curpos + (params->rbuf->cur_bufsize - params->extra_read_buffer_len), params->extra_read_buffer_len); | |
175 params->extra_read_buffer[params->extra_read_buffer_len] = '\0'; | |
176 params->rbuf->cur_bufsize -= params->extra_read_buffer_len; | |
209 | 177 params->rbuf->curpos[params->rbuf->cur_bufsize] = '\0'; |
168 | 178 } |
179 } | |
180 | |
181 params->chunked_transfer = chunked; | |
182 return (params->content_length > 0 ? params->content_length : params->chunk_size); | |
48 | 183 } |
184 | |
185 | |
58 | 186 static off_t |
168 | 187 rfc2068_send_command (gftp_request * request, const void *command, size_t len) |
48 | 188 { |
122 | 189 char *tempstr, *str, *proxy_hostname, *proxy_username, *proxy_password; |
325 | 190 intptr_t proxy_port; |
191 int conn_ret; | |
58 | 192 ssize_t ret; |
48 | 193 |
84 | 194 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
195 g_return_val_if_fail (command != NULL, GFTP_EFATAL); | |
48 | 196 |
219 | 197 if (request->datafd < 0 && (conn_ret = rfc2068_connect (request)) != 0) |
198 return (conn_ret); | |
199 | |
168 | 200 tempstr = g_strdup_printf ("%sUser-Agent: %s\nHost: %s\n", (char *) command, |
122 | 201 gftp_version, request->hostname); |
48 | 202 |
186 | 203 request->logging_function (gftp_logging_send, request, |
58 | 204 "%s", tempstr); |
205 | |
168 | 206 ret = request->write_function (request, tempstr, strlen (tempstr), |
169 | 207 request->datafd); |
58 | 208 g_free (tempstr); |
209 | |
210 if (ret < 0) | |
84 | 211 return (ret); |
48 | 212 |
122 | 213 gftp_lookup_request_option (request, "http_proxy_host", &proxy_hostname); |
214 gftp_lookup_request_option (request, "http_proxy_port", &proxy_port); | |
215 gftp_lookup_request_option (request, "http_proxy_username", &proxy_username); | |
216 gftp_lookup_request_option (request, "http_proxy_password", &proxy_password); | |
217 | |
218 if (request->use_proxy && proxy_username != NULL && *proxy_username != '\0') | |
48 | 219 { |
122 | 220 tempstr = g_strconcat (proxy_username, ":", proxy_password, NULL); |
48 | 221 str = base64_encode (tempstr); |
222 g_free (tempstr); | |
223 | |
186 | 224 request->logging_function (gftp_logging_send, request, |
48 | 225 "Proxy-authorization: Basic xxxx:xxxx\n"); |
169 | 226 ret = gftp_writefmt (request, request->datafd, |
58 | 227 "Proxy-authorization: Basic %s\n", str); |
48 | 228 g_free (str); |
58 | 229 if (ret < 0) |
168 | 230 return (ret); |
48 | 231 } |
232 | |
233 if (request->username != NULL && *request->username != '\0') | |
234 { | |
235 tempstr = g_strconcat (request->username, ":", request->password, NULL); | |
236 str = base64_encode (tempstr); | |
237 g_free (tempstr); | |
238 | |
186 | 239 request->logging_function (gftp_logging_send, request, |
48 | 240 "Authorization: Basic xxxx\n"); |
169 | 241 ret = gftp_writefmt (request, request->datafd, |
58 | 242 "Authorization: Basic %s\n", str); |
48 | 243 g_free (str); |
58 | 244 if (ret < 0) |
84 | 245 return (ret); |
48 | 246 } |
247 | |
169 | 248 if ((ret = request->write_function (request, "\n", 1, request->datafd)) < 0) |
84 | 249 return (ret); |
58 | 250 |
48 | 251 return (rfc2068_read_response (request)); |
1 | 252 } |
253 | |
254 | |
255 static void | |
256 rfc2068_disconnect (gftp_request * request) | |
257 { | |
258 g_return_if_fail (request != NULL); | |
259 | |
169 | 260 if (request->datafd > 0) |
1 | 261 { |
186 | 262 request->logging_function (gftp_logging_misc, request, |
1 | 263 _("Disconnecting from site %s\n"), |
264 request->hostname); | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
265 |
169 | 266 if (close (request->datafd) < 0) |
186 | 267 request->logging_function (gftp_logging_error, request, |
58 | 268 _("Error closing file descriptor: %s\n"), |
269 g_strerror (errno)); | |
270 | |
169 | 271 request->datafd = -1; |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
272 } |
1 | 273 } |
274 | |
275 | |
58 | 276 static off_t |
277 rfc2068_get_file (gftp_request * request, const char *filename, int fd, | |
1 | 278 off_t startsize) |
279 { | |
303 | 280 char *tempstr, *oldstr, *hf; |
460 | 281 rfc2068_params * params; |
325 | 282 intptr_t use_http11; |
460 | 283 int restarted; |
284 size_t len; | |
1 | 285 off_t size; |
286 | |
84 | 287 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
288 g_return_val_if_fail (filename != NULL, GFTP_EFATAL); | |
1 | 289 |
122 | 290 params = request->protocol_data; |
291 | |
292 gftp_lookup_request_option (request, "use_http11", &use_http11); | |
293 | |
58 | 294 if (fd > 0) |
169 | 295 request->datafd = fd; |
1 | 296 |
303 | 297 hf = gftp_build_path (request->hostname, filename, NULL); |
298 | |
7 | 299 if (request->username == NULL || *request->username == '\0') |
303 | 300 tempstr = g_strconcat ("GET ", request->url_prefix, "://", hf, |
1 | 301 use_http11 ? " HTTP/1.1\n" : " HTTP/1.0\n", NULL); |
302 else | |
122 | 303 tempstr = g_strconcat ("GET ", request->url_prefix, "://", |
303 | 304 request->username, "@", hf, |
1 | 305 use_http11 ? " HTTP/1.1\n" : " HTTP/1.0\n", NULL); |
306 | |
303 | 307 g_free (hf); |
1 | 308 |
168 | 309 if (use_http11 && startsize > 0) |
1 | 310 { |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
311 #if defined (_LARGEFILE_SOURCE) |
186 | 312 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
|
313 _("Starting the file transfer at offset %lld\n"), |
372 | 314 (long long) startsize); |
168 | 315 |
316 oldstr = tempstr; | |
317 tempstr = g_strdup_printf ("%sRange: bytes=%lld-\n", tempstr, startsize); | |
318 g_free (oldstr); | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
319 #else |
186 | 320 request->logging_function (gftp_logging_misc, request, |
1 | 321 _("Starting the file transfer at offset %ld\n"), |
322 startsize); | |
168 | 323 |
324 oldstr = tempstr; | |
325 tempstr = g_strdup_printf ("%sRange: bytes=%ld-\n", tempstr, startsize); | |
326 g_free (oldstr); | |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
327 #endif |
1 | 328 } |
329 | |
168 | 330 size = rfc2068_send_command (request, tempstr, strlen (tempstr)); |
1 | 331 g_free (tempstr); |
58 | 332 if (size < 0) |
84 | 333 return (size); |
1 | 334 |
335 restarted = 0; | |
460 | 336 len = strlen (request->last_ftp_response); |
337 if (len > 9 && strncmp (request->last_ftp_response + 9, "206", 3) == 0) | |
1 | 338 restarted = 1; |
460 | 339 else if (len < 9 || strncmp (request->last_ftp_response + 9, "200", 3) != 0) |
1 | 340 { |
186 | 341 request->logging_function (gftp_logging_error, request, |
1 | 342 _("Cannot retrieve file %s\n"), filename); |
84 | 343 return (GFTP_ERETRYABLE); |
1 | 344 } |
345 | |
168 | 346 params->read_bytes = 0; |
347 | |
1 | 348 return (restarted ? size + startsize : size); |
349 } | |
350 | |
351 | |
58 | 352 static ssize_t |
1 | 353 rfc2068_get_next_file_chunk (gftp_request * request, char *buf, size_t size) |
354 { | |
355 rfc2068_params * params; | |
356 size_t len; | |
357 | |
84 | 358 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 359 |
360 params = request->protocol_data; | |
304 | 361 if (params->rbuf != NULL && params->rbuf->curpos != NULL) |
362 { | |
363 len = params->rbuf->cur_bufsize > size ? size : params->rbuf->cur_bufsize; | |
364 memcpy (buf, params->rbuf->curpos, len); | |
1 | 365 |
304 | 366 if (len == params->rbuf->cur_bufsize) |
367 gftp_free_getline_buffer (¶ms->rbuf); | |
368 else | |
369 { | |
370 params->rbuf->curpos += len; | |
371 params->rbuf->cur_bufsize -= len; | |
372 } | |
1 | 373 |
304 | 374 return (len); |
375 } | |
376 | |
377 return (request->read_function (request, buf, size, request->datafd)); | |
1 | 378 } |
379 | |
380 | |
381 static int | |
382 rfc2068_end_transfer (gftp_request * request) | |
383 { | |
384 rfc2068_params * params; | |
385 | |
84 | 386 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 387 |
169 | 388 if (request->datafd < 0) |
84 | 389 return (GFTP_EFATAL); |
1 | 390 |
207 | 391 gftp_disconnect (request); |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
392 |
1 | 393 params = request->protocol_data; |
168 | 394 params->content_length = 0; |
395 params->chunked_transfer = 0; | |
396 params->chunk_size = 0; | |
209 | 397 params->eof = 0; |
14
83090328581e
* More largefile support. Hopefully all that is left is the configure stuff
masneyb
parents:
7
diff
changeset
|
398 |
1 | 399 return (0); |
400 } | |
401 | |
402 | |
403 static int | |
404 rfc2068_list_files (gftp_request * request) | |
405 { | |
406 rfc2068_params *params; | |
303 | 407 char *tempstr, *hd; |
325 | 408 intptr_t use_http11; |
58 | 409 off_t ret; |
1 | 410 |
84 | 411 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 412 |
413 params = request->protocol_data; | |
122 | 414 gftp_lookup_request_option (request, "use_http11", &use_http11); |
415 | |
369 | 416 if (strncmp (request->directory, "/", strlen (request->directory)) == 0) |
417 hd = g_strdup (request->hostname); | |
418 else | |
419 hd = gftp_build_path (request->hostname, request->directory, NULL); | |
303 | 420 |
7 | 421 if (request->username == NULL || *request->username == '\0') |
303 | 422 tempstr = g_strconcat ("GET ", request->url_prefix, "://", hd, |
1 | 423 use_http11 ? "/ HTTP/1.1\n" : "/ HTTP/1.0\n", NULL); |
424 else | |
122 | 425 tempstr = g_strconcat ("GET ", request->url_prefix, "://", |
303 | 426 request->username, "@", hd, |
1 | 427 use_http11 ? "/ HTTP/1.1\n" : "/ HTTP/1.0\n", NULL); |
428 | |
303 | 429 g_free (hd); |
1 | 430 |
168 | 431 ret = rfc2068_send_command (request, tempstr, strlen (tempstr)); |
1 | 432 g_free (tempstr); |
58 | 433 if (ret < 0) |
84 | 434 return ((int) ret); |
1 | 435 |
436 params->read_bytes = 0; | |
437 if (strlen (request->last_ftp_response) > 9 && | |
438 strncmp (request->last_ftp_response + 9, "200", 3) == 0) | |
439 { | |
186 | 440 request->logging_function (gftp_logging_misc, request, |
1 | 441 _("Retrieving directory listing...\n")); |
442 return (0); | |
443 } | |
168 | 444 |
207 | 445 gftp_end_transfer (request); |
446 | |
84 | 447 return (GFTP_ERETRYABLE); |
1 | 448 } |
449 | |
450 | |
451 static off_t | |
452 rfc2068_get_file_size (gftp_request * request, const char *filename) | |
453 { | |
303 | 454 char *tempstr, *hf; |
325 | 455 intptr_t use_http11; |
58 | 456 off_t size; |
1 | 457 |
84 | 458 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
459 g_return_val_if_fail (filename != NULL, GFTP_EFATAL); | |
1 | 460 |
122 | 461 gftp_lookup_request_option (request, "use_http11", &use_http11); |
462 | |
303 | 463 hf = gftp_build_path (request->hostname, filename, NULL); |
464 | |
7 | 465 if (request->username == NULL || *request->username == '\0') |
303 | 466 tempstr = g_strconcat ("HEAD ", request->url_prefix, "://", hf, |
1 | 467 use_http11 ? " HTTP/1.1\n" : " HTTP/1.0\n", NULL); |
468 else | |
219 | 469 tempstr = g_strconcat ("HEAD ", request->url_prefix, "://", |
303 | 470 request->username, "@", hf, |
1 | 471 use_http11 ? " HTTP/1.1\n" : " HTTP/1.0\n", NULL); |
472 | |
303 | 473 g_free (hf); |
1 | 474 |
168 | 475 size = rfc2068_send_command (request, tempstr, strlen (tempstr)); |
1 | 476 g_free (tempstr); |
477 return (size); | |
478 } | |
479 | |
480 | |
481 static int | |
482 parse_html_line (char *tempstr, gftp_file * fle) | |
483 { | |
207 | 484 char *stpos, *kpos, *mpos, *pos; |
1 | 485 long units; |
486 | |
487 memset (fle, 0, sizeof (*fle)); | |
488 | |
489 if ((pos = strstr (tempstr, "<A HREF=")) == NULL && | |
490 (pos = strstr (tempstr, "<a href=")) == NULL) | |
491 return (0); | |
492 | |
493 /* Find the filename */ | |
494 while (*pos != '"' && *pos != '\0') | |
495 pos++; | |
496 if (*pos == '\0') | |
497 return (0); | |
498 pos++; | |
499 | |
500 for (stpos = pos; *pos != '"' && *pos != '\0'; pos++); | |
501 if (*pos == '\0') | |
502 return (0); | |
503 *pos = '\0'; | |
504 | |
505 /* Copy file attributes. Just about the only thing we can get is whether it | |
506 is a directory or not */ | |
507 if (*(pos - 1) == '/') | |
508 { | |
499 | 509 fle->st_mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; |
1 | 510 *(pos - 1) = '\0'; |
511 } | |
336 | 512 else |
499 | 513 fle->st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; |
1 | 514 |
515 /* Copy filename */ | |
516 if (strchr (stpos, '/') != NULL || strncmp (stpos, "mailto:", 7) == 0 || | |
517 *stpos == '\0' || *stpos == '?') | |
518 return (0); | |
519 | |
105 | 520 fle->file = g_strdup (stpos); |
1 | 521 |
522 if (*(pos - 1) == '\0') | |
523 *(pos - 1) = '/'; | |
524 *pos = '"'; | |
525 pos++; | |
526 | |
527 /* Skip whitespace and html tags after file and before date */ | |
528 stpos = pos; | |
529 if ((pos = strstr (stpos, "</A>")) == NULL && | |
530 (pos = strstr (stpos, "</a>")) == NULL) | |
531 return (0); | |
532 | |
533 pos += 4; | |
534 | |
105 | 535 while (*pos == ' ' || *pos == '\t' || *pos == '.' || *pos == '<') |
1 | 536 { |
537 if (*pos == '<') | |
538 { | |
539 if (strncmp (pos, "<A ", 3) == 0 || strncmp (pos, "<a ", 3) == 0) | |
540 { | |
541 stpos = pos; | |
542 if ((pos = strstr (stpos, "</A>")) == NULL | |
543 && (pos = strstr (stpos, "</a>")) == NULL) | |
544 return (0); | |
545 pos += 4; | |
546 } | |
547 else | |
548 { | |
549 while (*pos != '>' && *pos != '\0') | |
550 pos++; | |
551 if (*pos == '\0') | |
552 return (0); | |
553 } | |
554 } | |
555 pos++; | |
556 } | |
557 | |
102 | 558 if (*pos == '[') |
559 pos++; | |
1 | 560 |
102 | 561 fle->datetime = parse_time (pos, &pos); |
1 | 562 |
168 | 563 fle->user = g_strdup (_("<unknown>")); |
564 fle->group = g_strdup (_("<unknown>")); | |
565 | |
105 | 566 if (pos == NULL) |
567 return (1); | |
568 | |
1 | 569 while (*pos == ' ' || *pos == ']') |
570 pos++; | |
571 | |
572 /* Get the size */ | |
207 | 573 |
574 kpos = strchr (pos, 'k'); | |
575 mpos = strchr (pos, 'M'); | |
427 | 576 |
577 if (kpos != NULL && (mpos == NULL || (kpos < mpos))) | |
207 | 578 stpos = kpos; |
579 else | |
580 stpos = mpos; | |
581 | |
1 | 582 if (stpos == NULL || !isdigit (*(stpos - 1))) |
583 return (1); /* Return successfully | |
584 since we got the file */ | |
207 | 585 |
1 | 586 if (*stpos == 'k') |
587 units = 1024; | |
588 else | |
589 units = 1048576; | |
590 | |
591 fle->size = 0; | |
592 | |
593 while (*(stpos - 1) != ' ' && *(stpos - 1) != '\t' && stpos > tempstr) | |
594 { | |
595 stpos--; | |
207 | 596 if ((*stpos == '.') && isdigit (*(stpos + 1))) |
1 | 597 { /* found decimal point */ |
244 | 598 fle->size = units * gftp_parse_file_size (stpos + 1) / 10;; |
1 | 599 } |
600 } | |
601 | |
244 | 602 fle->size += units * gftp_parse_file_size (stpos); |
168 | 603 |
1 | 604 return (1); |
605 } | |
606 | |
607 | |
168 | 608 int |
58 | 609 rfc2068_get_next_file (gftp_request * request, gftp_file * fle, int fd) |
1 | 610 { |
48 | 611 rfc2068_params * params; |
219 | 612 char tempstr[8192]; |
168 | 613 ssize_t len; |
84 | 614 int ret; |
1 | 615 |
84 | 616 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
617 g_return_val_if_fail (fle != NULL, GFTP_EFATAL); | |
48 | 618 |
619 params = request->protocol_data; | |
620 if (request->last_dir_entry) | |
621 { | |
622 g_free (request->last_dir_entry); | |
623 request->last_dir_entry = NULL; | |
624 } | |
625 | |
105 | 626 if (fd < 0) |
169 | 627 fd = request->datafd; |
105 | 628 |
48 | 629 while (1) |
630 { | |
168 | 631 if ((ret = gftp_get_line (request, ¶ms->rbuf, tempstr, sizeof (tempstr), fd)) <= 0) |
84 | 632 return (ret); |
1 | 633 |
48 | 634 if (parse_html_line (tempstr, fle) == 0 || fle->file == NULL) |
635 gftp_file_destroy (fle); | |
636 else | |
637 break; | |
638 } | |
639 | |
640 if (fle->file == NULL) | |
1 | 641 { |
48 | 642 gftp_file_destroy (fle); |
105 | 643 return (0); |
1 | 644 } |
645 | |
48 | 646 len = strlen (tempstr); |
647 if (!request->cached) | |
648 { | |
60 | 649 request->last_dir_entry = g_strdup_printf ("%s\n", tempstr); |
650 request->last_dir_entry_len = len + 1; | |
48 | 651 } |
219 | 652 |
58 | 653 return (len); |
48 | 654 } |
1 | 655 |
656 | |
48 | 657 static int |
658 rfc2068_chdir (gftp_request * request, const char *directory) | |
659 { | |
204 | 660 char *tempstr, *olddir; |
661 | |
84 | 662 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
663 g_return_val_if_fail (directory != NULL, GFTP_EFATAL); | |
48 | 664 |
665 if (request->directory != directory) | |
666 { | |
204 | 667 olddir = request->directory; |
668 | |
219 | 669 if (*directory != '/' && request->directory != NULL) |
204 | 670 { |
671 tempstr = g_strconcat (request->directory, "/", directory, NULL); | |
672 request->directory = expand_path (tempstr); | |
673 g_free (tempstr); | |
674 } | |
675 else | |
676 request->directory = expand_path (directory); | |
677 | |
678 if (olddir != NULL) | |
679 g_free (olddir); | |
48 | 680 } |
681 return (0); | |
682 } | |
683 | |
684 | |
177 | 685 static int |
58 | 686 rfc2068_set_config_options (gftp_request * request) |
687 { | |
177 | 688 return (0); |
58 | 689 } |
690 | |
691 | |
87 | 692 static void |
693 rfc2068_destroy (gftp_request * request) | |
694 { | |
207 | 695 rfc2068_params * params; |
696 | |
697 params = request->protocol_data; | |
698 | |
87 | 699 if (request->url_prefix) |
700 { | |
701 g_free (request->url_prefix); | |
702 request->url_prefix = NULL; | |
703 } | |
207 | 704 |
705 if (params->rbuf != NULL) | |
706 gftp_free_getline_buffer (¶ms->rbuf); | |
707 | |
708 if (params->extra_read_buffer != NULL) | |
709 { | |
710 g_free (params->extra_read_buffer); | |
711 params->extra_read_buffer = NULL; | |
209 | 712 params->extra_read_buffer_len = 0; |
207 | 713 } |
87 | 714 } |
715 | |
716 | |
168 | 717 static ssize_t |
718 rfc2068_chunked_read (gftp_request * request, void *ptr, size_t size, int fd) | |
719 { | |
303 | 720 size_t read_size, begin_ptr_len, current_size, crlfsize; |
721 rfc2068_params * params; | |
215 | 722 char *stpos, *crlfpos; |
209 | 723 void *read_ptr_pos; |
168 | 724 ssize_t retval; |
516 | 725 size_t sret; |
726 int ret; | |
168 | 727 |
728 params = request->protocol_data; | |
303 | 729 params->read_ref_cnt++; |
168 | 730 |
207 | 731 if (params->extra_read_buffer != NULL) |
168 | 732 { |
209 | 733 g_return_val_if_fail (params->extra_read_buffer_len <= size, GFTP_EFATAL); |
734 | |
735 memcpy (ptr, params->extra_read_buffer, params->extra_read_buffer_len); | |
736 | |
737 begin_ptr_len = params->extra_read_buffer_len; | |
738 read_ptr_pos = (char *) ptr + begin_ptr_len; | |
739 | |
740 /* Check for end of chunk */ | |
741 if (begin_ptr_len > 5 && strncmp ("\r\n0\r\n", (char *) ptr, 5) == 0) | |
742 read_size = 0; | |
743 else | |
744 read_size = size - begin_ptr_len; | |
745 | |
746 g_free (params->extra_read_buffer); | |
747 params->extra_read_buffer = NULL; | |
748 params->extra_read_buffer_len = 0; | |
207 | 749 } |
750 else | |
751 { | |
209 | 752 begin_ptr_len = 0; |
753 read_ptr_pos = ptr; | |
754 | |
207 | 755 read_size = size; |
756 if (params->content_length > 0) | |
757 { | |
758 if (params->content_length == params->read_bytes) | |
303 | 759 { |
760 params->read_ref_cnt--; | |
761 return (0); | |
762 } | |
168 | 763 |
207 | 764 if (read_size + params->read_bytes > params->content_length) |
765 read_size = params->content_length - params->read_bytes; | |
766 } | |
767 else if (params->chunked_transfer && params->chunk_size > 0 && | |
768 params->chunk_size < read_size) | |
769 read_size = params->chunk_size; | |
168 | 770 } |
771 | |
209 | 772 if (read_size > 0 && !params->eof) |
773 { | |
293 | 774 if (size == read_size) |
775 read_size--; /* decrement by one so that we can put the NUL character | |
776 in the buffer */ | |
777 | |
303 | 778 retval = params->real_read_function (request, read_ptr_pos, read_size, |
779 fd); | |
209 | 780 |
303 | 781 if (retval <= 0) |
782 { | |
783 params->read_ref_cnt--; | |
784 return (retval); | |
785 } | |
209 | 786 |
516 | 787 params->read_bytes += retval; |
303 | 788 if (params->chunk_size > 0) |
209 | 789 { |
790 params->chunk_size -= retval; | |
303 | 791 params->read_ref_cnt--; |
209 | 792 return (retval); |
793 } | |
794 | |
516 | 795 sret = retval + begin_ptr_len; |
209 | 796 } |
797 else | |
516 | 798 sret = begin_ptr_len; |
168 | 799 |
516 | 800 ((char *) ptr)[sret] = '\0'; |
215 | 801 |
303 | 802 if (!params->chunked_transfer) |
803 { | |
804 params->read_ref_cnt--; | |
516 | 805 return (sret); |
303 | 806 } |
168 | 807 |
209 | 808 stpos = (char *) ptr; |
809 while (params->chunk_size == 0) | |
168 | 810 { |
516 | 811 current_size = sret - (stpos - (char *) ptr); |
303 | 812 if (current_size == 0) |
813 break; | |
814 | |
815 if (*stpos == '\r' && *(stpos + 1) == '\n') | |
816 crlfpos = strstr (stpos + 2, "\r\n"); | |
817 else | |
818 crlfpos = NULL; | |
819 | |
820 if (crlfpos == NULL) | |
215 | 821 { |
822 /* The current chunk size is split between multiple packets. | |
823 Save this chunk and read the next */ | |
824 | |
825 params->extra_read_buffer = g_malloc (current_size + 1); | |
826 memcpy (params->extra_read_buffer, stpos, current_size); | |
827 params->extra_read_buffer[current_size] = '\0'; | |
828 params->extra_read_buffer_len = current_size; | |
516 | 829 sret -= current_size; |
215 | 830 |
516 | 831 if (sret == 0) |
303 | 832 { |
833 /* Don't let a hostile web server send us in an infinite recursive | |
834 loop */ | |
215 | 835 |
303 | 836 if (params->read_ref_cnt > 2) |
837 { | |
838 request->logging_function (gftp_logging_error, request, _("Received wrong response from server, disconnecting\n")); | |
839 gftp_disconnect (request); | |
840 params->read_ref_cnt--; | |
841 return (GFTP_EFATAL); | |
842 } | |
209 | 843 |
303 | 844 retval = rfc2068_chunked_read (request, ptr, size, fd); |
516 | 845 if (retval < 0) |
846 return (retval); | |
847 | |
848 sret = retval; | |
303 | 849 } |
850 | |
851 params->read_ref_cnt--; | |
516 | 852 return (sret); |
209 | 853 } |
168 | 854 |
215 | 855 *crlfpos = '\0'; |
856 crlfpos++; /* advance to line feed */ | |
209 | 857 |
516 | 858 #ifdef _LARGEFILE_SOURCE |
859 ret = sscanf (stpos + 2, "%llx", ¶ms->chunk_size); | |
860 #else | |
861 ret = sscanf (stpos + 2, "%lx", ¶ms->chunk_size); | |
862 #endif | |
863 | |
864 if (ret != 1) | |
209 | 865 { |
866 request->logging_function (gftp_logging_recv, request, | |
867 _("Received wrong response from server, disconnecting\nInvalid chunk size '%s' returned by the remote server\n"), | |
868 stpos + 2); | |
869 gftp_disconnect (request); | |
303 | 870 params->read_ref_cnt--; |
209 | 871 return (GFTP_EFATAL); |
872 } | |
873 | |
516 | 874 crlfsize = crlfpos - stpos + 1; |
875 sret -= crlfsize; | |
303 | 876 current_size -= crlfsize; |
215 | 877 |
209 | 878 if (params->chunk_size == 0) |
879 { | |
880 if (params->eof) | |
303 | 881 { |
882 params->read_ref_cnt--; | |
883 return (0); | |
884 } | |
209 | 885 |
886 params->eof = 1; | |
303 | 887 params->read_ref_cnt--; |
516 | 888 return (sret); |
209 | 889 } |
890 | |
303 | 891 memmove (stpos, crlfpos + 1, current_size + 1); |
209 | 892 |
303 | 893 if (params->chunk_size < current_size) |
209 | 894 { |
303 | 895 stpos += params->chunk_size; |
209 | 896 params->chunk_size = 0; |
168 | 897 } |
303 | 898 else |
899 params->chunk_size -= current_size; | |
168 | 900 } |
901 | |
303 | 902 params->read_ref_cnt--; |
516 | 903 return (sret); |
168 | 904 } |
905 | |
906 | |
122 | 907 void |
908 rfc2068_register_module (void) | |
909 { | |
910 gftp_register_config_vars (config_vars); | |
911 } | |
912 | |
913 | |
173 | 914 int |
48 | 915 rfc2068_init (gftp_request * request) |
916 { | |
168 | 917 rfc2068_params * params; |
918 | |
173 | 919 g_return_val_if_fail (request != NULL, GFTP_EFATAL); |
1 | 920 |
48 | 921 request->protonum = GFTP_HTTP_NUM; |
922 request->init = rfc2068_init; | |
309 | 923 request->copy_param_options = NULL; |
168 | 924 request->read_function = rfc2068_chunked_read; |
925 request->write_function = gftp_fd_write; | |
87 | 926 request->destroy = rfc2068_destroy; |
48 | 927 request->connect = rfc2068_connect; |
168 | 928 request->post_connect = NULL; |
48 | 929 request->disconnect = rfc2068_disconnect; |
930 request->get_file = rfc2068_get_file; | |
931 request->put_file = NULL; | |
932 request->transfer_file = NULL; | |
933 request->get_next_file_chunk = rfc2068_get_next_file_chunk; | |
934 request->put_next_file_chunk = NULL; | |
935 request->end_transfer = rfc2068_end_transfer; | |
936 request->abort_transfer = rfc2068_end_transfer; /* NOTE: uses end_transfer */ | |
500 | 937 request->stat_filename = NULL; |
48 | 938 request->list_files = rfc2068_list_files; |
939 request->get_next_file = rfc2068_get_next_file; | |
485 | 940 request->get_next_dirlist_line = NULL; |
48 | 941 request->get_file_size = rfc2068_get_file_size; |
942 request->chdir = rfc2068_chdir; | |
943 request->rmdir = NULL; | |
944 request->rmfile = NULL; | |
945 request->mkdir = NULL; | |
946 request->rename = NULL; | |
947 request->chmod = NULL; | |
948 request->site = NULL; | |
58 | 949 request->parse_url = NULL; |
63 | 950 request->swap_socks = NULL; |
58 | 951 request->set_config_options = rfc2068_set_config_options; |
87 | 952 request->url_prefix = g_strdup ("http"); |
48 | 953 request->need_hostport = 1; |
954 request->need_userpass = 0; | |
955 request->use_cache = 1; | |
105 | 956 request->always_connected = 1; |
168 | 957 |
48 | 958 request->protocol_data = g_malloc0 (sizeof (rfc2068_params)); |
168 | 959 params = request->protocol_data; |
960 params->real_read_function = gftp_fd_read; | |
961 | |
177 | 962 return (gftp_set_config_options (request)); |
48 | 963 } |
1 | 964 |