1 924d1172 2023-12-21 op // This is free and unencumbered software released into the public domain.
3 924d1172 2023-12-21 op // Anyone is free to copy, modify, publish, use, compile, sell, or
4 924d1172 2023-12-21 op // distribute this software, either in source code form or as a compiled
5 924d1172 2023-12-21 op // binary, for any purpose, commercial or non-commercial, and by any
8 924d1172 2023-12-21 op // In jurisdictions that recognize copyright laws, the author or authors
9 924d1172 2023-12-21 op // of this software dedicate any and all copyright interest in the
10 924d1172 2023-12-21 op // software to the public domain. We make this dedication for the benefit
11 924d1172 2023-12-21 op // of the public at large and to the detriment of our heirs and
12 924d1172 2023-12-21 op // successors. We intend this dedication to be an overt act of
13 924d1172 2023-12-21 op // relinquishment in perpetuity of all present and future rights to this
14 924d1172 2023-12-21 op // software under copyright law.
16 924d1172 2023-12-21 op // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 924d1172 2023-12-21 op // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 924d1172 2023-12-21 op // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 924d1172 2023-12-21 op // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 924d1172 2023-12-21 op // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 924d1172 2023-12-21 op // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 924d1172 2023-12-21 op // OTHER DEALINGS IN THE SOFTWARE.
25 924d1172 2023-12-21 op use encoding::utf8;
38 924d1172 2023-12-21 op export type version = enum {
43 924d1172 2023-12-21 op export type method = enum {
55 924d1172 2023-12-21 op export type request = struct {
57 924d1172 2023-12-21 op method: method,
58 924d1172 2023-12-21 op version: version,
61 924d1172 2023-12-21 op chunked: bool,
64 924d1172 2023-12-21 op export type routefn = *fn(*client) (void | error);
66 924d1172 2023-12-21 op export type client = struct {
68 924d1172 2023-12-21 op in: bufio::scanner,
69 924d1172 2023-12-21 op first_line: bool,
70 924d1172 2023-12-21 op request: request,
71 924d1172 2023-12-21 op chunked: bool, // whether to chunk the reply
72 924d1172 2023-12-21 op hdrdone: bool,
74 924d1172 2023-12-21 op // routefn: (void | routefn),
78 924d1172 2023-12-21 op export type route = struct {
80 924d1172 2023-12-21 op routefn: routefn,
83 924d1172 2023-12-21 op export type mux = []route;
85 924d1172 2023-12-21 op let global: mux = [];
87 924d1172 2023-12-21 op export fn handle(pattern: str, routefn: routefn) void = {
88 924d1172 2023-12-21 op append(global, route {
89 924d1172 2023-12-21 op pattern = pattern,
90 924d1172 2023-12-21 op routefn = routefn,
94 924d1172 2023-12-21 op // XXX generic listen would be very handy!
95 924d1172 2023-12-21 op // something like: http::listen("localhost:8080") or maybe even
96 924d1172 2023-12-21 op // http::listen("unix:/run/foo.sock")
98 924d1172 2023-12-21 op export fn listen_sock(f: io::file) void = {
99 924d1172 2023-12-21 op ev::add(f, ev::READ, &acceptclt, &global);
102 924d1172 2023-12-21 op fn acceptclt(f: io::file, ev: ev::event, data: nullable *opaque) void = {
103 924d1172 2023-12-21 op let sock = match(net::accept(f, net::sockflag::NONBLOCK)) {
104 924d1172 2023-12-21 op case let err: net::error =>
105 924d1172 2023-12-21 op log::printfln("accept: {}", net::strerror(err));
107 924d1172 2023-12-21 op case let sock: net::socket =>
111 924d1172 2023-12-21 op let mux = data: *mux;
112 924d1172 2023-12-21 op let clt = alloc(client {
115 924d1172 2023-12-21 op // routefn = void,
116 924d1172 2023-12-21 op in = bufio::newscanner(sock, types::SIZE_MAX),
117 924d1172 2023-12-21 op first_line = true,
121 924d1172 2023-12-21 op ev::add(sock, ev::READ, &handleclt, clt);
124 924d1172 2023-12-21 op fn parse_req(c: *client) (bool | error | encoding::utf8::invalid) = {
126 924d1172 2023-12-21 op let line = match(bufio::scan_string(&c.in, "\r\n")) {
127 924d1172 2023-12-21 op case io::EOF => return badrequest;
128 924d1172 2023-12-21 op case let err: io::error => return err;
129 924d1172 2023-12-21 op case let line: const str => yield line;
132 924d1172 2023-12-21 op if (c.first_line) {
133 924d1172 2023-12-21 op c.first_line = false;
135 924d1172 2023-12-21 op let (method, rest) = strings::cut(line, " ");
137 924d1172 2023-12-21 op switch (method) {
138 ef30005e 2023-12-21 op case "GET" => c.request.method = method::GET;
139 ef30005e 2023-12-21 op case "HEAD" => c.request.method = method::HEAD;
140 ef30005e 2023-12-21 op case "POST" => c.request.method = method::POST;
141 ef30005e 2023-12-21 op case "PUT" => c.request.method = method::PUT;
142 ef30005e 2023-12-21 op case "DELETE" => c.request.method = method::DELETE;
143 ef30005e 2023-12-21 op case "CONNECT" => c.request.method = method::CONNECT;
144 ef30005e 2023-12-21 op case "OPTIONS" => c.request.method = method::OPTIONS;
145 ef30005e 2023-12-21 op case "TRACE" => c.request.method = method::TRACE;
146 ef30005e 2023-12-21 op case "PATCH" => c.request.method = method::PATCH;
147 924d1172 2023-12-21 op case => return badrequest;
150 924d1172 2023-12-21 op let (path, version) = strings::cut(rest, " ");
151 924d1172 2023-12-21 op c.request.path = strings::dup(path);
153 924d1172 2023-12-21 op switch (version) {
154 924d1172 2023-12-21 op case "HTTP/1.0" =>
155 924d1172 2023-12-21 op c.request.version = version::HTTP_1_1;
156 924d1172 2023-12-21 op case "HTTP/1.1" =>
157 924d1172 2023-12-21 op c.request.version = version::HTTP_1_0;
158 924d1172 2023-12-21 op c.chunked = true;
159 924d1172 2023-12-21 op case => return badrequest;
165 924d1172 2023-12-21 op // ignore everything for the time being.
166 924d1172 2023-12-21 op if (line == "") {
174 924d1172 2023-12-21 op fn delclient(c: *client) void = {
175 924d1172 2023-12-21 op ev::del(c.fp);
177 924d1172 2023-12-21 op bufio::finish(&c.in);
178 924d1172 2023-12-21 op match (io::close(c.fp)) {
179 924d1172 2023-12-21 op case => yield;
185 924d1172 2023-12-21 op fn handleclt(f: io::file, ev: ev::event, data: nullable *opaque) void = {
186 924d1172 2023-12-21 op let clt = data: *client;
188 924d1172 2023-12-21 op match (do_request(clt)) {
189 924d1172 2023-12-21 op case let err: error =>
190 924d1172 2023-12-21 op log::printfln("failure: {}", strerror(err));
191 924d1172 2023-12-21 op delclient(clt);
194 924d1172 2023-12-21 op if (clt.done) {
195 924d1172 2023-12-21 op delclient(clt);
200 924d1172 2023-12-21 op fn do_request(clt: *client) (void | error) = {
201 924d1172 2023-12-21 op match (parse_req(clt)) {
202 924d1172 2023-12-21 op case let done: bool =>
204 924d1172 2023-12-21 op return; // try again later
206 924d1172 2023-12-21 op case errors::again => return; // try again later
207 924d1172 2023-12-21 op case badrequest =>
208 924d1172 2023-12-21 op route_badrequest(clt)?;
209 924d1172 2023-12-21 op clt.done = true;
211 924d1172 2023-12-21 op case let err: io::error =>
212 924d1172 2023-12-21 op log::printfln("parse_req: {}", io::strerror(err));
216 924d1172 2023-12-21 op let path = clt.request.path;
217 924d1172 2023-12-21 op log::printfln("< GET {}", path);
219 924d1172 2023-12-21 op let found = false;
220 924d1172 2023-12-21 op for (let i = 0z; i < len(clt.mux); i += 1) {
221 924d1172 2023-12-21 op if (fnmatch::fnmatch(clt.mux[i].pattern, path)) {
222 924d1172 2023-12-21 op found = true;
223 924d1172 2023-12-21 op clt.mux[i].routefn(clt)?;
228 924d1172 2023-12-21 op if (!found) {
229 924d1172 2023-12-21 op route_notfound(clt)?;
232 924d1172 2023-12-21 op write(clt, "")?; // flush
234 924d1172 2023-12-21 op clt.done = true;
237 924d1172 2023-12-21 op export fn reply(clt: *client, code: int, status: str) (void | error) = {
238 924d1172 2023-12-21 op log::printfln("> {} {}", code, status);
240 924d1172 2023-12-21 op let version = if (clt.request.version == version::HTTP_1_0)
241 924d1172 2023-12-21 op "HTTP/1.0" else "HTTP/1.1";
243 924d1172 2023-12-21 op fmt::fprintf(clt.fp, "{} {} {}\r\n", version, code, status)?;
246 924d1172 2023-12-21 op export fn header(clt: *client, header: str, value: str) (void | error) = {
247 924d1172 2023-12-21 op fmt::fprintf(clt.fp, "{}: {}\r\n", header, value)?;
250 924d1172 2023-12-21 op export fn write(clt: *client, body: str) (void | error) = {
251 924d1172 2023-12-21 op if (!clt.hdrdone) {
252 b28be21f 2023-12-21 op if (clt.chunked) {
253 b28be21f 2023-12-21 op header(clt, "Transfer-Encoding", "chunked")?;
255 924d1172 2023-12-21 op header(clt, "Connection", "closed")?;
257 924d1172 2023-12-21 op clt.hdrdone = true;
258 924d1172 2023-12-21 op fmt::fprint(clt.fp, "\r\n")?;
261 b28be21f 2023-12-21 op if (clt.chunked) {
262 b28be21f 2023-12-21 op fmt::fprintf(clt.fp, "{:x}\r\n{}\r\n", len(body), body)?;
264 b28be21f 2023-12-21 op fmt::fprintf(clt.fp, "{}", body)?;
268 924d1172 2023-12-21 op fn route_notfound(clt: *client) (void | error) = {
269 924d1172 2023-12-21 op reply(clt, 404, "Not Found")?;
270 924d1172 2023-12-21 op header(clt, "Content-Type", "text/plain")?;
271 924d1172 2023-12-21 op write(clt, "Page not found.\n")?;
274 924d1172 2023-12-21 op fn route_badrequest(clt: *client) (void | error) = {
275 924d1172 2023-12-21 op reply(clt, 400, "Bad Request")?;
276 924d1172 2023-12-21 op header(clt, "Content-Type", "text/plain")?;
277 924d1172 2023-12-21 op write(clt, "Bad Request")?;