Blob


1 #include <u.h>
2 #include <libc.h>
3 #include <bin.h>
4 #include <httpd.h>
6 typedef struct Strings Strings;
8 struct Strings
9 {
10 char *s1;
11 char *s2;
12 };
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*);
20 /*
21 * parse the next request line
22 * returns:
23 * 1 ok
24 * 0 eof
25 * -1 error
26 */
27 int
28 hparsereq(HConnect *c, int timeout)
29 {
30 Strings ss;
31 char *vs, *v, *search, *uri, *origuri, *extra;
33 if(c->bin != nil){
34 hfail(c, HInternal);
35 return -1;
36 }
38 /*
39 * serve requests until a magic request.
40 * later requests have to come quickly.
41 * only works for http/1.1 or later.
42 */
43 alarm(timeout);
44 if(hgethead(c, 0) < 0)
45 return -1;
46 alarm(0);
47 c->reqtime = time(nil);
48 c->req.meth = getword(c);
49 if(c->req.meth == nil){
50 hfail(c, HSyntax);
51 return -1;
52 }
53 uri = getword(c);
54 if(uri == nil || strlen(uri) == 0){
55 hfail(c, HSyntax);
56 return -1;
57 }
58 v = getword(c);
59 if(v == nil){
60 if(strcmp(c->req.meth, "GET") != 0){
61 hfail(c, HUnimp, c->req.meth);
62 return -1;
63 }
64 c->req.vermaj = 0;
65 c->req.vermin = 9;
66 }else{
67 vs = v;
68 if(strncmp(vs, "HTTP/", 5) != 0){
69 hfail(c, HUnkVers, vs);
70 return -1;
71 }
72 vs += 5;
73 c->req.vermaj = strtoul(vs, &vs, 10);
74 if(*vs != '.' || c->req.vermaj != 1){
75 hfail(c, HUnkVers, vs);
76 return -1;
77 }
78 vs++;
79 c->req.vermin = strtoul(vs, &vs, 10);
80 if(*vs != '\0'){
81 hfail(c, HUnkVers, vs);
82 return -1;
83 }
85 extra = getword(c);
86 if(extra != nil){
87 hfail(c, HSyntax);
88 return -1;
89 }
90 }
92 /*
93 * the fragment is not supposed to be sent
94 * strip it 'cause some clients send it
95 */
96 origuri = uri;
97 uri = strchr(origuri, '#');
98 if(uri != nil)
99 *uri = 0;
101 /*
102 * http/1.1 requires the server to accept absolute
103 * or relative uri's. convert to relative with an absolute path
104 */
105 if(http11(c)){
106 ss = parseuri(c, origuri);
107 uri = ss.s1;
108 c->req.urihost = ss.s2;
109 if(uri == nil){
110 hfail(c, HBadReq, uri);
111 return -1;
113 origuri = uri;
116 /*
117 * munge uri for search, protection, and magic
118 */
119 ss = stripsearch(origuri);
120 origuri = ss.s1;
121 search = ss.s2;
122 uri = hurlunesc(c, origuri);
123 uri = abspath(c, uri, "/");
124 if(uri == nil || uri[0] == '\0'){
125 hfail(c, HNotFound, "no object specified");
126 return -1;
129 c->req.uri = uri;
130 c->req.search = search;
132 return 1;
135 static Strings
136 parseuri(HConnect *c, char *uri)
138 Strings ss;
139 char *urihost, *p;
141 urihost = nil;
142 if(uri[0] != '/'){
143 if(cistrncmp(uri, "http://", 7) != 0){
144 ss.s1 = nil;
145 ss.s2 = nil;
146 return ss;
148 uri += 5; /* skip http: */
151 /*
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
155 */
156 if(uri[0] == '/' && uri[1] == '/'){
157 urihost = uri + 2;
158 p = strchr(urihost, '/');
159 if(p == nil)
160 uri = hstrdup(c, "/");
161 else{
162 uri = hstrdup(c, p);
163 *p = '\0';
165 p = strchr(urihost, ':');
166 if(p != nil)
167 *p = '\0';
170 if(uri[0] != '/' || uri[1] == '/'){
171 ss.s1 = nil;
172 ss.s2 = nil;
173 return ss;
176 ss.s1 = uri;
177 ss.s2 = hlower(urihost);
178 return ss;
180 static Strings
181 stripsearch(char *uri)
183 Strings ss;
184 char *search;
186 search = strchr(uri, '?');
187 if(search != nil)
188 *search++ = 0;
189 ss.s1 = uri;
190 ss.s2 = search;
191 return ss;
194 /*
195 * to circumscribe the accessible files we have to eliminate ..'s
196 * and resolve all names from the root.
197 */
198 static char*
199 abspath(HConnect *cc, char *origpath, char *curdir)
201 char *p, *sp, *path, *work, *rpath;
202 int len, n, c;
204 if(curdir == nil)
205 curdir = "/";
206 if(origpath == nil)
207 origpath = "";
208 work = hstrdup(cc, origpath);
209 path = work;
211 /*
212 * remove any really special characters
213 */
214 for(sp = "`;| "; *sp; sp++){
215 p = strchr(path, *sp);
216 if(p)
217 *p = 0;
220 len = strlen(curdir) + strlen(path) + 2 + UTFmax;
221 if(len < 10)
222 len = 10;
223 rpath = halloc(cc, len);
224 if(*path == '/')
225 rpath[0] = 0;
226 else
227 strcpy(rpath, curdir);
228 n = strlen(rpath);
230 while(path){
231 p = strchr(path, '/');
232 if(p)
233 *p++ = 0;
234 if(strcmp(path, "..") == 0){
235 while(n > 1){
236 n--;
237 c = rpath[n];
238 rpath[n] = 0;
239 if(c == '/')
240 break;
242 }else if(strcmp(path, ".") == 0){
244 }else if(n == 1)
245 n += snprint(rpath+n, len-n, "%s", path);
246 else
247 n += snprint(rpath+n, len-n, "/%s", path);
248 path = p;
251 if(strncmp(rpath, "/bin/", 5) == 0)
252 strcpy(rpath, "/");
253 return rpath;
256 static char*
257 getword(HConnect *c)
259 char *buf;
260 int ch, n;
262 while((ch = getc(c)) == ' ' || ch == '\t' || ch == '\r')
264 if(ch == '\n')
265 return nil;
266 n = 0;
267 buf = halloc(c, 1);
268 for(;;){
269 switch(ch){
270 case ' ':
271 case '\t':
272 case '\r':
273 case '\n':
274 buf[n] = '\0';
275 return hstrdup(c, buf);
278 if(n < HMaxWord-1){
279 buf = bingrow(&c->bin, buf, n, n + 1, 0);
280 if(buf == nil)
281 return nil;
282 buf[n++] = ch;
284 ch = getc(c);
286 return nil;
289 static int
290 getc(HConnect *c)
292 if(c->hpos < c->hstop)
293 return *c->hpos++;
294 return '\n';