]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/cmd/webfs/http.c
webfs: change %H (hostname) format to %N to not collide with encodefmt's %H (hex)
[plan9front.git] / sys / src / cmd / webfs / http.c
index b8b91ea105b119f6ed9077a7845b05ed31f457cd..93b0cfa47660525be6702b26245676298e27e81b 100644 (file)
@@ -73,19 +73,22 @@ tlstrace(char *fmt, ...)
 }
 
 static int
-tlswrap(int fd)
+tlswrap(int fd, char *servername)
 {
        TLSconn conn;
 
        memset(&conn, 0, sizeof(conn));
        if(debug)
                conn.trace = tlstrace;
+       if(servername != nil)
+               conn.serverName = smprint("%N", servername);
        if((fd = tlsClient(fd, &conn)) < 0){
                if(debug) fprint(2, "tlsClient: %r\n");
                return -1;
        }
        free(conn.cert);
        free(conn.sessionID);
+       free(conn.serverName);
        return fd;
 }
 
@@ -123,10 +126,10 @@ hdial(Url *u)
        if((fd = dial(addr, 0, 0, &ctl)) >= 0){
                if(proxy){
                        if(strcmp(proxy->scheme, "https") == 0)
-                               fd = tlswrap(fd);
+                               fd = tlswrap(fd, proxy->host);
                } else {
                        if(strcmp(u->scheme, "https") == 0)
-                               fd = tlswrap(fd);
+                               fd = tlswrap(fd, u->host);
                }
        }
        if(fd < 0){
@@ -237,7 +240,7 @@ hclose(Hconn *h)
                                        /* free the tail */
                                        hcloseall(x);
                                } while(i);
-                               exits(0);
+                               exits(nil);
                        }
                        return;
                }
@@ -357,7 +360,6 @@ authenticate(Url *u, Url *ru, char *method, char *s)
        pass = u->pass;
        realm = nonce = opaque = nil;
        if(!cistrncmp(s, "Basic ", 6)){
-               char cred[128], plain[128];
                UserPasswd *up;
 
                s += 6;
@@ -380,19 +382,20 @@ authenticate(Url *u, Url *ru, char *method, char *s)
                        user = up->user;
                        pass = up->passwd;
                }
-               n = snprint(plain, sizeof(plain), "%s:%s", user ? user : "", pass ? pass : "");
+               fmtstrinit(&fmt);
+               fmtprint(&fmt, "%s:%s", user ? user : "", pass ? pass : "");
                if(up){
                        memset(up->user, 0, strlen(up->user));
                        memset(up->passwd, 0, strlen(up->passwd));
                        free(up);
                }
-               n = enc64(cred, sizeof(cred), (uchar*)plain, n);
-               memset(plain, 0, sizeof(plain));
-               if(n == -1)
+               if((s = fmtstrflush(&fmt)) == nil)
                        return -1;
+               n = strlen(s);
                fmtstrinit(&fmt);
-               fmtprint(&fmt, "Basic %s", cred);
-               memset(cred, 0, sizeof(cred));
+               fmtprint(&fmt, "Basic %.*[", n, s);
+               memset(s, 0, n);
+               free(s);
                u = saneurl(url(".", u));       /* all uris below the requested one */
        }else
        if(!cistrncmp(s, "Digest ", 7)){
@@ -425,7 +428,7 @@ authenticate(Url *u, Url *ru, char *method, char *s)
                fmtprint(&fmt, "Digest ");
                fmtprint(&fmt, "username=\"%s\", ", ouser);
                fmtprint(&fmt, "realm=\"%s\", ", realm);
-               fmtprint(&fmt, "host=\"%H\", ", u->host);
+               fmtprint(&fmt, "host=\"%N\", ", u->host);
                fmtprint(&fmt, "uri=\"%U\", ", ru);
                fmtprint(&fmt, "nonce=\"%s\", ", nonce);
                fmtprint(&fmt, "response=\"%s\"", resp);
@@ -489,7 +492,7 @@ Again:
 static void
 catch(void *, char *msg)
 {
-       if(strstr("alarm", msg) || strstr("die", msg))
+       if(strstr("alarm", msg) != nil)
                noted(NCONT);
        else
                noted(NDFLT);
@@ -605,7 +608,7 @@ http(char *m, Url *u, Key *shdr, Buq *qbody, Buq *qpost)
 
                /* http requires ascii encoding of host */
                free(host);
-               host = smprint("%H", u->host);
+               host = smprint("%N", u->host);
 
                if(proxy && strcmp(u->scheme, "https") != 0){
                        ru = *u;
@@ -707,7 +710,7 @@ http(char *m, Url *u, Key *shdr, Buq *qbody, Buq *qpost)
                                }else
                                        h->keep = 0;
                                if(pid == 0)
-                                       exits(0);
+                                       exits(nil);
                        }
                        /* no timeout when posting */
                        alarm(0);
@@ -857,7 +860,7 @@ http(char *m, Url *u, Key *shdr, Buq *qbody, Buq *qpost)
                                goto Error;
                        if(x = lookkey(shdr, "Proxy-Authorization"))
                                flushauth(proxy, x);
-                       if(hauthenticate(u, &ru, method, "Proxy-Authenticate", rhdr) < 0)
+                       if(hauthenticate(proxy, proxy, method, "Proxy-Authenticate", rhdr) < 0)
                                goto Error;
                        }
                case 0:         /* No status */
@@ -905,7 +908,7 @@ http(char *m, Url *u, Key *shdr, Buq *qbody, Buq *qpost)
                 * then the proxy server has established the connection.
                 */
                if(h->tunnel && !retry && (i/100) == 2){
-                       if((h->fd = tlswrap(h->fd)) < 0)
+                       if((h->fd = tlswrap(h->fd, host)) < 0)
                                break;
 
                        /* proceed to the original request */
@@ -997,5 +1000,5 @@ http(char *m, Url *u, Key *shdr, Buq *qbody, Buq *qpost)
                shdr = k->next;
                free(k);
        }
-       exits(0);
+       exits(nil);
 }