10 #define BUFFER_SIZE 128
12 #define HTTP_VERSION "HTTP/1.0"
13 #define SERVER_NAME "Webserver/0.0.1"
20 "301 Moved Permanently",
21 "302 Moved Temporarily",
27 "500 Internal Server Error",
28 "501 Not Implemented",
30 "503 Service Unavailable"};
33 c200
= 0, c201
, c202
, c204
,
34 c301
, c302
, c304
, c400
,
35 c401
, c403
, c404
, c500
,
40 not_supported_e
= 0, get_e
, head_e
, post_e
46 char *content_encoding
;
52 char *if_modified_since
;
59 char *www_authenticate
;
70 mime_t mimes
[NB_MIMES
] = {
71 {"js", "application/javascript", "iso-8859-1"},
72 {"css", "text/css", "iso-8859-1"},
73 {"htm", "text/html", "iso-8859-1"},
74 {"html", "text/html", "iso-8859-1"},
75 {"png", "image/png", NULL
},
76 {"jpeg", "image/jpeg", NULL
},
77 {"jpg", "image/jpeg", NULL
},
78 {"txt", "text/plain", "iso-8859-1"}
83 mime_t
*find_mime_type (char *filename
)
86 char *ext
= filename
+ strlen (filename
);
87 while (--ext
> filename
) {
92 if (ext
== filename
) {
98 for (i
= 0; i
< NB_MIMES
; i
++) {
99 if (strcmp (ext
, (mimes
+ i
)->ext
) == 0) {
107 /* print header values */
109 void print_header_values (header_t
*header
)
111 printf ("Header values\n");
112 if (header
->allow
) printf ("allow = '%s'\n", header
->allow
);
113 if (header
->authorization
) printf ("authorization = '%s'\n", header
->authorization
);
114 if (header
->content_encoding
) printf ("content_encoding = '%s'\n", header
->content_encoding
);
115 if (header
->content_length
) printf ("content_length = '%s'\n", header
->content_length
);
116 if (header
->content_type
) printf ("content_type = '%s'\n", header
->content_type
);
117 if (header
->date
) printf ("date = '%s'\n", header
->date
);
118 if (header
->expires
) printf ("expires = '%s'\n", header
->expires
);
119 if (header
->from
) printf ("from = '%s'\n", header
->from
);
120 if (header
->if_modified_since
) printf ("if_modified_since = '%s'\n", header
->if_modified_since
);
121 if (header
->last_modified
) printf ("last_modified = '%s'\n", header
->last_modified
);
122 if (header
->location
) printf ("location = '%s'\n", header
->location
);
123 if (header
->pragma
) printf ("pragma = '%s'\n", header
->pragma
);
124 if (header
->referer
) printf ("referer = '%s'\n", header
->referer
);
125 if (header
->server
) printf ("server = '%s'\n", header
->server
);
126 if (header
->user_agent
) printf ("user_agent = '%s'\n", header
->user_agent
);
127 if (header
->www_authenticate
) printf ("www_authenticate = '%s'\n", header
->www_authenticate
);
132 char *find_sequence (char *data
, int len
, char *seq
, char **pdata
)
135 int size
= strlen (seq
);
138 for (i
= 0; i
< len
- size
+ 1; i
++) {
139 if (strncmp (data
+ i
, seq
, size
) == 0) {
142 *pdata
= data
+ i
+ size
;
151 /* response entity */
153 int add_line (char **buffer
, char *str
)
155 VERBOSE (DEBUG
, PRINT ("add line: %s\n", str
));
156 int len
= ((*buffer
) ? strlen (*buffer
) : 0) + strlen (str
) + 3;
157 VERBOSE (DEBUG
, PRINT ("len: %d\n", len
));
159 *buffer
= (char *)realloc (*buffer
, len
);
161 *buffer
= (char *)calloc (len
, 1);
163 strcat (*buffer
, str
);
164 strcat (*buffer
, "\r\n");
168 int add_status_line (char **buffer
, code_t code
)
170 char tmp
[BUFFER_SIZE
] = {0};
173 sprintf (tmp
, "%s %s", HTTP_VERSION
, codes
[code
]);
174 add_line (buffer
, tmp
);
176 return strlen (*buffer
);
179 int add_general_header (char **buffer
)
181 char tmp
[BUFFER_SIZE
] = {0};
184 time_t ts
= time (NULL
);
185 sprintf (tmp
, "Date: %s", ctime (&ts
));
186 tmp
[strlen (tmp
) - 1] = 0; // remove last \n
187 add_line (buffer
, tmp
);
191 return strlen (*buffer
);
194 int add_response_header (char **buffer
, char *uri
)
196 char tmp
[BUFFER_SIZE
] = {0};
199 sprintf (tmp
, "Location: %s", uri
);
200 add_line (buffer
, tmp
);
203 sprintf (tmp
, "Server: %s", SERVER_NAME
);
204 add_line (buffer
, tmp
);
206 /* WWW-Authentificate */
208 return strlen (*buffer
);
211 int add_entity (char **buffer
, char *entity
, int size
, char *type
, char *encoding
)
213 char tmp
[BUFFER_SIZE
] = {0};
214 int len
= strlen (*buffer
);
220 if (entity
!= NULL
) {
222 /* Content-Encoding */
223 if (encoding
!= NULL
) {
224 sprintf (tmp
, "Content-Encoding: %sn", encoding
);
225 add_line (buffer
, tmp
);
229 sprintf (tmp
, "Content-Length: %d", size
);
230 add_line (buffer
, tmp
);
233 sprintf (tmp
, "Content-Type: %s", type
);
234 add_line (buffer
, tmp
);
236 add_line (buffer
, "");
239 len
= strlen (*buffer
);
240 *buffer
= realloc (*buffer
, len
+ size
);
241 memcpy (*buffer
+ len
, entity
, size
);
250 int error_400 (char **buffer
)
252 add_status_line (buffer
, c400
);
253 add_general_header (buffer
);
254 add_response_header (buffer
, NULL
);
256 char *response
= "<html><head><title>Error 400</title></head><body><p>Bad Request</p></body></html>";
257 return add_entity (buffer
, response
, strlen (response
), "text/html", "iso-8859-1");
262 int error_404 (char **buffer
, char *uri
)
264 add_status_line (buffer
, c404
);
265 add_general_header (buffer
);
266 add_response_header (buffer
, uri
);
268 char *response
= "<html><head><title>Error 404</title></head><body><p>File not found</p></body></html>";
269 return add_entity (buffer
, response
, strlen (response
), "text/html", "iso-8859-1");
272 /* generic response */
274 int generic_response (char **buffer
, char *location
, char *response
, int size
)
277 VERBOSE (DEBUG
, PRINT ("add_status_line %d\n", len
));
278 len
= add_status_line (buffer
, c200
);
279 VERBOSE (DEBUG
, PRINT ("add_general_header %d\n", len
));
280 len
= add_general_header (buffer
);
281 VERBOSE (DEBUG
, PRINT ("add_response_header %d\n", len
));
282 len
= add_response_header (buffer
, location
);
283 mime_t
*mime
= find_mime_type (location
);
285 VERBOSE (DEBUG
, PRINT ("add_entity %d\n", len
));
286 return add_entity (buffer
, response
, size
, mime
->type
, mime
->charset
);
291 char *trim (char *str
)
294 while ((*str
== ' ') || (*str
== '\t')) {
301 /* main HTTP processing */
303 int processing (char *data
, int len
, conf_t
*conf
, char **pdata
)
305 char location
[BUFFER_SIZE
] = {0};
306 VERBOSE (DEBUG
, PRINT ("Start processing\n"));
309 char *line
= find_sequence (data
, len
, "\r\n", &data
);
311 VERBOSE (WARNING
, PRINT ("Unknown received data\n"));
317 VERBOSE (DEBUG
, PRINT ("Command line: '%s'\n", line
));
319 char *method
= strtok (line
, " ");
320 char *uri
= strtok (NULL
, " ");
321 char *version
= strtok (NULL
, " ");
322 method_t type
= not_supported_e
;
323 if (strcmp (method
, "GET") == 0) {
325 } else if (strcmp ("HEAD", method
) == 0) {
327 } else if (strcmp ("POST", method
) == 0) {
330 VERBOSE (WARNING
, PRINT ("Unkown method: %s\n", method
));
331 return error_400 (pdata
);
333 VERBOSE (INFO
, PRINT ("%s %s (%s)\n", (type
== get_e
) ? "Get" : (type
== head_e
) ? "Head" : "Post", uri
, version
));
336 char *filename
= (char *)calloc (strlen (conf
->root
) + strlen (uri
) + 2, 1);
337 //sprintf (filename, "%s%s%s", conf->root, ((conf->root[strlen (conf->root) - 1] != '/') && (uri[0] != '/')) ? "/" : "", uri);
338 sprintf (filename
, "%s/%s", conf
->root
, uri
);
341 header_t header
= {0};
342 while (strcmp (line
= find_sequence (data
, len
, "\r\n", &data
), "") != 0) {
343 VERBOSE (DEBUG
, PRINT ("Header line: '%s'\n", line
));
344 char *field
= strtok (line
, ":");
345 char *value
= trim (strtok (NULL
, "\r"));
346 VERBOSE (DEBUG
, PRINT ("Field: %s\nValue: %s\n", field
, value
));
351 VERBOSE (DEBUG
, PRINT ("Analyse field\n"));
352 if (strcmp (field
, "Allow") == 0) {
353 header
.allow
= value
;
354 } else if (strcmp (field
, "Authorization") == 0) {
355 header
.authorization
= value
;
356 } else if (strcmp (field
, "Content-Encoding") == 0) {
357 header
.content_encoding
= value
;
358 } else if (strcmp (field
, "Content-Length") == 0) {
359 header
.content_length
= value
;
360 } else if (strcmp (field
, "Content-Type") == 0) {
361 header
.content_type
= value
;
362 } else if (strcmp (field
, "Date") == 0) {
364 } else if (strcmp (field
, "Expires") == 0) {
365 header
.expires
= value
;
366 } else if (strcmp (field
, "From") == 0) {
368 } else if (strcmp (field
, "If-Modified-Since") == 0) {
369 header
.if_modified_since
= value
;
370 } else if (strcmp (field
, "Last-Modified") == 0) {
371 header
.last_modified
= value
;
372 } else if (strcmp (field
, "Location") == 0) {
373 header
.location
= value
;
374 } else if (strcmp (field
, "Pragma") == 0) {
375 header
.pragma
= value
;
376 } else if (strcmp (field
, "Referer") == 0) {
377 header
.referer
= value
;
378 } else if (strcmp (field
, "Server") == 0) {
379 header
.server
= value
;
380 } else if (strcmp (field
, "User-Agent") == 0) {
381 header
.user_agent
= value
;
382 } else if (strcmp (field
, "WWW-Authenticate") == 0) {
383 header
.www_authenticate
= value
;
385 VERBOSE (WARNING
, PRINT ("Unknown header field: '%s'\n", field
));
388 VERBOSE (DEBUG
, print_header_values (&header
));
394 VERBOSE (DEBUG
, PRINT ("Read file %s\n", filename
));
395 len
= readfile (&buffer
, filename
);
397 sprintf (location
, "http://%s/", conf
->servername
);
398 len
= error_404 (pdata
, location
);
400 sprintf (location
, "http://%s%s", conf
->servername
, filename
);
401 len
= generic_response (pdata
, location
, buffer
, len
);
408 VERBOSE (DEBUG
, PRINT ("Write file %s\n", filename
));
410 case not_supported_e
:
415 VERBOSE (DEBUG
, PRINT ("Cleaning\n"));
417 VERBOSE (DEBUG
, PRINT ("Cleaning filename\n"));
424 /* vim: set ts=4 sw=4 et: */