-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathechttp_static.c
270 lines (235 loc) · 8.73 KB
/
echttp_static.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* echttp - Embedded HTTP server.
*
* Copyright 2019, Pascal Martin
*
* This program 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 2
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* ------------------------------------------------------------------------
*
* A minimal HTTP server library designed for simplicity and embedding in
* existing applications.
*
* void echttp_static_default (const char *arg);
*
* Declare a default option, which will be ignored if an alternate value
* is provided in the command line (see below).
*
* void echttp_static_initialize (int argc, const char *argv[]);
*
* Initialize this module according to command line options.
*
* Compatibility with previous versions: this initialization is optional.
*
* int echttp_static_route (const char *uri, const char *path);
*
* Declare a mapping between an URI and a local file or folder.
* The same URI may be mapped multiple times: only the latest path
* declared will be used. This allows applications to move the root
* directory while running.
*
* void echttp_static_content_map (const char *extension, const char *content);
*
* Declare an additional file content type. The most common file types are
* already declared, so this function should only be used in rare cases.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "echttp.h"
#include "echttp_static.h"
#include "echttp_catalog.h"
static echttp_catalog echttp_static_roots;
static echttp_catalog echttp_static_type;
/* Define default content type for the most frequent file extensions.
* Don't define too many: it would load the catalog with lot of unused items.
*/
static struct {
const char *extension;
const char *content;
} echttp_static_default_types[] = {
{"html", "text/html"},
{"htm", "text/html"},
{"css", "text/css"},
{"csv", "text/csv"},
{"json", "application/json"},
{"jsn", "application/json"},
{"js", "application/javascript"},
{"xml", "text/xml"},
{"txt", "text/plain"},
{"jpeg", "image/jpeg"},
{"png", "image/png"},
{"gif", "image/gif"},
{"svg", "image/svg+xml"},
{"avi", "video/x-msvideo"},
{"mkv", "video/x-matroska"},
{"mp4", "video/mp4"},
{0, 0}
};
static const char *echttp_static_file (int page, const char *filename) {
char index[1040];
if (page < 0) {
echttp_error (404, "Not found");
return "";
}
struct stat fileinfo;
if (fstat(page, &fileinfo) < 0) goto unsupported;
switch (fileinfo.st_mode & S_IFMT) {
case S_IFDIR: // Use "index.html" as the default page name.
close(page);
snprintf (index, sizeof(index), "%s/index.html", filename);
if (echttp_isdebug()) printf ("Directory, defaulting to %s\n", index);
filename = index;
page = open (filename, O_RDONLY);
if (page < 0) goto unsupported; // Not 404: the directory exists.
if (fstat(page, &fileinfo) < 0) goto unsupported;
if ((fileinfo.st_mode & S_IFMT) != S_IFREG) goto unsupported;
break;
case S_IFREG: break; // Normal case.
default: goto unsupported;
}
if (fileinfo.st_size < 0) goto unsupported;
if (echttp_isdebug()) printf ("Serving static file: %s\n", filename);
const char *sep = strrchr (filename, '.');
if (sep) {
const char *content = echttp_catalog_get (&echttp_static_type, sep+1);
if (content) {
echttp_content_type_set (content);
}
}
off_t size = fileinfo.st_size;
// Support for partial content requests.
//
const char *rangespec = echttp_attribute_get ("Range");
if (rangespec) {
// This only supports a single range,
// supported format is: 'bytes=' begin '-' [end].
while (*rangespec == ' ') rangespec += 1;
if (strncasecmp (rangespec, "bytes=", 6)) goto fullcontent;
rangespec += 6;
if (strchr (rangespec, ',')) goto fullcontent;
const char *sep = strchr (rangespec, '-');
if (!sep) goto fullcontent;
off_t offset = atol (rangespec);
if (offset < 0) goto fullcontent;
off_t end = atoi (sep+1);
if (end > 0) {
if (end <= offset) goto fullcontent;
size = end + 1 - offset;
} else {
size -= offset; // All the rest of the file.
}
if (offset > 0) {
if (lseek (page, offset, SEEK_SET) != offset) goto unsupported;
}
if (size != fileinfo.st_size) {
char ascii[128];
snprintf (ascii, sizeof(ascii), "bytes %ld-%ld/%ld",
offset, offset+size-1, fileinfo.st_size);
echttp_attribute_set ("Content-Range", ascii);
echttp_error (206, "Partial Content"); // Not really an error.
}
}
fullcontent:
echttp_transfer (page, size);
return "";
unsupported:
if (echttp_isdebug()) printf ("File type violation: %s\n", filename);
echttp_error (406, "File Not Acceptable");
close (page);
return "";
}
static const char *echttp_static_page (const char *action,
const char *uri,
const char *data, int length) {
const char *path;
char rooturi[1024];
char filename[1024];
if (strstr(uri, "../")) {
if (echttp_isdebug()) printf ("Security violation: %s\n", uri);
echttp_error (406, "Path Not Acceptable");
return "";
}
strncpy (rooturi, uri, sizeof(rooturi)); // Make a writable copy.
rooturi[sizeof(rooturi)-1] = 0;
for(;;) {
if (echttp_isdebug()) printf ("Searching static map for %s\n", rooturi);
path = echttp_catalog_get (&echttp_static_roots, rooturi);
if (path) break;
char *sep = strrchr (rooturi+1, '/');
if (sep == 0) break;
*sep = 0;
}
if (path == 0) {
rooturi[0] = 0;
path = echttp_catalog_get (&echttp_static_roots, "/");
if (path == 0) {
echttp_error (404, "Page was lost.."); // Should never happen, but.
return "";
}
}
if (echttp_isdebug()) printf ("found match for %s: %s\n", rooturi, path);
size_t pathlen = strlen(path);
strncpy (filename, path, sizeof(filename));
strncpy (filename+pathlen, uri+strlen(rooturi), sizeof(filename)-pathlen);
filename[sizeof(filename)-1] = 0;
return echttp_static_file (open (filename, O_RDONLY), filename);
}
static void echttp_static_internal_initialization (void) {
static int Initialized = 0;
if (!Initialized) {
// Create some common default content types.
int i;
for (i = 0; echttp_static_default_types[i].extension; ++i) {
echttp_catalog_set (&echttp_static_type,
echttp_static_default_types[i].extension,
echttp_static_default_types[i].content);
}
Initialized = 1;
}
}
void echttp_static_content_map (const char *extension, const char *content) {
echttp_static_internal_initialization ();
echttp_catalog_set (&echttp_static_type, extension, content);
}
int echttp_static_route (const char *uri, const char *path) {
echttp_static_internal_initialization ();
int route = echttp_route_find (uri);
if (route >= 0) {
const char *existing = echttp_catalog_get (&echttp_static_roots, uri);
if (strcmp (existing, path)) {
echttp_catalog_set (&echttp_static_roots, uri, path); // Changed.
}
return route;
}
echttp_catalog_set (&echttp_static_roots, uri, path);
return echttp_route_match (uri, echttp_static_page); // All new URI.
}
static const char *EchttpStaticRoot = 0;
void echttp_static_default (const char *arg) {
if (echttp_option_match ("-http-root=", arg, &EchttpStaticRoot)) return;
}
void echttp_static_initialize (int argc, const char *argv[]) {
int i;
for (i = 1; i < argc; ++i) {
echttp_static_default (argv[i]); // Override the defaults.
}
echttp_static_internal_initialization ();
if (EchttpStaticRoot) echttp_static_route ("/", EchttpStaticRoot);
}