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