15 typedef struct Hconn Hconn;
16 typedef struct Hpool Hpool;
17 typedef struct Hauth Hauth;
50 static Hpool hpool = {
52 .idle = 5, /* seconds */
58 static void hclose(Hconn *h);
67 snprint(addr, sizeof(addr), "tcp!%s!%s", u->host, u->port ? u->port : u->scheme);
70 for(p = nil, h = hpool.head; h; p = h, h = h->next){
71 if(strcmp(h->addr, addr) == 0){
84 fprint(2, "hdial [%d] %s\n", hpool.active, addr);
86 if((fd = dial(addr, 0, 0, 0)) < 0)
88 if(strcmp(u->scheme, "https") == 0){
91 tc = emalloc(sizeof(*tc));
92 fd = tlsClient(ofd = fd, tc);
94 /* BUG: should validate but how? */
102 h = emalloc(sizeof(*h));
109 strncpy(h->addr, addr, sizeof(h->addr));
137 if(h->keep && h->fd >= 0){
138 for(i = 0, t = nil, x = hpool.head; x; x = x->next){
139 if(strcmp(x->addr, h->addr) == 0)
141 if(++i < hpool.limit)
145 /* return connection to pool */
147 h->next = hpool.head;
163 * if h is first one in pool, spawn proc to close
167 if(rfork(RFMEM|RFPROC|RFNOWAIT) == 0){
180 if((now - h->time) > hpool.idle){
191 i = hpool.head != nil;
206 fprint(2, "hclose [%d] %s\n", hpool.active, h->addr);
214 hread(Hconn *h, void *data, int len)
219 memmove(data, h->buf, len);
222 memmove(h->buf, h->buf + len, h->len);
225 if((len = read(h->fd, data, len)) <= 0)
231 hwrite(Hconn *h, void *data, int len)
233 if(write(h->fd, data, len) != len){
241 hline(Hconn *h, char *data, int len, int cont)
249 while(x = memchr(h->buf, '\n', h->len)){
251 if(n > 0 && x[-1] == '\r')
255 for(y = x+1; y < e; y++)
256 if(!strchr("\t ", *y))
258 if(y >= e || strchr("\t ", *y))
261 if(x > h->buf && x[-1] == '\r')
263 memmove(x, y, e - y);
270 memmove(data, h->buf, len);
272 h->len -= (++x - h->buf);
274 memmove(h->buf, x, h->len);
278 if(h->len >= sizeof(h->buf))
280 if((n = read(h->fd, h->buf + h->len, sizeof(h->buf) - h->len)) <= 0){
289 authenticate(Url *u, Url *ru, char *method, char *s)
291 char *user, *pass, *realm, *nonce, *opaque, *x;
300 realm = nonce = opaque = nil;
302 if(!cistrncmp(s, "Basic ", 6)){
306 if(x = cistrstr(s, "realm="))
307 realm = unquote(x+6, &s);
310 if(user == nil || pass == nil){
311 fmtprint(&fmt, " realm=%q", realm);
313 fmtprint(&fmt, " user=%q", user);
314 if((s = fmtstrflush(&fmt)) == nil)
316 if((up = auth_getuserpasswd(nil, "proto=pass service=http server=%q%s",
323 fmtprint(&fmt, "%s:%s", user ? user : "", pass ? pass : "");
325 if((s = fmtstrflush(&fmt)) == nil)
327 n = enc64(cred, sizeof(cred), (uchar*)s, strlen(s));
328 memset(s, 0, strlen(s));
333 fmtprint(&fmt, "Basic %s", cred);
334 u = saneurl(url(".", u)); /* all uris below the requested one */
336 if(!cistrncmp(s, "Digest ", 7)){
337 char chal[1024], ouser[128], resp[2*MD5LEN+1];
341 if(x = cistrstr(s, "realm="))
342 realm = unquote(x+6, &s);
343 if(x = cistrstr(s, "nonce="))
344 nonce = unquote(x+6, &s);
345 if(x = cistrstr(s, "opaque="))
346 opaque = unquote(x+7, &s);
347 if(realm == nil || nonce == nil)
349 nchal = snprint(chal, sizeof(chal), "%s %s %U", nonce, method, ru);
350 fmtprint(&fmt, " realm=%q", realm);
352 fmtprint(&fmt, " user=%q", user);
353 if((s = fmtstrflush(&fmt)) == nil)
355 if(auth_respond(chal, nchal, ouser, sizeof ouser, resp, sizeof resp, nil,
356 "proto=httpdigest role=client server=%q%s", u->host, s) < 0)
359 fmtprint(&fmt, "Digest ");
360 fmtprint(&fmt, "username=\"%s\", ", ouser);
361 fmtprint(&fmt, "realm=\"%s\", ", realm);
362 fmtprint(&fmt, "host=\"%s\", ", u->host);
363 fmtprint(&fmt, "uri=\"%U\", ", ru);
364 fmtprint(&fmt, "nonce=\"%s\", ", nonce);
365 fmtprint(&fmt, "response=\"%s\"", resp);
367 fmtprint(&fmt, ", opaque=\"%s\"", opaque);
368 u = saneurl(url("/", u)); /* BUG: should be the ones in domain= only */
373 if((s = fmtstrflush(&fmt)) == nil){
377 a = emalloc(sizeof(*a));
389 flushauth(Url *u, char *t)
395 for(p = nil, a = hauth; a; p = a, a = a->next)
396 if(matchurl(u, a->url) && (t == nil || !strcmp(t, a->auth))){
402 fprint(2, "flushauth for %U\n", a->url);
404 memset(a->auth, 0, strlen(a->auth));
413 catch(void *, char *msg)
415 if(strstr("alarm", msg) || strstr("die", msg))
421 #define NOLENGTH 0x7fffffffffffffffLL
424 http(char *m, Url *u, Key *shdr, Buq *qbody, Buq *qpost)
426 int i, l, n, try, pid, fd, cfd, chunked, retry, nobody;
427 char *s, *x, buf[8192+2], status[256], method[16];
428 vlong length, offset;
435 if(qpost) incref(qpost);
436 strncpy(method, m, sizeof(method));
437 switch(rfork(RFPROC|RFMEM|RFNOWAIT)){
441 buclose(qbody, "can't fork");
443 buclose(qpost, "can't fork");
457 /* file for spooling the postbody if we need to restart the request */
458 snprint(buf, sizeof(buf), "/tmp/http.%d.%d.post", getppid(), getpid());
459 fd = create(buf, OEXCL|ORDWR|ORCLOSE, 0600);
465 werrstr("too many errors");
466 for(try = 0; try < 6; try++){
467 if(u == nil || (strcmp(u->scheme, "http") && strcmp(u->scheme, "https"))){
473 fprint(2, "http(%d): %s %U\n", try, method, u);
475 /* preemptive authentication from hauth cache */
477 if(proxy && !lookkey(shdr, "Proxy-Authorization"))
478 for(a = hauth; a; a = a->next)
479 if(matchurl(a->url, proxy)){
480 shdr = addkey(shdr, "Proxy-Authorization", a->auth);
483 if(!lookkey(shdr, "Authorization"))
484 for(a = hauth; a; a = a->next)
485 if(matchurl(a->url, u)){
486 shdr = addkey(shdr, "Authorization", a->auth);
495 memset(&ru, 0, sizeof(tu));
499 n = snprint(buf, sizeof(buf), "%s %U HTTP/1.1\r\nHost: %s%s%s\r\n",
500 method, &ru, u->host, u->port ? ":" : "", u->port ? u->port : "");
502 for(k = shdr; k; k = k->next)
503 n += snprint(buf+n, sizeof(buf)-2 - n, "%s: %s\r\n", k->key, k->val);
505 if(n >= sizeof(buf)-64){
506 werrstr("request too large");
510 nobody = !cistrcmp(method, "HEAD");
515 /* wait until buffer is full, most posts are small */
516 while(!qpost->closed && qpost->size < qpost->limit)
519 if(lookkey(shdr, "Content-Length"))
521 else if(x = lookkey(shdr, "Transfer-Encoding"))
522 chunked = cistrstr(x, "chunked") != nil;
523 else if(chunked = !qpost->closed)
524 n += snprint(buf+n, sizeof(buf)-n, "Transfer-Encoding: chunked\r\n");
525 else if(qpost->closed){
527 length = seek(fd, 0, 2);
531 length += qpost->size;
532 n += snprint(buf+n, sizeof(buf)-n, "Content-Length: %lld\r\n", length);
537 /* give 5 seconds to dial */
540 if((h = hdial(proxy ? proxy : u)) == nil)
544 if((cfd = open("/mnt/webcookies/http", ORDWR)) >= 0){
545 /* only scheme, host and path are relevant for cookies */
546 memset(&tu, 0, sizeof(tu));
547 tu.scheme = u->scheme;
550 fprint(cfd, "%U", &tu);
552 if(n >= sizeof(buf)-2){
554 fprint(2, "-> %.*s", n, buf);
555 if(hwrite(h, buf, n) != n)
559 if((l = read(cfd, buf+n, sizeof(buf)-2 - n)) == 0)
570 n += snprint(buf+n, sizeof(buf)-n, "\r\n");
572 fprint(2, "-> %.*s", n, buf);
573 if(hwrite(h, buf, n) != n){
581 if((pid = rfork(RFMEM|RFPROC)) <= 0){
588 if((ifd < 0) || ((n = read(ifd, buf, sizeof(buf)-2)) <= 0)){
590 if((n = buread(qpost, buf, sizeof(buf)-2)) <= 0)
593 if(write(fd, buf, n) != n)
598 hwrite(h, tmp, snprint(tmp, sizeof(tmp), "%x\r\n", n));
602 if(hwrite(h, buf, n) != n)
606 hwrite(h, "0\r\n\r\n", 5);
612 /* no timeout when posting */
615 /* wait 10 seconds for the response */
626 for(l = 0; hline(h, s = buf, sizeof(buf)-1, 1) > 0; l++){
628 fprint(2, "<- %s\n", s);
630 if(x = strchr(s, ' '))
633 if(cistrncmp(s, "HTTP", 4)){
635 if(cistrcmp(s, "ICY"))
638 strncpy(status, x, sizeof(status));
641 if((k = parsehdr(s)) == nil)
643 if(!cistrcmp(k->key, "Connection")){
644 if(cistrstr(k->val, "close"))
647 else if(!cistrcmp(k->key, "Content-Length"))
648 length = atoll(k->val);
649 else if(!cistrcmp(k->key, "Transfer-Encoding")){
650 if(cistrstr(k->val, "chunked"))
653 else if(!cistrcmp(k->key, "Set-Cookie") ||
654 !cistrcmp(k->key, "Set-Cookie2")){
656 fprint(cfd, "Set-Cookie: %s\n", k->val);
669 if((i = atoi(status)) < 0)
678 case 100: /* Continue */
679 case 101: /* Switching Protocols */
685 case 304: /* Not Modified */
687 case 305: /* Use Proxy */
688 case 400: /* Bad Request */
689 case 402: /* Payment Required */
690 case 403: /* Forbidden */
691 case 404: /* Not Found */
692 case 405: /* Method Not Allowed */
693 case 406: /* Not Acceptable */
694 case 408: /* Request Timeout */
695 case 409: /* Conflict */
697 case 411: /* Length Required */
698 case 412: /* Precondition Failed */
699 case 413: /* Request Entity Too Large */
700 case 414: /* Request URI Too Large */
701 case 415: /* Unsupported Media Type */
702 case 416: /* Requested Range Not Satisfiable */
703 case 417: /* Expectation Failed */
704 case 500: /* Internal server error */
705 case 501: /* Not implemented */
706 case 502: /* Bad gateway */
707 case 503: /* Service unavailable */
708 case 504: /* Gateway Timeout */
709 case 505: /* HTTP Version not Supported */
712 buclose(qbody, status);
713 buclose(qpost, status);
715 case 300: /* Multiple choices */
716 case 302: /* Found */
717 case 303: /* See Other */
727 if(cistrcmp(method, "HEAD"))
728 strncpy(method, "GET", sizeof(method));
729 case 301: /* Moved Permanently */
730 case 307: /* Temporary Redirect */
731 case 308: /* Resume Incomplete */
732 if((x = lookkey(rhdr, "Location")) == nil)
734 if((nu = saneurl(url(x, u))) == nil)
739 case 401: /* Unauthorized */
740 if(x = lookkey(shdr, "Authorization"))
742 if((x = lookkey(rhdr, "WWW-Authenticate")) == nil)
744 if(authenticate(u, &ru, method, x) < 0)
748 case 407: /* Proxy Auth */
751 if(x = lookkey(shdr, "Proxy-Authorization"))
753 if((x = lookkey(rhdr, "Proxy-Authenticate")) == nil)
755 if(authenticate(proxy, proxy, method, x) < 0)
758 case 0: /* No status */
767 case 204: /* No Content */
768 case 205: /* Reset Content */
771 case 201: /* Created */
772 case 202: /* Accepted */
773 case 203: /* Non-Authoritative Information */
774 case 206: /* Partial Content */
775 qbody->url = u; u = nil;
776 qbody->hdr = rhdr; rhdr = nil;
788 * remove authorization headers so on the next round, we use
789 * the hauth cache (wich checks the scope url). this makes
790 * sure we wont send credentials to the wrong url after
793 shdr = delkey(shdr, "Proxy-Authorization");
794 shdr = delkey(shdr, "Authorization");
796 if(!chunked && length == NOLENGTH)
800 * read the response body (if any). retry means we'r just
801 * skipping the error page so we wont touch qbody.
804 if((qbody->closed || retry) && !h->keep)
807 if(hline(h, buf, sizeof(buf)-1, 0) <= 0)
809 length = strtoll(buf, nil, 16);
812 while(offset < length){
814 if(l > (length - offset))
815 l = (length - offset);
816 if((n = hread(h, buf, l)) <= 0)
820 if(buwrite(qbody, buf, n) != n)
823 if(offset != length){
825 if(length != NOLENGTH)
829 while(hline(h, buf, sizeof(buf)-1, 1) > 0){
831 fprint(2, "<= %s\n", buf);
833 if(k = parsehdr(buf)){
834 k->next = qbody->hdr;
860 rerrstr(buf, sizeof(buf));