xref: /plan9/sys/src/cmd/webfs/http.c (revision f9e1cf08d3be51592e03e639fc848a68dc31a55e)
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ip.h>
5 #include <plumb.h>
6 #include <thread.h>
7 #include <fcall.h>
8 #include <9p.h>
9 #include <libsec.h>
10 #include <auth.h>
11 #include "dat.h"
12 #include "fns.h"
13 
14 char PostContentType[] = "application/x-www-form-urlencoded";
15 int httpdebug;
16 
17 typedef struct HttpState HttpState;
18 struct HttpState
19 {
20 	int fd;
21 	Client *c;
22 	char *location;
23 	char *setcookie;
24 	char *netaddr;
25 	char *credentials;
26 	char autherror[ERRMAX];
27 	Ibuf	b;
28 };
29 
30 static void
31 location(HttpState *hs, char *value)
32 {
33 	if(hs->location == nil)
34 		hs->location = estrdup(value);
35 }
36 
37 static void
38 contenttype(HttpState *hs, char *value)
39 {
40 	if(hs->c->contenttype != nil)
41 		free(hs->c->contenttype);
42 	hs->c->contenttype = estrdup(value);
43 }
44 
45 static void
46 setcookie(HttpState *hs, char *value)
47 {
48 	char *s, *t;
49 	Fmt f;
50 
51 	s = hs->setcookie;
52 	fmtstrinit(&f);
53 	if(s)
54 		fmtprint(&f, "%s", s);
55 	fmtprint(&f, "set-cookie: ");
56 	fmtprint(&f, "%s", value);
57 	fmtprint(&f, "\n");
58 	t = fmtstrflush(&f);
59 	if(t){
60 		free(s);
61 		hs->setcookie = t;
62 	}
63 }
64 
65 static char*
66 unquote(char *s, char **ps)
67 {
68 	char *p;
69 
70 	if(*s != '"'){
71 		p = strpbrk(s, " \t\r\n");
72 		*p++ = 0;
73 		*ps = p;
74 		return s;
75 	}
76 	for(p=s+1; *p; p++){
77 		if(*p == '\"'){
78 			*p++ = 0;
79 			break;
80 		}
81 		if(*p == '\\' && *(p+1)){
82 			p++;
83 			continue;
84 		}
85 	}
86 	memmove(s, s+1, p-(s+1));
87 	s[p-(s+1)] = 0;
88 	*ps = p;
89 	return s;
90 }
91 
92 static char*
93 servername(char *addr)
94 {
95 	char *p;
96 
97 	if(strncmp(addr, "tcp!", 4) == 0
98 	|| strncmp(addr, "net!", 4) == 0)
99 		addr += 4;
100 	addr = estrdup(addr);
101 	p = addr+strlen(addr);
102 	if(p>addr && *(p-1) == 's')
103 		p--;
104 	if(p>addr+5 && strcmp(p-5, "!http") == 0)
105 		p[-5] = 0;
106 	return addr;
107 }
108 
109 void
110 wwwauthenticate(HttpState *hs, char *line)
111 {
112 	char cred[64], *user, *pass, *realm, *s, *spec, *name;
113 	Fmt fmt;
114 	UserPasswd *up;
115 
116 	spec = nil;
117 	up = nil;
118 	cred[0] = 0;
119 	hs->autherror[0] = 0;
120 	if(cistrncmp(line, "basic ", 6) != 0){
121 		werrstr("unknown auth: %s", line);
122 		goto error;
123 	}
124 	line += 6;
125 	if(cistrncmp(line, "realm=", 6) != 0){
126 		werrstr("missing realm: %s", line);
127 		goto error;
128 	}
129 	line += 6;
130 	user = hs->c->url->user;
131 	pass = hs->c->url->passwd;
132 	if(user==nil || pass==nil){
133 		realm = unquote(line, &line);
134 		fmtstrinit(&fmt);
135 		name = servername(hs->netaddr);
136 		fmtprint(&fmt, "proto=pass service=http server=%q realm=%q", name, realm);
137 		free(name);
138 		if(hs->c->url->user)
139 			fmtprint(&fmt, " user=%q", hs->c->url->user);
140 		spec = fmtstrflush(&fmt);
141 		if(spec == nil)
142 			goto error;
143 		if((up = auth_getuserpasswd(nil, "%s", spec)) == nil)
144 			goto error;
145 		user = up->user;
146 		pass = up->passwd;
147 	}
148 	if((s = smprint("%s:%s", user, pass)) == nil)
149 		goto error;
150 	free(up);
151 	enc64(cred, sizeof(cred), (uchar*)s, strlen(s));
152 	memset(s, 0, strlen(s));
153 	free(s);
154 	hs->credentials = smprint("Basic %s", cred);
155 	if(hs->credentials == nil)
156 		goto error;
157 	return;
158 
159 error:
160 	free(up);
161 	free(spec);
162 	snprint(hs->autherror, sizeof hs->autherror, "%r");
163 }
164 
165 struct {
166 	char *name;									/* Case-insensitive */
167 	void (*fn)(HttpState *hs, char *value);
168 } hdrtab[] = {
169 	{ "location:", location },
170 	{ "content-type:", contenttype },
171 	{ "set-cookie:", setcookie },
172 	{ "www-authenticate:", wwwauthenticate },
173 };
174 
175 static int
176 httprcode(HttpState *hs)
177 {
178 	int n;
179 	char *p;
180 	char buf[256];
181 
182 	n = readline(&hs->b, buf, sizeof(buf)-1);
183 	if(n <= 0)
184 		return n;
185 	if(httpdebug)
186 		fprint(2, "-> %s\n", buf);
187 	p = strchr(buf, ' ');
188 	if(memcmp(buf, "HTTP/", 5) != 0 || p == nil){
189 		werrstr("bad response from server");
190 		return -1;
191 	}
192 	buf[n] = 0;
193 	return atoi(p+1);
194 }
195 
196 /*
197  *  read a single mime header, collect continuations.
198  *
199  *  this routine assumes that there is a blank line twixt
200  *  the header and the message body, otherwise bytes will
201  *  be lost.
202  */
203 static int
204 getheader(HttpState *hs, char *buf, int n)
205 {
206 	char *p, *e;
207 	int i;
208 
209 	n--;
210 	p = buf;
211 	for(e = p + n; ; p += i){
212 		i = readline(&hs->b, p, e-p);
213 		if(i < 0)
214 			return i;
215 
216 		if(p == buf){
217 			/* first line */
218 			if(strchr(buf, ':') == nil)
219 				break;		/* end of headers */
220 		} else {
221 			/* continuation line */
222 			if(*p != ' ' && *p != '\t'){
223 				unreadline(&hs->b, p);
224 				*p = 0;
225 				break;		/* end of this header */
226 			}
227 		}
228 	}
229 
230 	if(httpdebug)
231 		fprint(2, "-> %s\n", buf);
232 	return p-buf;
233 }
234 
235 static int
236 httpheaders(HttpState *hs)
237 {
238 	char buf[2048];
239 	char *p;
240 	int i, n;
241 
242 	for(;;){
243 		n = getheader(hs, buf, sizeof(buf));
244 		if(n < 0)
245 			return -1;
246 		if(n == 0)
247 			return 0;
248 		//	print("http header: '%.*s'\n", n, buf);
249 		for(i = 0; i < nelem(hdrtab); i++){
250 			n = strlen(hdrtab[i].name);
251 			if(cistrncmp(buf, hdrtab[i].name, n) == 0){
252 				/* skip field name and leading white */
253 				p = buf + n;
254 				while(*p == ' ' || *p == '\t')
255 					p++;
256 				(*hdrtab[i].fn)(hs, p);
257 				break;
258 			}
259 		}
260 	}
261 }
262 
263 int
264 httpopen(Client *c, Url *url)
265 {
266 	int fd, code, redirect, authenticate;
267 	char *cookies;
268 	Ioproc *io;
269 	HttpState *hs;
270 
271 	if(httpdebug)
272 		fprint(2, "httpopen\n");
273 	io = c->io;
274 	hs = emalloc(sizeof(*hs));
275 	hs->c = c;
276 	hs->netaddr = estrdup(netmkaddr(url->host, 0, url->scheme));
277 	c->aux = hs;
278 	if(httpdebug)
279 		fprint(2, "dial %s\n", hs->netaddr);
280 	fd = iotlsdial(io, hs->netaddr, 0, 0, 0, url->ischeme==UShttps);
281 	if(fd < 0){
282 	Error:
283 		if(httpdebug)
284 			fprint(2, "iodial: %r\n");
285 		free(hs->netaddr);
286 		close(hs->fd);
287 		hs->fd = -1;
288 		free(hs);
289 		c->aux = nil;
290 		return -1;
291 	}
292 	hs->fd = fd;
293 	if(httpdebug)
294 		fprint(2, "<- %s %s HTTP/1.0\n<- Host: %s\n",
295 			c->havepostbody? "POST": " GET", url->http.page_spec, url->host);
296 	ioprint(io, fd, "%s %s HTTP/1.0\r\nHost: %s\r\n",
297 		c->havepostbody? "POST" : "GET", url->http.page_spec, url->host);
298 	if(httpdebug)
299 		fprint(2, "<- User-Agent: %s\n", c->ctl.useragent);
300 	if(c->ctl.useragent)
301 		ioprint(io, fd, "User-Agent: %s\r\n", c->ctl.useragent);
302 	if(c->ctl.sendcookies){
303 		/* should we use url->page here?  sometimes it is nil. */
304 		cookies = httpcookies(url->host, url->http.page_spec, 0);
305 		if(cookies && cookies[0])
306 			ioprint(io, fd, "%s", cookies);
307 		if(httpdebug)
308 			fprint(2, "<- %s", cookies);
309 		free(cookies);
310 	}
311 	if(c->havepostbody){
312 		ioprint(io, fd, "Content-type: %s\r\n", PostContentType);
313 		ioprint(io, fd, "Content-length: %ud\r\n", c->npostbody);
314 		if(httpdebug){
315 			fprint(2, "<- Content-type: %s\n", PostContentType);
316 			fprint(2, "<- Content-length: %ud\n", c->npostbody);
317 		}
318 	}
319 	if(c->authenticate){
320 		ioprint(io, fd, "Authorization: %s\r\n", c->authenticate);
321 		if(httpdebug)
322 			fprint(2, "<- Authorization: %s\n", c->authenticate);
323 	}
324 	ioprint(io, fd, "\r\n");
325 	if(c->havepostbody)
326 		if(iowrite(io, fd, c->postbody, c->npostbody) != c->npostbody)
327 			goto Error;
328 
329 	c->havepostbody = 0;
330 	redirect = 0;
331 	authenticate = 0;
332 	initibuf(&hs->b, io, fd);
333 	code = httprcode(hs);
334 
335 	switch(code){
336 	case -1:	/* connection timed out */
337 		goto Error;
338 
339 /*
340 	case Eof:
341 		werrstr("EOF from HTTP server");
342 		goto Error;
343 */
344 
345 	case 200:	/* OK */
346 	case 201:	/* Created */
347 	case 202:	/* Accepted */
348 	case 204:	/* No Content */
349 	case 205: /* Reset Content */
350 #ifdef NOT_DEFINED
351 		if(ofile == nil && r->start != 0)
352 			sysfatal("page changed underfoot");
353 #endif
354 		break;
355 
356 	case 206:	/* Partial Content */
357 		werrstr("Partial Content (206)");
358 		goto Error;
359 
360 	case 301:	/* Moved Permanently */
361 	case 302:	/* Moved Temporarily */
362 	case 303:	/* See Other */
363 	case 307: /* Temporary Redirect  */
364 		redirect = 1;
365 		break;
366 
367 	case 304:	/* Not Modified */
368 		break;
369 
370 	case 400:	/* Bad Request */
371 		werrstr("Bad Request (400)");
372 		goto Error;
373 
374 	case 401:	/* Unauthorized */
375 		if(c->authenticate){
376 			werrstr("Authentication failed (401)");
377 			goto Error;
378 		}
379 		authenticate = 1;
380 		break;
381 	case 402:	/* Payment Required */
382 		werrstr("Payment Required (402)");
383 		goto Error;
384 
385 	case 403:	/* Forbidden */
386 		werrstr("Forbidden by server (403)");
387 		goto Error;
388 
389 	case 404:	/* Not Found */
390 		werrstr("Not found on server (404)");
391 		goto Error;
392 
393 	case 405:	/* Method Not Allowed  */
394 		werrstr("Method not allowed (405)");
395 		goto Error;
396 
397 	case 406: /* Not Acceptable */
398 		werrstr("Not Acceptable (406)");
399 		goto Error;
400 
401 	case 407:	/* Proxy auth */
402 		werrstr("Proxy authentication required (407)");
403 		goto Error;
404 
405 	case 408: /* Request Timeout */
406 		werrstr("Request Timeout (408)");
407 		goto Error;
408 
409 	case 409: /* Conflict */
410 		werrstr("Conflict  (409)");
411 		goto Error;
412 
413 	case 410: /* Gone */
414 		werrstr("Gone  (410)");
415 		goto Error;
416 
417 	case 411: /* Length Required */
418 		werrstr("Length Required  (411)");
419 		goto Error;
420 
421 	case 412: /* Precondition Failed */
422 		werrstr("Precondition Failed  (412)");
423 		goto Error;
424 
425 	case 413: /* Request Entity Too Large */
426 		werrstr("Request Entity Too Large  (413)");
427 		goto Error;
428 
429 	case 414: /* Request-URI Too Long */
430 		werrstr("Request-URI Too Long  (414)");
431 		goto Error;
432 
433 	case 415: /* Unsupported Media Type */
434 		werrstr("Unsupported Media Type  (415)");
435 		goto Error;
436 
437 	case 416: /* Requested Range Not Satisfiable */
438 		werrstr("Requested Range Not Satisfiable  (416)");
439 		goto Error;
440 
441 	case 417: /* Expectation Failed */
442 		werrstr("Expectation Failed  (417)");
443 		goto Error;
444 
445 	case 500:	/* Internal server error */
446 		werrstr("Server choked (500)");
447 		goto Error;
448 
449 	case 501:	/* Not implemented */
450 		werrstr("Server can't do it (501)");
451 		goto Error;
452 
453 	case 502:	/* Bad gateway */
454 		werrstr("Bad gateway (502)");
455 		goto Error;
456 
457 	case 503:	/* Service unavailable */
458 		werrstr("Service unavailable (503)");
459 		goto Error;
460 
461 	default:
462 		/* Bogus: we should treat unknown code XYZ as code X00 */
463 		werrstr("Unknown response code %d", code);
464 		goto Error;
465 	}
466 
467 	if(httpheaders(hs) < 0)
468 		goto Error;
469 	if(c->ctl.acceptcookies && hs->setcookie)
470 		httpsetcookie(hs->setcookie, url->host, url->path);
471 	if(authenticate){
472 		if(!hs->credentials){
473 			if(hs->autherror[0])
474 				werrstr("%s", hs->autherror);
475 			else
476 				werrstr("unauthorized; no www-authenticate: header");
477 			return -1;
478 		}
479 		c->authenticate = hs->credentials;
480 		hs->credentials = nil;
481 	}
482 	if(redirect){
483 		if(!hs->location){
484 			werrstr("redirection without Location: header");
485 			return -1;
486 		}
487 		c->redirect = hs->location;
488 		hs->location = nil;
489 	}
490 	return 0;
491 }
492 
493 int
494 httpread(Client *c, Req *r)
495 {
496 	HttpState *hs;
497 	long n;
498 
499 	hs = c->aux;
500 	n = readibuf(&hs->b, r->ofcall.data, r->ifcall.count);
501 	if(n < 0)
502 		return -1;
503 
504 	r->ofcall.count = n;
505 	return 0;
506 }
507 
508 void
509 httpclose(Client *c)
510 {
511 	HttpState *hs;
512 
513 	hs = c->aux;
514 	if(hs == nil)
515 		return;
516 	ioclose(c->io, hs->fd);
517 	hs->fd = -1;
518 	free(hs->location);
519 	free(hs->setcookie);
520 	free(hs->netaddr);
521 	free(hs->credentials);
522 	free(hs);
523 	c->aux = nil;
524 }
525 
526