15 typedef struct Hconn Hconn;
16 typedef struct Hpool Hpool;
17 typedef struct Hauth Hauth;
52 static Hpool hpool = {
55 .idle = 5, /* seconds */
61 static void hclose(Hconn *h);
70 snprint(addr, sizeof(addr), "tcp!%s!%s", u->host, u->port ? u->port : u->scheme);
73 for(p = nil, h = hpool.head; h; p = h, h = h->next){
74 if(strcmp(h->addr, addr) == 0){
87 fprint(2, "hdial [%d] %s\n", hpool.active, addr);
89 if((fd = dial(addr, 0, 0, &ctl)) < 0)
91 if(strcmp(u->scheme, "https") == 0){
95 strcpy(err, "tls error");
96 memset(&conn, 0, sizeof(conn));
97 if((fd = tlsClient(ofd = fd, &conn)) < 0)
98 errstr(err, sizeof(err));
100 free(conn.sessionID);
104 if(debug) fprint(2, "tlsClient: %s\n", err);
105 errstr(err, sizeof(err));
110 h = emalloc(sizeof(*h));
118 nstrcpy(h->addr, addr, sizeof(h->addr));
146 if(h->keep && h->fd >= 0){
147 for(n = 0, i = 0, t = nil, x = hpool.head; x; x = x->next){
148 if(strcmp(x->addr, h->addr) == 0)
151 if(++i < hpool.limit)
155 /* return connection to pool */
157 h->next = hpool.head;
173 * if h is first one in pool, spawn proc to close
177 if(rfork(RFMEM|RFPROC|RFNOWAIT) == 0){
190 if((now - h->time) > hpool.idle){
201 i = hpool.head != nil;
216 fprint(2, "hclose [%d] %s\n", hpool.active, h->addr);
229 fprint(2, "hangup pc=%p: %r\n", getcallerpc(&h));
236 hread(Hconn *h, void *data, int len)
241 memmove(data, h->buf, len);
244 memmove(h->buf, h->buf + len, h->len);
247 if((len = read(h->fd, data, len)) == 0)
255 hwrite(Hconn *h, void *data, int len)
257 if(write(h->fd, data, len) != len){
265 hline(Hconn *h, char *data, int len, int cont)
273 while(x = memchr(h->buf, '\n', h->len)){
275 if(n > 0 && x[-1] == '\r')
279 for(y = x+1; y < e; y++)
280 if(*y != ' ' && *y != '\t')
282 if(y >= e || *y == 0)
285 if(x > h->buf && x[-1] == '\r')
287 memmove(x, y, e - y);
294 memmove(data, h->buf, len);
296 h->len -= (++x - h->buf);
298 memmove(h->buf, x, h->len);
302 if(h->len >= sizeof(h->buf))
304 if((n = read(h->fd, h->buf + h->len, sizeof(h->buf) - h->len)) <= 0){
313 authenticate(Url *u, Url *ru, char *method, char *s)
315 char *user, *pass, *realm, *nonce, *opaque, *x;
322 realm = nonce = opaque = nil;
323 if(!cistrncmp(s, "Basic ", 6)){
324 char cred[128], plain[128];
328 if(x = cistrstr(s, "realm="))
329 realm = unquote(x+6, &s);
333 if(user == nil || pass == nil){
335 fmtprint(&fmt, " realm=%q", realm);
337 fmtprint(&fmt, " user=%q", user);
338 if((s = fmtstrflush(&fmt)) == nil)
340 up = auth_getuserpasswd(nil, "proto=pass service=http server=%q%s", u->host, s);
347 n = snprint(plain, sizeof(plain), "%s:%s", user ? user : "", pass ? pass : "");
349 memset(up->user, 0, strlen(up->user));
350 memset(up->passwd, 0, strlen(up->passwd));
353 n = enc64(cred, sizeof(cred), (uchar*)plain, n);
354 memset(plain, 0, sizeof(plain));
358 fmtprint(&fmt, "Basic %s", cred);
359 memset(cred, 0, sizeof(cred));
360 u = saneurl(url(".", u)); /* all uris below the requested one */
362 if(!cistrncmp(s, "Digest ", 7)){
363 char chal[1024], ouser[128], resp[2*MD5LEN+1];
367 if(x = cistrstr(s, "realm="))
368 realm = unquote(x+6, &s);
369 if(x = cistrstr(s, "nonce="))
370 nonce = unquote(x+6, &s);
371 if(x = cistrstr(s, "opaque="))
372 opaque = unquote(x+7, &s);
373 if(realm == nil || nonce == nil)
376 fmtprint(&fmt, " realm=%q", realm);
378 fmtprint(&fmt, " user=%q", user);
379 if((s = fmtstrflush(&fmt)) == nil)
381 nchal = snprint(chal, sizeof(chal), "%s %s %U", nonce, method, ru);
382 n = auth_respond(chal, nchal, ouser, sizeof ouser, resp, sizeof resp, nil,
383 "proto=httpdigest role=client server=%q%s", u->host, s);
384 memset(chal, 0, sizeof(chal));
389 fmtprint(&fmt, "Digest ");
390 fmtprint(&fmt, "username=\"%s\", ", ouser);
391 fmtprint(&fmt, "realm=\"%s\", ", realm);
392 fmtprint(&fmt, "host=\"%s\", ", u->host);
393 fmtprint(&fmt, "uri=\"%U\", ", ru);
394 fmtprint(&fmt, "nonce=\"%s\", ", nonce);
395 fmtprint(&fmt, "response=\"%s\"", resp);
397 fmtprint(&fmt, ", opaque=\"%s\"", opaque);
398 u = saneurl(url("/", u)); /* BUG: should be the ones in domain= only */
401 if((s = fmtstrflush(&fmt)) == nil){
409 a = emalloc(sizeof(*a));
421 hauthenticate(Url *u, Url *ru, char *method, char *key, Key *hdr)
423 for(hdr = getkey(hdr, key); hdr != nil; hdr = getkey(hdr->next, key))
424 if(authenticate(u, ru, method, hdr->val) == 0)
430 flushauth(Url *u, char *t)
436 for(p = nil, a = hauth; a; p = a, a = a->next)
437 if(matchurl(u, a->url) && (t == nil || !strcmp(t, a->auth))){
443 fprint(2, "flushauth for %U\n", a->url);
445 memset(a->auth, 0, strlen(a->auth));
454 catch(void *, char *msg)
456 if(strstr("alarm", msg) || strstr("die", msg))
462 #define NOLENGTH 0x7fffffffffffffffLL
465 http(char *m, Url *u, Key *shdr, Buq *qbody, Buq *qpost)
467 int i, l, n, try, pid, fd, cfd, needlength, chunked, retry, nobody;
468 char *s, *x, buf[8192+2], status[256], method[16];
469 vlong length, offset;
476 if(qpost) incref(qpost);
477 nstrcpy(method, m, sizeof(method));
478 switch(rfork(RFPROC|RFMEM|RFNOWAIT)){
482 buclose(qbody, "can't fork");
484 buclose(qpost, "can't fork");
498 /* file for spooling the postbody if we need to restart the request */
499 snprint(buf, sizeof(buf), "/tmp/http.%d.%d.post", getppid(), getpid());
500 fd = create(buf, OEXCL|ORDWR|ORCLOSE, 0600);
507 for(try = 0; try < 12; try++){
508 strcpy(status, "0 No status");
509 if(u == nil || (strcmp(u->scheme, "http") && strcmp(u->scheme, "https"))){
510 werrstr("bad url scheme");
515 fprint(2, "http(%d): %s %U\n", try, method, u);
517 /* preemptive authentication from hauth cache */
519 if(proxy && !lookkey(shdr, "Proxy-Authorization"))
520 for(a = hauth; a; a = a->next)
521 if(matchurl(a->url, proxy)){
522 shdr = addkey(shdr, "Proxy-Authorization", a->auth);
525 if(!lookkey(shdr, "Authorization"))
526 for(a = hauth; a; a = a->next)
527 if(matchurl(a->url, u)){
528 shdr = addkey(shdr, "Authorization", a->auth);
536 /* have to read it to temp file to figure out the length */
537 if(fd >= 0 && needlength && lookkey(shdr, "Content-Length") == nil){
539 while((n = buread(qpost, buf, sizeof(buf))) > 0)
541 shdr = delkey(shdr, "Transfer-Encoding");
545 /* wait until buffer is full, most posts are small */
546 while(!qpost->closed && qpost->size < qpost->limit && qpost->nwq == 0)
549 if(lookkey(shdr, "Content-Length"))
551 else if(x = lookkey(shdr, "Transfer-Encoding"))
552 chunked = cistrstr(x, "chunked") != nil;
553 else if(chunked = !qpost->closed)
554 shdr = addkey(shdr, "Transfer-Encoding", "chunked");
555 else if(qpost->closed){
557 length = seek(fd, 0, 2);
561 length += qpost->size;
562 snprint(buf, sizeof(buf), "%lld", length);
563 shdr = addkey(shdr, "Content-Length", buf);
572 memset(&ru, 0, sizeof(tu));
576 n = snprint(buf, sizeof(buf), "%s %U HTTP/1.1\r\nHost: %H%s%s\r\n",
577 method, &ru, u->host, u->port ? ":" : "", u->port ? u->port : "");
578 if(n >= sizeof(buf)-64){
579 werrstr("request too large");
584 if((h = hdial(proxy ? proxy : u)) == nil)
588 if((cfd = open("/mnt/webcookies/http", ORDWR)) >= 0){
589 /* only scheme, host and path are relevant for cookies */
590 memset(&tu, 0, sizeof(tu));
591 tu.scheme = u->scheme;
594 fprint(cfd, "%U", &tu);
596 if(n >= sizeof(buf)-2){
598 fprint(2, "-> %.*s", n, buf);
599 if(hwrite(h, buf, n) != n)
603 if((l = read(cfd, buf+n, sizeof(buf)-2 - n)) == 0)
614 for(k = shdr; k; k = k->next)
615 n += snprint(buf+n, sizeof(buf)-2 - n, "%s: %s\r\n", k->key, k->val);
616 n += snprint(buf+n, sizeof(buf)-n, "\r\n");
618 fprint(2, "-> %.*s", n, buf);
619 if(hwrite(h, buf, n) != n){
627 if((pid = rfork(RFMEM|RFPROC)) <= 0){
634 if((ifd < 0) || ((n = read(ifd, buf, sizeof(buf)-2)) <= 0)){
636 if((n = buread(qpost, buf, sizeof(buf)-2)) <= 0)
639 if(write(fd, buf, n) != n)
645 if(hwrite(h, tmp, snprint(tmp, sizeof(tmp), "%x\r\n", n)) < 0)
650 if(hwrite(h, buf, n) != n)
655 hwrite(h, "0\r\n\r\n", 5);
661 /* no timeout when posting */
672 for(l = 0; hline(h, s = buf, sizeof(buf)-1, 1) > 0; l++){
674 fprint(2, "<- %s\n", s);
676 if(x = strchr(s, ' '))
679 if(cistrncmp(s, "HTTP", 4)){
681 if(cistrcmp(s, "ICY"))
685 nstrcpy(status, x, sizeof(status));
688 if((k = parsehdr(s)) == nil)
690 if(!cistrcmp(k->key, "Connection")){
691 if(cistrstr(k->val, "close"))
694 else if(!cistrcmp(k->key, "Content-Length"))
695 length = atoll(k->val);
696 else if(!cistrcmp(k->key, "Transfer-Encoding")){
697 if(cistrstr(k->val, "chunked"))
700 else if(!cistrcmp(k->key, "Set-Cookie") ||
701 !cistrcmp(k->key, "Set-Cookie2")){
703 fprint(cfd, "Set-Cookie: %s\n", k->val);
716 nobody = !cistrcmp(method, "HEAD");
717 if((i = atoi(status)) < 0)
726 case 100: /* Continue */
727 case 101: /* Switching Protocols */
733 case 304: /* Not Modified */
735 case 305: /* Use Proxy */
736 case 400: /* Bad Request */
737 case 402: /* Payment Required */
738 case 403: /* Forbidden */
739 case 404: /* Not Found */
740 case 405: /* Method Not Allowed */
741 case 406: /* Not Acceptable */
742 case 408: /* Request Timeout */
743 case 409: /* Conflict */
746 case 411: /* Length Required */
753 case 412: /* Precondition Failed */
754 case 413: /* Request Entity Too Large */
755 case 414: /* Request URI Too Large */
756 case 415: /* Unsupported Media Type */
757 case 416: /* Requested Range Not Satisfiable */
758 case 417: /* Expectation Failed */
759 case 500: /* Internal server error */
760 case 501: /* Not implemented */
761 case 502: /* Bad gateway */
762 case 503: /* Service unavailable */
763 case 504: /* Gateway Timeout */
764 case 505: /* HTTP Version not Supported */
767 buclose(qbody, status);
768 buclose(qpost, status);
770 case 300: /* Multiple choices */
771 case 302: /* Found */
772 case 303: /* See Other */
782 shdr = delkey(shdr, "Content-Length");
783 shdr = delkey(shdr, "Content-Type");
784 shdr = delkey(shdr, "Transfer-Encoding");
785 if(cistrcmp(method, "HEAD"))
786 nstrcpy(method, "GET", sizeof(method));
787 case 301: /* Moved Permanently */
788 case 307: /* Temporary Redirect */
789 case 308: /* Resume Incomplete */
790 if((x = lookkey(rhdr, "Location")) == nil)
792 if((nu = saneurl(url(x, u))) == nil)
797 case 401: /* Unauthorized */
798 if(x = lookkey(shdr, "Authorization"))
800 if(hauthenticate(u, &ru, method, "WWW-Authenticate", rhdr) < 0)
804 case 407: /* Proxy Auth */
807 if(x = lookkey(shdr, "Proxy-Authorization"))
809 if(hauthenticate(u, &ru, method, "Proxy-Authenticate", rhdr) < 0)
812 case 0: /* No status */
821 case 204: /* No Content */
822 case 205: /* Reset Content */
825 case 201: /* Created */
826 case 202: /* Accepted */
827 case 203: /* Non-Authoritative Information */
828 case 206: /* Partial Content */
829 qbody->url = u; u = nil;
830 qbody->hdr = rhdr; rhdr = nil;
842 * remove authorization headers so on the next round, we use
843 * the hauth cache (wich checks the scope url). this makes
844 * sure we wont send credentials to the wrong url after
847 shdr = delkey(shdr, "Proxy-Authorization");
848 shdr = delkey(shdr, "Authorization");
850 if(!chunked && length == NOLENGTH)
854 * read the response body (if any). retry means we'r just
855 * skipping the error page so we wont touch qbody.
858 if((qbody->closed || retry) && !h->keep)
861 if(hline(h, buf, sizeof(buf)-1, 0) <= 0)
863 length = strtoll(buf, nil, 16);
866 while(offset < length){
868 if(l > (length - offset))
869 l = (length - offset);
870 if((n = hread(h, buf, l)) <= 0)
874 if(buwrite(qbody, buf, n) != n)
877 if(offset != length){
879 if(length != NOLENGTH)
883 while(hline(h, buf, sizeof(buf)-1, 1) > 0){
885 fprint(2, "<= %s\n", buf);
887 if(k = parsehdr(buf)){
888 k->next = qbody->hdr;
913 snprint(buf, sizeof(buf), "%s %r", status);