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