-
Notifications
You must be signed in to change notification settings - Fork 1
/
webroot.c
249 lines (205 loc) · 5.04 KB
/
webroot.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
#include <ashttpd.h>
#include <webroot-format.h>
#include <os.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#if 0
#define dprintf printf
#else
#define dprintf(x...) do {} while(0)
#endif
#include "webroot-common.h"
static gidx_oid_t trie_query(struct _webroot *r,
const struct trie_dedge *re,
unsigned int num_edges,
struct ro_vec *str)
{
unsigned int i;
while(num_edges) {
uint32_t edges_idx;
struct ro_vec match;
int cmp;
i = (num_edges / 2);
edges_idx = re[i].re_edges_idx;
match = *str;
match.v_len = re[i].re_strlen;
cmp = memcmp(match.v_ptr, re[i].re_str, match.v_len);
dprintf("'%.*s' vs '%.*s' (idx[%lu] / %u) = %d\n",
(int)match.v_len,
match.v_ptr,
(int)re[i].re_strlen,
re[i].re_str,
&re[i] - r->r_trie,
num_edges,
cmp);
if ( cmp < 0 ) {
num_edges = i;
}else if ( cmp > 0 ) {
re = re + (i + 1);
num_edges = num_edges - (i + 1);
}else{
struct ro_vec suff;
suff = *str;
suff.v_ptr += match.v_len;
suff.v_len -= match.v_len;
if ( !suff.v_len ) {
dprintf("found %d\n", re[i].re_oid);
return re[i].re_oid;
}
dprintf("RECURSE %d => %d\n", edges_idx,
re[i].re_num_edges);
assert(edges_idx < r->r_num_edges);
return trie_query(r, r->r_trie + edges_idx,
re[i].re_num_edges, &suff);
}
}
return GIDX_INVALID_OID;
}
static int map_webroot(struct _webroot *r, uint64_t sz)
{
struct stat st;
if ( fstat(r->r_fd, &st) )
return 0;
if ( (uint64_t)st.st_size < sz ) {
fprintf(stderr, "webroot: unable to map index\n");
return 0;
}
r->r_map = mmap(NULL, sz, PROT_READ, MAP_SHARED, r->r_fd, 0);
if ( r->r_map == MAP_FAILED )
return 0;
r->r_map_sz = sz;
return 1;
}
webroot_t webroot_open(const char *fn)
{
struct _webroot *r;
struct webroot_hdr hdr;
const uint8_t *ptr;
size_t sz;
int eof;
r = calloc(1, sizeof(*r));
if ( NULL == r )
goto out;
r->r_fd = open(fn, O_RDONLY);
if ( r->r_fd < 0 ) {
fprintf(stderr, "webroot: %s: %s\n", fn, os_err());
goto out_free;
}
sz = sizeof(hdr);
if ( !fd_read(r->r_fd, &hdr, &sz, &eof) || eof || sz != sizeof(hdr) ) {
fprintf(stderr, "webroot: %s: failed to read header\n", fn);
goto out_close;
}
if ( hdr.h_magic != WEBROOT_MAGIC ) {
fprintf(stderr, "webroot: %s: bad magic\n", fn);
goto out_close;
}
if ( hdr.h_vers != WEBROOT_CURRENT_VER ) {
fprintf(stderr, "webroot: %s: unexpected version\n", fn);
goto out_close;
}
if ( !map_webroot(r, hdr.h_files_begin) )
goto out_close;
r->r_num_edges = hdr.h_num_edges;
r->r_num_redirect = hdr.h_num_redirect;
r->r_num_oid = hdr.h_num_redirect + hdr.h_num_file;
ptr = r->r_map + sizeof(hdr);
r->r_trie = (struct trie_dedge *)ptr;
ptr += hdr.h_num_edges * sizeof(*r->r_trie);
r->r_redir = (struct webroot_redirect *)ptr;
ptr += hdr.h_num_redirect * sizeof(*r->r_redir);
r->r_file = (struct webroot_file *)ptr;
ptr += hdr.h_num_file * sizeof(*r->r_file);
r->r_strtab = ptr;
ptr += hdr.h_strtab_sz;
if ( ptr > (uint8_t *)r->r_map + r->r_map_sz ) {
fprintf(stderr, "webroot: %s: index truncated\n", fn);
goto out_unmap;
}
r->r_ref = 1;
goto out; /* success */
out_unmap:
munmap((void *)r->r_map, r->r_map_sz);
out_close:
close(r->r_fd);
out_free:
free(r);
r = NULL;
out:
return r;
}
int webroot_get_fd(webroot_t r)
{
return r->r_fd;
}
int webroot_find(webroot_t r, const struct ro_vec *uri,
struct webroot_name *out)
{
struct ro_vec match = *uri;
gidx_oid_t idx;
dprintf("matching %.*s\n", (int)match.v_len, match.v_ptr);
idx = trie_query(r, r->r_trie, 1, &match);
if ( idx == GIDX_INVALID_OID ) {
dprintf("NOPE\n\n");
return 0;
}
assert(idx < r->r_num_oid);
if ( idx < r->r_num_redirect ) {
const struct webroot_redirect *redir;
redir = r->r_redir + idx;
if ( redir->r_off == WEBROOT_INVALID_REDIRECT ) {
out->code = redir->r_len;
out->u.moved.v_len = redir->r_len;
dprintf("redirect %.*s -> code %u\n",
(int)uri->v_len,
uri->v_ptr,
out->code);
}else{
out->code = HTTP_MOVED_PERMANENTLY;
out->u.moved.v_ptr = r->r_map + redir->r_off;
out->u.moved.v_len = redir->r_len;
dprintf("redirect %.*s -> %.*s\n",
(int)uri->v_len,
uri->v_ptr,
(int)redir->r_len,
(char *)r->r_map + redir->r_off);
}
}else{
const struct webroot_file *file;
dprintf("file %u - %u\n", idx, r->r_num_redirect);
file = r->r_file + (idx - r->r_num_redirect);
out->code = HTTP_FOUND;
out->mime_type.v_ptr = r->r_map + file->f_type;
out->mime_type.v_len = file->f_type_len;
out->u.data.f_ofs = file->f_off;
out->u.data.f_len = file->f_len;
out->u.data.f_mtime = file->f_modified;
/* XXX: The assumption being the size of the etag is
* smaller than or equal to the size of the digest
*/
memcpy(out->u.data.f_etag, file->f_digest, ETAG_SZ);
}
dprintf("\n");
return 1;
}
static void dtor(webroot_t r)
{
if ( r ) {
munmap((void *)r->r_map, r->r_map_sz);
close(r->r_fd);
free(r);
}
}
void webroot_unref(webroot_t r)
{
if ( r && !--r->r_ref )
dtor(r);
}
webroot_t webroot_ref(webroot_t r)
{
r->r_ref++;
return r;
}