-
Notifications
You must be signed in to change notification settings - Fork 0
/
echttp_static.c
186 lines (163 loc) · 5.96 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
/* 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.
*
* int echttp_static_route (const char *uri, const char *path);
*
* Declare a mapping between an URI and a local file or folder.
*
* 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"},
{0, 0}
};
static const char *echttp_static_file (int page, const char *filename) {
char index[1024];
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);
}
}
echttp_transfer (page, fileinfo.st_size);
return "";
unsupported:
if (echttp_isdebug()) printf ("File type violation: %s\n", filename);
echttp_error (406, "File Not Acceptable");
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_initialize (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_initialize();
echttp_catalog_set (&echttp_static_type, extension, content);
}
int echttp_static_route (const char *uri, const char *path) {
echttp_static_initialize();
echttp_catalog_set (&echttp_static_roots, uri, path);
return echttp_route_match (uri, echttp_static_page);
}