11 #define BUFFER_SIZE 128
13 #define HTTP_VERSION "HTTP/1.0"
14 #define SERVER_NAME "Webserver/0.0.1"
21 "301 Moved Permanently",
22 "302 Moved Temporarily",
28 "500 Internal Server Error",
29 "501 Not Implemented",
31 "503 Service Unavailable"};
34 c200
= 0, c201
, c202
, c204
,
35 c301
, c302
, c304
, c400
,
36 c401
, c403
, c404
, c500
,
41 not_supported_e
= 0, get_e
, head_e
, post_e
45 encoding_plain_e
= 0, encoding_gzip_e
, encoding_compress_e
51 encoding_t content_encoding
;
57 char *if_modified_since
;
64 char *www_authenticate
;
75 mime_t mimes
[NB_MIMES
] = {
76 {"js", "application/javascript", "iso-8859-1"},
77 {"css", "text/css", "iso-8859-1"},
78 {"htm", "text/html", "iso-8859-1"},
79 {"html", "text/html", "iso-8859-1"},
80 {"png", "image/png", NULL
},
81 {"jpeg", "image/jpeg", NULL
},
82 {"jpg", "image/jpeg", NULL
},
83 {"txt", "text/plain", "iso-8859-1"}
88 mime_t
*find_mime_type (char *filename
)
91 char *ext
= filename
+ strlen (filename
);
92 while (--ext
> filename
) {
97 if (ext
== filename
) {
103 for (i
= 0; i
< NB_MIMES
; i
++) {
104 if (strcmp (ext
, (mimes
+ i
)->ext
) == 0) {
112 /* print header values */
114 void print_header_values (header_t
*header
)
116 printf ("Header values\n");
117 if (header
->allow
) printf ("allow = '%s'\n", header
->allow
);
118 if (header
->authorization
) printf ("authorization = '%s'\n", header
->authorization
);
119 if (header
->content_encoding
) {
120 printf ("content_encoding = ");
121 switch (header
->content_encoding
) {
122 case encoding_plain_e
: printf ("plain\n"); break;
123 case encoding_gzip_e
: printf ("gzip\n"); break;
124 case encoding_compress_e
: printf ("compress\n"); break;
127 if (header
->content_length
) printf ("content_length = '%s'\n", header
->content_length
);
128 if (header
->content_type
) printf ("content_type = '%s'\n", header
->content_type
);
129 if (header
->date
) printf ("date = '%s'\n", header
->date
);
130 if (header
->expires
) printf ("expires = '%s'\n", header
->expires
);
131 if (header
->from
) printf ("from = '%s'\n", header
->from
);
132 if (header
->if_modified_since
) printf ("if_modified_since = '%s'\n", header
->if_modified_since
);
133 if (header
->last_modified
) printf ("last_modified = '%s'\n", header
->last_modified
);
134 if (header
->location
) printf ("location = '%s'\n", header
->location
);
135 if (header
->pragma
) printf ("pragma = '%s'\n", header
->pragma
);
136 if (header
->referer
) printf ("referer = '%s'\n", header
->referer
);
137 if (header
->server
) printf ("server = '%s'\n", header
->server
);
138 if (header
->user_agent
) printf ("user_agent = '%s'\n", header
->user_agent
);
139 if (header
->www_authenticate
) printf ("www_authenticate = '%s'\n", header
->www_authenticate
);
144 char *find_sequence (char *data
, int len
, char *seq
, char **pdata
)
147 int size
= strlen (seq
);
150 for (i
= 0; i
< len
- size
+ 1; i
++) {
151 if (strncmp (data
+ i
, seq
, size
) == 0) {
154 *pdata
= data
+ i
+ size
;
163 /* response entity */
165 int add_line (char **buffer
, char *str
)
167 VERBOSE (DEBUG
, PRINT ("add line: %s\n", str
));
168 int len
= ((*buffer
) ? strlen (*buffer
) : 0) + strlen (str
) + 3;
169 VERBOSE (DEBUG
, PRINT ("len: %d\n", len
));
171 *buffer
= (char *)realloc (*buffer
, len
);
173 *buffer
= (char *)calloc (len
, 1);
175 strcat (*buffer
, str
);
176 strcat (*buffer
, "\r\n");
180 int add_status_line (char **buffer
, code_t code
)
182 char tmp
[BUFFER_SIZE
] = {0};
185 sprintf (tmp
, "%s %s", HTTP_VERSION
, codes
[code
]);
186 add_line (buffer
, tmp
);
188 return strlen (*buffer
);
191 int add_general_header (char **buffer
)
193 char tmp
[BUFFER_SIZE
] = {0};
196 time_t ts
= time (NULL
);
197 sprintf (tmp
, "Date: %s", ctime (&ts
));
198 tmp
[strlen (tmp
) - 1] = 0; // remove last \n
199 add_line (buffer
, tmp
);
203 return strlen (*buffer
);
206 int add_response_header (char **buffer
, char *uri
)
208 char tmp
[BUFFER_SIZE
] = {0};
211 sprintf (tmp
, "Location: %s", uri
);
212 add_line (buffer
, tmp
);
215 sprintf (tmp
, "Server: %s", SERVER_NAME
);
216 add_line (buffer
, tmp
);
218 /* WWW-Authentificate */
220 return strlen (*buffer
);
223 int add_entity (char **buffer
, char *entity
, int size
, char *type
, char *encoding
)
225 char tmp
[BUFFER_SIZE
] = {0};
226 int len
= strlen (*buffer
);
232 if (entity
!= NULL
) {
234 /* Content-Encoding */
235 if (encoding
!= NULL
) {
236 sprintf (tmp
, "Content-Encoding: %s", encoding
);
237 add_line (buffer
, tmp
);
241 sprintf (tmp
, "Content-Length: %d", size
);
242 add_line (buffer
, tmp
);
245 sprintf (tmp
, "Content-Type: %s", type
);
246 add_line (buffer
, tmp
);
248 add_line (buffer
, "");
251 len
= strlen (*buffer
);
252 *buffer
= realloc (*buffer
, len
+ size
);
253 memcpy (*buffer
+ len
, entity
, size
);
262 int error_400 (char **buffer
)
264 add_status_line (buffer
, c400
);
265 add_general_header (buffer
);
266 add_response_header (buffer
, NULL
);
268 char *response
= "<html><head><title>Error 400</title></head><body><p>Bad Request</p></body></html>";
269 return add_entity (buffer
, response
, strlen (response
), "text/html", "iso-8859-1");
274 int error_404 (char **buffer
, char *uri
)
276 add_status_line (buffer
, c404
);
277 add_general_header (buffer
);
278 add_response_header (buffer
, uri
);
280 char *response
= "<html><head><title>Error 404</title></head><body><p>File not found</p></body></html>";
281 return add_entity (buffer
, response
, strlen (response
), "text/html", "iso-8859-1");
284 /* generic response */
286 int generic_response (char **buffer
, char *location
, char *response
, int size
)
289 VERBOSE (DEBUG
, PRINT ("add_status_line %d\n", len
));
290 len
= add_status_line (buffer
, c200
);
291 VERBOSE (DEBUG
, PRINT ("add_general_header %d\n", len
));
292 len
= add_general_header (buffer
);
293 VERBOSE (DEBUG
, PRINT ("add_response_header %d\n", len
));
294 len
= add_response_header (buffer
, location
);
295 mime_t
*mime
= find_mime_type (location
);
297 VERBOSE (DEBUG
, PRINT ("add_entity %d\n", len
));
298 return add_entity (buffer
, response
, size
, mime
->type
, mime
->charset
);
303 char *trim (char *str
)
306 while ((*str
== ' ') || (*str
== '\t')) {
313 /* main HTTP processing */
315 int processing (char *data
, int len
, conf_t
*conf
, char **pdata
)
317 char *saved_data
= data
;
318 char location
[BUFFER_SIZE
] = {0};
319 VERBOSE (DEBUG
, PRINT ("Start processing\n"));
322 char *line
= find_sequence (data
, len
+ data
- saved_data
, "\r\n", &data
);
324 VERBOSE (WARNING
, PRINT ("Unknown received data\n"));
330 VERBOSE (DEBUG
, PRINT ("Command line: '%s'\n", line
));
332 char *method
= strtok (line
, " ");
333 char *uri
= strtok (NULL
, " ");
334 char *version
= strtok (NULL
, " ");
335 method_t type
= not_supported_e
;
336 if (strcmp (method
, "GET") == 0) {
338 } else if (strcmp ("HEAD", method
) == 0) {
340 } else if (strcmp ("POST", method
) == 0) {
343 VERBOSE (WARNING
, PRINT ("Unkown method: %s\n", method
));
344 return error_400 (pdata
);
346 VERBOSE (INFO
, PRINT ("%s %s (%s)\n", (type
== get_e
) ? "Get" : (type
== head_e
) ? "Head" : "Post", uri
, version
));
349 char *filename
= strtok (uri
, "&");
350 char *variables
= strtok (NULL
, "\n");
351 char *path
= (char *) calloc (strlen (conf
->root
) + strlen (filename
) + 2, 1);
352 //sprintf (filename, "%s%s%s", conf->root, ((conf->root[strlen (conf->root) - 1] != '/') && (uri[0] != '/')) ? "/" : "", uri);
353 sprintf (path
, "%s/%s", conf
->root
, filename
);
356 header_t header
= {0};
357 while (strcmp (line
= find_sequence (data
, len
+ data
- saved_data
, "\r\n", &data
), "") != 0) {
358 VERBOSE (DEBUG
, PRINT ("Header line: '%s'\n", line
));
359 char *field
= strtok (line
, ":");
360 char *value
= trim (strtok (NULL
, "\r"));
361 VERBOSE (DEBUG
, PRINT ("Field: %s\nValue: %s\n", field
, value
));
366 VERBOSE (DEBUG
, PRINT ("Analyse field\n"));
367 if (strcmp (field
, "Allow") == 0) { header
.allow
= value
; }
368 else if (strcmp (field
, "Authorization") == 0) { header
.authorization
= value
; }
369 else if (strcmp (field
, "Content-Encoding") == 0) {
370 if (strcmp (value
, "x-gzip") == 0) {
371 header
.content_encoding
= encoding_gzip_e
;
372 } else if (strcmp (value
, "x-compress") == 0) {
373 header
.content_encoding
= encoding_compress_e
;
375 VERBOSE (WARNING
, PRINT ("Unknown content encoding: %s\n", value
));
377 } else if (strcmp (field
, "Content-Length") == 0) { header
.content_length
= value
; }
378 else if (strcmp (field
, "Content-Type") == 0) { header
.content_type
= value
; }
379 else if (strcmp (field
, "Date") == 0) { header
.date
= value
; }
380 else if (strcmp (field
, "Expires") == 0) { header
.expires
= value
; }
381 else if (strcmp (field
, "From") == 0) { header
.from
= value
; }
382 else if (strcmp (field
, "If-Modified-Since") == 0) { header
.if_modified_since
= value
; }
383 else if (strcmp (field
, "Last-Modified") == 0) { header
.last_modified
= value
; }
384 else if (strcmp (field
, "Location") == 0) { header
.location
= value
; }
385 else if (strcmp (field
, "Pragma") == 0) { header
.pragma
= value
; }
386 else if (strcmp (field
, "Referer") == 0) { header
.referer
= value
; }
387 else if (strcmp (field
, "Server") == 0) { header
.server
= value
; }
388 else if (strcmp (field
, "User-Agent") == 0) { header
.user_agent
= value
; }
389 else if (strcmp (field
, "WWW-Authenticate") == 0) { header
.www_authenticate
= value
; }
390 else { VERBOSE (WARNING
, PRINT ("Unknown header field: '%s'\n", field
)); }
392 VERBOSE (DEBUG
, print_header_values (&header
));
396 len
-= saved_data
- data
;
397 if (len
!= (header
.content_length
? atoi (header
.content_length
) : 0)) {
398 VERBOSE (WARNING
, PRINT ("Incoherent size (%d <> %s)\n", len
, header
.content_length
));
401 switch (header
.content_encoding
) {
402 case encoding_plain_e
: break;
403 case encoding_gzip_e
: VERBOSE (WARNING
, PRINT ("Gzip encoding not supported yet\n")); break;
404 case encoding_compress_e
: VERBOSE (WARNING
, PRINT ("Compress encoding not supported yet\n")); break;
413 case post_e
: /* fall through */
414 VERBOSE (DEBUG
, PRINT ("Read file %s\n", path
));
415 len
= readfile (&buffer
, path
);
417 sprintf (location
, "http://%s/", conf
->servername
);
418 len
= error_404 (pdata
, location
);
420 sprintf (location
, "http://%s%s", conf
->servername
, path
);
421 len
= generic_response (pdata
, location
, buffer
, len
);
426 VERBOSE (DEBUG
, PRINT ("Test file %s\n", path
));
427 fid
= fopen (path
, "rb");
429 sprintf (location
, "http://%s/", conf
->servername
);
430 len
= error_404 (pdata
, location
);
433 sprintf (location
, "http://%s%s", conf
->servername
, path
);
434 len
= generic_response (pdata
, location
, NULL
, 0);
437 case not_supported_e
:
442 VERBOSE (DEBUG
, PRINT ("Cleaning\n"));
444 VERBOSE (DEBUG
, PRINT ("Cleaning path\n"));
451 /* vim: set ts=4 sw=4 et: */