-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_emb.c
286 lines (236 loc) · 7.21 KB
/
io_emb.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Copyright (c) 2004-2005 Sergey Lyubka <[email protected]>
* All rights reserved
*
* "THE BEER-WARE LICENSE" (Revision 42):
* Sergey Lyubka wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return.
*/
#define MY_DEBUGGING 1
#include "defs.h"
const char* shttpd_version(void)
{
return (VERSION);
}
static void call_user(struct conn *c, struct shttpd_arg *arg, shttpd_callback_t func)
{
arg->priv = c;
arg->state = c->loc.chan.emb.state;
arg->out.buf = io_space(&c->loc.io);
arg->out.len = io_space_len(&c->loc.io);
arg->out.num_bytes = 0;
arg->in.buf = io_data(&c->rem.io);;
arg->in.len = io_data_len(&c->rem.io);
arg->in.num_bytes = 0;
if (io_data_len(&c->rem.io) >= c->rem.io.size)
arg->flags |= SHTTPD_POST_BUFFER_FULL;
if (c->rem.content_len > 0 && c->rem.io.total < c->rem.content_len)
arg->flags |= SHTTPD_MORE_POST_DATA;
// MY_DEBUG("%s:%d.\n", __FILE__, __LINE__);
func(arg);
// MY_DEBUG("%s:%d.\n", __FILE__, __LINE__);
io_inc_head(&c->loc.io, arg->out.num_bytes);
io_inc_tail(&c->rem.io, arg->in.num_bytes);
c->loc.chan.emb.state = arg->state; /* Save state */
/*
* If callback finished output, that means it did all cleanup.
* If the connection is terminated unexpectedly, we canna call
* the callback via the stream close() method from disconnect.
* However, if cleanup is already done, we set close() method to
* NULL, to prevent the call from disconnect().
*/
if (arg->flags & SHTTPD_END_OF_OUTPUT) {
c->loc.flags &= ~FLAG_DONT_CLOSE;
}
else
c->loc.flags |= FLAG_DONT_CLOSE;
if (arg->flags & SHTTPD_SUSPEND)
c->loc.flags |= FLAG_SUSPEND;
}
static int do_embedded(struct stream *stream, void *buf, size_t len)
{
struct shttpd_arg arg;
buf = NULL; len = 0; /* Squash warnings */
arg.user_data = stream->conn->loc.chan.emb.data;
arg.flags = 0;
call_user(stream->conn, &arg, (shttpd_callback_t)
stream->conn->loc.chan.emb.func.v_func);
return (0);
}
static void close_embedded(struct stream *stream)
{
struct shttpd_arg arg;
struct conn *c = stream->conn;
arg.flags = SHTTPD_CONNECTION_ERROR;
arg.user_data = c->loc.chan.emb.data;
/*
* Do not call the user function if SHTTPD_END_OF_OUTPUT was set,
* i.e. the callback already terminated correctly
*/
if (stream->flags & FLAG_DONT_CLOSE)
call_user(stream->conn, &arg, (shttpd_callback_t)
c->loc.chan.emb.func.v_func);
}
size_t shttpd_printf(struct shttpd_arg *arg, const char *fmt, ...)
{
char *buf = arg->out.buf + arg->out.num_bytes;
int buflen = arg->out.len - arg->out.num_bytes, len = 0;
va_list ap;
if (buflen > 0) {
va_start(ap, fmt);
len = vsnprintf(buf, buflen, fmt, ap);
va_end(ap);
if (len < 0 || len > buflen)
len = buflen;
arg->out.num_bytes += len;
// MY_DEBUG("%*s", len, buf);
}
return (len);
}
const char* shttpd_get_header(struct shttpd_arg *arg, const char *header_name)
{
struct conn *c = arg->priv;
char *p, *s, *e;
size_t len;
p = c->headers;
e = c->request + c->rem.headers_len;
len = strlen(header_name);
while (p < e) {
if ((s = strchr(p, '\n')) != NULL)
s[s[-1] == '\r' ? -1 : 0] = '\0';
if (_shttpd_strncasecmp(header_name, p, len) == 0)
return (p + len + 2);
p += strlen(p) + 1;
}
return (NULL);
}
const char* shttpd_getenv(struct shttpd_arg *arg, const char *env_name)
{
struct conn *c = arg->priv;
struct vec *vec;
if (!_shttpd_strncasecmp(env_name, "HTTP_", 5)) {
return shttpd_get_header(arg, env_name+5);
} else if (!strcasecmp(env_name, "REQUEST_METHOD")) {
return (_shttpd_known_http_methods[c->method].ptr);
} else if (!strcasecmp(env_name, "REQUEST_URI")) {
return (c->uri);
} else if (!strcasecmp(env_name, "QUERY_STRING")) {
return (c->query);
} else if (!strcasecmp(env_name, "AUTH_TYPE")) {
return "Digest";
} else if (!strcasecmp(env_name, "REMOTE_USER")) {
vec = &c->ch.user.v_vec;
if (vec->len > 0) {
((char *) vec->ptr)[vec->len] = '\0';
return (vec->ptr);
}
} else if (!strcasecmp(env_name, "REMOTE_ADDR")) {
return (inet_ntoa(c->sa.u.sin.sin_addr));/* FIXME NOT MT safe */
} else if (!strcasecmp(env_name, "DOCUMENT_ROOT")) {
return c->ctx->options[OPT_ROOT];
} else if (!strcasecmp(env_name, "PATH_INFO")) {
return (c->path_info);
}
return shttpd_get_header(arg, env_name);
}
void shttpd_get_http_version(struct shttpd_arg *arg,
unsigned long *major, unsigned long *minor)
{
struct conn *c = arg->priv;
*major = c->major_version;
*minor = c->minor_version;
}
void shttpd_register_uri(struct shttpd_ctx *ctx,
const char *uri, shttpd_callback_t callback, void *data)
{
struct registered_uri *e;
if ((e = malloc(sizeof(*e))) != NULL) {
e->uri = _shttpd_strdup(uri);
e->callback.v_func = (void (*)(void)) callback;
e->callback_data = data;
LL_TAIL(&ctx->registered_uris, &e->link);
}
}
int shttpd_get_var(const char *var, const char *buf, int buf_len,
char *value, int value_len)
{
const char *p, *e, *s;
size_t var_len;
var_len = strlen(var);
e = buf + buf_len; /* End of QUERY_STRING buffer */
/* buf is "var1=val1&var2=val2...". Find variable first */
for (p = buf; p + var_len < e; p++)
if ((p == buf || p[-1] == '&') &&
p[var_len] == '=' &&
!_shttpd_strncasecmp(var, p, var_len)) {
/* Point 'p' to var value, 's' to the end of value */
p += var_len + 1;
if ((s = memchr(p, '&', e - p)) == NULL)
s = e;
/* URL-decode value. Return result length */
return (_shttpd_url_decode(p, s - p, value, value_len));
}
return (-1);
}
static int match_regexp(const char *regexp, const char *text)
{
if (*regexp == '\0')
return (*text == '\0');
if (*regexp == '*')
do {
if (match_regexp(regexp + 1, text))
return (1);
} while (*text++ != '\0');
if (*text != '\0' && *regexp == *text)
return (match_regexp(regexp + 1, text + 1));
return (0);
}
struct registered_uri* _shttpd_is_registered_uri(struct shttpd_ctx *ctx, const char *uri)
{
struct llhead *lp;
struct registered_uri *reg_uri;
LL_FOREACH(&ctx->registered_uris, lp) {
reg_uri = LL_ENTRY(lp, struct registered_uri, link);
if (match_regexp(reg_uri->uri, uri))
return (reg_uri);
}
return (NULL);
}
void _shttpd_setup_embedded_stream(struct conn *c, union variant func, void *data)
{
c->loc.chan.emb.state = NULL;
c->loc.chan.emb.func = func;
c->loc.chan.emb.data = data;
c->loc.io_class = &_shttpd_io_embedded;
c->loc.flags |= FLAG_R | FLAG_W |FLAG_ALWAYS_READY;
}
void shttpd_handle_error(struct shttpd_ctx *ctx, int code,
shttpd_callback_t func, void *data)
{
struct error_handler *e;
if ((e = malloc(sizeof(*e))) != NULL) {
e->code = code;
e->callback.v_func = (void (*)(void)) func;
e->callback_data = data;
LL_TAIL(&ctx->error_handlers, &e->link);
}
}
void shttpd_wakeup(const void *priv)
{
const struct conn *conn = priv;
char buf[sizeof(int) + sizeof(void *)];
int cmd = CTL_WAKEUP;
#if 0
conn->flags &= ~SHTTPD_SUSPEND;
#endif
(void) memcpy(buf, &cmd, sizeof(cmd));
(void) memcpy(buf + sizeof(cmd), conn, sizeof(conn));
(void) send(conn->worker->ctl[1], buf, sizeof(buf), 0);
}
const struct io_class _shttpd_io_embedded = {
"embedded",
do_embedded,
(int (*)(struct stream *, const void *, size_t)) do_embedded,
close_embedded
};