Blame


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