2 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * Sergey Lyubka wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return.
20 call_user(struct conn *c, struct shttpd_arg *arg, shttpd_callback_t func)
23 arg->state = c->loc.chan.emb.state;
24 arg->out.buf = io_space(&c->loc.io);
25 arg->out.len = io_space_len(&c->loc.io);
26 arg->out.num_bytes = 0;
27 arg->in.buf = io_data(&c->rem.io);;
28 arg->in.len = io_data_len(&c->rem.io);
29 arg->in.num_bytes = 0;
31 if (io_data_len(&c->rem.io) >= c->rem.io.size)
32 arg->flags |= SHTTPD_POST_BUFFER_FULL;
34 if (c->rem.content_len > 0 && c->rem.io.total < c->rem.content_len)
35 arg->flags |= SHTTPD_MORE_POST_DATA;
39 io_inc_head(&c->loc.io, arg->out.num_bytes);
40 io_inc_tail(&c->rem.io, arg->in.num_bytes);
41 c->loc.chan.emb.state = arg->state; /* Save state */
44 * If callback finished output, that means it did all cleanup.
45 * If the connection is terminated unexpectedly, we canna call
46 * the callback via the stream close() method from disconnect.
47 * However, if cleanup is already done, we set close() method to
48 * NULL, to prevent the call from disconnect().
51 if (arg->flags & SHTTPD_END_OF_OUTPUT)
52 c->loc.flags &= ~FLAG_DONT_CLOSE;
54 c->loc.flags |= FLAG_DONT_CLOSE;
58 do_embedded(struct stream *stream, void *buf, size_t len)
60 struct shttpd_arg arg;
61 buf = NULL; len = 0; /* Squash warnings */
63 arg.user_data = stream->conn->loc.chan.emb.data;
66 call_user(stream->conn, &arg, (shttpd_callback_t)
67 stream->conn->loc.chan.emb.func.v_func);
73 close_embedded(struct stream *stream)
75 struct shttpd_arg arg;
76 struct conn *c = stream->conn;
78 arg.flags = SHTTPD_CONNECTION_ERROR;
79 arg.user_data = c->loc.chan.emb.data;
82 * Do not call the user function if SHTTPD_END_OF_OUTPUT was set,
83 * i.e. the callback already terminated correctly
85 if (stream->flags & FLAG_DONT_CLOSE)
86 call_user(stream->conn, &arg, (shttpd_callback_t)
87 c->loc.chan.emb.func.v_func);
91 shttpd_printf(struct shttpd_arg *arg, const char *fmt, ...)
93 struct conn *c = arg->priv;
94 struct io *io = &c->loc.io;
95 char *buf = arg->out.buf + arg->out.num_bytes;
96 int buflen = arg->out.len - arg->out.num_bytes, len = 0;
99 assert(buf <= io->buf + io->size);
103 len = vsnprintf(buf, buflen, fmt, ap);
106 if (len < 0 || len > buflen)
108 arg->out.num_bytes += len;
115 shttpd_get_header(struct shttpd_arg *arg, const char *header_name)
117 struct conn *c = arg->priv;
122 e = c->request + c->rem.headers_len;
123 len = strlen(header_name);
126 if ((s = strchr(p, '\n')) != NULL)
127 s[s[-1] == '\r' ? -1 : 0] = '\0';
128 if (my_strncasecmp(header_name, p, len) == 0)
129 return (p + len + 2);
138 shttpd_get_env(struct shttpd_arg *arg, const char *env_name)
140 struct conn *c = arg->priv;
143 if (strcmp(env_name, "REQUEST_METHOD") == 0) {
144 return (known_http_methods[c->method].ptr);
145 } else if (strcmp(env_name, "REQUEST_URI") == 0) {
147 } else if (strcmp(env_name, "QUERY_STRING") == 0) {
149 } else if (strcmp(env_name, "REMOTE_USER") == 0) {
150 vec = &c->ch.user.v_vec;
152 ((char *) vec->ptr)[vec->len] = '\0';
155 } else if (strcmp(env_name, "REMOTE_ADDR") == 0) {
156 return (inet_ntoa(c->sa.u.sin.sin_addr));/* FIXME NOT MT safe */
163 shttpd_get_http_version(struct shttpd_arg *arg, unsigned long *major, unsigned long *minor)
165 struct conn *c = arg->priv;
167 *major = c->major_version;
168 *minor = c->minor_version;
172 shttpd_register_uri(struct shttpd_ctx *ctx,
173 const char *uri, shttpd_callback_t callback, void *data)
175 struct registered_uri *e;
177 if ((e = malloc(sizeof(*e))) != NULL) {
178 e->uri = my_strdup(uri);
179 e->callback.v_func = (void (*)(void)) callback;
180 e->callback_data = data;
181 LL_TAIL(&ctx->registered_uris, &e->link);
186 shttpd_get_var(const char *var, const char *buf, int buf_len,
187 char *value, int value_len)
189 const char *p, *e, *s;
192 var_len = strlen(var);
193 e = buf + buf_len; /* End of QUERY_STRING buffer */
195 /* buf is "var1=val1&var2=val2...". Find variable first */
196 for (p = buf; p + var_len < e; p++)
197 if ((p == buf || p[-1] == '&') &&
199 !my_strncasecmp(var, p, var_len)) {
201 /* Point 'p' to var value, 's' to the end of value */
203 if ((s = memchr(p, '&', e - p)) == NULL)
206 /* URL-decode value. Return result length */
207 return (url_decode(p, s - p, value, value_len));
214 match_regexp(const char *regexp, const char *text)
217 return (*text == '\0');
221 if (match_regexp(regexp + 1, text))
223 } while (*text++ != '\0');
225 if (*text != '\0' && *regexp == *text)
226 return (match_regexp(regexp + 1, text + 1));
231 struct registered_uri *
232 is_registered_uri(struct shttpd_ctx *ctx, const char *uri)
235 struct registered_uri *reg_uri;
237 LL_FOREACH(&ctx->registered_uris, lp) {
238 reg_uri = LL_ENTRY(lp, struct registered_uri, link);
239 if (match_regexp(reg_uri->uri, uri))
247 setup_embedded_stream(struct conn *c, union variant func, void *data)
249 c->loc.chan.emb.state = NULL;
250 c->loc.chan.emb.func = func;
251 c->loc.chan.emb.data = data;
252 c->loc.io_class = &io_embedded;
253 c->loc.flags |= FLAG_R | FLAG_W |FLAG_ALWAYS_READY;
257 shttpd_handle_error(struct shttpd_ctx *ctx, int code,
258 shttpd_callback_t func, void *data)
260 struct error_handler *e;
262 if ((e = malloc(sizeof(*e))) != NULL) {
264 e->callback.v_func = (void (*)(void)) func;
265 e->callback_data = data;
266 LL_TAIL(&ctx->error_handlers, &e->link);
270 const struct io_class io_embedded = {
273 (int (*)(struct stream *, const void *, size_t)) do_embedded,