7 typedef struct Hlex Hlex;
8 typedef struct MimeHead MimeHead;
19 #define UlongMax 4294967295UL
25 int eol; /* end of header line encountered? */
26 uchar *hstart; /* start of header */
27 jmp_buf jmp; /* jmp here to parse header */
28 char wordval[HMaxWord];
35 void (*parse)(Hlex*, char*);
40 static void mimeaccept(Hlex*, char*);
41 static void mimeacceptchar(Hlex*, char*);
42 static void mimeacceptenc(Hlex*, char*);
43 static void mimeacceptlang(Hlex*, char*);
44 static void mimeagent(Hlex*, char*);
45 static void mimeauthorization(Hlex*, char*);
46 static void mimeconnection(Hlex*, char*);
47 static void mimecontlen(Hlex*, char*);
48 static void mimeexpect(Hlex*, char*);
49 static void mimefresh(Hlex*, char*);
50 static void mimefrom(Hlex*, char*);
51 static void mimehost(Hlex*, char*);
52 static void mimeifrange(Hlex*, char*);
53 static void mimeignore(Hlex*, char*);
54 static void mimematch(Hlex*, char*);
55 static void mimemodified(Hlex*, char*);
56 static void mimenomatch(Hlex*, char*);
57 static void mimerange(Hlex*, char*);
58 static void mimetransenc(Hlex*, char*);
59 static void mimeunmodified(Hlex*, char*);
62 * headers seen also include
63 * allow cache-control chargeto
64 * content-encoding content-language content-location content-md5 content-range content-type
65 * date etag expires forwarded last-modified max-forwards pragma
66 * proxy-agent proxy-authorization proxy-connection
67 * ua-color ua-cpu ua-os ua-pixels
68 * upgrade via x-afs-tokens x-serial-number
70 static MimeHead mimehead[] =
72 {"accept", mimeaccept},
73 {"accept-charset", mimeacceptchar},
74 {"accept-encoding", mimeacceptenc},
75 {"accept-language", mimeacceptlang},
76 {"authorization", mimeauthorization},
77 {"connection", mimeconnection},
78 {"content-length", mimecontlen},
79 {"expect", mimeexpect},
83 {"if-match", mimematch},
84 {"if-modified-since", mimemodified},
85 {"if-none-match", mimenomatch},
86 {"if-range", mimeifrange},
87 {"if-unmodified-since", mimeunmodified},
89 {"transfer-encoding", mimetransenc},
90 {"user-agent", mimeagent},
94 char* hversion = "HTTP/1.1";
96 static void lexhead(Hlex*);
97 static void parsejump(Hlex*, char*);
98 static int getc(Hlex*);
99 static void ungetc(Hlex*);
100 static int wordcr(Hlex*);
101 static int wordnl(Hlex*);
102 static void word(Hlex*, char*);
103 static int lex1(Hlex*, int);
104 static int lex(Hlex*);
105 static int lexbase64(Hlex*);
106 static ulong digtoul(char *s, char **e);
109 * flush an clean up junk from a request
112 hreqcleanup(HConnect *c)
116 hxferenc(&c->hout, 0);
117 memset(&c->req, 0, sizeof(c->req));
118 memset(&c->head, 0, sizeof(c->head));
120 c->hstop = c->header;
122 for(i = 0; i < nelem(mimehead); i++){
123 mimehead[i].seen = 0;
124 mimehead[i].ignore = 0;
130 * if the client is HTTP/1.0,
131 * ignore headers which match one of the tokens.
132 * restarts parsing if necessary.
135 mimeconnection(Hlex *h, char *unused)
142 while(lex(h) != Word)
146 if(cistrcmp(h->wordval, "keep-alive") == 0)
147 h->c->head.persist = 1;
148 else if(cistrcmp(h->wordval, "close") == 0)
149 h->c->head.closeit = 1;
150 else if(!http11(h->c)){
151 for(i = 0; i < nelem(mimehead); i++){
152 if(cistrcmp(mimehead[i].name, h->wordval) == 0){
153 reparse = mimehead[i].seen && !mimehead[i].ignore;
154 mimehead[i].ignore = 1;
155 if(cistrcmp(mimehead[i].name, "authorization") == 0){
156 h->c->head.authuser = nil;
157 h->c->head.authpass = nil;
169 * if need to ignore headers we've already parsed,
170 * reset & start over. need to save authorization
171 * info because it's written over when parsed.
174 u = h->c->head.authuser;
175 p = h->c->head.authpass;
176 memset(&h->c->head, 0, sizeof(h->c->head));
177 h->c->head.authuser = u;
178 h->c->head.authpass = p;
180 h->c->hpos = h->hstart;
186 hparseheaders(HConnect *c, int timeout)
190 c->head.fresh_thresh = 0;
191 c->head.fresh_have = 0;
193 if(c->req.vermaj == 0){
194 c->head.host = hmydomain;
198 memset(&h, 0, sizeof(h));
202 if(hgethead(c, 1) < 0)
208 if(setjmp(h.jmp) == -1)
214 while(lex(&h) != '\n'){
215 if(h.tok == Word && lex(&h) == ':')
216 parsejump(&h, hstrdup(c, h.wordval));
224 * according to the http/1.1 spec,
225 * these rules must be followed
227 if(c->head.host == nil){
228 hfail(c, HBadReq, nil);
231 if(c->req.urihost != nil)
232 c->head.host = c->req.urihost;
234 * also need to check host is actually this one
236 }else if(c->head.host == nil)
237 c->head.host = hmydomain;
242 * mimeparams : | mimeparams ";" mimepara
243 * mimeparam : token "=" token | token "=" qstring
255 s = hstrdup(h->c, h->wordval);
256 if(lex(h) != Word && h->tok != QString)
258 p = hmkspairs(h->c, s, hstrdup(h->c, h->wordval), p);
260 return hrevspairs(p);
264 * mimehfields : mimehfield | mimehfields commas mimehfield
265 * mimehfield : token mimeparams
266 * commas : "," | commas ","
275 while(lex(h) != Word)
279 f = hmkhfields(h->c, hstrdup(h->c, h->wordval), nil, f);
282 f->params = mimeparams(h);
287 return hrevhfields(f);
291 * parse a list of acceptable types, encodings, languages, etc.
294 mimeok(Hlex *h, char *name, int multipart, HContent *head)
296 char *generic, *specific, *s;
300 * each type is separated by one or more commas
302 while(lex(h) != Word)
306 generic = hstrdup(h->c, h->wordval);
308 if(h->tok == '/' || multipart){
310 * at one time, IE5 improperly said '*' for single types
316 specific = hstrdup(h->c, h->wordval);
317 if(!multipart && strcmp(specific, "*") != 0)
322 head = hmkcontent(h->c, generic, specific, head);
328 * should make a list of these params
329 * for accept, they fall into two classes:
330 * up to a q=..., they modify the media type.
331 * afterwards, they acceptance criteria
334 s = hstrdup(h->c, h->wordval);
335 if(lex(h) != '=' || lex(h) != Word && h->tok != QString)
337 v = strtod(h->wordval, nil);
338 if(strcmp(s, "q") == 0)
340 else if(strcmp(s, "mxb") == 0)
345 return mimeok(h, name, multipart, head);
355 * parse a list of entity tags
357 * entity-tag = [weak] opaque-tag
359 * opaque-tag = quoted-string
362 mimeetag(Hlex *h, HETag *head)
368 while(lex(h) != Word && h->tok != QString)
373 if(h->tok == Word && strcmp(h->wordval, "*") != 0){
374 if(strcmp(h->wordval, "W") != 0)
376 if(lex(h) != '/' || lex(h) != QString)
381 e = halloc(h->c, sizeof(HETag));
382 e->etag = hstrdup(h->c, h->wordval);
394 * ranges-specifier = byte-ranges-specifier
395 * byte-ranges-specifier = "bytes" "=" byte-range-set
396 * byte-range-set = 1#(byte-range-spec|suffix-byte-range-spec)
397 * byte-range-spec = byte-pos "-" [byte-pos]
399 * suffix-byte-range-spec = "-" suffix-length
400 * suffix-length = 1*DIGIT
402 * syntactically invalid range specifiers cause the
403 * entire header field to be ignored.
404 * it is syntactically incorrect for the second byte pos
405 * to be smaller than the first byte pos
408 mimeranges(Hlex *h, HRange *head)
410 HRange *r, *rh, *tail;
415 if(lex(h) != Word || strcmp(h->wordval, "bytes") != 0 || lex(h) != '=')
421 while(lex(h) != Word){
434 start = digtoul(w, &w);
441 stop = digtoul(w, &w);
444 if(!suf && stop < start)
448 r = halloc(h->c, sizeof(HRange));
470 for(tail = head; tail->next != nil; tail = tail->next)
477 mimeaccept(Hlex *h, char *name)
479 h->c->head.oktype = mimeok(h, name, 1, h->c->head.oktype);
483 mimeacceptchar(Hlex *h, char *name)
485 h->c->head.okchar = mimeok(h, name, 0, h->c->head.okchar);
489 mimeacceptenc(Hlex *h, char *name)
491 h->c->head.okencode = mimeok(h, name, 0, h->c->head.okencode);
495 mimeacceptlang(Hlex *h, char *name)
497 h->c->head.oklang = mimeok(h, name, 0, h->c->head.oklang);
501 mimemodified(Hlex *h, char *unused)
504 h->c->head.ifmodsince = hdate2sec(h->wordval);
508 mimeunmodified(Hlex *h, char *unused)
511 h->c->head.ifunmodsince = hdate2sec(h->wordval);
515 mimematch(Hlex *h, char *unused)
517 h->c->head.ifmatch = mimeetag(h, h->c->head.ifmatch);
521 mimenomatch(Hlex *h, char *unused)
523 h->c->head.ifnomatch = mimeetag(h, h->c->head.ifnomatch);
527 * argument is either etag or date
530 mimeifrange(Hlex *h, char *unused)
536 while(c == ' ' || c == '\t')
548 h->c->head.ifrangeetag = mimeetag(h, h->c->head.ifrangeetag);
551 h->c->head.ifrangedate = hdate2sec(h->wordval);
556 mimerange(Hlex *h, char *unused)
558 h->c->head.range = mimeranges(h, h->c->head.range);
562 * note: netscape and ie through versions 4.7 and 4
563 * support only basic authorization, so that is all that is supported here
565 * "Authorization" ":" "Basic" base64-user-pass
566 * where base64-user-pass is the base64 encoding of
567 * username ":" password
570 mimeauthorization(Hlex *h, char *unused)
575 if(lex(h) != Word || cistrcmp(h->wordval, "basic") != 0)
583 * wipe out source for password, so it won't be logged.
584 * it is replaced by a single =,
585 * which is valid base64, but not ok for an auth reponse.
586 * therefore future parses of the header field will not overwrite
587 * authuser and authpass.
589 memmove(h->c->hpos - (n - 1), h->c->hpos, h->c->hstop - h->c->hpos);
590 h->c->hstop -= n - 1;
593 h->c->hpos[-1] = '=';
595 up = halloc(h->c, n + 1);
596 n = dec64((uchar*)up, n, h->wordval, n);
601 h->c->head.authuser = hstrdup(h->c, up);
602 h->c->head.authpass = hstrdup(h->c, p);
607 mimeagent(Hlex *h, char *unused)
610 h->c->head.client = hstrdup(h->c, h->wordval);
614 mimefrom(Hlex *h, char *unused)
620 mimehost(Hlex *h, char *unused)
625 for(hd = h->wordval; *hd == ' ' || *hd == '\t'; hd++)
627 h->c->head.host = hlower(hstrdup(h->c, hd));
631 * if present, implies that a message body follows the headers
632 * "content-length" ":" digits
635 mimecontlen(Hlex *h, char *unused)
644 if(v == ~0UL || *e != '\0')
646 h->c->head.contlen = v;
650 * mimexpect : "expect" ":" expects
651 * expects : | expects "," expect
652 * expect : "100-continue" | token | token "=" token expectparams | token "=" qstring expectparams
653 * expectparams : ";" token | ";" token "=" token | token "=" qstring
654 * for now, we merely parse "100-continue" or anything else.
657 mimeexpect(Hlex *h, char *unused)
659 if(lex(h) != Word || cistrcmp(h->wordval, "100-continue") != 0 || lex(h) != '\n')
660 h->c->head.expectother = 1;
661 h->c->head.expectcont = 1;
665 mimetransenc(Hlex *h, char *unused)
667 h->c->head.transenc = mimehfields(h);
671 mimefresh(Hlex *h, char *unused)
676 for(s = h->wordval; *s && (*s==' ' || *s=='\t'); s++)
678 if(strncmp(s, "pathstat/", 9) == 0)
679 h->c->head.fresh_thresh = atoi(s+9);
680 else if(strncmp(s, "have/", 5) == 0)
681 h->c->head.fresh_have = atoi(s+5);
685 mimeignore(Hlex *h, char *unused)
691 parsejump(Hlex *h, char *k)
696 r = nelem(mimehead) - 1;
699 if(cistrcmp(mimehead[m].name, k) <= 0)
705 if(cistrcmp(mimehead[m].name, k) == 0 && !mimehead[m].ignore){
706 mimehead[m].seen = 1;
707 (*mimehead[m].parse)(h, k);
715 return h->tok = lex1(h, 0);
726 while((c = getc(h)) >= 0){
727 if(!(c >= 'A' && c <= 'Z'
728 || c >= 'a' && c <= 'z'
729 || c >= '0' && c <= '9'
730 || c == '+' || c == '/')){
738 h->wordval[n] = '\0';
743 * rfc 822/rfc 1521 lexical analyzer
746 lex1(Hlex *h, int skipwhite)
758 while((c = getc(h)) >= 0){
767 else if(c == ')' && --level == 0)
773 if(c == ')' && --level == 0)
775 if(c != ' ' && c != '\t'){
804 if(c != ' ' && c != '\t'){
815 case ',': case ';': case ':': case '?': case '=':
828 getc(h); /* skip the closing quote */
835 word(h, "\"(){}<>@,;:/[]?=\r\n \t");
836 if(h->wordval[0] == '\0'){
837 h->c->head.closeit = 1;
838 hfail(h->c, HSyntax);
848 * return the rest of an rfc 822, including \n
849 * do not map to lower case
857 while((c = getc(h)) >= 0){
875 h->wordval[n] = '\0';
879 word(Hlex *h, char *stop)
884 while((c = getc(h)) >= 0){
893 }else if(c < 32 || strchr(stop, c) != nil){
901 h->wordval[n] = '\0';
922 if(c == ' ' || c == '\t')
934 if(h->c->hpos < h->c->hstop)
935 return *h->c->hpos++;
950 digtoul(char *s, char **e)
959 if(c < '0' || c > '9')
963 if(v > UlongMax/10 || v == UlongMax/10 && c >= UlongMax%10)
978 return c->req.vermaj > 1 || c->req.vermaj == 1 && c->req.vermin > 0;
982 hmkmimeboundary(HConnect *c)
987 srand((time(0)<<16)|getpid());
988 strcpy(buf, "upas-");
989 for(i = 5; i < sizeof(buf)-1; i++)
990 buf[i] = 'a' + nrand(26);
992 return hstrdup(c, buf);
996 hmkspairs(HConnect *c, char *s, char *t, HSPairs *next)
1000 sp = halloc(c, sizeof *sp);
1008 hrevspairs(HSPairs *sp)
1010 HSPairs *last, *next;
1013 for(; sp != nil; sp = next){
1022 hmkhfields(HConnect *c, char *s, HSPairs *p, HFields *next)
1026 hf = halloc(c, sizeof *hf);
1034 hrevhfields(HFields *hf)
1036 HFields *last, *next;
1039 for(; hf != nil; hf = next){
1048 hmkcontent(HConnect *c, char *generic, char *specific, HContent *next)
1052 ct = halloc(c, sizeof(HContent));
1053 ct->generic = generic;
1054 ct->specific = specific;