11 static char reserved[] = "%:/?#[]@!$&'()*+,;=";
16 if('0' <= c && c <= '9')
18 if('a' <= c && c <= 'f')
20 if('A' <= c && c <= 'F')
26 unescape(char *s, char *spec)
33 for(r=w=s; x = *r; r++){
34 if(x == '%' && isxdigit(r[1]) && isxdigit(r[2])){
35 x = (dhex(r[1])<<4)|dhex(r[2]);
36 if(spec && strchr(spec, x)){
58 s2 = va_arg(f->args, Str2);
62 if(*s == '%' && isxdigit(s[1]) && isxdigit(s[2])){
63 fmtprint(f, "%%%c%c", toupper(s[1]), toupper(s[2]));
66 else if(isalnum(*s) || strchr(".-_~!$&'()*,;=", *s) || strchr(spec, *s))
67 fmtprint(f, "%c", *s);
69 fmtprint(f, "%%%.2X", *s & 0xff);
78 s = va_arg(f->args, char*);
80 if(utf2idn(s, d, Domlen) == nil)
94 if((u = va_arg(f->args, Url*)) == nil)
95 return fmtprint(f, "nil");
97 fmtprint(f, "%s:", u->scheme);
98 if(u->user || u->host)
101 fmtprint(f, "%E", (Str2){u->user, ""});
103 fmtprint(f, ":%E", (Str2){u->pass, ""});
107 fmtprint(f, strchr(u->host, ':') ? "[%s]" : "%s", u->host);
109 fmtprint(f, ":%s", u->port);
112 fmtprint(f, "%E", (Str2){s, "/:@+"});
114 fmtprint(f, "?%E", (Str2){u->query, "/:@"});
116 fmtprint(f, "#%E", (Str2){u->fragment, "/:@?+"});
126 if(u->user || u->host)
143 if(p = strchr(s, '/'))
145 if(*s == '.' && ((s[1] == 0) || (s[1] == '.' && s[2] == 0))){
154 if((n = strlen(s)) > 0)
166 abspath(char *s, char *b)
171 if(s == nil || *s == 0)
173 if(*s != '/' && (x = strrchr(b, '/'))){
174 a = emalloc((x - b) + strlen(s) + 4);
175 sprint(a, "%.*s/%s", (int)(x - b), b, s);
182 a = emalloc(strlen(s) + 4);
192 if(p == nil || *p == nil)
213 cp += runetochar(cp, &r);
228 u = emalloc(sizeof(*u));
239 if((p == s) || ((!isdigit(*p) && strchr("+-.", *p) == nil)))
246 memmove(u, b, sizeof(*u));
249 memmove(u, b, sizeof(*u));
253 memmove(u, b, sizeof(*u));
254 u->fragment = u->query = nil;
258 u->scheme = b->scheme;
263 memmove(u, b, sizeof(*u));
264 u->fragment = u->query = u->path = nil;
269 if(x = strchr(p, '#')){
273 if(x = strchr(p, '?')){
277 if(p[0] == '/' && p[1] == '/'){
279 if(x = strchr(p, '/')){
280 u->path = t = abspath(x, Upath(b));
283 if(x = strchr(p, '@')){
285 if(y = strchr(p, ':')){
292 if((x = strrchr(p, ']')) == nil)
294 if(x = strrchr(x, ':')){
298 if(x = strchr(p, '[')){
300 if(y = strchr(p, ']'))
305 u->path = t = abspath(p, Upath(b));
315 pstrdup(&u->fragment);
319 /* the + character encodes space only in query part */
321 while(s = strchr(s, '+'))
326 if(idn2utf(s, t, Domlen)){
327 u->host = estrdup(t);
333 unescape(u->user, nil);
334 unescape(u->pass, nil);
335 unescape(u->path, reserved);
336 unescape(u->query, reserved);
337 unescape(u->fragment, reserved);
338 mklowcase(u->scheme);
348 if(u == nil || u->scheme == nil || u->host == nil || Upath(u) == nil){
353 /* remove default ports */
354 switch(atoi(u->port)){
355 case 21: if(!strcmp(u->scheme, "ftp")) goto Defport; break;
356 case 70: if(!strcmp(u->scheme, "gopher"))goto Defport; break;
357 case 80: if(!strcmp(u->scheme, "http")) goto Defport; break;
358 case 443: if(!strcmp(u->scheme, "https")) goto Defport; break;
359 default: if(!strcmp(u->scheme, u->port)) goto Defport; break;
369 matchurl(Url *u, Url *s)
376 if(u->scheme && (s->scheme == nil || strcmp(u->scheme, s->scheme)))
378 if(u->user && (s->user == nil || strcmp(u->user, s->user)))
380 if(u->host && (s->host == nil || strcmp(u->host, s->host)))
382 if(u->port && (s->port == nil || strcmp(u->port, s->port)))
386 if(b == nil || strncmp(a, b, strlen(a)))