Blob


1 /*
2 * This is free and unencumbered software released into the public domain.
3 *
4 * Anyone is free to copy, modify, publish, use, compile, sell, or
5 * distribute this software, either in source code form or as a compiled
6 * binary, for any purpose, commercial or non-commercial, and by any
7 * means.
8 *
9 * In jurisdictions that recognize copyright laws, the author or authors
10 * of this software dedicate any and all copyright interest in the
11 * software to the public domain. We make this dedication for the benefit
12 * of the public at large and to the detriment of our heirs and
13 * successors. We intend this dedication to be an overt act of
14 * relinquishment in perpetuity of all present and future rights to this
15 * software under copyright law.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
26 enum http_method {
27 METHOD_UNKNOWN,
28 METHOD_GET,
29 METHOD_POST,
30 };
32 enum http_version {
33 HTTP_1_0,
34 HTTP_1_1,
35 };
37 struct bufio;
39 struct request {
40 char *path;
41 int method;
42 int version;
43 char *secret;
44 char *ctype;
45 char *body;
46 size_t clen;
48 #define R_CONNUPGR 0x01
49 #define R_UPGRADEWS 0x02
50 #define R_WSVERSION 0x04
51 int flags;
52 };
54 struct client;
55 typedef void (*route_fn)(struct client *);
57 TAILQ_HEAD(clthead, client);
58 struct client {
59 char *buf;
60 size_t len;
61 size_t cap;
62 struct bufio bio;
63 struct request req;
64 int err;
65 int chunked;
66 int ws; /* if talking ws:// */
67 int reqdone; /* done parsing the request */
68 int done; /* done handling the client */
69 route_fn route;
71 TAILQ_ENTRY(client) clients;
72 };
74 int http_init(struct client *, int);
75 int http_parse(struct client *);
76 int http_read(struct client *);
77 void http_postdata(struct client *, char **, size_t *);
78 int http_reply(struct client *, int, const char *, const char *);
79 int http_flush(struct client *);
80 int http_write(struct client *, const char *, size_t);
81 int http_writes(struct client *, const char *);
82 int http_fmt(struct client *, const char *, ...);
83 int http_urlescape(struct client *, const char *);
84 int http_htmlescape(struct client *, const char *);
85 int http_close(struct client *);
86 void http_free(struct client *);