xref: /plan9/sys/src/libhttpd/gethead.c (revision e059317eeee4a35883e310a859cb2906cb172f75)
1 #include <u.h>
2 #include <libc.h>
3 #include <bin.h>
4 #include <httpd.h>
5 
6 /*
7  * read in some header lines, either one or all of them.
8  * copy results into header log buffer.
9  */
10 int
hgethead(HConnect * c,int many)11 hgethead(HConnect *c, int many)
12 {
13 	Hio *hin;
14 	char *s, *p, *pp;
15 	int n;
16 
17 	hin = &c->hin;
18 	for(;;){
19 		s = (char*)hin->pos;
20 		pp = s;
21 		while(p = memchr(pp, '\n', (char*)hin->stop - pp)){
22 			if(!many || p == pp || (p == pp + 1 && *pp == '\r')){
23 				pp = p + 1;
24 				break;
25 			}
26 			pp = p + 1;
27 		}
28 		hin->pos = (uchar*)pp;
29 		n = pp - s;
30 		if(c->hstop + n > &c->header[HBufSize])
31 			return -1;
32 		memmove(c->hstop, s, n);
33 		c->hstop += n;
34 		*c->hstop = '\0';
35 		if(p != nil)
36 			return 0;
37 		if(hreadbuf(hin, hin->pos) == nil || hin->state == Hend)
38 			return -1;
39 	}
40 }
41