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
|
#include "libphysfs-serve.h"
#include <physfs.h>
#include <stddef.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <err.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
static void ignore_ret(bool b, ...) {};
#define ignore_ret(x) ignore_ret(true, x);
static void
response_501(struct physfs_serve *serve, const int fd) {
const char buf[] = "HTTP/1.1 501 Not Implemented\r\n" \
"Server: libphysfs-serve\r\n" \
"Connection: close\r\n";
ignore_ret(write(fd, buf, sizeof(buf) - 1));
}
static void
response_404(struct physfs_serve *serve, const int fd) {
const char buf[] = "HTTP/1.1 404 Not Found\r\n" \
"Server: libphysfs-serve\r\n" \
"Connection: close\r\n";
ignore_ret(write(fd, buf, sizeof(buf) - 1));
}
static bool
get(struct physfs_serve *serve, const int fd, const char *path) {
struct PHYSFS_Stat st = {0};
const int exists = PHYSFS_stat(path, &st);
st.filetype = (exists ? st.filetype : -1);
char fullpath[1024];
snprintf(fullpath, sizeof(fullpath), "%s", path);
if (serve->path_rewrite(serve, fullpath, sizeof(fullpath), &st)) {
if (!PHYSFS_stat(fullpath, &st)) {
response_404(serve, fd);
return false;
}
}
PHYSFS_File *f;
if (!(f = PHYSFS_openRead(fullpath))) {
response_404(serve, fd);
return false;
}
char buf[1024];
snprintf(buf, sizeof(buf),
"HTTP/1.1 200 OK\r\n" \
"Server: libphysfs-serve\r\n" \
"Content-Type: %s\r\n" \
"Content-Length: %zu\r\n\r\n", serve->content_type(serve, fullpath), (size_t)st.filesize);
ignore_ret(write(fd, buf, strlen(buf)));
for (int ret = 0; (ret = PHYSFS_readBytes(f, buf, sizeof(buf))) > 0;)
ignore_ret(write(fd, buf, ret));
PHYSFS_close(f);
return true;
}
static char*
strchr_safe(const char *s, int c) {
char *ret = strchr(s, c);
return (ret ? ret : (char*)s + strlen(s));
}
static bool
urldecode(const char *in, char *out) {
static const char tbl[256] = {
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
-1,10,11,12,13,14,15,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,10,11,12,13,14,15,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1
};
for (char c; (c = *in++); *out++ = c) {
if (c != '%') continue;
char v1, v2;
if ((v1 = tbl[(uint8_t)*in++]) < 0 || (v2 = tbl[(uint8_t)*in++]) < 0)
return false;
c = (v1 << 4) | v2;
}
*out = 0;
return true;
}
static bool
handle_request(struct physfs_serve *serve, const int fd) {
char buf[1024];
size_t len;
if (!(len = read(fd, buf, sizeof(buf) - 1)))
return false;
buf[len] = 0;
const char method[] = "GET";
if (strncmp(method, buf, sizeof(method) - 1)) {
response_501(serve, fd);
return false;
} else {
const char *path = buf + sizeof(method);
*strchr_safe(path, ' ') = 0;
char decoded[1024];
assert(strlen(path) < sizeof(decoded));
urldecode(path, decoded);
*strchr_safe(decoded, '?') = 0;
get(serve, fd, decoded);
}
return true;
}
union sockaddr_u {
struct sockaddr out;
struct sockaddr_in in;
};
bool
physfs_serve_init(struct physfs_serve *serve, const unsigned int port, const char *addr) {
int listen_fd, conn_fd;
if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
warn("socket");
goto fail;
}
union sockaddr_u server = {0};
inet_pton(AF_INET, addr, &server.in);
server.in.sin_port = htons(port);
setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, (int[]){1}, sizeof(int));
if (bind(listen_fd, &server.out, sizeof(server.out)) == -1) {
warn("bind");
goto fail2;
}
if (listen(listen_fd, 0) == -1) {
warn("listen");
goto fail2;
}
if (!serve->content_type) serve->content_type = physfs_default_content_type;
if (!serve->path_rewrite) serve->path_rewrite = physfs_default_path_rewrite;
memset(serve->fd, -1, sizeof(serve->fd));
serve->fd[serve->nfds++] = listen_fd;
return true;
fail2:
close(listen_fd);
fail:
PHYSFS_deinit();
return false;
}
bool
physfs_serve_event(struct physfs_serve *serve, const int index) {
if (index == 0) {
if (serve->nfds >= ARRAY_SIZE(serve->fd)) {
warnx("max connections reached");
return true;
}
union sockaddr_u client = {0};
if ((serve->fd[serve->nfds++] = accept(serve->fd[index], &client.out, (socklen_t[]){sizeof(client.out)})) == -1) {
warn("accept");
return false;
}
} else {
assert(serve->nfds > 1);
if (!handle_request(serve, serve->fd[index])) {
close(serve->fd[index]);
serve->fd[index] = serve->fd[--serve->nfds];
serve->fd[serve->nfds] = -1;
}
}
return true;
}
void
physfs_serve_free(struct physfs_serve *serve) {
if (!serve) return;
for (int i = 0; i < ARRAY_SIZE(serve->fd); ++i)
if (serve->fd[i] != -1) close(serve->fd[i]);
*serve = (struct physfs_serve){0};
memset(serve->fd, -1, sizeof(serve->fd));
}
const char*
physfs_default_content_type(struct physfs_serve *serve, const char *path) {
if (physfs_ends_with(path, ".html")) return "text/html";
else if (physfs_ends_with(path, ".js")) return "application/javascript";
else if (physfs_ends_with(path, ".json")) return "application/json";
else if (physfs_ends_with(path, ".css")) return "text/css";
else if (physfs_ends_with(path, ".ttf")) return "font/ttf";
else if (physfs_ends_with(path, ".ico")) return "image/x-icon";
else if (physfs_ends_with(path, ".png")) return "image/png";
else if (physfs_ends_with(path, ".jpg")) return "image/jpg";
return "";
}
bool
physfs_default_path_rewrite(struct physfs_serve *serve, char buf[], const size_t bufsz, struct PHYSFS_Stat *st) {
if (st->filetype == PHYSFS_FILETYPE_DIRECTORY) {
physfs_append(buf, bufsz, "/index.html");
return true;
}
return false;
}
bool
physfs_ends_with(const char *str, const char *suf) {
const size_t strn = strlen(str), sufn = strlen(suf);
if (strn < sufn) return false;
return !strcmp(str + strn - sufn, suf);
}
void
physfs_append(char buf[], const size_t bufsz, const char *str) {
const size_t len = strlen(buf), left = bufsz - len, want = strlen(str);
const size_t need = (left > want ? want : left);
memcpy(buf + len, str, need);
buf[len + need] = 0;
}
|