http.c (60707) | http.c (60737) |
---|---|
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 60707 2000-05-19 09:45:42Z des $ | 28 * $FreeBSD: head/lib/libfetch/http.c 60737 2000-05-20 18:23:51Z ume $ |
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 --- 19 unchanged lines hidden (view full) --- 56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 57 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 58 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 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> | 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 --- 19 unchanged lines hidden (view full) --- 56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 57 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 58 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 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#include <sys/socket.h> |
|
64 65#include <err.h> 66#include <ctype.h> 67#include <locale.h> 68#include <netdb.h> 69#include <stdarg.h> 70#include <stdio.h> 71#include <stdlib.h> --- 225 unchanged lines hidden (view full) --- 297 298/* 299 * Connect to server or proxy 300 */ 301FILE * 302_http_connect(struct url *URL, char *flags) 303{ 304 int direct, sd = -1, verbose; | 65 66#include <err.h> 67#include <ctype.h> 68#include <locale.h> 69#include <netdb.h> 70#include <stdarg.h> 71#include <stdio.h> 72#include <stdlib.h> --- 225 unchanged lines hidden (view full) --- 298 299/* 300 * Connect to server or proxy 301 */ 302FILE * 303_http_connect(struct url *URL, char *flags) 304{ 305 int direct, sd = -1, verbose; |
306#ifdef INET6 307 int af = AF_UNSPEC; 308#else 309 int af = AF_INET; 310#endif |
|
305 size_t len; 306 char *px; 307 FILE *f; 308 309 direct = (flags && strchr(flags, 'd')); 310 verbose = (flags && strchr(flags, 'v')); | 311 size_t len; 312 char *px; 313 FILE *f; 314 315 direct = (flags && strchr(flags, 'd')); 316 verbose = (flags && strchr(flags, 'v')); |
317 if ((flags && strchr(flags, '4'))) 318 af = AF_INET; 319 else if ((flags && strchr(flags, '6'))) 320 af = AF_INET6; |
|
311 312 /* check port */ 313 if (!URL->port) { 314 struct servent *se; 315 316 if (strcasecmp(URL->scheme, "ftp") == 0) 317 if ((se = getservbyname("ftp", "tcp")) != NULL) 318 URL->port = ntohs(se->s_port); --- 7 unchanged lines hidden (view full) --- 326 } 327 328 /* attempt to connect to proxy server */ 329 if (!direct && (px = getenv("HTTP_PROXY")) != NULL) { 330 char host[MAXHOSTNAMELEN]; 331 int port = 0; 332 333 /* measure length */ | 321 322 /* check port */ 323 if (!URL->port) { 324 struct servent *se; 325 326 if (strcasecmp(URL->scheme, "ftp") == 0) 327 if ((se = getservbyname("ftp", "tcp")) != NULL) 328 URL->port = ntohs(se->s_port); --- 7 unchanged lines hidden (view full) --- 336 } 337 338 /* attempt to connect to proxy server */ 339 if (!direct && (px = getenv("HTTP_PROXY")) != NULL) { 340 char host[MAXHOSTNAMELEN]; 341 int port = 0; 342 343 /* measure length */ |
334 len = strcspn(px, ":"); | 344#ifdef INET6 345 if (px[0] != '[' || 346 (len = strcspn(px, "]")) >= strlen(px) || 347 (px[++len] != '\0' && px[len] != ':')) 348#endif 349 len = strcspn(px, ":"); |
335 336 /* get port (XXX atoi is a little too tolerant perhaps?) */ 337 if (px[len] == ':') { 338 if (strspn(px+len+1, "0123456789") != strlen(px+len+1) 339 || strlen(px+len+1) > 5) { 340 /* XXX we should emit some kind of warning */ 341 } 342 port = atoi(px+len+1); --- 12 unchanged lines hidden (view full) --- 355 if ((se = getservbyname("xxxx", "tcp")) != NULL) 356 port = ntohs(se->s_port); 357 else 358#endif 359 port = 3128; 360 } 361 362 /* get host name */ | 350 351 /* get port (XXX atoi is a little too tolerant perhaps?) */ 352 if (px[len] == ':') { 353 if (strspn(px+len+1, "0123456789") != strlen(px+len+1) 354 || strlen(px+len+1) > 5) { 355 /* XXX we should emit some kind of warning */ 356 } 357 port = atoi(px+len+1); --- 12 unchanged lines hidden (view full) --- 370 if ((se = getservbyname("xxxx", "tcp")) != NULL) 371 port = ntohs(se->s_port); 372 else 373#endif 374 port = 3128; 375 } 376 377 /* get host name */ |
378#ifdef INET6 379 if (len > 1 && px[0] == '[' && px[len - 1] == ']') { 380 px++; 381 len -= 2; 382 } 383#endif |
|
363 if (len >= MAXHOSTNAMELEN) 364 len = MAXHOSTNAMELEN - 1; 365 strncpy(host, px, len); 366 host[len] = 0; 367 368 /* connect */ | 384 if (len >= MAXHOSTNAMELEN) 385 len = MAXHOSTNAMELEN - 1; 386 strncpy(host, px, len); 387 host[len] = 0; 388 389 /* connect */ |
369 sd = _fetch_connect(host, port, verbose); | 390 sd = _fetch_connect(host, port, af, verbose); |
370 } 371 372 /* if no proxy is configured or could be contacted, try direct */ 373 if (sd == -1) { 374 if (strcasecmp(URL->scheme, "ftp") == 0) 375 goto ouch; | 391 } 392 393 /* if no proxy is configured or could be contacted, try direct */ 394 if (sd == -1) { 395 if (strcasecmp(URL->scheme, "ftp") == 0) 396 goto ouch; |
376 if ((sd = _fetch_connect(URL->host, URL->port, verbose)) == -1) | 397 if ((sd = _fetch_connect(URL->host, URL->port, af, verbose)) == -1) |
377 goto ouch; 378 } 379 380 /* reopen as stream */ 381 if ((f = fdopen(sd, "r+")) == NULL) 382 goto ouch; 383 384 return f; --- 9 unchanged lines hidden (view full) --- 394 * Send a HEAD or GET request 395 */ 396int 397_http_request(FILE *f, char *op, struct url *URL, char *flags) 398{ 399 int e, verbose; 400 char *ln, *p; 401 size_t len; | 398 goto ouch; 399 } 400 401 /* reopen as stream */ 402 if ((f = fdopen(sd, "r+")) == NULL) 403 goto ouch; 404 405 return f; --- 9 unchanged lines hidden (view full) --- 415 * Send a HEAD or GET request 416 */ 417int 418_http_request(FILE *f, char *op, struct url *URL, char *flags) 419{ 420 int e, verbose; 421 char *ln, *p; 422 size_t len; |
423 char *host; 424#ifdef INET6 425 char hbuf[MAXHOSTNAMELEN + 1]; 426#endif |
|
402 403 verbose = (flags && strchr(flags, 'v')); | 427 428 verbose = (flags && strchr(flags, 'v')); |
429 430 host = URL->host; 431#ifdef INET6 432 if (strchr(URL->host, ':')) { 433 snprintf(hbuf, sizeof(hbuf), "[%s]", URL->host); 434 host = hbuf; 435 } 436#endif |
|
404 405 /* send request (proxies require absolute form, so use that) */ 406 if (verbose) 407 _fetch_info("requesting %s://%s:%d%s", | 437 438 /* send request (proxies require absolute form, so use that) */ 439 if (verbose) 440 _fetch_info("requesting %s://%s:%d%s", |
408 URL->scheme, URL->host, URL->port, URL->doc); | 441 URL->scheme, host, URL->port, URL->doc); |
409 _http_cmd(f, "%s %s://%s:%d%s HTTP/1.1" ENDL, | 442 _http_cmd(f, "%s %s://%s:%d%s HTTP/1.1" ENDL, |
410 op, URL->scheme, URL->host, URL->port, URL->doc); | 443 op, URL->scheme, host, URL->port, URL->doc); |
411 412 /* start sending headers away */ 413 if (URL->user[0] || URL->pwd[0]) { 414 char *auth_str = _http_auth(URL->user, URL->pwd); 415 if (!auth_str) 416 return 999; /* XXX wrong */ 417 _http_cmd(f, "Authorization: Basic %s" ENDL, auth_str); 418 free(auth_str); 419 } | 444 445 /* start sending headers away */ 446 if (URL->user[0] || URL->pwd[0]) { 447 char *auth_str = _http_auth(URL->user, URL->pwd); 448 if (!auth_str) 449 return 999; /* XXX wrong */ 450 _http_cmd(f, "Authorization: Basic %s" ENDL, auth_str); 451 free(auth_str); 452 } |
420 _http_cmd(f, "Host: %s:%d" ENDL, URL->host, URL->port); | 453 _http_cmd(f, "Host: %s:%d" ENDL, host, URL->port); |
421 _http_cmd(f, "User-Agent: %s " _LIBFETCH_VER ENDL, __progname); 422 if (URL->offset) 423 _http_cmd(f, "Range: bytes=%lld-" ENDL, URL->offset); 424 _http_cmd(f, "Connection: close" ENDL ENDL); 425 426 /* get response */ 427 if ((ln = fgetln(f, &len)) == NULL) 428 return 999; --- 206 unchanged lines hidden --- | 454 _http_cmd(f, "User-Agent: %s " _LIBFETCH_VER ENDL, __progname); 455 if (URL->offset) 456 _http_cmd(f, "Range: bytes=%lld-" ENDL, URL->offset); 457 _http_cmd(f, "Connection: close" ENDL ENDL); 458 459 /* get response */ 460 if ((ln = fgetln(f, &len)) == NULL) 461 return 999; --- 206 unchanged lines hidden --- |