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;
37 0c267caa 2023-12-21 op type badrequest = !void;
39 924d1172 2023-12-21 op export type version = enum {
44 924d1172 2023-12-21 op export type method = enum {
56 924d1172 2023-12-21 op export type request = struct {
58 924d1172 2023-12-21 op method: method,
59 924d1172 2023-12-21 op version: version,
62 924d1172 2023-12-21 op chunked: bool,
65 0c267caa 2023-12-21 op export type routefn = *fn(*request, *reswriter) (void | io::error);
67 4ea63b45 2023-12-21 op type connection = struct {
69 924d1172 2023-12-21 op in: bufio::scanner,
70 924d1172 2023-12-21 op first_line: bool,
71 d5ea6c0a 2023-12-21 op request_done: bool,
72 924d1172 2023-12-21 op request: request,
73 94eeacc5 2023-12-21 op response: reswriter,
74 d5ea6c0a 2023-12-21 op routefn: routefn,
78 924d1172 2023-12-21 op // XXX generic listen would be very handy!
79 924d1172 2023-12-21 op // something like: http::listen("localhost:8080") or maybe even
80 924d1172 2023-12-21 op // http::listen("unix:/run/foo.sock")
82 ed8cff62 2023-12-21 op export fn listen_sock(f: io::file, mux: nullable *mux) void = {
83 ed8cff62 2023-12-21 op ev::add(f, ev::READ, &acceptconn, match(mux) {
84 ed8cff62 2023-12-21 op case null => yield &global;
85 ed8cff62 2023-12-21 op case => yield mux;
89 4ea63b45 2023-12-21 op fn acceptconn(f: io::file, ev: ev::event, data: nullable *opaque) void = {
90 924d1172 2023-12-21 op let sock = match(net::accept(f, net::sockflag::NONBLOCK)) {
91 924d1172 2023-12-21 op case let err: net::error =>
92 924d1172 2023-12-21 op log::printfln("accept: {}", net::strerror(err));
94 924d1172 2023-12-21 op case let sock: net::socket =>
98 924d1172 2023-12-21 op let mux = data: *mux;
99 4ea63b45 2023-12-21 op let conn = alloc(connection {
102 924d1172 2023-12-21 op in = bufio::newscanner(sock, types::SIZE_MAX),
103 924d1172 2023-12-21 op first_line = true,
104 94eeacc5 2023-12-21 op response = new_reswriter(sock),
105 d5ea6c0a 2023-12-21 op routefn = &route_notfound,
109 4ea63b45 2023-12-21 op ev::add(sock, ev::READ, &handleconn, conn);
112 0c267caa 2023-12-21 op fn parse_req(
113 4ea63b45 2023-12-21 op conn: *connection
114 0c267caa 2023-12-21 op ) (bool | badrequest | io::error | encoding::utf8::invalid) = {
115 4ea63b45 2023-12-21 op let req = &conn.request;
116 4ea63b45 2023-12-21 op let res = &conn.response;
119 4ea63b45 2023-12-21 op let line = match(bufio::scan_string(&conn.in, "\r\n")) {
120 924d1172 2023-12-21 op case io::EOF => return badrequest;
121 924d1172 2023-12-21 op case let err: io::error => return err;
122 924d1172 2023-12-21 op case let line: const str => yield line;
125 4ea63b45 2023-12-21 op if (conn.first_line) {
126 4ea63b45 2023-12-21 op conn.first_line = false;
128 924d1172 2023-12-21 op let (method, rest) = strings::cut(line, " ");
130 924d1172 2023-12-21 op switch (method) {
131 4ea63b45 2023-12-21 op case "GET" => req.method = method::GET;
132 4ea63b45 2023-12-21 op case "HEAD" => req.method = method::HEAD;
133 4ea63b45 2023-12-21 op case "POST" => req.method = method::POST;
134 4ea63b45 2023-12-21 op case "PUT" => req.method = method::PUT;
135 4ea63b45 2023-12-21 op case "DELETE" => req.method = method::DELETE;
136 4ea63b45 2023-12-21 op case "CONNECT" => req.method = method::CONNECT;
137 4ea63b45 2023-12-21 op case "OPTIONS" => req.method = method::OPTIONS;
138 4ea63b45 2023-12-21 op case "TRACE" => req.method = method::TRACE;
139 4ea63b45 2023-12-21 op case "PATCH" => req.method = method::PATCH;
140 924d1172 2023-12-21 op case => return badrequest;
143 924d1172 2023-12-21 op let (path, version) = strings::cut(rest, " ");
144 4ea63b45 2023-12-21 op req.path = strings::dup(path);
146 924d1172 2023-12-21 op switch (version) {
147 924d1172 2023-12-21 op case "HTTP/1.0" =>
148 4ea63b45 2023-12-21 op req.version = version::HTTP_1_0;
149 94eeacc5 2023-12-21 op case "HTTP/1.1" =>
150 4ea63b45 2023-12-21 op req.version = version::HTTP_1_1;
151 4ea63b45 2023-12-21 op res.chunked = true;
152 924d1172 2023-12-21 op case => return badrequest;
158 924d1172 2023-12-21 op // ignore everything for the time being.
159 924d1172 2023-12-21 op if (line == "") {
167 4ea63b45 2023-12-21 op fn close_conn(conn: *connection) void = {
168 4ea63b45 2023-12-21 op ev::del(conn.fp);
170 4ea63b45 2023-12-21 op bufio::finish(&conn.in);
171 4ea63b45 2023-12-21 op match (io::close(conn.fp)) {
172 924d1172 2023-12-21 op case => yield;
175 4ea63b45 2023-12-21 op match (io::close(&conn.response)) {
176 94eeacc5 2023-12-21 op case => yield;
182 4ea63b45 2023-12-21 op fn handleconn(f: io::file, ev: ev::event, data: nullable *opaque) void = {
183 4ea63b45 2023-12-21 op let conn = data: *connection;
185 4ea63b45 2023-12-21 op match (respond(conn)) {
186 0c267caa 2023-12-21 op case let err: io::error =>
187 0c267caa 2023-12-21 op log::printfln("failure: {}", io::strerror(err));
188 4ea63b45 2023-12-21 op close_conn(conn);
191 94eeacc5 2023-12-21 op // XXX wait if there is still data pending.
192 4ea63b45 2023-12-21 op if (conn.response.done) {
193 4ea63b45 2023-12-21 op close_conn(conn);
197 4ea63b45 2023-12-21 op ev::add(conn.fp, ev::READ | ev::WRITE, &handleconn, conn);
201 4ea63b45 2023-12-21 op fn handle_request(conn: *connection) (void | io::error) = {
202 4ea63b45 2023-12-21 op match (parse_req(conn)) {
203 924d1172 2023-12-21 op case let done: bool =>
205 924d1172 2023-12-21 op return; // try again later
207 924d1172 2023-12-21 op case errors::again => return; // try again later
208 924d1172 2023-12-21 op case badrequest =>
209 4ea63b45 2023-12-21 op route_badrequest(&conn.request, &conn.response)?;
210 4ea63b45 2023-12-21 op conn.response.done = true;
211 4ea63b45 2023-12-21 op reswriter_flush(&conn.response)?;
213 924d1172 2023-12-21 op case let err: io::error =>
214 924d1172 2023-12-21 op log::printfln("parse_req: {}", io::strerror(err));
218 ed8cff62 2023-12-21 op log::printfln("< GET {}", conn.request.path);
220 ed8cff62 2023-12-21 op conn.routefn = mux_match(conn.mux, &conn.request, &route_notfound);
221 4ea63b45 2023-12-21 op conn.request_done = true;
224 4ea63b45 2023-12-21 op fn respond(conn: *connection) (void | io::error) = {
225 4ea63b45 2023-12-21 op if (!conn.request_done)
226 4ea63b45 2023-12-21 op handle_request(conn)?;
227 4ea63b45 2023-12-21 op if (!conn.request_done)
230 4ea63b45 2023-12-21 op if (!conn.response.done) {
231 4ea63b45 2023-12-21 op conn.response.done = true;
232 4ea63b45 2023-12-21 op conn.routefn(&conn.request, &conn.response)?;
235 4ea63b45 2023-12-21 op reswriter_flush(&conn.response)?;
238 0c267caa 2023-12-21 op fn route_notfound(req: *request, res: *reswriter) (void | io::error) = {
239 94eeacc5 2023-12-21 op reply(req, res, 404, "Not Found")?;
240 94eeacc5 2023-12-21 op header(res, "Content-Type", "text/plain")?;
241 94eeacc5 2023-12-21 op fmt::fprintln(res, "Page not found.")?;
244 0c267caa 2023-12-21 op fn route_badrequest(req: *request, res: *reswriter) (void | io::error) = {
245 94eeacc5 2023-12-21 op reply(req, res, 400, "Bad Request")?;
246 94eeacc5 2023-12-21 op header(res, "Content-Type", "text/plain")?;
247 94eeacc5 2023-12-21 op fmt::fprintln(res, "Bad Request")?;