-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_header_parser.c
200 lines (167 loc) · 4.64 KB
/
http_header_parser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Copyright (C) Cihangir Akturk 2012
*
* This file is part of chngr's simple stupid web server (ssws).
*
* ssws is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ssws is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ssws. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
#include "http_header_parser.h"
struct request_metod {
const char *name;
int value;
};
static struct request_metod supported_methods[] = {
{ "GET" , GET },
{ "HEAD", HEAD },
{ NULL , 0 }
};
struct mime_type {
const char *ext;
const char *file_type;
};
static struct mime_type supported_mime_types[] = {
{ "html", "text/html" },
{ "htm" , "text/html" },
{ "jpg" , "image/jpg" },
{ "jpeg", "image/jpeg" },
{ "png" , "image/png" },
{ "gif" , "image/gif" },
{ "tgz" , "application/x-gz" },
{ "gz" , "application/x-gz" },
{ "tar" , "application/x-tar" },
{ "zip" , "application/zip" },
{ NULL , NULL }
};
struct server_data {
struct http_header *header;
char *payload;
};
static char *get_next_line(char **next, char *end)
{
char *str = *next;
char *result = str;
if (!str || *(str+1) == '\n')
return NULL;
*next = NULL;
for (; str < end; ++str) {
if (*str == '\n') {
*str = '\0';
if (str + 1 < end && *(str + 1) != '\0')
*next = str + 1;
break;
}
}
return result;
}
int parse_header(struct http_header *hdr,
char *hdr_data,
size_t size)
{
struct request_metod *method;
char *i, *next, *end;
int result = -1;
end = hdr_data + size;
next = hdr_data;
i = get_next_line(&next, end);
if (!i)
goto out;
if((i = strtok(i, " ")) == NULL)
goto out;
for (method = supported_methods; method != NULL; ++method) {
if (strncmp(i, method->name, strlen(method->name)) == 0) {
hdr->request_type = method->value;
break;
}
}
if((i = strtok(NULL, " ")) == NULL)
goto out;
hdr->request_path = i + 1;
/* We got enough header data */
result = 0;
if ((i = get_next_line(&next, end)) == NULL)
goto out;
if((i = strtok(i, " ")) == NULL)
goto out;
if (strncmp(i, "Host:", strlen("Host:")) == 0) {
i = strtok(NULL, " ");
hdr->host = i;
}
while ((i = get_next_line(&next, end))) {
char *token = strtok(i, " ");
int found = 0;
while (token) {
if (strncmp(token, "User-Agent:",
strlen("User-Agent:")) == 0)
found = 1;
if (found) {
hdr->user_agent = token + strlen(token) + 1;
goto out;
} else {
token = strtok(NULL, " ");
}
}
}
out:
return result;
}
inline char *getfileext(const char *fname)
{
char *ret = strrchr(fname, '.');
if (ret)
ret = ret + 1;
return ret;
}
int filename(char *buf, size_t size, const char *request_path)
{
struct stat sb;
size_t len;
int isdir = 0;
if (strstr(request_path, ".."))
return FORBIDDEN;
if (stat(request_path, &sb) == 0)
isdir = S_ISDIR(sb.st_mode);
len = strlen(request_path);
if (len == 0 && request_path[0] == '\0') {
strncpy(buf, "index.html", size);
} else if (len && isdir) {
if (*(request_path + len - 1) == '/')
snprintf(buf, size, "%s%s",
request_path,
"index.html");
else
snprintf(buf, size, "%s%s",
request_path,
"/index.html");
} else {
strncpy(buf, request_path, size);
}
return OK;
}
const char *mimestr(const char *file_name)
{
struct mime_type *m = supported_mime_types;
char *ext = getfileext(file_name);
size_t len;
if (ext) {
len = strlen(ext);
for (; m->ext != NULL; ++m)
if (strncmp(m->ext, ext, len) == 0)
return m->file_type;
}
return NULL;
}