improve behaviour under VPC, fixes from nicolas tittley.
[minix.git] / commands / httpd / request.c
blob2b5b36a4fa14d555d39b90c842562ceecca3a190
1 /* request.c
3 * This file is part of httpd.
5 * 02/17/1996 Michael Temari <Michael@TemWare.Com>
6 * 07/07/1996 Initial Release Michael Temari <Michael@TemWare.Com>
7 * 12/29/2002 Michael Temari <Michael@TemWare.Com>
9 */
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <signal.h>
13 #include <ctype.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdlib.h>
17 #include <time.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <pwd.h>
21 #ifdef _MINIX
22 #include <minix/minlib.h>
23 #endif
24 #include <errno.h>
26 #include "http.h"
27 #include "utility.h"
28 #include "config.h"
30 _PROTOTYPE(static void Timeout, (int sig));
31 _PROTOTYPE(static int getline, (char *buffer, int size));
32 _PROTOTYPE(static void authorize, (char *p, struct http_request *rq));
33 _PROTOTYPE(static void decurl, (char *u));
35 static int TimeOut;
37 static void Timeout(sig)
38 int sig;
40 TimeOut = 1;
43 static int getline(buffer, size)
44 char *buffer;
45 int size;
47 char *p;
48 int s;
50 p = buffer;
52 while(p < (buffer + size - 1)) {
53 TimeOut = 0;
54 signal(SIGALRM, Timeout);
55 alarm(5*60);
56 s = read(0, p, 1);
57 alarm(0);
58 if(TimeOut)
59 return(-1);
60 if(s != 1)
61 return(-1);
62 if(*p == '\n') break;
63 p++;
65 *++p = '\0';
67 p = &buffer[strlen(buffer) - 1];
68 if(p >= buffer && (*p == '\r' || *p == '\n')) *p-- ='\0';
69 if(p >= buffer && (*p == '\r' || *p == '\n')) *p-- ='\0';
71 return(strlen(buffer));
74 static void authorize(p, rq)
75 char *p;
76 struct http_request *rq;
78 char *s;
80 if(toupper(*p++) == 'B' &&
81 toupper(*p++) == 'A' &&
82 toupper(*p++) == 'S' &&
83 toupper(*p++) == 'I' &&
84 toupper(*p++) == 'C' &&
85 toupper(*p++) == ' ') ;
86 else
87 return;
89 s = decode64(p);
91 if((p = strchr(s, ':')) == (char *)NULL)
92 p = "";
93 else
94 *p++ = '\0';
96 strncpy(rq->authuser, s, sizeof(rq->authuser));
97 strncpy(rq->authpass, p, sizeof(rq->authpass));
99 return;
102 int getrequest(rq)
103 struct http_request *rq;
105 static char line[4096];
106 char *p, *p2, *ps;
107 int s, len;
108 struct vhost *ph;
110 /* get request, it may be simple */
112 s = getline(line, sizeof(line));
113 if(s < 0)
114 return(-1);
116 if(dbglog != (FILE *)NULL) {
117 fprintf(dbglog, "REQUEST: %s\n", line);
118 fflush(dbglog);
121 /* clear http_request */
122 memset(rq, 0, sizeof(*rq));
123 rq->ifmodsince = (time_t) -1;
125 /* assume simple request */
126 rq->type = HTTP_REQUEST_TYPE_SIMPLE;
128 /* parse the method */
129 p = line;
130 while(*p && !LWS(*p)) {
131 *p = toupper(*p);
132 p++;
134 if(*p) *p++ = '\0';
136 if(!strcmp(line, "GET"))
137 rq->method = HTTP_METHOD_GET; else
138 if(!strcmp(line, "HEAD"))
139 rq->method = HTTP_METHOD_HEAD; else
140 if(!strcmp(line, "POST"))
141 rq->method = HTTP_METHOD_POST; else
142 if(!strcmp(line, "PUT"))
143 rq->method = HTTP_METHOD_PUT; else
144 #if 0
145 if(!strcmp(line, "OPTIONS"))
146 rq->method = HTTP_METHOD_OPTIONS; else
147 if(!strcmp(line, "PATCH"))
148 rq->method = HTTP_METHOD_PATCH; else
149 if(!strcmp(line, "COPY"))
150 rq->method = HTTP_METHOD_COPY; else
151 if(!strcmp(line, "MOVE"))
152 rq->method = HTTP_METHOD_MOVE; else
153 if(!strcmp(line, "DELETE"))
154 rq->method = HTTP_METHOD_DELETE; else
155 if(!strcmp(line, "LINK"))
156 rq->method = HTTP_METHOD_LINK; else
157 if(!strcmp(line, "UNLINK"))
158 rq->method = HTTP_METHOD_UNLINK; else
159 if(!strcmp(line, "TRACE"))
160 rq->method = HTTP_METHOD_TRACE; else
161 if(!strcmp(line, "WRAPPED"))
162 rq->method = HTTP_METHOD_WRAPPED; else
163 #endif
164 rq->method = HTTP_METHOD_UNKNOWN;
166 /* parse the requested URI */
167 p2 = rq->uri;
168 len = sizeof(rq->uri) - 1;
169 while(*p && !LWS(*p) && len > 0) {
170 *p2++ = *p++;
171 len--;
173 *p2 = '\0';
175 /* eat up any leftovers if uri was too big */
176 while(*p && !LWS(*p))
177 p++;
179 /* save for continued processing later */
180 ps = p;
182 /* parse the requested URL */
183 p = rq->uri;
184 p2 = rq->url;
185 len = sizeof(rq->url) - 1;
186 while(*p && !LWS(*p) && *p != '?' && len > 0) {
187 *p2++ = *p++;
188 len--;
190 *p2 = '\0';
192 /* See if there is a query string */
193 if(*p == '?') {
194 p++;
195 p2 = rq->query;
196 len = sizeof(rq->query) - 1;
197 while(*p && !LWS(*p) && len > 0) {
198 *p2++ = *p++;
199 len--;
203 /* eat up any leftovers */
204 while(*p && !LWS(*p)) p++;
206 if(rq->url[0] == '\0') {
207 rq->url[0] = '/';
208 rq->url[1] = '\0';
211 /* url is a decoded copy of the uri */
212 decurl(rq->url);
214 /* restore and continue processing */
215 p = ps;
217 /* if this is true it is a simple request */
218 if(*p == '\0')
219 return(0);
221 /* parse HTTP version */
222 while(*p && LWS(*p)) p++;
223 if(toupper(*p++) != 'H') return(0);
224 if(toupper(*p++) != 'T') return(0);
225 if(toupper(*p++) != 'T') return(0);
226 if(toupper(*p++) != 'P') return(0);
227 if( *p++ != '/') return(0);
229 /* version major */
230 rq->vmajor = 0;
231 while((*p >= '0') && (*p <= '9'))
232 rq->vmajor = rq->vmajor * 10 + (*p++ - '0');
233 if(*p != '.')
234 return(0);
235 p++;
237 /* version minor */
238 rq->vminor = 0;
239 while((*p >= '0') && (*p <= '9'))
240 rq->vminor = rq->vminor * 10 + (*p++ - '0');
241 if(*p)
242 return(0);
244 rq->type = HTTP_REQUEST_TYPE_FULL;
246 p = rq->uri;
248 /* check if it is a proxy request */
249 if(toupper(*p++) == 'H' &&
250 toupper(*p++) == 'T' &&
251 toupper(*p++) == 'T' &&
252 toupper(*p++) == 'P' &&
253 toupper(*p++) == ':')
254 rq->type = HTTP_REQUEST_TYPE_PROXY;
256 /* parse any header fields */
257 while((s = getline(line, sizeof(line))) > 0) {
258 if(toupper(line[0]) == 'A' &&
259 toupper(line[1]) == 'U')
260 if(dbglog != (FILE *)NULL) {
261 fprintf(dbglog, "REQUEST: Authorization:\n");
262 fflush(dbglog);
263 } else ;
264 else
265 if(dbglog != (FILE *)NULL) {
266 fprintf(dbglog, "REQUEST: %s\n", line);
267 fflush(dbglog);
269 p = line;
270 while(*p && *p != ':') {
271 *p = toupper(*p);
272 p++;
274 if(*p != ':') continue; /* bad header field, skip it */
275 *p++ = '\0';
276 while(*p && LWS(*p)) p++;
278 /* header field value parsing here */
279 if(!strcmp(line, "HOST")) {
280 strncpy(rq->host, p, sizeof(rq->host));
281 p2 = strrchr(rq->host, ':');
282 if(p2 != (char *)NULL) {
283 *p2++ = '\0';
284 rq->port = atoi(p2);
286 /* if unknown virtual host then exit quietly */
287 for(ph = vhost; ph != NULL; ph = ph->next) {
288 if(!strcasecmp(ph->hname, "*")) break;
289 if(!strcasecmp(ph->hname, rq->host)) break;
291 if(rq->type != HTTP_REQUEST_TYPE_PROXY)
292 if(ph == NULL && vhost != NULL) return(1);
293 } else
294 if(!strcmp(line, "USER-AGENT"))
295 strncpy(rq->useragent, p, sizeof(rq->useragent)); else
296 if(!strcmp(line, "CONNECTION"))
297 rq->keepopen = strcasecmp(p, "Keep-Alive") ? 0 : 1; else
298 if(!strcmp(line, "IF-MODIFIED-SINCE"))
299 rq->ifmodsince = httptime(p); else
300 if(!strcmp(line, "CONTENT-LENGTH"))
301 rq->size = atol(p); else
302 if(!strcmp(line, "AUTHORIZATION")) {
303 strncpy(rq->wwwauth, p, sizeof(rq->wwwauth));
304 if(rq->type != HTTP_REQUEST_TYPE_PROXY)
305 authorize(p, rq);
306 } else
307 if(!strcmp(line, "PROXY-AUTHORIZATION")) {
308 if(rq->type == HTTP_REQUEST_TYPE_PROXY)
309 authorize(p, rq);
310 } else
311 if(!strcmp(line, "DATE"))
312 rq->msgdate = httptime(p); else
313 if(!strcmp(line, "COOKIE")) {
314 strncpy(rq->cookie, p, sizeof(rq->cookie)-1);
315 rq->cookie[sizeof(rq->cookie)-1] = '\0';
319 if(rq->type != HTTP_REQUEST_TYPE_PROXY)
320 if(*rq->host == '\0' && vhost != NULL) return(1);
322 if(dbglog != (FILE *)NULL && rq->authuser[0] != '\0') {
323 fprintf(dbglog, "REQUEST: AuthUser=%s\n", rq->authuser);
324 fflush(dbglog);
327 if(s < 0) {
328 fprintf(stderr, "httpd: getrequest: Error getline (header fields)\n");
329 return(-1);
332 return(0);
335 static void decurl(u)
336 char *u;
338 char *p;
339 char h1, h2;
340 char c;
342 p = u;
343 while(*p) {
344 switch(*p) {
345 case '\0':
346 c = '\0';
347 break;
348 case '+':
349 c = ' ';
350 p++;
351 break;
352 case '%':
353 h1 = '0';
354 h2 = '0';
355 p++;
356 h1 = tolower(*p);
357 if(*p) p++;
358 h2 = tolower(*p);
359 if(*p) p++;
360 c = (h1 > '9') ? (10 + h1 - 'a') : (h1 - '0');
361 c = 16 * c + ((h2 > '9') ? (10 + h2 - 'a') : (h2 - '0'));
362 break;
363 default:
364 c = *p++;
366 *u++ = c;
368 *u = '\0';