http.c (55544) | http.c (60189) |
---|---|
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 55544 2000-01-07 10:59:12Z des $ | 28 * $FreeBSD: head/lib/libfetch/http.c 60189 2000-05-07 20:01:55Z 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 * 38 * Permission to use, copy, modify, and distribute this software and 39 * its documentation for any purpose and without fee is hereby 40 * granted, provided that both the above copyright notice and this 41 * permission notice appear in all copies, that both the above 42 * copyright notice and this permission notice appear in all 43 * supporting documentation, and that the name of M.I.T. not be used 44 * in advertising or publicity pertaining to distribution of the | 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 * 38 * Permission to use, copy, modify, and distribute this software and 39 * its documentation for any purpose and without fee is hereby 40 * granted, provided that both the above copyright notice and this 41 * permission notice appear in all copies, that both the above 42 * copyright notice and this permission notice appear in all 43 * supporting documentation, and that the name of M.I.T. not be used 44 * in advertising or publicity pertaining to distribution of the |
45 * software without specific, written prior permission. M.I.T. makes | 45 * software without specific, written prior permission. M.I.T. makes |
46 * no representations about the suitability of this software for any 47 * purpose. It is provided "as is" without express or implied 48 * warranty. 49 * 50 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 51 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 52 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 53 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT --- 5 unchanged lines hidden (view full) --- 59 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 60 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 * SUCH DAMAGE. */ 62 63#include <sys/param.h> 64 65#include <err.h> 66#include <ctype.h> | 46 * no representations about the suitability of this software for any 47 * purpose. It is provided "as is" without express or implied 48 * warranty. 49 * 50 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 51 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 52 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 53 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT --- 5 unchanged lines hidden (view full) --- 59 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 60 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 * SUCH DAMAGE. */ 62 63#include <sys/param.h> 64 65#include <err.h> 66#include <ctype.h> |
67#include <netdb.h> |
|
67#include <stdarg.h> 68#include <stdio.h> 69#include <stdlib.h> 70#include <string.h> 71#include <unistd.h> 72 73#include "fetch.h" 74#include "common.h" --- 223 unchanged lines hidden (view full) --- 298 char *ln, *p, *px, *q; 299 FILE *f, *cf; 300 size_t len; 301 302 direct = (flags && strchr(flags, 'd')); 303 verbose = (flags && strchr(flags, 'v')); 304 305 /* allocate cookie */ | 68#include <stdarg.h> 69#include <stdio.h> 70#include <stdlib.h> 71#include <string.h> 72#include <unistd.h> 73 74#include "fetch.h" 75#include "common.h" --- 223 unchanged lines hidden (view full) --- 299 char *ln, *p, *px, *q; 300 FILE *f, *cf; 301 size_t len; 302 303 direct = (flags && strchr(flags, 'd')); 304 verbose = (flags && strchr(flags, 'v')); 305 306 /* allocate cookie */ |
306 if ((c = calloc(1, sizeof(struct cookie))) == NULL) | 307 if ((c = calloc(1, sizeof *c)) == NULL) |
307 return NULL; 308 309 /* check port */ | 308 return NULL; 309 310 /* check port */ |
310 if (!URL->port) 311 URL->port = 80; /* default HTTP port */ | 311 if (!URL->port) { 312 struct servent *se; 313 314 if ((se = getservbyname("http", "tcp")) != NULL) 315 URL->port = ntohs(se->s_port); 316 else 317 URL->port = 80; 318 } |
312 313 /* attempt to connect to proxy server */ 314 if (!direct && (px = getenv("HTTP_PROXY")) != NULL) { 315 char host[MAXHOSTNAMELEN]; | 319 320 /* attempt to connect to proxy server */ 321 if (!direct && (px = getenv("HTTP_PROXY")) != NULL) { 322 char host[MAXHOSTNAMELEN]; |
316 int port = 3128; /* XXX I think 3128 is default... check? */ | 323 int port = 0; |
317 318 /* measure length */ 319 len = strcspn(px, ":"); 320 321 /* get port (XXX atoi is a little too tolerant perhaps?) */ | 324 325 /* measure length */ 326 len = strcspn(px, ":"); 327 328 /* get port (XXX atoi is a little too tolerant perhaps?) */ |
322 if (px[len] == ':') | 329 if (px[len] == ':') { 330 if (strspn(px+len+1, "0123456789") != strlen(px+len+1) 331 || strlen(px+len+1) > 5) { 332 /* XXX we should emit some kind of warning */ 333 } |
323 port = atoi(px+len+1); | 334 port = atoi(px+len+1); |
335 if (port < 1 || port > 65535) { 336 /* XXX we should emit some kind of warning */ 337 } 338 } 339 if (!port) { 340#if 0 341 /* 342 * commented out, since there is currently no service name 343 * for HTTP proxies 344 */ 345 struct servent *se; 346 347 if ((se = getservbyname("xxxx", "tcp")) != NULL) 348 port = ntohs(se->s_port); 349 else 350#endif 351 port = 3128; 352 } |
|
324 325 /* get host name */ 326 if (len >= MAXHOSTNAMELEN) 327 len = MAXHOSTNAMELEN - 1; 328 strncpy(host, px, len); 329 host[len] = 0; 330 331 /* connect */ --- 54 unchanged lines hidden (view full) --- 386 } 387 388 /* browse through header */ 389 while (1) { 390 if ((ln = fgetln(f, &len)) == NULL) 391 goto fouch; 392 if ((ln[0] == '\r') || (ln[0] == '\n')) 393 break; | 353 354 /* get host name */ 355 if (len >= MAXHOSTNAMELEN) 356 len = MAXHOSTNAMELEN - 1; 357 strncpy(host, px, len); 358 host[len] = 0; 359 360 /* connect */ --- 54 unchanged lines hidden (view full) --- 415 } 416 417 /* browse through header */ 418 while (1) { 419 if ((ln = fgetln(f, &len)) == NULL) 420 goto fouch; 421 if ((ln[0] == '\r') || (ln[0] == '\n')) 422 break; |
394 DEBUG(fprintf(stderr, "header: [\033[1m%*.*s\033[m]\n", | 423 DEBUG(fprintf(stderr, "header: [\033[1m%*.*s\033[m]\n", |
395 (int)len-2, (int)len-2, ln)); 396#define XFERENC "Transfer-Encoding:" | 424 (int)len-2, (int)len-2, ln)); 425#define XFERENC "Transfer-Encoding:" |
397 if (strncasecmp(ln, XFERENC, sizeof(XFERENC)-1) == 0) { 398 p = ln + sizeof(XFERENC) - 1; | 426 if (strncasecmp(ln, XFERENC, sizeof XFERENC - 1) == 0) { 427 p = ln + sizeof XFERENC - 1; |
399 while ((p < ln + len) && isspace(*p)) 400 p++; 401 for (q = p; (q < ln + len) && !isspace(*q); q++) 402 /* VOID */ ; 403 *q = 0; 404 if (strcasecmp(p, "chunked") == 0) 405 enc = ENC_CHUNKED; 406 DEBUG(fprintf(stderr, "xferenc: [\033[1m%s\033[m]\n", p)); 407#undef XFERENC 408#define CONTTYPE "Content-Type:" | 428 while ((p < ln + len) && isspace(*p)) 429 p++; 430 for (q = p; (q < ln + len) && !isspace(*q); q++) 431 /* VOID */ ; 432 *q = 0; 433 if (strcasecmp(p, "chunked") == 0) 434 enc = ENC_CHUNKED; 435 DEBUG(fprintf(stderr, "xferenc: [\033[1m%s\033[m]\n", p)); 436#undef XFERENC 437#define CONTTYPE "Content-Type:" |
409 } else if (strncasecmp(ln, CONTTYPE, sizeof(CONTTYPE)-1) == 0) { 410 p = ln + sizeof(CONTTYPE) - 1; | 438 } else if (strncasecmp(ln, CONTTYPE, sizeof CONTTYPE - 1) == 0) { 439 p = ln + sizeof CONTTYPE - 1; |
411 while ((p < ln + len) && isspace(*p)) 412 p++; 413 for (i = 0; p < ln + len; p++) 414 if (i < HTTPCTYPELEN) 415 c->content_type[i++] = *p; 416 do c->content_type[i--] = 0; while (isspace(c->content_type[i])); 417 DEBUG(fprintf(stderr, "conttype: [\033[1m%s\033[m]\n", 418 c->content_type)); --- 5 unchanged lines hidden (view full) --- 424 c->encoding = enc; 425 cf = funopen(c, 426 (int (*)(void *, char *, int))_http_readfn, 427 (int (*)(void *, const char *, int))_http_writefn, 428 (fpos_t (*)(void *, fpos_t, int))NULL, 429 (int (*)(void *))_http_closefn); 430 if (cf == NULL) 431 goto fouch; | 440 while ((p < ln + len) && isspace(*p)) 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)); --- 5 unchanged lines hidden (view full) --- 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 |
|
432 return cf; 433 434ouch: 435 if (sd >= 0) 436 close(sd); 437 free(c); 438 _http_seterr(999); /* XXX do this properly RSN */ 439 return NULL; --- 33 unchanged lines hidden --- | 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; --- 33 unchanged lines hidden --- |