2024-05-28 10:23:52 +00:00
|
|
|
use getopt;
|
|
|
|
use net;
|
|
|
|
use net::ip;
|
|
|
|
use net::http;
|
|
|
|
use net::dial;
|
|
|
|
use os;
|
|
|
|
use memio;
|
|
|
|
use io;
|
|
|
|
use fmt;
|
|
|
|
use bufio;
|
|
|
|
use strings;
|
2024-05-28 10:54:42 +00:00
|
|
|
use thread;
|
|
|
|
use time;
|
2024-05-28 10:23:52 +00:00
|
|
|
|
|
|
|
const usage: [_]getopt::help = [
|
|
|
|
"HTTP server",
|
|
|
|
('a', "address", "listened address (ex: 127.0.0.1:8080)")
|
|
|
|
];
|
|
|
|
|
|
|
|
export fn main() void = {
|
|
|
|
const cmd = getopt::parse(os::args, usage...);
|
|
|
|
defer getopt::finish(&cmd);
|
|
|
|
|
|
|
|
let port: u16 = 8080;
|
|
|
|
let ip_addr: ip::addr4 = [127, 0, 0, 1];
|
|
|
|
|
|
|
|
for (let i = 0z; i < len(cmd.opts); i += 1) {
|
|
|
|
const opt = cmd.opts[i];
|
|
|
|
switch (opt.0) {
|
|
|
|
case 'a' =>
|
|
|
|
match (dial::splitaddr(opt.1, "")) {
|
|
|
|
case let value: (str, u16) =>
|
|
|
|
ip_addr = ip::parsev4(value.0)!;
|
|
|
|
port = value.1;
|
|
|
|
case dial::invalid_address =>
|
|
|
|
abort("Invalid address");
|
|
|
|
};
|
|
|
|
case => abort(); // unreachable
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const server = match (http::listen(ip_addr, port)) {
|
|
|
|
case let this: *http::server =>
|
|
|
|
yield this;
|
|
|
|
case net::error => abort("failure while listening");
|
|
|
|
};
|
|
|
|
defer http::server_finish(server);
|
|
|
|
|
|
|
|
for (true) {
|
|
|
|
const serv_req = match (http::serve(server)) {
|
|
|
|
case let this: *http::server_request =>
|
|
|
|
yield this;
|
|
|
|
case net::error => abort("failure while serving");
|
|
|
|
};
|
|
|
|
|
2024-05-28 10:54:42 +00:00
|
|
|
let tid = thread::spawn(&handle_req, serv_req)!;
|
|
|
|
thread::detach(tid)!;
|
2024-05-28 10:23:52 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-05-28 10:54:42 +00:00
|
|
|
fn handle_req(arg: nullable *opaque) void = {
|
|
|
|
let serv_req = arg: *http::server_request;
|
|
|
|
defer http::serve_finish(serv_req);
|
|
|
|
fmt::printfln("got a request. serving now.")!;
|
|
|
|
|
|
|
|
let buf = memio::dynamic();
|
|
|
|
defer io::close(&buf)!;
|
|
|
|
handlereq(&buf, &serv_req.request);
|
|
|
|
|
|
|
|
http::response_write(
|
|
|
|
serv_req.socket,
|
|
|
|
http::STATUS_OK,
|
|
|
|
&buf,
|
|
|
|
("Content-Type", "text/plain")
|
|
|
|
)!;
|
|
|
|
};
|
|
|
|
|
2024-05-28 10:23:52 +00:00
|
|
|
export fn handlereq(buf: *io::stream, request: *http::request) void = {
|
|
|
|
fmt::fprintfln(buf, "Method: {}", request.method)!;
|
|
|
|
fmt::fprintfln(buf, "Path: {}", request.target.path)!;
|
|
|
|
fmt::fprintfln(buf, "Fragment: {}", request.target.fragment)!;
|
|
|
|
fmt::fprintfln(buf, "Query: {}", request.target.query)!;
|
|
|
|
fmt::fprintfln(buf, "Headers: <<EOF")!;
|
|
|
|
http::write_header(buf, &request.header)!;
|
|
|
|
fmt::fprintfln(buf, "EOF")!;
|
|
|
|
|
2024-05-28 10:54:42 +00:00
|
|
|
time::sleep(5 * time::SECOND);
|
|
|
|
|
2024-05-28 10:23:52 +00:00
|
|
|
match (request.body) {
|
|
|
|
case void => void;
|
|
|
|
case let body: io::handle =>
|
|
|
|
fmt::fprintfln(buf, "Body: <<EOF")!;
|
|
|
|
for (true) {
|
|
|
|
match (bufio::read_line(body)!) {
|
|
|
|
case let line: []u8 =>
|
|
|
|
fmt::fprintln(buf, strings::fromutf8(line)!)!;
|
|
|
|
break;
|
|
|
|
case io::EOF =>
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
fmt::fprintfln(buf, "EOF")!;
|
|
|
|
};
|
|
|
|
};
|