aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--example.cpp5
-rw-r--r--flask.h13
-rw-r--r--http_connection.h4
-rw-r--r--http_request.h11
-rw-r--r--http_response.h1
-rw-r--r--parser.h16
-rw-r--r--test.py4
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();
}
diff --git a/flask.h b/flask.h
index ca78da3..f61da16 100644
--- a/flask.h
+++ b/flask.h
@@ -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) {}
diff --git a/parser.h b/parser.h
index b5375a1..af1d6c3 100644
--- a/parser.h
+++ b/parser.h
@@ -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;
diff --git a/test.py b/test.py
index 49b7368..ee45450 100644
--- a/test.py
+++ b/test.py
@@ -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()