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