diff options
-rw-r--r-- | example.cpp | 5 | ||||
-rw-r--r-- | flask.h | 13 | ||||
-rw-r--r-- | http_connection.h | 4 | ||||
-rw-r--r-- | http_request.h | 11 | ||||
-rw-r--r-- | http_response.h | 1 | ||||
-rw-r--r-- | parser.h | 16 | ||||
-rw-r--r-- | test.py | 4 |
7 files changed, 39 insertions, 15 deletions
diff --git a/example.cpp b/example.cpp index 904e1ff..9357967 100644 --- a/example.cpp +++ b/example.cpp @@ -11,6 +11,11 @@ int main() return "Hello World!"; }); + app.route("/about", + []{ + return "About Flask example."; + }); + app.port(8080) .run(); } @@ -20,17 +20,22 @@ namespace flask { } - response handle() + response handle(const request& req) { - return yameHandler_(); + if (yameHandlers_.count(req.url) == 0) + { + return response(404); + } + return yameHandlers_[req.url](); } template <typename F> void route(const std::string& url, F f) { - yameHandler_ = [f]{ + auto yameHandler = [f]{ return response(f()); }; + yameHandlers_.emplace(url, yameHandler); } Flask& port(std::uint16_t port) @@ -48,7 +53,7 @@ namespace flask uint16_t port_ = 80; // Someday I will become real handler! - std::function<response()> yameHandler_; + std::unordered_map<std::string, std::function<response()>> yameHandlers_; }; }; diff --git a/http_connection.h b/http_connection.h index c25c61d..7fd9a43 100644 --- a/http_connection.h +++ b/http_connection.h @@ -27,8 +27,8 @@ namespace flask void handle() { - // request = make_request_from_parser - res = handler_->handle(); + request req = parser_.to_request(); + res = handler_->handle(req); static std::string seperator = ": "; static std::string crlf = "\r\n"; diff --git a/http_request.h b/http_request.h new file mode 100644 index 0000000..e5a8da0 --- /dev/null +++ b/http_request.h @@ -0,0 +1,11 @@ +#pragma once + +namespace flask +{ + struct request + { + std::string url; + std::unordered_map<std::string, std::string> headers; + std::string body; + }; +} diff --git a/http_response.h b/http_response.h index 1221dd2..7927d6d 100644 --- a/http_response.h +++ b/http_response.h @@ -32,6 +32,7 @@ namespace flask std::string body; std::unordered_map<std::string, std::string> headers; response() {} + response(int status) : status(status) {} response(const std::string& body) : body(body) {} response(std::string&& body) : body(std::move(body)) {} response(const std::string& body, int status) : body(body), status(status) {} @@ -1,6 +1,10 @@ +#pragma once + #include <string> #include <unordered_map> +#include "http_request.h" + namespace flask { template <typename Handler> @@ -96,14 +100,12 @@ namespace flask bool feed(const char* buffer, int length) { - std::cerr << "<|" << std::string(buffer, buffer+length) << "|>" << std::endl; int nparsed = http_parser_execute(this, &settings_, buffer, length); return nparsed == length; } bool done() { - std::cerr << "(done)" << std::endl; int nparsed = http_parser_execute(this, &settings_, nullptr, 0); return nparsed == 0; } @@ -120,14 +122,14 @@ namespace flask void process_message() { - //std::cout << "URL: "<< url << std::endl; - //for(auto& kv : headers) - //{ - //std::cout << kv.first << ": " << kv.second << std::endl; - //} handler_->handle(); } + request to_request() + { + return request{std::move(url), std::move(headers), std::move(body)}; + } + std::string url; int header_building_state = 0; std::string header_field; @@ -1,3 +1,3 @@ import urllib -d = urllib.urlopen('http://localhost:8080').read() -print d +assert "Hello World!" == urllib.urlopen('http://localhost:8080').read() +assert "About Flask example." == urllib.urlopen('http://localhost:8080/about').read() |