http.c (60189) | http.c (60196) |
---|---|
1/*- 2 * Copyright (c) 1998 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 11 unchanged lines hidden (view full) --- 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * | 1/*- 2 * Copyright (c) 1998 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 11 unchanged lines hidden (view full) --- 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * |
28 * $FreeBSD: head/lib/libfetch/http.c 60189 2000-05-07 20:01:55Z des $ | 28 * $FreeBSD: head/lib/libfetch/http.c 60196 2000-05-07 20:51:31Z des $ |
29 */ 30 31/* 32 * The base64 code in this file is based on code from MIT fetch, which 33 * has the following copyright and license: 34 * 35 *- 36 * Copyright 1997 Massachusetts Institute of Technology --- 37 unchanged lines hidden (view full) --- 74#include "fetch.h" 75#include "common.h" 76#include "httperr.h" 77 78extern char *__progname; 79 80#define ENDL "\r\n" 81 | 29 */ 30 31/* 32 * The base64 code in this file is based on code from MIT fetch, which 33 * has the following copyright and license: 34 * 35 *- 36 * Copyright 1997 Massachusetts Institute of Technology --- 37 unchanged lines hidden (view full) --- 74#include "fetch.h" 75#include "common.h" 76#include "httperr.h" 77 78extern char *__progname; 79 80#define ENDL "\r\n" 81 |
82#define HTTP_OK 200 83#define HTTP_PARTIAL 206 84 |
|
82struct cookie 83{ 84 FILE *real_f; 85#define ENC_NONE 0 86#define ENC_CHUNKED 1 87 int encoding; /* 1 = chunked, 0 = none */ 88#define HTTPCTYPELEN 59 89 char content_type[HTTPCTYPELEN+1]; --- 204 unchanged lines hidden (view full) --- 294FILE * 295fetchGetHTTP(struct url *URL, char *flags) 296{ 297 int sd = -1, e, i, enc = ENC_NONE, direct, verbose; 298 struct cookie *c; 299 char *ln, *p, *px, *q; 300 FILE *f, *cf; 301 size_t len; | 85struct cookie 86{ 87 FILE *real_f; 88#define ENC_NONE 0 89#define ENC_CHUNKED 1 90 int encoding; /* 1 = chunked, 0 = none */ 91#define HTTPCTYPELEN 59 92 char content_type[HTTPCTYPELEN+1]; --- 204 unchanged lines hidden (view full) --- 297FILE * 298fetchGetHTTP(struct url *URL, char *flags) 299{ 300 int sd = -1, e, i, enc = ENC_NONE, direct, verbose; 301 struct cookie *c; 302 char *ln, *p, *px, *q; 303 FILE *f, *cf; 304 size_t len; |
305 off_t pos = 0; |
|
302 303 direct = (flags && strchr(flags, 'd')); 304 verbose = (flags && strchr(flags, 'v')); 305 306 /* allocate cookie */ 307 if ((c = calloc(1, sizeof *c)) == NULL) 308 return NULL; 309 --- 74 unchanged lines hidden (view full) --- 384 char *auth_str = _http_auth(URL->user, URL->pwd); 385 if (!auth_str) 386 goto fouch; 387 _http_cmd(f, "Authorization: Basic %s" ENDL, auth_str); 388 free(auth_str); 389 } 390 _http_cmd(f, "Host: %s:%d" ENDL, URL->host, URL->port); 391 _http_cmd(f, "User-Agent: %s " _LIBFETCH_VER ENDL, __progname); | 306 307 direct = (flags && strchr(flags, 'd')); 308 verbose = (flags && strchr(flags, 'v')); 309 310 /* allocate cookie */ 311 if ((c = calloc(1, sizeof *c)) == NULL) 312 return NULL; 313 --- 74 unchanged lines hidden (view full) --- 388 char *auth_str = _http_auth(URL->user, URL->pwd); 389 if (!auth_str) 390 goto fouch; 391 _http_cmd(f, "Authorization: Basic %s" ENDL, auth_str); 392 free(auth_str); 393 } 394 _http_cmd(f, "Host: %s:%d" ENDL, URL->host, URL->port); 395 _http_cmd(f, "User-Agent: %s " _LIBFETCH_VER ENDL, __progname); |
396 if (URL->offset) 397 _http_cmd(f, "Range: bytes=%lld-" ENDL, URL->offset); |
|
392 _http_cmd(f, "Connection: close" ENDL ENDL); 393 394 /* get response */ 395 if ((ln = fgetln(f, &len)) == NULL) 396 goto fouch; 397 DEBUG(fprintf(stderr, "response: [\033[1m%*.*s\033[m]\n", 398 (int)len-2, (int)len-2, ln)); 399 --- 4 unchanged lines hidden (view full) --- 404 while ((p < ln + len) && !isdigit(*p)) 405 p++; 406 if (!isdigit(*p)) 407 goto fouch; 408 e = atoi(p); 409 DEBUG(fprintf(stderr, "code: [\033[1m%d\033[m]\n", e)); 410 411 /* add code to handle redirects later */ | 398 _http_cmd(f, "Connection: close" ENDL ENDL); 399 400 /* get response */ 401 if ((ln = fgetln(f, &len)) == NULL) 402 goto fouch; 403 DEBUG(fprintf(stderr, "response: [\033[1m%*.*s\033[m]\n", 404 (int)len-2, (int)len-2, ln)); 405 --- 4 unchanged lines hidden (view full) --- 410 while ((p < ln + len) && !isdigit(*p)) 411 p++; 412 if (!isdigit(*p)) 413 goto fouch; 414 e = atoi(p); 415 DEBUG(fprintf(stderr, "code: [\033[1m%d\033[m]\n", e)); 416 417 /* add code to handle redirects later */ |
412 if (e != 200) { | 418 if (e != (URL->offset ? HTTP_PARTIAL : HTTP_OK)) { |
413 _http_seterr(e); 414 goto fouch; 415 } 416 417 /* browse through header */ 418 while (1) { 419 if ((ln = fgetln(f, &len)) == NULL) 420 goto fouch; --- 20 unchanged lines hidden (view full) --- 441 p++; 442 for (i = 0; p < ln + len; p++) 443 if (i < HTTPCTYPELEN) 444 c->content_type[i++] = *p; 445 do c->content_type[i--] = 0; while (isspace(c->content_type[i])); 446 DEBUG(fprintf(stderr, "conttype: [\033[1m%s\033[m]\n", 447 c->content_type)); 448#undef CONTTYPE | 419 _http_seterr(e); 420 goto fouch; 421 } 422 423 /* browse through header */ 424 while (1) { 425 if ((ln = fgetln(f, &len)) == NULL) 426 goto fouch; --- 20 unchanged lines hidden (view full) --- 447 p++; 448 for (i = 0; p < ln + len; p++) 449 if (i < HTTPCTYPELEN) 450 c->content_type[i++] = *p; 451 do c->content_type[i--] = 0; while (isspace(c->content_type[i])); 452 DEBUG(fprintf(stderr, "conttype: [\033[1m%s\033[m]\n", 453 c->content_type)); 454#undef CONTTYPE |
455#define CONTRANGE "Content-Range:" 456#define BYTES "bytes " 457 } else if (strncasecmp(ln, CONTRANGE, sizeof CONTRANGE - 1) == 0) { 458 p = ln + sizeof CONTRANGE - 1; 459 while ((p < ln + len) && isspace(*p)) 460 p++; 461 if (strncasecmp(p, BYTES, sizeof BYTES - 1) != 0 462 || (p += 6) >= ln + len) 463 goto fouch; 464 while ((p < ln + len) && isdigit(*p)) 465 pos = pos * 10 + (*p++ - '0'); 466 /* XXX wouldn't hurt to be slightly more paranoid here */ 467 DEBUG(fprintf(stderr, "contrange: [\033[1m%lld-\033[m]\n", pos)); 468 if (pos > URL->offset) 469 goto fouch; 470#undef BYTES 471#undef CONTRANGE |
|
449 } 450 } 451 452 /* only body remains */ 453 c->encoding = enc; 454 cf = funopen(c, 455 (int (*)(void *, char *, int))_http_readfn, 456 (int (*)(void *, const char *, int))_http_writefn, 457 (fpos_t (*)(void *, fpos_t, int))NULL, 458 (int (*)(void *))_http_closefn); 459 if (cf == NULL) 460 goto fouch; 461 | 472 } 473 } 474 475 /* only body remains */ 476 c->encoding = enc; 477 cf = funopen(c, 478 (int (*)(void *, char *, int))_http_readfn, 479 (int (*)(void *, const char *, int))_http_writefn, 480 (fpos_t (*)(void *, fpos_t, int))NULL, 481 (int (*)(void *))_http_closefn); 482 if (cf == NULL) 483 goto fouch; 484 |
485 while (pos < URL->offset) 486 if (fgetc(cf) == EOF) 487 goto cfouch; 488 |
|
462 return cf; 463 464ouch: 465 if (sd >= 0) 466 close(sd); 467 free(c); 468 _http_seterr(999); /* XXX do this properly RSN */ 469 return NULL; 470fouch: 471 fclose(f); 472 free(c); 473 _http_seterr(999); /* XXX do this properly RSN */ 474 return NULL; | 489 return cf; 490 491ouch: 492 if (sd >= 0) 493 close(sd); 494 free(c); 495 _http_seterr(999); /* XXX do this properly RSN */ 496 return NULL; 497fouch: 498 fclose(f); 499 free(c); 500 _http_seterr(999); /* XXX do this properly RSN */ 501 return NULL; |
502cfouch: 503 fclose(cf); 504 _http_seterr(999); /* XXX do this properly RSN */ 505 return NULL; |
|
475} 476 477FILE * 478fetchPutHTTP(struct url *URL, char *flags) 479{ 480 warnx("fetchPutHTTP(): not implemented"); 481 return NULL; 482} --- 20 unchanged lines hidden --- | 506} 507 508FILE * 509fetchPutHTTP(struct url *URL, char *flags) 510{ 511 warnx("fetchPutHTTP(): not implemented"); 512 return NULL; 513} --- 20 unchanged lines hidden --- |