http.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. }
  239. int
  240. httpopen(Client *c, Url *url)
  241. {
  242. int fd, code, redirect, authenticate;
  243. char *cookies;
  244. Ioproc *io;
  245. HttpState *hs;
  246. if(httpdebug)
  247. fprint(2, "httpopen\n");
  248. io = c->io;
  249. hs = emalloc(sizeof(*hs));
  250. hs->c = c;
  251. hs->netaddr = estrdup(netmkaddr(url->host, 0, url->scheme));
  252. c->aux = hs;
  253. if(httpdebug)
  254. fprint(2, "dial %s\n", hs->netaddr);
  255. fd = iotlsdial(io, hs->netaddr, 0, 0, 0, url->ischeme==UShttps);
  256. if(fd < 0){
  257. Error:
  258. if(httpdebug)
  259. fprint(2, "iodial: %r\n");
  260. free(hs->netaddr);
  261. close(hs->fd);
  262. hs->fd = -1;
  263. free(hs);
  264. c->aux = nil;
  265. return -1;
  266. }
  267. hs->fd = fd;
  268. if(httpdebug)
  269. fprint(2, "<- %s %s HTTP/1.0\n<- Host: %s\n",
  270. c->havepostbody? "POST": " GET", url->http.page_spec, url->host);
  271. ioprint(io, fd, "%s %s HTTP/1.0\r\nHost: %s\r\n",
  272. c->havepostbody? "POST" : "GET", url->http.page_spec, url->host);
  273. if(httpdebug)
  274. fprint(2, "<- User-Agent: %s\n", c->ctl.useragent);
  275. if(c->ctl.useragent)
  276. ioprint(io, fd, "User-Agent: %s\r\n", c->ctl.useragent);
  277. if(c->ctl.sendcookies){
  278. /* should we use url->page here? sometimes it is nil. */
  279. cookies = httpcookies(url->host, url->http.page_spec, 0);
  280. if(cookies && cookies[0])
  281. ioprint(io, fd, "%s", cookies);
  282. if(httpdebug)
  283. fprint(2, "<- %s", cookies);
  284. free(cookies);
  285. }
  286. if(c->havepostbody){
  287. ioprint(io, fd, "Content-type: %s\r\n", PostContentType);
  288. ioprint(io, fd, "Content-length: %ud\r\n", c->npostbody);
  289. if(httpdebug){
  290. fprint(2, "<- Content-type: %s\n", PostContentType);
  291. fprint(2, "<- Content-length: %ud\n", c->npostbody);
  292. }
  293. }
  294. if(c->authenticate){
  295. ioprint(io, fd, "Authorization: %s\r\n", c->authenticate);
  296. if(httpdebug)
  297. fprint(2, "<- Authorization: %s\n", c->authenticate);
  298. }
  299. ioprint(io, fd, "\r\n");
  300. if(c->havepostbody)
  301. if(iowrite(io, fd, c->postbody, c->npostbody) != c->npostbody)
  302. goto Error;
  303. c->havepostbody = 0;
  304. redirect = 0;
  305. authenticate = 0;
  306. initibuf(&hs->b, io, fd);
  307. code = httprcode(hs);
  308. switch(code){
  309. case -1: /* connection timed out */
  310. goto Error;
  311. /*
  312. case Eof:
  313. werrstr("EOF from HTTP server");
  314. goto Error;
  315. */
  316. case 200: /* OK */
  317. case 201: /* Created */
  318. case 202: /* Accepted */
  319. case 204: /* No Content */
  320. #ifdef NOT_DEFINED
  321. if(ofile == nil && r->start != 0)
  322. sysfatal("page changed underfoot");
  323. #endif
  324. break;
  325. case 206: /* Partial Content */
  326. werrstr("Partial Content (206)");
  327. goto Error;
  328. case 301: /* Moved Permanently */
  329. case 302: /* Moved Temporarily */
  330. redirect = 1;
  331. break;
  332. case 304: /* Not Modified */
  333. break;
  334. case 400: /* Bad Request */
  335. werrstr("Bad Request (400)");
  336. goto Error;
  337. case 401: /* Unauthorized */
  338. if(c->authenticate){
  339. werrstr("Authentication failed (401)");
  340. goto Error;
  341. }
  342. authenticate = 1;
  343. break;
  344. case 402: /* ??? */
  345. werrstr("Unauthorized (402)");
  346. goto Error;
  347. case 403: /* Forbidden */
  348. werrstr("Forbidden by server (403)");
  349. goto Error;
  350. case 404: /* Not Found */
  351. werrstr("Not found on server (404)");
  352. goto Error;
  353. case 405: /* Method Not Allowed */
  354. werrstr("Method not allowed (405)");
  355. goto Error;
  356. case 407: /* Proxy auth */
  357. werrstr("Proxy authentication required (407)");
  358. goto Error;
  359. case 500: /* Internal server error */
  360. werrstr("Server choked (500)");
  361. goto Error;
  362. case 501: /* Not implemented */
  363. werrstr("Server can't do it (501)");
  364. goto Error;
  365. case 502: /* Bad gateway */
  366. werrstr("Bad gateway (502)");
  367. goto Error;
  368. case 503: /* Service unavailable */
  369. werrstr("Service unavailable (503)");
  370. goto Error;
  371. default:
  372. /* Bogus: we should treat unknown code XYZ as code X00 */
  373. werrstr("Unknown response code %d", code);
  374. goto Error;
  375. }
  376. if(httpheaders(hs) < 0)
  377. goto Error;
  378. if(c->ctl.acceptcookies && hs->setcookie)
  379. httpsetcookie(hs->setcookie, url->host, url->path);
  380. if(authenticate){
  381. if(!hs->credentials){
  382. if(hs->autherror[0])
  383. werrstr("%s", hs->autherror);
  384. else
  385. werrstr("unauthorized; no www-authenticate: header");
  386. return -1;
  387. }
  388. c->authenticate = hs->credentials;
  389. hs->credentials = nil;
  390. }
  391. if(redirect){
  392. if(!hs->location){
  393. werrstr("redirection without Location: header");
  394. return -1;
  395. }
  396. c->redirect = hs->location;
  397. hs->location = nil;
  398. }
  399. return 0;
  400. }
  401. int
  402. httpread(Client *c, Req *r)
  403. {
  404. char *dst;
  405. HttpState *hs;
  406. int n;
  407. long rlen, tot, len;
  408. hs = c->aux;
  409. dst = r->ofcall.data;
  410. len = r->ifcall.count;
  411. tot = 0;
  412. while (tot < len){
  413. rlen = len - tot;
  414. n = readibuf(&hs->b, dst + tot, rlen);
  415. if(n == 0)
  416. break;
  417. else if(n < 0){
  418. if(tot == 0)
  419. return -1;
  420. else
  421. return tot;
  422. }
  423. tot += n;
  424. }
  425. r->ofcall.count = tot;
  426. return 0;
  427. }
  428. void
  429. httpclose(Client *c)
  430. {
  431. HttpState *hs;
  432. hs = c->aux;
  433. if(hs == nil)
  434. return;
  435. ioclose(c->io, hs->fd);
  436. hs->fd = -1;
  437. free(hs->location);
  438. free(hs->setcookie);
  439. free(hs->netaddr);
  440. free(hs->credentials);
  441. free(hs);
  442. c->aux = nil;
  443. }