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