http.c 9.2 KB

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