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);
354 * parse a list of entity tags
356 * entity-tag = [weak] opaque-tag
358 * opaque-tag = quoted-string
361 mimeetag(Hlex *h, HETag *head)
367 while(lex(h) != Word && h->tok != QString)
372 if(h->tok == Word && strcmp(h->wordval, "*") != 0){
373 if(strcmp(h->wordval, "W") != 0)
375 if(lex(h) != '/' || lex(h) != QString)
380 e = halloc(h->c, sizeof(HETag));
381 e->etag = hstrdup(h->c, h->wordval);
392 * ranges-specifier = byte-ranges-specifier
393 * byte-ranges-specifier = "bytes" "=" byte-range-set
394 * byte-range-set = 1#(byte-range-spec|suffix-byte-range-spec)
395 * byte-range-spec = byte-pos "-" [byte-pos]
397 * suffix-byte-range-spec = "-" suffix-length
398 * suffix-length = 1*DIGIT
400 * syntactically invalid range specifiers cause the
401 * entire header field to be ignored.
402 * it is syntactically incorrect for the second byte pos
403 * to be smaller than the first byte pos
406 mimeranges(Hlex *h, HRange *head)
408 HRange *r, *rh, *tail;
413 if(lex(h) != Word || strcmp(h->wordval, "bytes") != 0 || lex(h) != '=')
419 while(lex(h) != Word){
432 start = digtoul(w, &w);
439 stop = digtoul(w, &w);
442 if(!suf && stop < start)
446 r = halloc(h->c, sizeof(HRange));
468 for(tail = head; tail->next != nil; tail = tail->next)
475 mimeaccept(Hlex *h, char *name)
477 h->c->head.oktype = mimeok(h, name, 1, h->c->head.oktype);
481 mimeacceptchar(Hlex *h, char *name)
483 h->c->head.okchar = mimeok(h, name, 0, h->c->head.okchar);
487 mimeacceptenc(Hlex *h, char *name)
489 h->c->head.okencode = mimeok(h, name, 0, h->c->head.okencode);
493 mimeacceptlang(Hlex *h, char *name)
495 h->c->head.oklang = mimeok(h, name, 0, h->c->head.oklang);
499 mimemodified(Hlex *h, char *unused)
502 h->c->head.ifmodsince = hdate2sec(h->wordval);
506 mimeunmodified(Hlex *h, char *unused)
509 h->c->head.ifunmodsince = hdate2sec(h->wordval);
513 mimematch(Hlex *h, char *unused)
515 h->c->head.ifmatch = mimeetag(h, h->c->head.ifmatch);
519 mimenomatch(Hlex *h, char *unused)
521 h->c->head.ifnomatch = mimeetag(h, h->c->head.ifnomatch);
525 * argument is either etag or date
528 mimeifrange(Hlex *h, char *unused)
534 while(c == ' ' || c == '\t')
546 h->c->head.ifrangeetag = mimeetag(h, h->c->head.ifrangeetag);
549 h->c->head.ifrangedate = hdate2sec(h->wordval);
554 mimerange(Hlex *h, char *unused)
556 h->c->head.range = mimeranges(h, h->c->head.range);
560 * note: netscape and ie through versions 4.7 and 4
561 * support only basic authorization, so that is all that is supported here
563 * "Authorization" ":" "Basic" base64-user-pass
564 * where base64-user-pass is the base64 encoding of
565 * username ":" password
568 mimeauthorization(Hlex *h, char *unused)
573 if(lex(h) != Word || cistrcmp(h->wordval, "basic") != 0)
581 * wipe out source for password, so it won't be logged.
582 * it is replaced by a single =,
583 * which is valid base64, but not ok for an auth reponse.
584 * therefore future parses of the header field will not overwrite
585 * authuser and authpass.
587 memmove(h->c->hpos - (n - 1), h->c->hpos, h->c->hstop - h->c->hpos);
588 h->c->hstop -= n - 1;
591 h->c->hpos[-1] = '=';
593 up = halloc(h->c, n + 1);
594 n = dec64((uchar*)up, n, h->wordval, n);
599 h->c->head.authuser = hstrdup(h->c, up);
600 h->c->head.authpass = hstrdup(h->c, p);
605 mimeagent(Hlex *h, char *unused)
608 h->c->head.client = hstrdup(h->c, h->wordval);
612 mimefrom(Hlex *h, char *unused)
618 mimehost(Hlex *h, char *unused)
623 for(hd = h->wordval; *hd == ' ' || *hd == '\t'; hd++)
625 h->c->head.host = hlower(hstrdup(h->c, hd));
629 * if present, implies that a message body follows the headers
630 * "content-length" ":" digits
633 mimecontlen(Hlex *h, char *unused)
642 if(v == ~0UL || *e != '\0')
644 h->c->head.contlen = v;
648 * mimexpect : "expect" ":" expects
649 * expects : | expects "," expect
650 * expect : "100-continue" | token | token "=" token expectparams | token "=" qstring expectparams
651 * expectparams : ";" token | ";" token "=" token | token "=" qstring
652 * for now, we merely parse "100-continue" or anything else.
655 mimeexpect(Hlex *h, char *unused)
657 if(lex(h) != Word || cistrcmp(h->wordval, "100-continue") != 0 || lex(h) != '\n')
658 h->c->head.expectother = 1;
659 h->c->head.expectcont = 1;
663 mimetransenc(Hlex *h, char *unused)
665 h->c->head.transenc = mimehfields(h);
669 mimefresh(Hlex *h, char *unused)
674 for(s = h->wordval; *s && (*s==' ' || *s=='\t'); s++)
676 if(strncmp(s, "pathstat/", 9) == 0)
677 h->c->head.fresh_thresh = atoi(s+9);
678 else if(strncmp(s, "have/", 5) == 0)
679 h->c->head.fresh_have = atoi(s+5);
683 mimeignore(Hlex *h, char *unused)
689 parsejump(Hlex *h, char *k)
694 r = nelem(mimehead) - 1;
697 if(cistrcmp(mimehead[m].name, k) <= 0)
703 if(cistrcmp(mimehead[m].name, k) == 0 && !mimehead[m].ignore){
704 mimehead[m].seen = 1;
705 (*mimehead[m].parse)(h, k);
713 return h->tok = lex1(h, 0);
724 while((c = getc(h)) >= 0){
725 if(!(c >= 'A' && c <= 'Z'
726 || c >= 'a' && c <= 'z'
727 || c >= '0' && c <= '9'
728 || c == '+' || c == '/')){
736 h->wordval[n] = '\0';
741 * rfc 822/rfc 1521 lexical analyzer
744 lex1(Hlex *h, int skipwhite)
756 while((c = getc(h)) >= 0){
765 else if(c == ')' && --level == 0)
771 if(c == ')' && --level == 0)
773 if(c != ' ' && c != '\t'){
802 if(c != ' ' && c != '\t'){
813 case ',': case ';': case ':': case '?': case '=':
826 getc(h); /* skip the closing quote */
833 word(h, "\"(){}<>@,;:/[]?=\r\n \t");
834 if(h->wordval[0] == '\0'){
835 h->c->head.closeit = 1;
836 hfail(h->c, HSyntax);
845 * return the rest of an rfc 822, including \n
846 * do not map to lower case
854 while((c = getc(h)) >= 0){
872 h->wordval[n] = '\0';
876 word(Hlex *h, char *stop)
881 while((c = getc(h)) >= 0){
890 }else if(c < 32 || strchr(stop, c) != nil){
898 h->wordval[n] = '\0';
919 if(c == ' ' || c == '\t')
931 if(h->c->hpos < h->c->hstop)
932 return *h->c->hpos++;
947 digtoul(char *s, char **e)
956 if(c < '0' || c > '9')
960 if(v > UlongMax/10 || v == UlongMax/10 && c >= UlongMax%10)
975 return c->req.vermaj > 1 || c->req.vermaj == 1 && c->req.vermin > 0;
979 hmkmimeboundary(HConnect *c)
984 srand((time(0)<<16)|getpid());
985 strcpy(buf, "upas-");
986 for(i = 5; i < sizeof(buf)-1; i++)
987 buf[i] = 'a' + nrand(26);
989 return hstrdup(c, buf);
993 hmkspairs(HConnect *c, char *s, char *t, HSPairs *next)
997 sp = halloc(c, sizeof *sp);
1005 hrevspairs(HSPairs *sp)
1007 HSPairs *last, *next;
1010 for(; sp != nil; sp = next){
1019 hmkhfields(HConnect *c, char *s, HSPairs *p, HFields *next)
1023 hf = halloc(c, sizeof *hf);
1031 hrevhfields(HFields *hf)
1033 HFields *last, *next;
1036 for(; hf != nil; hf = next){
1045 hmkcontent(HConnect *c, char *generic, char *specific, HContent *next)
1049 ct = halloc(c, sizeof(HContent));
1050 ct->generic = generic;
1051 ct->specific = specific;