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;
36 0c267caa 2023-12-21 op type badrequest = !void;
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 0c267caa 2023-12-21 op export type routefn = *fn(*request, *reswriter) (void | io::error);
66 4ea63b45 2023-12-21 op type connection = struct {
68 924d1172 2023-12-21 op in: bufio::scanner,
69 924d1172 2023-12-21 op first_line: bool,
70 d5ea6c0a 2023-12-21 op request_done: bool,
71 924d1172 2023-12-21 op request: request,
72 94eeacc5 2023-12-21 op response: reswriter,
73 d5ea6c0a 2023-12-21 op routefn: routefn,
77 4ea63b45 2023-12-21 op fn acceptconn(f: io::file, ev: ev::event, data: nullable *opaque) void = {
78 924d1172 2023-12-21 op let sock = match(net::accept(f, net::sockflag::NONBLOCK)) {
79 924d1172 2023-12-21 op case let err: net::error =>
80 924d1172 2023-12-21 op log::printfln("accept: {}", net::strerror(err));
82 924d1172 2023-12-21 op case let sock: net::socket =>
86 924d1172 2023-12-21 op let mux = data: *mux;
87 4ea63b45 2023-12-21 op let conn = alloc(connection {
90 924d1172 2023-12-21 op in = bufio::newscanner(sock, types::SIZE_MAX),
91 924d1172 2023-12-21 op first_line = true,
92 94eeacc5 2023-12-21 op response = new_reswriter(sock),
93 d5ea6c0a 2023-12-21 op routefn = &route_notfound,
97 4ea63b45 2023-12-21 op ev::add(sock, ev::READ, &handleconn, conn);
100 0c267caa 2023-12-21 op fn parse_req(
101 4ea63b45 2023-12-21 op conn: *connection
102 0c267caa 2023-12-21 op ) (bool | badrequest | io::error | encoding::utf8::invalid) = {
103 4ea63b45 2023-12-21 op let req = &conn.request;
104 4ea63b45 2023-12-21 op let res = &conn.response;
107 4ea63b45 2023-12-21 op let line = match(bufio::scan_string(&conn.in, "\r\n")) {
108 924d1172 2023-12-21 op case io::EOF => return badrequest;
109 924d1172 2023-12-21 op case let err: io::error => return err;
110 924d1172 2023-12-21 op case let line: const str => yield line;
113 4ea63b45 2023-12-21 op if (conn.first_line) {
114 4ea63b45 2023-12-21 op conn.first_line = false;
116 924d1172 2023-12-21 op let (method, rest) = strings::cut(line, " ");
118 924d1172 2023-12-21 op switch (method) {
119 4ea63b45 2023-12-21 op case "GET" => req.method = method::GET;
120 4ea63b45 2023-12-21 op case "HEAD" => req.method = method::HEAD;
121 4ea63b45 2023-12-21 op case "POST" => req.method = method::POST;
122 4ea63b45 2023-12-21 op case "PUT" => req.method = method::PUT;
123 4ea63b45 2023-12-21 op case "DELETE" => req.method = method::DELETE;
124 4ea63b45 2023-12-21 op case "CONNECT" => req.method = method::CONNECT;
125 4ea63b45 2023-12-21 op case "OPTIONS" => req.method = method::OPTIONS;
126 4ea63b45 2023-12-21 op case "TRACE" => req.method = method::TRACE;
127 4ea63b45 2023-12-21 op case "PATCH" => req.method = method::PATCH;
128 924d1172 2023-12-21 op case => return badrequest;
131 924d1172 2023-12-21 op let (path, version) = strings::cut(rest, " ");
132 4ea63b45 2023-12-21 op req.path = strings::dup(path);
134 924d1172 2023-12-21 op switch (version) {
135 924d1172 2023-12-21 op case "HTTP/1.0" =>
136 4ea63b45 2023-12-21 op req.version = version::HTTP_1_0;
137 94eeacc5 2023-12-21 op case "HTTP/1.1" =>
138 4ea63b45 2023-12-21 op req.version = version::HTTP_1_1;
139 4ea63b45 2023-12-21 op res.chunked = true;
140 924d1172 2023-12-21 op case => return badrequest;
146 924d1172 2023-12-21 op // ignore everything for the time being.
147 924d1172 2023-12-21 op if (line == "") {
155 4ea63b45 2023-12-21 op fn close_conn(conn: *connection) void = {
156 4ea63b45 2023-12-21 op ev::del(conn.fp);
158 4ea63b45 2023-12-21 op bufio::finish(&conn.in);
159 4ea63b45 2023-12-21 op match (io::close(conn.fp)) {
160 924d1172 2023-12-21 op case => yield;
163 4ea63b45 2023-12-21 op match (io::close(&conn.response)) {
164 94eeacc5 2023-12-21 op case => yield;
170 4ea63b45 2023-12-21 op fn handleconn(f: io::file, ev: ev::event, data: nullable *opaque) void = {
171 4ea63b45 2023-12-21 op let conn = data: *connection;
173 4ea63b45 2023-12-21 op match (respond(conn)) {
174 0c267caa 2023-12-21 op case let err: io::error =>
175 0c267caa 2023-12-21 op log::printfln("failure: {}", io::strerror(err));
176 4ea63b45 2023-12-21 op close_conn(conn);
179 94eeacc5 2023-12-21 op // XXX wait if there is still data pending.
180 4ea63b45 2023-12-21 op if (conn.response.done) {
181 4ea63b45 2023-12-21 op close_conn(conn);
185 4ea63b45 2023-12-21 op ev::add(conn.fp, ev::READ | ev::WRITE, &handleconn, conn);
189 4ea63b45 2023-12-21 op fn handle_request(conn: *connection) (void | io::error) = {
190 4ea63b45 2023-12-21 op match (parse_req(conn)) {
191 924d1172 2023-12-21 op case let done: bool =>
193 924d1172 2023-12-21 op return; // try again later
195 924d1172 2023-12-21 op case errors::again => return; // try again later
196 924d1172 2023-12-21 op case badrequest =>
197 4ea63b45 2023-12-21 op route_badrequest(&conn.request, &conn.response)?;
198 4ea63b45 2023-12-21 op conn.response.done = true;
199 4ea63b45 2023-12-21 op reswriter_flush(&conn.response)?;
201 924d1172 2023-12-21 op case let err: io::error =>
202 924d1172 2023-12-21 op log::printfln("parse_req: {}", io::strerror(err));
206 ed8cff62 2023-12-21 op log::printfln("< GET {}", conn.request.path);
208 ed8cff62 2023-12-21 op conn.routefn = mux_match(conn.mux, &conn.request, &route_notfound);
209 4ea63b45 2023-12-21 op conn.request_done = true;
212 4ea63b45 2023-12-21 op fn respond(conn: *connection) (void | io::error) = {
213 4ea63b45 2023-12-21 op if (!conn.request_done)
214 4ea63b45 2023-12-21 op handle_request(conn)?;
215 4ea63b45 2023-12-21 op if (!conn.request_done)
218 4ea63b45 2023-12-21 op if (!conn.response.done) {
219 4ea63b45 2023-12-21 op conn.response.done = true;
220 4ea63b45 2023-12-21 op conn.routefn(&conn.request, &conn.response)?;
223 4ea63b45 2023-12-21 op reswriter_flush(&conn.response)?;
226 0c267caa 2023-12-21 op fn route_notfound(req: *request, res: *reswriter) (void | io::error) = {
227 94eeacc5 2023-12-21 op reply(req, res, 404, "Not Found")?;
228 94eeacc5 2023-12-21 op header(res, "Content-Type", "text/plain")?;
229 94eeacc5 2023-12-21 op fmt::fprintln(res, "Page not found.")?;
232 0c267caa 2023-12-21 op fn route_badrequest(req: *request, res: *reswriter) (void | io::error) = {
233 94eeacc5 2023-12-21 op reply(req, res, 400, "Bad Request")?;
234 94eeacc5 2023-12-21 op header(res, "Content-Type", "text/plain")?;
235 94eeacc5 2023-12-21 op fmt::fprintln(res, "Bad Request")?;