24 #define BASE_HTTP_HEADER\
25 "HTTP/1.1 200 OK\r\nContent-Type: %s\r\nContent-Length: %d\r\n\r\n"
27 #define NOT_FOUND_HEADER\
28 "HTTP/1.1 404 Not Found\r\n\r\n"
30 #define NOT_IMPLEMENTED_HEADER\
31 "HTTP/1.1 501 Not Implemented\r\n\r\n"
33 #define DEFAULT_MIME_TYPE\
37 char *extension = path;
38 for (; *extension !=
'.' && *extension !=
'\0'; extension++);
40 if (*extension ==
'\0')
46 if (strcasecmp(extension,
"html") == 0 ||
47 strcasecmp(extension,
"htm") == 0 ||
48 strcasecmp(extension,
"txt") == 0) {
50 }
else if (strcasecmp(extension,
"png") == 0) {
52 }
else if (strcasecmp(extension,
"jpg") == 0 ||
53 strcasecmp(extension,
"jpeg") == 0) {
55 }
else if (strcasecmp(extension,
"gif") == 0) {
62 int url_to_path(
char *url,
char *dir,
char *path,
int capacity) {
64 for (
char *c = url; *c !=
'\0' && *c !=
'?'; c++, len++);
66 int dir_len = strlen(dir);
67 int dir_slash = (dir[dir_len - 1] ==
'/');
68 int url_slash = (url[0] ==
'/');
69 dir_len -= (dir_slash && url_slash);
70 int both_missing_slash = (!dir_slash && !url_slash);
72 if (dir_len + len + both_missing_slash >= capacity) {
73 log_error(
"Path from url (%s) too large for buffer (%d)", url, capacity);
79 strncpy(dest, dir, dir_len);
80 if (both_missing_slash) {
83 strncpy(&dest[dir_len + both_missing_slash], url, len);
84 dest[dir_len + both_missing_slash + len] =
'\0';
88 int generate_header(
char *dest,
int code,
int capacity,
int body_len,
char *mime_type) {
91 }
else if (code == 404) {
Logging of status messages to the terminal.
char * path_to_mimetype(char *path)
#define log_error(fmt,...)
#define NOT_IMPLEMENTED_HEADER
#define DEFAULT_MIME_TYPE
int generate_header(char *dest, int code, int capacity, int body_len, char *mime_type)
int url_to_path(char *url, char *dir, char *path, int capacity)