Created
June 26, 2020 19:10
-
-
Save qookei/604c65fb03affa78d86202bd571e539e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
while (sock->connected()) { | |
auto b = co_await sock->recv(); | |
frg::string_view req{static_cast<char *>(b.data()), b.size()}; | |
size_t newl = req.find_first('\n'); | |
auto http_req = req.sub_string(0, newl); | |
auto s1 = http_req.find_first(' '); | |
auto s2 = http_req.find_last(' '); | |
if (s1 == size_t(-1) || s2 == size_t(-1)) | |
continue; | |
auto type = http_req.sub_string(0, s1); | |
auto path = http_req.sub_string(s1 + 1, s2 - s1 - 1); | |
lib::log("tart: got a http request!, type \"%.*s\", path \"%.*s\"\r\n", type.size(), type.data(), path.size(), path.data()); | |
frg::string_view resp; | |
if (type != "GET") | |
resp = "HTTP/1.1 400 Bad request\nServer: tart\nContent-Type: text/html\nContent-Length: 20\n\nBad request ...what?"; | |
if (path == "/" || path == "/index.html") | |
resp = "HTTP/1.1 200 OK\nServer: tart\nContent-Type: text/html\nContent-Length: 101\n\nHello, world! This web page was served to you from <a href=\"https://github.com/qookei/tart\">tart</a>!"; | |
else if (path == "/other.html") | |
resp = "HTTP/1.1 200 OK\nServer: tart\nContent-Type: text/html\nContent-Length: 80\n\nHello, world ...again! This is a different page. <a href=\"index.html\">Go back</a>"; | |
else | |
resp = "HTTP/1.1 404 File not found\nServer: tart\nContent-Type: text/html\nContent-Length: 38\n\nI don't know what you're asking for..."; | |
co_await sock->send(const_cast<char *>(resp.data()), resp.size()); | |
} | |
co_await sock->close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment