ref: 151039caf055a741c69656ae9034b4829e4d6c94
parent: 1e97adc86b3ca9ef5c9834fa802e4003df1b86e2
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Fri Mar 22 20:49:07 EDT 2019
webfs: bracket literal ipv6 host in "Host:" header
--- a/sys/src/cmd/webfs/fns.h
+++ b/sys/src/cmd/webfs/fns.h
@@ -15,10 +15,12 @@
#pragma varargck type "U" Url*
#pragma varargck type "E" Str2
#pragma varargck type "N" char*
+#pragma varargck type "]" char*
int Efmt(Fmt*);
int Nfmt(Fmt*);
int Ufmt(Fmt*);
+int Mfmt(Fmt*);
char* Upath(Url *);
Url* url(char *s, Url *b);
Url* saneurl(Url *u);
--- a/sys/src/cmd/webfs/fs.c
+++ b/sys/src/cmd/webfs/fs.c
@@ -766,6 +766,7 @@
quotefmtinstall();
fmtinstall('U', Ufmt);
fmtinstall('N', Nfmt);
+ fmtinstall(']', Mfmt);
fmtinstall('E', Efmt);
fmtinstall('[', encodefmt);
fmtinstall('H', encodefmt);
--- a/sys/src/cmd/webfs/http.c
+++ b/sys/src/cmd/webfs/http.c
@@ -637,7 +637,7 @@
ru.path = Upath(u);
ru.query = u->query;
}
- n = snprint(buf, sizeof(buf), "%s %U HTTP/1.1\r\nHost: %s%s%s\r\n",
+ n = snprint(buf, sizeof(buf), "%s %U HTTP/1.1\r\nHost: %]%s%s\r\n",
method, &ru, host, u->port ? ":" : "", u->port ? u->port : "");
if(n >= sizeof(buf)-64){
werrstr("request too large");
@@ -649,7 +649,7 @@
break;
}
if(h->tunnel){
- n = snprint(buf, sizeof(buf), "CONNECT %s:%s HTTP/1.1\r\nHost: %s:%s\r\n",
+ n = snprint(buf, sizeof(buf), "CONNECT %]:%s HTTP/1.1\r\nHost: %]:%s\r\n",
host, u->port ? u->port : "443",
host, u->port ? u->port : "443");
}
--- a/sys/src/cmd/webfs/url.c
+++ b/sys/src/cmd/webfs/url.c
@@ -83,6 +83,14 @@
}
int
+Mfmt(Fmt *f)
+{
+ char *s = va_arg(f->args, char*);
+ fmtprint(f, (*s != '[' && strchr(s, ':') != nil)? "[%s]" : "%s", s);
+ return 0;
+}
+
+int
Ufmt(Fmt *f)
{
char *s;
@@ -101,7 +109,7 @@
fmtprint(f, "@");
}
if(u->host){
- fmtprint(f, strchr(u->host, ':') ? "[%s]" : "%s", u->host);
+ fmtprint(f, "%]", u->host);
if(u->port)
fmtprint(f, ":%s", u->port);
}