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.
47 c->reqtime = time(nil);
48 c->req.meth = getword(c);
49 if(c->req.meth == nil){
54 if(uri == nil || strlen(uri) == 0){
60 if(strcmp(c->req.meth, "GET") != 0){
61 hfail(c, HUnimp, c->req.meth);
68 if(strncmp(vs, "HTTP/", 5) != 0){
69 hfail(c, HUnkVers, vs);
73 c->req.vermaj = strtoul(vs, &vs, 10);
74 if(*vs != '.' || c->req.vermaj != 1){
75 hfail(c, HUnkVers, vs);
79 c->req.vermin = strtoul(vs, &vs, 10);
81 hfail(c, HUnkVers, vs);
93 * the fragment is not supposed to be sent
94 * strip it 'cause some clients send it
97 uri = strchr(origuri, '#');
102 * http/1.1 requires the server to accept absolute
103 * or relative uri's. convert to relative with an absolute path
106 ss = parseuri(c, origuri);
108 c->req.urihost = ss.s2;
110 hfail(c, HBadReq, uri);
117 * munge uri for search, protection, and magic
119 ss = stripsearch(origuri);
122 uri = hurlunesc(c, origuri);
123 uri = abspath(c, uri, "/");
124 if(uri == nil || uri[0] == '\0'){
125 hfail(c, HNotFound, "no object specified");
130 c->req.search = search;
136 parseuri(HConnect *c, char *uri)
143 if(cistrncmp(uri, "http://", 7) != 0){
148 uri += 5; /* skip http: */
152 * anything starting with // is a host name or number
153 * hostnames constists of letters, digits, - and .
154 * for now, just ignore any port given
156 if(uri[0] == '/' && uri[1] == '/'){
158 p = strchr(urihost, '/');
160 uri = hstrdup(c, "/");
165 p = strchr(urihost, ':');
170 if(uri[0] != '/' || uri[1] == '/'){
177 ss.s2 = hlower(urihost);
181 stripsearch(char *uri)
186 search = strchr(uri, '?');
195 * to circumscribe the accessible files we have to eliminate ..'s
196 * and resolve all names from the root.
199 abspath(HConnect *cc, char *origpath, char *curdir)
201 char *p, *sp, *path, *work, *rpath;
208 work = hstrdup(cc, origpath);
212 * remove any really special characters
214 for(sp = "`;| "; *sp; sp++){
215 p = strchr(path, *sp);
220 len = strlen(curdir) + strlen(path) + 2 + UTFmax;
223 rpath = halloc(cc, len);
227 strcpy(rpath, curdir);
231 p = strchr(path, '/');
234 if(strcmp(path, "..") == 0){
242 }else if(strcmp(path, ".") == 0){
245 n += snprint(rpath+n, len-n, "%s", path);
247 n += snprint(rpath+n, len-n, "/%s", path);
251 if(strncmp(rpath, "/bin/", 5) == 0)
262 while((ch = getc(c)) == ' ' || ch == '\t' || ch == '\r')
275 return hstrdup(c, buf);
279 buf = bingrow(&c->bin, buf, n, n + 1, 0);
292 if(c->hpos < c->hstop)