Skip to content

Commit

Permalink
Switch to a custom async http server based on epoll.
Browse files Browse the repository at this point in the history
  • Loading branch information
matt-42 committed Dec 15, 2019
1 parent d741dc9 commit b312a1d
Show file tree
Hide file tree
Showing 37 changed files with 1,621 additions and 139 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ build
*.db
*.sqlite
old
todo
*.out
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ link_build_include("sql" "/libraries/sql/sql")

include_directories(${CMAKE_BINARY_DIR}/include)

#set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -march=native -mtune=native -flto")

add_custom_target(
symbols_generation DEPENDS li_symbol_generator
COMMAND ${CMAKE_BINARY_DIR}/libraries/symbol/li_symbol_generator ${CMAKE_CURRENT_SOURCE_DIR}/libraries)
Expand Down
2 changes: 1 addition & 1 deletion libraries/http_backend/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 2.8)

project(silicon)
find_package(Boost REQUIRED filesystem)
find_package(Boost REQUIRED filesystem context)

include_directories(${Boost_INCLUDE_DIRS})

Expand Down
2 changes: 1 addition & 1 deletion libraries/http_backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ https://github.com/matt-42/lithium/blob/master/libraries/http_backend/http_backe
```c++
// Use the s::non_blocking parameters so the http_serve will run in a separate thread.
auto ctx = http_serve(my_api, 12344, s::non_blocking);
http_serve(my_api, 12344, s::non_blocking);
// Use li::http_client to test your API.
auto r = http_get("http://localhost:12344/hello_world", s::get_parameters = mmm(s::name = "John")));
Expand Down
3 changes: 2 additions & 1 deletion libraries/http_backend/examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ find_package(MYSQL REQUIRED)
find_package(SQLite3 REQUIRED)
find_package(CURL REQUIRED)
find_package(microhttpd REQUIRED)
find_package(Threads REQUIRED)

include_directories(${SQLite3_INCLUDE_DIRS} ${CURL_INCLUDE_DIRS} ${MICROHTTPD_INCLUDE_DIRS} ${MYSQL_INCLUDE_DIR})

Expand All @@ -21,7 +22,7 @@ if (OPENSSL_FOUND)
include_directories(${OPENSSL_INCLUDE_DIR})
endif (OPENSSL_FOUND)

set(LIBS ${MICROHTTPD_LIBRARIES} ${SQLite3_LIBRARIES} ${CURL_LIBRARIES} ${MYSQL_LIBRARY} ${Boost_LIBRARIES})
set(LIBS ${MICROHTTPD_LIBRARIES} ${SQLite3_LIBRARIES} ${CURL_LIBRARIES} ${MYSQL_LIBRARY} ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
message(${Boost_LIBRARIES})
li_add_executable(blog blog.cc)
target_link_libraries(blog ${LIBS})
4 changes: 2 additions & 2 deletions libraries/http_backend/http_backend/api.hh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ template <typename Req, typename Resp> struct api {
H& put(std::string_view r) { return this->operator()(PUT, r); }
H& delete_(std::string_view r) { return this->operator()(HTTP_DELETE, r); }

int parse_verb(std::string_view method) {
int parse_verb(std::string_view method) const {
if (method == "GET")
return GET;
if (method == "PUT")
Expand All @@ -76,7 +76,7 @@ template <typename Req, typename Resp> struct api {
routes_map_.for_all_routes([this](auto r, auto h) { std::cout << r << '\n'; });
std::cout << std::endl;
}
auto call(const char* method, std::string route, Req& request, Resp& response) {
auto call(std::string_view method, std::string_view route, Req& request, Resp& response) const {
// skip the last / of the url.
std::string_view route2(route);
if (route2.size() != 0 and route2[route2.size() - 1] == '/')
Expand Down
27 changes: 18 additions & 9 deletions libraries/http_backend/http_backend/dynamic_routing_table.hh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <unordered_map>
#include <map>
#include <string_view>
#include <memory>
Expand All @@ -13,7 +14,7 @@ template <typename V> struct drt_node {

drt_node() : v_{0, nullptr} {}
struct iterator {
drt_node<V>* ptr;
const drt_node<V>* ptr;
std::string_view first;
V second;

Expand All @@ -35,7 +36,15 @@ template <typename V> struct drt_node {
c++;
std::string_view k = r.substr(s, c - s);

auto& v_ = children_[k].find_or_create(r, c);
auto it = children_.find(k);
if (it != children_.end())
return children_[k]->find_or_create(r, c);
else
{
auto new_node = new drt_node();
children_.insert({k, new_node});
return new_node->find_or_create(r, c);
}

return v_;
}
Expand All @@ -47,10 +56,10 @@ template <typename V> struct drt_node {
if (prefix.size() && prefix.back() != '/')
prefix += '/';
for (auto pair : children_)
pair.second.for_all_routes(f, prefix + std::string(pair.first));
pair.second->for_all_routes(f, prefix + std::string(pair.first));
}
}
iterator find(const std::string_view& r, unsigned int c) {
iterator find(const std::string_view& r, unsigned int c) const {
// We found the route r.
if ((c == r.size() and v_.handler != nullptr) or (children_.size() == 0))
return iterator{this, r, v_};
Expand All @@ -73,8 +82,8 @@ template <typename V> struct drt_node {
// look for k in the children.
auto it = children_.find(k);
if (it != children_.end()) {
auto it2 = it->second.find(r, c); // search in the corresponding child.
if (it2 != it->second.end())
auto it2 = it->second->find(r, c); // search in the corresponding child.
if (it2 != it->second->end())
return it2;
}

Expand All @@ -84,14 +93,14 @@ template <typename V> struct drt_node {
auto name = kv.first;
if (name.size() > 4 and name[0] == '{' and name[1] == '{' and
name[name.size() - 2] == '}' and name[name.size() - 1] == '}')
return kv.second.find(r, c);
return kv.second->find(r, c);
}
return end();
}
}

V v_;
std::map<std::string_view, drt_node> children_;
std::unordered_map<std::string_view, drt_node*> children_;
};
} // namespace internal

Expand All @@ -110,7 +119,7 @@ template <typename V> struct dynamic_routing_table {
}

// Find a route and return an iterator.
auto find(const std::string_view& r) { return root.find(r, 0); }
auto find(const std::string_view& r) const { return root.find(r, 0); }

template <typename F> void for_all_routes(F f) const { root.for_all_routes(f); }
auto end() const { return root.end(); }
Expand Down
3 changes: 2 additions & 1 deletion libraries/http_backend/http_backend/http_backend.hh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <li/sql/sql_orm.hh>

#include <li/http_backend/api.hh>
#include <li/http_backend/http_listen2.hh>
#include <li/http_backend/request.hh>
#include <li/http_backend/response.hh>

Expand All @@ -14,7 +15,7 @@ using http_api = api<http_request, http_response>;

#include <li/http_backend/hashmap_http_session.hh>
#include <li/http_backend/http_authentication.hh>
#include <li/http_backend/mhd.hh>
//#include <li/http_backend/mhd.hh>
#include <li/http_backend/serve_directory.hh>
#include <li/http_backend/sql_crud_api.hh>
#include <li/http_backend/sql_http_session.hh>
Expand Down
Loading

0 comments on commit b312a1d

Please sign in to comment.