Deleted Added
sdiff udiff text old ( 60189 ) new ( 60196 )
full compact
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 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
82#define HTTP_OK 200
83#define HTTP_PARTIAL 206
84
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;
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);
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 */
418 if (e != (URL->offset ? HTTP_PARTIAL : HTTP_OK)) {
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
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
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;
506}
507
508FILE *
509fetchPutHTTP(struct url *URL, char *flags)
510{
511 warnx("fetchPutHTTP(): not implemented");
512 return NULL;
513}

--- 20 unchanged lines hidden ---