parse.c 20 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <ctype.h>
  12. #include <libsec.h>
  13. #include <bin.h>
  14. #include <httpd.h>
  15. #include "escape.h"
  16. typedef struct Hlex Hlex;
  17. typedef struct MimeHead MimeHead;
  18. enum
  19. {
  20. /*
  21. * tokens
  22. */
  23. Word = 1,
  24. QString,
  25. };
  26. #define UlongMax 4294967295UL
  27. struct Hlex
  28. {
  29. int tok;
  30. int eoh;
  31. int eol; /* end of header line encountered? */
  32. uint8_t *hstart; /* start of header */
  33. jmp_buf jmp; /* jmp here to parse header */
  34. char wordval[HMaxWord];
  35. HConnect *c;
  36. };
  37. struct MimeHead
  38. {
  39. char *name;
  40. void (*parse)(Hlex*, char*);
  41. uint8_t seen;
  42. uint8_t ignore;
  43. };
  44. static void mimeaccept(Hlex*, char*);
  45. static void mimeacceptchar(Hlex*, char*);
  46. static void mimeacceptenc(Hlex*, char*);
  47. static void mimeacceptlang(Hlex*, char*);
  48. static void mimeagent(Hlex*, char*);
  49. static void mimeauthorization(Hlex*, char*);
  50. static void mimeconnection(Hlex*, char*);
  51. static void mimecontlen(Hlex*, char*);
  52. static void mimecookie(Hlex*, char*);
  53. static void mimeexpect(Hlex*, char*);
  54. static void mimefresh(Hlex*, char*);
  55. static void mimefrom(Hlex*, char*);
  56. static void mimehost(Hlex*, char*);
  57. static void mimeifrange(Hlex*, char*);
  58. static void mimeignore(Hlex*, char*);
  59. static void mimematch(Hlex*, char*);
  60. static void mimemodified(Hlex*, char*);
  61. static void mimenomatch(Hlex*, char*);
  62. static void mimerange(Hlex*, char*);
  63. static void mimetransenc(Hlex*, char*);
  64. static void mimeunmodified(Hlex*, char*);
  65. /*
  66. * headers seen also include
  67. * allow cache-control chargeto
  68. * content-encoding content-language content-location content-md5 content-range content-type
  69. * date etag expires forwarded last-modified max-forwards pragma
  70. * proxy-agent proxy-authorization proxy-connection
  71. * ua-color ua-cpu ua-os ua-pixels
  72. * upgrade via x-afs-tokens x-serial-number
  73. */
  74. static MimeHead mimehead[] =
  75. {
  76. {"accept", mimeaccept},
  77. {"accept-charset", mimeacceptchar},
  78. {"accept-encoding", mimeacceptenc},
  79. {"accept-language", mimeacceptlang},
  80. {"authorization", mimeauthorization},
  81. {"connection", mimeconnection},
  82. {"content-length", mimecontlen},
  83. {"cookie", mimecookie},
  84. {"expect", mimeexpect},
  85. {"fresh", mimefresh},
  86. {"from", mimefrom},
  87. {"host", mimehost},
  88. {"if-match", mimematch},
  89. {"if-modified-since", mimemodified},
  90. {"if-none-match", mimenomatch},
  91. {"if-range", mimeifrange},
  92. {"if-unmodified-since", mimeunmodified},
  93. {"range", mimerange},
  94. {"transfer-encoding", mimetransenc},
  95. {"user-agent", mimeagent},
  96. };
  97. char* hmydomain;
  98. char* hversion = "HTTP/1.1";
  99. static void lexhead(Hlex*);
  100. static void parsejump(Hlex*, char*);
  101. static int getc(Hlex*);
  102. static void ungetc(Hlex*);
  103. static int wordcr(Hlex*);
  104. static int wordnl(Hlex*);
  105. static void word(Hlex*, char*);
  106. static int lex1(Hlex*, int);
  107. static int lex(Hlex*);
  108. static int lexbase64(Hlex*);
  109. static uint32_t digtoul(char *s, char **e);
  110. /*
  111. * flush and clean up junk from a request
  112. */
  113. void
  114. hreqcleanup(HConnect *c)
  115. {
  116. int i;
  117. hxferenc(&c->hout, 0);
  118. memset(&c->req, 0, sizeof(c->req));
  119. memset(&c->head, 0, sizeof(c->head));
  120. c->hpos = c->header;
  121. c->hstop = c->header;
  122. binfree(&c->bin);
  123. for(i = 0; i < nelem(mimehead); i++){
  124. mimehead[i].seen = 0;
  125. mimehead[i].ignore = 0;
  126. }
  127. }
  128. /*
  129. * list of tokens
  130. * if the client is HTTP/1.0,
  131. * ignore headers which match one of the tokens.
  132. * restarts parsing if necessary.
  133. */
  134. static void
  135. mimeconnection(Hlex *h, char *c)
  136. {
  137. char *u, *p;
  138. int reparse, i;
  139. reparse = 0;
  140. for(;;){
  141. while(lex(h) != Word)
  142. if(h->tok != ',')
  143. goto breakout;
  144. if(cistrcmp(h->wordval, "keep-alive") == 0)
  145. h->c->head.persist = 1;
  146. else if(cistrcmp(h->wordval, "close") == 0)
  147. h->c->head.closeit = 1;
  148. else if(!http11(h->c)){
  149. for(i = 0; i < nelem(mimehead); i++){
  150. if(cistrcmp(mimehead[i].name, h->wordval) == 0){
  151. reparse = mimehead[i].seen && !mimehead[i].ignore;
  152. mimehead[i].ignore = 1;
  153. if(cistrcmp(mimehead[i].name, "authorization") == 0){
  154. h->c->head.authuser = nil;
  155. h->c->head.authpass = nil;
  156. }
  157. }
  158. }
  159. }
  160. if(lex(h) != ',')
  161. break;
  162. }
  163. breakout:;
  164. /*
  165. * if need to ignore headers we've already parsed,
  166. * reset & start over. need to save authorization
  167. * info because it's written over when parsed.
  168. */
  169. if(reparse){
  170. u = h->c->head.authuser;
  171. p = h->c->head.authpass;
  172. memset(&h->c->head, 0, sizeof(h->c->head));
  173. h->c->head.authuser = u;
  174. h->c->head.authpass = p;
  175. h->c->hpos = h->hstart;
  176. longjmp(h->jmp, 1);
  177. }
  178. }
  179. int
  180. hparseheaders(HConnect *c, int timeout)
  181. {
  182. Hlex h;
  183. c->head.fresh_thresh = 0;
  184. c->head.fresh_have = 0;
  185. c->head.persist = 0;
  186. if(c->req.vermaj == 0){
  187. c->head.host = hmydomain;
  188. return 1;
  189. }
  190. memset(&h, 0, sizeof(h));
  191. h.c = c;
  192. if(timeout)
  193. alarm(timeout);
  194. if(hgethead(c, 1) < 0)
  195. return -1;
  196. if(timeout)
  197. alarm(0);
  198. h.hstart = c->hpos;
  199. if(setjmp(h.jmp) == -1)
  200. return -1;
  201. h.eol = 0;
  202. h.eoh = 0;
  203. h.tok = '\n';
  204. while(lex(&h) != '\n'){
  205. if(h.tok == Word && lex(&h) == ':')
  206. parsejump(&h, hstrdup(c, h.wordval));
  207. while(h.tok != '\n')
  208. lex(&h);
  209. h.eol = h.eoh;
  210. }
  211. if(http11(c)){
  212. /*
  213. * according to the http/1.1 spec,
  214. * these rules must be followed
  215. */
  216. if(c->head.host == nil){
  217. hfail(c, HBadReq, nil);
  218. return -1;
  219. }
  220. if(c->req.urihost != nil)
  221. c->head.host = c->req.urihost;
  222. /*
  223. * also need to check host is actually this one
  224. */
  225. }else if(c->head.host == nil)
  226. c->head.host = hmydomain;
  227. return 1;
  228. }
  229. /*
  230. * mimeparams : | mimeparams ";" mimepara
  231. * mimeparam : token "=" token | token "=" qstring
  232. */
  233. static HSPairs*
  234. mimeparams(Hlex *h)
  235. {
  236. HSPairs *p;
  237. char *s;
  238. p = nil;
  239. for(;;){
  240. if(lex(h) != Word)
  241. break;
  242. s = hstrdup(h->c, h->wordval);
  243. if(lex(h) != Word && h->tok != QString)
  244. break;
  245. p = hmkspairs(h->c, s, hstrdup(h->c, h->wordval), p);
  246. }
  247. return hrevspairs(p);
  248. }
  249. /*
  250. * mimehfields : mimehfield | mimehfields commas mimehfield
  251. * mimehfield : token mimeparams
  252. * commas : "," | commas ","
  253. */
  254. static HFields*
  255. mimehfields(Hlex *h)
  256. {
  257. HFields *f;
  258. f = nil;
  259. for(;;){
  260. while(lex(h) != Word)
  261. if(h->tok != ',')
  262. goto breakout;
  263. f = hmkhfields(h->c, hstrdup(h->c, h->wordval), nil, f);
  264. if(lex(h) == ';')
  265. f->params = mimeparams(h);
  266. if(h->tok != ',')
  267. break;
  268. }
  269. breakout:;
  270. return hrevhfields(f);
  271. }
  272. /*
  273. * parse a list of acceptable types, encodings, languages, etc.
  274. */
  275. static HContent*
  276. mimeok(Hlex *h, char *name, int multipart, HContent *head)
  277. {
  278. char *generic, *specific, *s;
  279. float v;
  280. /*
  281. * each type is separated by one or more commas
  282. */
  283. while(lex(h) != Word)
  284. if(h->tok != ',')
  285. return head;
  286. generic = hstrdup(h->c, h->wordval);
  287. lex(h);
  288. if(h->tok == '/' || multipart){
  289. /*
  290. * at one time, IE5 improperly said '*' for single types
  291. */
  292. if(h->tok != '/')
  293. return nil;
  294. if(lex(h) != Word)
  295. return head;
  296. specific = hstrdup(h->c, h->wordval);
  297. if(!multipart && strcmp(specific, "*") != 0)
  298. return head;
  299. lex(h);
  300. }else
  301. specific = nil;
  302. head = hmkcontent(h->c, generic, specific, head);
  303. for(;;){
  304. switch(h->tok){
  305. case ';':
  306. /*
  307. * should make a list of these params
  308. * for accept, they fall into two classes:
  309. * up to a q=..., they modify the media type.
  310. * afterwards, they acceptance criteria
  311. */
  312. if(lex(h) == Word){
  313. s = hstrdup(h->c, h->wordval);
  314. if(lex(h) != '=' || lex(h) != Word && h->tok != QString)
  315. return head;
  316. v = strtod(h->wordval, nil);
  317. if(strcmp(s, "q") == 0)
  318. head->q = v;
  319. else if(strcmp(s, "mxb") == 0)
  320. head->mxb = v;
  321. else{
  322. /* cope with accept: application/xhtml+xml; profile=http://www.wapforum.org/xhtml, */
  323. while(lex(h) == Word || (h->tok != ',' && h->eol == 0) )
  324. ;
  325. return mimeok(h, name, multipart, head);
  326. }
  327. }
  328. break;
  329. case ',':
  330. return mimeok(h, name, multipart, head);
  331. default:
  332. return head;
  333. }
  334. lex(h);
  335. }
  336. }
  337. /*
  338. * parse a list of entity tags
  339. * 1#entity-tag
  340. * entity-tag = [weak] opaque-tag
  341. * weak = "W/"
  342. * opaque-tag = quoted-string
  343. */
  344. static HETag*
  345. mimeetag(Hlex *h, HETag *head)
  346. {
  347. HETag *e;
  348. int weak;
  349. for(;;){
  350. while(lex(h) != Word && h->tok != QString)
  351. if(h->tok != ',')
  352. return head;
  353. weak = 0;
  354. if(h->tok == Word && strcmp(h->wordval, "*") != 0){
  355. if(strcmp(h->wordval, "W") != 0)
  356. return head;
  357. if(lex(h) != '/' || lex(h) != QString)
  358. return head;
  359. weak = 1;
  360. }
  361. e = halloc(h->c, sizeof(HETag));
  362. e->etag = hstrdup(h->c, h->wordval);
  363. e->weak = weak;
  364. e->next = head;
  365. head = e;
  366. if(lex(h) != ',')
  367. return head;
  368. }
  369. }
  370. /*
  371. * ranges-specifier = byte-ranges-specifier
  372. * byte-ranges-specifier = "bytes" "=" byte-range-set
  373. * byte-range-set = 1#(byte-range-spec|suffix-byte-range-spec)
  374. * byte-range-spec = byte-pos "-" [byte-pos]
  375. * byte-pos = 1*DIGIT
  376. * suffix-byte-range-spec = "-" suffix-length
  377. * suffix-length = 1*DIGIT
  378. *
  379. * syntactically invalid range specifiers cause the
  380. * entire header field to be ignored.
  381. * it is syntactically incorrect for the second byte pos
  382. * to be smaller than the first byte pos
  383. */
  384. static HRange*
  385. mimeranges(Hlex *h, HRange *head)
  386. {
  387. HRange *r, *rh, *tail;
  388. char *w;
  389. uint32_t start, stop;
  390. int suf;
  391. if(lex(h) != Word || strcmp(h->wordval, "bytes") != 0 || lex(h) != '=')
  392. return head;
  393. rh = nil;
  394. tail = nil;
  395. for(;;){
  396. while(lex(h) != Word){
  397. if(h->tok != ','){
  398. if(h->tok == '\n')
  399. goto breakout;
  400. return head;
  401. }
  402. }
  403. w = h->wordval;
  404. start = 0;
  405. suf = 1;
  406. if(w[0] != '-'){
  407. suf = 0;
  408. start = digtoul(w, &w);
  409. if(w[0] != '-')
  410. return head;
  411. }
  412. w++;
  413. stop = ~0UL;
  414. if(w[0] != '\0'){
  415. stop = digtoul(w, &w);
  416. if(w[0] != '\0')
  417. return head;
  418. if(!suf && stop < start)
  419. return head;
  420. }
  421. r = halloc(h->c, sizeof(HRange));
  422. r->suffix = suf;
  423. r->start = start;
  424. r->stop = stop;
  425. r->next = nil;
  426. if(rh == nil)
  427. rh = r;
  428. else
  429. tail->next = r;
  430. tail = r;
  431. if(lex(h) != ','){
  432. if(h->tok == '\n')
  433. break;
  434. return head;
  435. }
  436. }
  437. breakout:;
  438. if(head == nil)
  439. return rh;
  440. for(tail = head; tail->next != nil; tail = tail->next)
  441. ;
  442. tail->next = rh;
  443. return head;
  444. }
  445. static void
  446. mimeaccept(Hlex *h, char *name)
  447. {
  448. h->c->head.oktype = mimeok(h, name, 1, h->c->head.oktype);
  449. }
  450. static void
  451. mimeacceptchar(Hlex *h, char *name)
  452. {
  453. h->c->head.okchar = mimeok(h, name, 0, h->c->head.okchar);
  454. }
  455. static void
  456. mimeacceptenc(Hlex *h, char *name)
  457. {
  458. h->c->head.okencode = mimeok(h, name, 0, h->c->head.okencode);
  459. }
  460. static void
  461. mimeacceptlang(Hlex *h, char *name)
  462. {
  463. h->c->head.oklang = mimeok(h, name, 0, h->c->head.oklang);
  464. }
  465. static void
  466. mimemodified(Hlex *h, char *name)
  467. {
  468. lexhead(h);
  469. h->c->head.ifmodsince = hdate2sec(h->wordval);
  470. }
  471. static void
  472. mimeunmodified(Hlex *h, char *name)
  473. {
  474. lexhead(h);
  475. h->c->head.ifunmodsince = hdate2sec(h->wordval);
  476. }
  477. static void
  478. mimematch(Hlex *h, char *name)
  479. {
  480. h->c->head.ifmatch = mimeetag(h, h->c->head.ifmatch);
  481. }
  482. static void
  483. mimenomatch(Hlex *h, char *name)
  484. {
  485. h->c->head.ifnomatch = mimeetag(h, h->c->head.ifnomatch);
  486. }
  487. /*
  488. * argument is either etag or date
  489. */
  490. static void
  491. mimeifrange(Hlex *h, char *name)
  492. {
  493. int c, d, et;
  494. et = 0;
  495. c = getc(h);
  496. while(c == ' ' || c == '\t')
  497. c = getc(h);
  498. if(c == '"')
  499. et = 1;
  500. else if(c == 'W'){
  501. d = getc(h);
  502. if(d == '/')
  503. et = 1;
  504. ungetc(h);
  505. }
  506. ungetc(h);
  507. if(et){
  508. h->c->head.ifrangeetag = mimeetag(h, h->c->head.ifrangeetag);
  509. }else{
  510. lexhead(h);
  511. h->c->head.ifrangedate = hdate2sec(h->wordval);
  512. }
  513. }
  514. static void
  515. mimerange(Hlex *h, char *name)
  516. {
  517. h->c->head.range = mimeranges(h, h->c->head.range);
  518. }
  519. /*
  520. * parse it like cookies
  521. */
  522. static void
  523. authdigest(Hlex *h, char *name)
  524. {
  525. char *s;
  526. HSPairs *p;
  527. p = nil;
  528. for(;;){
  529. while(lex(h) != Word)
  530. if(h->tok != ';' && h->tok != ',')
  531. goto breakout;
  532. s = hstrdup(h->c, h->wordval);
  533. while (lex(h) != Word && h->tok != QString)
  534. if (h->tok != '=')
  535. goto breakout;
  536. p = hmkspairs(h->c, s, hstrdup(h->c, h->wordval), p);
  537. }
  538. breakout:
  539. h->c->head.authinfo = hrevspairs(p);
  540. }
  541. /*
  542. * note: netscape and ie through versions 4.7 and 4
  543. * support only basic authorization, so that is all that is supported here
  544. *
  545. * "Authorization" ":" "Basic" base64-user-pass
  546. * where base64-user-pass is the base64 encoding of
  547. * username ":" password
  548. */
  549. static void
  550. authbasic(Hlex *h, char *name)
  551. {
  552. char *up, *p;
  553. int n;
  554. n = lexbase64(h);
  555. if(!n)
  556. return;
  557. /*
  558. * wipe out source for password, so it won't be logged.
  559. * it is replaced by a single =,
  560. * which is valid base64, but not ok for an auth reponse.
  561. * therefore future parses of the header field will not overwrite
  562. * authuser and authpass.
  563. */
  564. memmove(h->c->hpos - (n - 1), h->c->hpos, h->c->hstop - h->c->hpos);
  565. h->c->hstop -= n - 1;
  566. *h->c->hstop = '\0';
  567. h->c->hpos -= n - 1;
  568. h->c->hpos[-1] = '=';
  569. up = halloc(h->c, n + 1);
  570. n = dec64((uint8_t*)up, n, h->wordval, n);
  571. up[n] = '\0';
  572. p = strchr(up, ':');
  573. if(p != nil){
  574. *p++ = '\0';
  575. h->c->head.authuser = hstrdup(h->c, up);
  576. h->c->head.authpass = hstrdup(h->c, p);
  577. }
  578. }
  579. /*
  580. * "Authorization" ":" "Basic" | "Digest" ...
  581. */
  582. static void
  583. mimeauthorization(Hlex *h, char *name)
  584. {
  585. int i;
  586. static MimeHead authparser[] = {
  587. { "basic", authbasic },
  588. { "digest", authdigest },
  589. };
  590. if(lex(h) != Word)
  591. return;
  592. for (i = 0; i < nelem(authparser); i++)
  593. if (cistrcmp(h->wordval, authparser[i].name) == 0) {
  594. (*authparser[i].parse)(h, nil);
  595. break;
  596. }
  597. }
  598. static void
  599. mimeagent(Hlex *h, char *name)
  600. {
  601. lexhead(h);
  602. h->c->head.client = hstrdup(h->c, h->wordval);
  603. }
  604. static void
  605. mimefrom(Hlex *h, char *name)
  606. {
  607. lexhead(h);
  608. }
  609. static void
  610. mimehost(Hlex *h, char *name)
  611. {
  612. char *hd;
  613. lexhead(h);
  614. for(hd = h->wordval; *hd == ' ' || *hd == '\t'; hd++)
  615. ;
  616. h->c->head.host = hlower(hstrdup(h->c, hd));
  617. }
  618. /*
  619. * if present, implies that a message body follows the headers
  620. * "content-length" ":" digits
  621. */
  622. static void
  623. mimecontlen(Hlex *h, char *name)
  624. {
  625. char *e;
  626. uint32_t v;
  627. if(lex(h) != Word)
  628. return;
  629. e = h->wordval;
  630. v = digtoul(e, &e);
  631. if(v == ~0UL || *e != '\0')
  632. return;
  633. h->c->head.contlen = v;
  634. }
  635. /*
  636. * mimexpect : "expect" ":" expects
  637. * expects : | expects "," expect
  638. * expect : "100-continue" | token | token "=" token expectparams | token "=" qstring expectparams
  639. * expectparams : ";" token | ";" token "=" token | token "=" qstring
  640. * for now, we merely parse "100-continue" or anything else.
  641. */
  642. static void
  643. mimeexpect(Hlex *h, char *name)
  644. {
  645. if(lex(h) != Word || cistrcmp(h->wordval, "100-continue") != 0 || lex(h) != '\n')
  646. h->c->head.expectother = 1;
  647. h->c->head.expectcont = 1;
  648. }
  649. static void
  650. mimetransenc(Hlex *h, char *name)
  651. {
  652. h->c->head.transenc = mimehfields(h);
  653. }
  654. static void
  655. mimecookie(Hlex *h, char *name)
  656. {
  657. char *s;
  658. HSPairs *p;
  659. p = nil;
  660. for(;;){
  661. while(lex(h) != Word)
  662. if(h->tok != ';' && h->tok != ',')
  663. goto breakout;
  664. s = hstrdup(h->c, h->wordval);
  665. while (lex(h) != Word && h->tok != QString)
  666. if (h->tok != '=')
  667. goto breakout;
  668. p = hmkspairs(h->c, s, hstrdup(h->c, h->wordval), p);
  669. }
  670. breakout:
  671. h->c->head.cookie = hrevspairs(p);
  672. }
  673. static void
  674. mimefresh(Hlex *h, char *name)
  675. {
  676. char *s;
  677. lexhead(h);
  678. for(s = h->wordval; *s && (*s==' ' || *s=='\t'); s++)
  679. ;
  680. if(strncmp(s, "pathstat/", 9) == 0)
  681. h->c->head.fresh_thresh = atoi(s+9);
  682. else if(strncmp(s, "have/", 5) == 0)
  683. h->c->head.fresh_have = atoi(s+5);
  684. }
  685. static void
  686. mimeignore(Hlex *h, char *name)
  687. {
  688. lexhead(h);
  689. }
  690. static void
  691. parsejump(Hlex *h, char *k)
  692. {
  693. int l, r, m;
  694. l = 1;
  695. r = nelem(mimehead) - 1;
  696. while(l <= r){
  697. m = (r + l) >> 1;
  698. if(cistrcmp(mimehead[m].name, k) <= 0)
  699. l = m + 1;
  700. else
  701. r = m - 1;
  702. }
  703. m = l - 1;
  704. if(cistrcmp(mimehead[m].name, k) == 0 && !mimehead[m].ignore){
  705. mimehead[m].seen = 1;
  706. (*mimehead[m].parse)(h, k);
  707. }else
  708. mimeignore(h, k);
  709. }
  710. static int
  711. lex(Hlex *h)
  712. {
  713. return h->tok = lex1(h, 0);
  714. }
  715. static int
  716. lexbase64(Hlex *h)
  717. {
  718. int c, n;
  719. n = 0;
  720. lex1(h, 1);
  721. while((c = getc(h)) >= 0){
  722. if(!isalnum(c) && c != '+' && c != '/'){
  723. ungetc(h);
  724. break;
  725. }
  726. if(n < HMaxWord-1)
  727. h->wordval[n++] = c;
  728. }
  729. h->wordval[n] = '\0';
  730. return n;
  731. }
  732. /*
  733. * rfc 822/rfc 1521 lexical analyzer
  734. */
  735. static int
  736. lex1(Hlex *h, int skipwhite)
  737. {
  738. int level, c;
  739. if(h->eol)
  740. return '\n';
  741. top:
  742. c = getc(h);
  743. switch(c){
  744. case '(':
  745. level = 1;
  746. while((c = getc(h)) >= 0){
  747. if(c == '\\'){
  748. c = getc(h);
  749. if(c < 0)
  750. return '\n';
  751. continue;
  752. }
  753. if(c == '(')
  754. level++;
  755. else if(c == ')' && --level == 0)
  756. break;
  757. else if(c == '\n'){
  758. c = getc(h);
  759. if(c < 0)
  760. return '\n';
  761. if(c == ')' && --level == 0)
  762. break;
  763. if(c != ' ' && c != '\t'){
  764. ungetc(h);
  765. return '\n';
  766. }
  767. }
  768. }
  769. goto top;
  770. case ' ': case '\t':
  771. goto top;
  772. case '\r':
  773. c = getc(h);
  774. if(c != '\n'){
  775. ungetc(h);
  776. goto top;
  777. }
  778. case '\n':
  779. if(h->tok == '\n'){
  780. h->eol = 1;
  781. h->eoh = 1;
  782. return '\n';
  783. }
  784. c = getc(h);
  785. if(c < 0){
  786. h->eol = 1;
  787. return '\n';
  788. }
  789. if(c != ' ' && c != '\t'){
  790. ungetc(h);
  791. h->eol = 1;
  792. return '\n';
  793. }
  794. goto top;
  795. case ')':
  796. case '<': case '>':
  797. case '[': case ']':
  798. case '@': case '/':
  799. case ',': case ';': case ':': case '?': case '=':
  800. if(skipwhite){
  801. ungetc(h);
  802. return c;
  803. }
  804. return c;
  805. case '"':
  806. if(skipwhite){
  807. ungetc(h);
  808. return c;
  809. }
  810. word(h, "\"");
  811. getc(h); /* skip the closing quote */
  812. return QString;
  813. default:
  814. ungetc(h);
  815. if(skipwhite)
  816. return c;
  817. word(h, "\"(){}<>@,;:/[]?=\r\n \t");
  818. if(h->wordval[0] == '\0'){
  819. h->c->head.closeit = 1;
  820. hfail(h->c, HSyntax);
  821. longjmp(h->jmp, -1);
  822. }
  823. return Word;
  824. }
  825. /* not reached */
  826. }
  827. /*
  828. * return the rest of an rfc 822, including \n
  829. * do not map to lower case
  830. */
  831. static void
  832. lexhead(Hlex *h)
  833. {
  834. int c, n;
  835. n = 0;
  836. while((c = getc(h)) >= 0){
  837. if(c == '\r')
  838. c = wordcr(h);
  839. else if(c == '\n')
  840. c = wordnl(h);
  841. if(c == '\n')
  842. break;
  843. if(c == '\\'){
  844. c = getc(h);
  845. if(c < 0)
  846. break;
  847. }
  848. if(n < HMaxWord-1)
  849. h->wordval[n++] = c;
  850. }
  851. h->tok = '\n';
  852. h->eol = 1;
  853. h->wordval[n] = '\0';
  854. }
  855. static void
  856. word(Hlex *h, char *stop)
  857. {
  858. int c, n;
  859. n = 0;
  860. while((c = getc(h)) >= 0){
  861. if(c == '\r')
  862. c = wordcr(h);
  863. else if(c == '\n')
  864. c = wordnl(h);
  865. if(c == '\\'){
  866. c = getc(h);
  867. if(c < 0)
  868. break;
  869. }else if(c < 32 || strchr(stop, c) != nil){
  870. ungetc(h);
  871. break;
  872. }
  873. if(n < HMaxWord-1)
  874. h->wordval[n++] = c;
  875. }
  876. h->wordval[n] = '\0';
  877. }
  878. static int
  879. wordcr(Hlex *h)
  880. {
  881. int c;
  882. c = getc(h);
  883. if(c == '\n')
  884. return wordnl(h);
  885. ungetc(h);
  886. return ' ';
  887. }
  888. static int
  889. wordnl(Hlex *h)
  890. {
  891. int c;
  892. c = getc(h);
  893. if(c == ' ' || c == '\t')
  894. return c;
  895. ungetc(h);
  896. return '\n';
  897. }
  898. static int
  899. getc(Hlex *h)
  900. {
  901. if(h->eoh)
  902. return -1;
  903. if(h->c->hpos < h->c->hstop)
  904. return *h->c->hpos++;
  905. h->eoh = 1;
  906. h->eol = 1;
  907. return -1;
  908. }
  909. static void
  910. ungetc(Hlex *h)
  911. {
  912. if(h->eoh)
  913. return;
  914. h->c->hpos--;
  915. }
  916. static uint32_t
  917. digtoul(char *s, char **e)
  918. {
  919. uint32_t v;
  920. int c, ovfl;
  921. v = 0;
  922. ovfl = 0;
  923. for(;;){
  924. c = *s;
  925. if(c < '0' || c > '9')
  926. break;
  927. s++;
  928. c -= '0';
  929. if(v > UlongMax/10 || v == UlongMax/10 && c >= UlongMax%10)
  930. ovfl = 1;
  931. v = v * 10 + c;
  932. }
  933. if(e)
  934. *e = s;
  935. if(ovfl)
  936. return UlongMax;
  937. return v;
  938. }
  939. int
  940. http11(HConnect *c)
  941. {
  942. return c->req.vermaj > 1 || c->req.vermaj == 1 && c->req.vermin > 0;
  943. }
  944. char*
  945. hmkmimeboundary(HConnect *c)
  946. {
  947. char buf[32];
  948. int i;
  949. srand((time(0)<<16)|getpid());
  950. strcpy(buf, "upas-");
  951. for(i = 5; i < sizeof(buf)-1; i++)
  952. buf[i] = 'a' + nrand(26);
  953. buf[i] = 0;
  954. return hstrdup(c, buf);
  955. }
  956. HSPairs*
  957. hmkspairs(HConnect *c, char *s, char *t, HSPairs *next)
  958. {
  959. HSPairs *sp;
  960. sp = halloc(c, sizeof *sp);
  961. sp->s = s;
  962. sp->t = t;
  963. sp->next = next;
  964. return sp;
  965. }
  966. HSPairs*
  967. hrevspairs(HSPairs *sp)
  968. {
  969. HSPairs *last, *next;
  970. last = nil;
  971. for(; sp != nil; sp = next){
  972. next = sp->next;
  973. sp->next = last;
  974. last = sp;
  975. }
  976. return last;
  977. }
  978. HFields*
  979. hmkhfields(HConnect *c, char *s, HSPairs *p, HFields *next)
  980. {
  981. HFields *hf;
  982. hf = halloc(c, sizeof *hf);
  983. hf->s = s;
  984. hf->params = p;
  985. hf->next = next;
  986. return hf;
  987. }
  988. HFields*
  989. hrevhfields(HFields *hf)
  990. {
  991. HFields *last, *next;
  992. last = nil;
  993. for(; hf != nil; hf = next){
  994. next = hf->next;
  995. hf->next = last;
  996. last = hf;
  997. }
  998. return last;
  999. }
  1000. HContent*
  1001. hmkcontent(HConnect *c, char *generic, char *specific, HContent *next)
  1002. {
  1003. HContent *ct;
  1004. ct = halloc(c, sizeof(HContent));
  1005. ct->generic = generic;
  1006. ct->specific = specific;
  1007. ct->next = next;
  1008. ct->q = 1;
  1009. ct->mxb = 0;
  1010. return ct;
  1011. }