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
47 char *content_encoding
;
53 char *if_modified_since
;
60 char *www_authenticate
;
71 mime_t mimes
[NB_MIMES
] = {
72 {"js", "application/javascript", "iso-8859-1"},
73 {"css", "text/css", "iso-8859-1"},
74 {"htm", "text/html", "iso-8859-1"},
75 {"html", "text/html", "iso-8859-1"},
76 {"png", "image/png", NULL
},
77 {"jpeg", "image/jpeg", NULL
},
78 {"jpg", "image/jpeg", NULL
},
79 {"txt", "text/plain", "iso-8859-1"}
84 mime_t
*find_mime_type (char *filename
)
87 char *ext
= filename
+ strlen (filename
);
88 while (--ext
> filename
) {
93 if (ext
== filename
) {
99 for (i
= 0; i
< NB_MIMES
; i
++) {
100 if (strcmp (ext
, (mimes
+ i
)->ext
) == 0) {
108 /* print header values */
110 void print_header_values (header_t
*header
)
112 printf ("Header values\n");
113 if (header
->allow
) printf ("allow = '%s'\n", header
->allow
);
114 if (header
->authorization
) printf ("authorization = '%s'\n", header
->authorization
);
115 if (header
->content_encoding
) printf ("content_encoding = '%s'\n", header
->content_encoding
);
116 if (header
->content_length
) printf ("content_length = '%s'\n", header
->content_length
);
117 if (header
->content_type
) printf ("content_type = '%s'\n", header
->content_type
);
118 if (header
->date
) printf ("date = '%s'\n", header
->date
);
119 if (header
->expires
) printf ("expires = '%s'\n", header
->expires
);
120 if (header
->from
) printf ("from = '%s'\n", header
->from
);
121 if (header
->if_modified_since
) printf ("if_modified_since = '%s'\n", header
->if_modified_since
);
122 if (header
->last_modified
) printf ("last_modified = '%s'\n", header
->last_modified
);
123 if (header
->location
) printf ("location = '%s'\n", header
->location
);
124 if (header
->pragma
) printf ("pragma = '%s'\n", header
->pragma
);
125 if (header
->referer
) printf ("referer = '%s'\n", header
->referer
);
126 if (header
->server
) printf ("server = '%s'\n", header
->server
);
127 if (header
->user_agent
) printf ("user_agent = '%s'\n", header
->user_agent
);
128 if (header
->www_authenticate
) printf ("www_authenticate = '%s'\n", header
->www_authenticate
);
133 char *find_sequence (char *data
, int len
, char *seq
, char **pdata
)
136 int size
= strlen (seq
);
139 for (i
= 0; i
< len
- size
+ 1; i
++) {
140 if (strncmp (data
+ i
, seq
, size
) == 0) {
143 *pdata
= data
+ i
+ size
;
152 /* response entity */
154 int add_line (char **buffer
, char *str
)
156 VERBOSE (DEBUG
, PRINT ("add line: %s\n", str
));
157 int len
= ((*buffer
) ? strlen (*buffer
) : 0) + strlen (str
) + 3;
158 VERBOSE (DEBUG
, PRINT ("len: %d\n", len
));
160 *buffer
= (char *)realloc (*buffer
, len
);
162 *buffer
= (char *)calloc (len
, 1);
164 strcat (*buffer
, str
);
165 strcat (*buffer
, "\r\n");
169 int add_status_line (char **buffer
, code_t code
)
171 char tmp
[BUFFER_SIZE
] = {0};
174 sprintf (tmp
, "%s %s", HTTP_VERSION
, codes
[code
]);
175 add_line (buffer
, tmp
);
177 return strlen (*buffer
);
180 int add_general_header (char **buffer
)
182 char tmp
[BUFFER_SIZE
] = {0};
185 time_t ts
= time (NULL
);
186 sprintf (tmp
, "Date: %s", ctime (&ts
));
187 tmp
[strlen (tmp
) - 1] = 0; // remove last \n
188 add_line (buffer
, tmp
);
192 return strlen (*buffer
);
195 int add_response_header (char **buffer
, char *uri
)
197 char tmp
[BUFFER_SIZE
] = {0};
200 sprintf (tmp
, "Location: %s", uri
);
201 add_line (buffer
, tmp
);
204 sprintf (tmp
, "Server: %s", SERVER_NAME
);
205 add_line (buffer
, tmp
);
207 /* WWW-Authentificate */
209 return strlen (*buffer
);
212 int add_entity (char **buffer
, char *entity
, int size
, char *type
, char *encoding
)
214 char tmp
[BUFFER_SIZE
] = {0};
215 int len
= strlen (*buffer
);
221 if (entity
!= NULL
) {
223 /* Content-Encoding */
224 if (encoding
!= NULL
) {
225 sprintf (tmp
, "Content-Encoding: %s", encoding
);
226 add_line (buffer
, tmp
);
230 sprintf (tmp
, "Content-Length: %d", size
);
231 add_line (buffer
, tmp
);
234 sprintf (tmp
, "Content-Type: %s", type
);
235 add_line (buffer
, tmp
);
237 add_line (buffer
, "");
240 len
= strlen (*buffer
);
241 *buffer
= realloc (*buffer
, len
+ size
);
242 memcpy (*buffer
+ len
, entity
, size
);
251 int error_400 (char **buffer
)
253 add_status_line (buffer
, c400
);
254 add_general_header (buffer
);
255 add_response_header (buffer
, NULL
);
257 char *response
= "<html><head><title>Error 400</title></head><body><p>Bad Request</p></body></html>";
258 return add_entity (buffer
, response
, strlen (response
), "text/html", "iso-8859-1");
263 int error_404 (char **buffer
, char *uri
)
265 add_status_line (buffer
, c404
);
266 add_general_header (buffer
);
267 add_response_header (buffer
, uri
);
269 char *response
= "<html><head><title>Error 404</title></head><body><p>File not found</p></body></html>";
270 return add_entity (buffer
, response
, strlen (response
), "text/html", "iso-8859-1");
273 /* generic response */
275 int generic_response (char **buffer
, char *location
, char *response
, int size
)
278 VERBOSE (DEBUG
, PRINT ("add_status_line %d\n", len
));
279 len
= add_status_line (buffer
, c200
);
280 VERBOSE (DEBUG
, PRINT ("add_general_header %d\n", len
));
281 len
= add_general_header (buffer
);
282 VERBOSE (DEBUG
, PRINT ("add_response_header %d\n", len
));
283 len
= add_response_header (buffer
, location
);
284 mime_t
*mime
= find_mime_type (location
);
286 VERBOSE (DEBUG
, PRINT ("add_entity %d\n", len
));
287 return add_entity (buffer
, response
, size
, mime
->type
, mime
->charset
);
292 char *trim (char *str
)
295 while ((*str
== ' ') || (*str
== '\t')) {
302 /* main HTTP processing */
304 int processing (char *data
, int len
, conf_t
*conf
, char **pdata
)
306 char *saved_data
= data
;
307 char location
[BUFFER_SIZE
] = {0};
308 VERBOSE (DEBUG
, PRINT ("Start processing\n"));
311 char *line
= find_sequence (data
, len
+ data
- saved_data
, "\r\n", &data
);
313 VERBOSE (WARNING
, PRINT ("Unknown received data\n"));
319 VERBOSE (DEBUG
, PRINT ("Command line: '%s'\n", line
));
321 char *method
= strtok (line
, " ");
322 char *uri
= strtok (NULL
, " ");
323 char *version
= strtok (NULL
, " ");
324 method_t type
= not_supported_e
;
325 if (strcmp (method
, "GET") == 0) {
327 } else if (strcmp ("HEAD", method
) == 0) {
329 } else if (strcmp ("POST", method
) == 0) {
332 VERBOSE (WARNING
, PRINT ("Unkown method: %s\n", method
));
333 return error_400 (pdata
);
335 VERBOSE (INFO
, PRINT ("%s %s (%s)\n", (type
== get_e
) ? "Get" : (type
== head_e
) ? "Head" : "Post", uri
, version
));
338 char *filename
= strtok (uri
, "&");
339 char *variables
= strtok (NULL
, "\n");
340 char *path
= (char *) calloc (strlen (conf
->root
) + strlen (filename
) + 2, 1);
341 //sprintf (filename, "%s%s%s", conf->root, ((conf->root[strlen (conf->root) - 1] != '/') && (uri[0] != '/')) ? "/" : "", uri);
342 sprintf (path
, "%s/%s", conf
->root
, filename
);
345 header_t header
= {0};
346 while (strcmp (line
= find_sequence (data
, len
+ data
- saved_data
, "\r\n", &data
), "") != 0) {
347 VERBOSE (DEBUG
, PRINT ("Header line: '%s'\n", line
));
348 char *field
= strtok (line
, ":");
349 char *value
= trim (strtok (NULL
, "\r"));
350 VERBOSE (DEBUG
, PRINT ("Field: %s\nValue: %s\n", field
, value
));
355 VERBOSE (DEBUG
, PRINT ("Analyse field\n"));
356 if (strcmp (field
, "Allow") == 0) { header
.allow
= value
; }
357 else if (strcmp (field
, "Authorization") == 0) { header
.authorization
= value
; }
358 else if (strcmp (field
, "Content-Encoding") == 0) { header
.content_encoding
= value
; }
359 else if (strcmp (field
, "Content-Length") == 0) { header
.content_length
= value
; }
360 else if (strcmp (field
, "Content-Type") == 0) { header
.content_type
= value
; }
361 else if (strcmp (field
, "Date") == 0) { header
.date
= value
; }
362 else if (strcmp (field
, "Expires") == 0) { header
.expires
= value
; }
363 else if (strcmp (field
, "From") == 0) { header
.from
= value
; }
364 else if (strcmp (field
, "If-Modified-Since") == 0) { header
.if_modified_since
= value
; }
365 else if (strcmp (field
, "Last-Modified") == 0) { header
.last_modified
= value
; }
366 else if (strcmp (field
, "Location") == 0) { header
.location
= value
; }
367 else if (strcmp (field
, "Pragma") == 0) { header
.pragma
= value
; }
368 else if (strcmp (field
, "Referer") == 0) { header
.referer
= value
; }
369 else if (strcmp (field
, "Server") == 0) { header
.server
= value
; }
370 else if (strcmp (field
, "User-Agent") == 0) { header
.user_agent
= value
; }
371 else if (strcmp (field
, "WWW-Authenticate") == 0) { header
.www_authenticate
= value
; }
372 else { VERBOSE (WARNING
, PRINT ("Unknown header field: '%s'\n", field
)); }
374 VERBOSE (DEBUG
, print_header_values (&header
));
378 len
-= saved_data
- data
;
379 if (len
!= (header
.content_length
? atoi (header
.content_length
) : 0)) {
380 VERBOSE (WARNING
, PRINT ("Incoherent size (%d <> %s)\n", len
, header
.content_length
));
388 case post_e
: /* fall through */
389 VERBOSE (DEBUG
, PRINT ("Read file %s\n", path
));
390 len
= readfile (&buffer
, path
);
392 sprintf (location
, "http://%s/", conf
->servername
);
393 len
= error_404 (pdata
, location
);
395 sprintf (location
, "http://%s%s", conf
->servername
, path
);
396 len
= generic_response (pdata
, location
, buffer
, len
);
401 VERBOSE (DEBUG
, PRINT ("Test file %s\n", path
));
402 fid
= fopen (path
, "rb");
404 sprintf (location
, "http://%s/", conf
->servername
);
405 len
= error_404 (pdata
, location
);
408 sprintf (location
, "http://%s%s", conf
->servername
, path
);
409 len
= generic_response (pdata
, location
, NULL
, 0);
412 case not_supported_e
:
417 VERBOSE (DEBUG
, PRINT ("Cleaning\n"));
419 VERBOSE (DEBUG
, PRINT ("Cleaning path\n"));
426 /* vim: set ts=4 sw=4 et: */