6 typedef struct Strings Strings;
14 static char* abspath(HConnect *cc, char *origpath, char *curdir);
15 static int getc(HConnect*);
16 static char* getword(HConnect*);
17 static Strings parseuri(HConnect *c, char*);
18 static Strings stripsearch(char*);
21 * parse the next request line
28 hparsereq(HConnect *c, int timeout)
31 char *vs, *v, *search, *uri, *origuri, *extra;
39 * serve requests until a magic request.
40 * later requests have to come quickly.
41 * only works for http/1.1 or later.
45 if(hgethead(c, 0) < 0)
49 c->reqtime = time(nil);
50 c->req.meth = getword(c);
51 if(c->req.meth == nil){
56 if(uri == nil || strlen(uri) == 0){
62 if(strcmp(c->req.meth, "GET") != 0){
63 hfail(c, HUnimp, c->req.meth);
70 if(strncmp(vs, "HTTP/", 5) != 0){
71 hfail(c, HUnkVers, vs);
75 c->req.vermaj = strtoul(vs, &vs, 10);
76 if(*vs != '.' || c->req.vermaj != 1){
77 hfail(c, HUnkVers, vs);
81 c->req.vermin = strtoul(vs, &vs, 10);
83 hfail(c, HUnkVers, vs);
95 * the fragment is not supposed to be sent
96 * strip it 'cause some clients send it
99 uri = strchr(origuri, '#');
104 * http/1.1 requires the server to accept absolute
105 * or relative uri's. convert to relative with an absolute path
108 ss = parseuri(c, origuri);
110 c->req.urihost = ss.s2;
112 hfail(c, HBadReq, uri);
119 * munge uri for search, protection, and magic
121 ss = stripsearch(origuri);
124 uri = hurlunesc(c, origuri);
125 uri = abspath(c, uri, "/");
126 if(uri == nil || uri[0] == '\0'){
127 hfail(c, HNotFound, "no object specified");
132 c->req.search = search;
134 c->req.searchpairs = hparsequery(c, hstrdup(c, search));
140 parseuri(HConnect *c, char *uri)
147 if(cistrncmp(uri, "http://", 7) != 0){
152 uri += 5; /* skip http: */
156 * anything starting with // is a host name or number
157 * hostnames consists of letters, digits, - and .
158 * for now, just ignore any port given
160 if(uri[0] == '/' && uri[1] == '/'){
162 p = strchr(urihost, '/');
164 uri = hstrdup(c, "/");
169 p = strchr(urihost, ':');
174 if(uri[0] != '/' || uri[1] == '/'){
181 ss.s2 = hlower(urihost);
185 stripsearch(char *uri)
190 search = strchr(uri, '?');
199 * to circumscribe the accessible files we have to eliminate ..'s
200 * and resolve all names from the root.
203 abspath(HConnect *cc, char *origpath, char *curdir)
205 char *p, *sp, *path, *work, *rpath;
212 work = hstrdup(cc, origpath);
216 * remove any really special characters
218 for(sp = "`;| "; *sp; sp++){
219 p = strchr(path, *sp);
224 len = strlen(curdir) + strlen(path) + 2 + UTFmax;
227 rpath = halloc(cc, len);
231 strcpy(rpath, curdir);
235 p = strchr(path, '/');
238 if(strcmp(path, "..") == 0){
246 }else if(strcmp(path, ".") == 0){
249 n += snprint(rpath+n, len-n, "%s", path);
251 n += snprint(rpath+n, len-n, "/%s", path);
255 if(strncmp(rpath, "/bin/", 5) == 0)
266 while((ch = getc(c)) == ' ' || ch == '\t' || ch == '\r')
279 return hstrdup(c, buf);
283 buf = bingrow(&c->bin, buf, n, n + 1, 0);
295 if(c->hpos < c->hstop)