diff options
author | ipknHama <ipknhama@gmail.com> | 2014-08-07 01:18:33 +0900 |
---|---|---|
committer | ipknHama <ipknhama@gmail.com> | 2014-08-07 01:18:33 +0900 |
commit | 031615ac866cc3c8f1900dd4b4aae2106ad31230 (patch) | |
tree | b8b7206ffbd2043368580ec269c97436929fe452 | |
parent | a0c93f5b84cc11b30bc6320ac26127832ef8bf7a (diff) | |
download | crow-031615ac866cc3c8f1900dd4b4aae2106ad31230.tar.gz crow-031615ac866cc3c8f1900dd4b4aae2106ad31230.zip |
source resturcturing + CMake
-rw-r--r-- | CMakeLists.txt | 23 | ||||
-rw-r--r-- | Makefile | 42 | ||||
-rw-r--r-- | examples/CMakeLists.txt | 21 | ||||
-rw-r--r-- | examples/example.cpp (renamed from example.cpp) | 0 | ||||
-rw-r--r-- | examples/example.py (renamed from example.py) | 0 | ||||
-rw-r--r-- | examples/example_chat.cpp (renamed from example_chat.cpp) | 28 | ||||
-rw-r--r-- | examples/example_chat.html (renamed from example_chat.html) | 8 | ||||
-rw-r--r-- | include/common.h (renamed from common.h) | 0 | ||||
-rw-r--r-- | include/crow.h (renamed from crow.h) | 0 | ||||
-rw-r--r-- | include/datetime.h (renamed from datetime.h) | 0 | ||||
-rw-r--r-- | include/http_connection.h (renamed from http_connection.h) | 1 | ||||
-rw-r--r-- | include/http_request.h (renamed from http_request.h) | 0 | ||||
-rw-r--r-- | include/http_response.h (renamed from http_response.h) | 7 | ||||
-rw-r--r-- | include/http_server.h (renamed from http_server.h) | 0 | ||||
-rw-r--r-- | include/json.h (renamed from json.h) | 42 | ||||
-rw-r--r-- | include/logging.h (renamed from logging.h) | 0 | ||||
-rw-r--r-- | include/mustache.h (renamed from mustache.h) | 0 | ||||
-rw-r--r-- | include/parser.h (renamed from parser.h) | 0 | ||||
-rw-r--r-- | include/routing.h (renamed from routing.h) | 0 | ||||
-rw-r--r-- | include/settings.h (renamed from settings.h) | 0 | ||||
-rw-r--r-- | include/utility.h (renamed from utility.h) | 0 | ||||
-rw-r--r-- | src/CMakeLists.txt | 18 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 17 | ||||
-rw-r--r-- | tests/template/Makefile (renamed from template_test/Makefile) | 0 | ||||
-rw-r--r-- | tests/template/README.template_test (renamed from template_test/README.template_test) | 0 | ||||
-rw-r--r-- | tests/template/comments.json (renamed from template_test/comments.json) | 0 | ||||
-rw-r--r-- | tests/template/comments.yml (renamed from template_test/comments.yml) | 0 | ||||
-rw-r--r-- | tests/template/delimiters.json (renamed from template_test/delimiters.json) | 0 | ||||
-rw-r--r-- | tests/template/delimiters.yml (renamed from template_test/delimiters.yml) | 0 | ||||
-rw-r--r-- | tests/template/interpolation.json (renamed from template_test/interpolation.json) | 0 | ||||
-rw-r--r-- | tests/template/interpolation.yml (renamed from template_test/interpolation.yml) | 0 | ||||
-rw-r--r-- | tests/template/inverted.json (renamed from template_test/inverted.json) | 0 | ||||
-rw-r--r-- | tests/template/inverted.yml (renamed from template_test/inverted.yml) | 0 | ||||
-rw-r--r-- | tests/template/mustachetest.cc (renamed from template_test/mustachetest.cc) | 0 | ||||
-rw-r--r-- | tests/template/partials.json (renamed from template_test/partials.json) | 0 | ||||
-rw-r--r-- | tests/template/partials.yml (renamed from template_test/partials.yml) | 0 | ||||
-rw-r--r-- | tests/template/sections.json (renamed from template_test/sections.json) | 0 | ||||
-rw-r--r-- | tests/template/sections.yml (renamed from template_test/sections.yml) | 0 | ||||
-rwxr-xr-x | tests/template/test.py (renamed from template_test/test.py) | 0 | ||||
-rw-r--r-- | tests/template/~lambdas.json (renamed from template_test/~lambdas.json) | 0 | ||||
-rw-r--r-- | tests/template/~lambdas.yml (renamed from template_test/~lambdas.yml) | 0 | ||||
-rw-r--r-- | tests/test.py (renamed from test.py) | 0 | ||||
-rw-r--r-- | tests/unittest.cpp (renamed from unittest.cpp) | 0 |
43 files changed, 151 insertions, 56 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..9fd219f --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 2.8) +project (crow_all) + +find_package( Boost 1.40 COMPONENTS date_time filesystem system thread REQUIRED ) +#find_package( Tcmalloc ) +include_directories( ${Boost_INCLUDE_DIR} ) + +#set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build) + +#set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}) +#set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}) + +set(PROJECT_INCLUDE_DIR +${PROJECT_SOURCE_DIR}/include +${PROJECT_SOURCE_DIR}/http-parser +) + +include_directories("${PROJECT_INCLUDE_DIR}") +include_directories("${PROJECT_SOURCE_DIR}") + +add_subdirectory(src) +add_subdirectory(tests) +add_subdirectory(examples) diff --git a/Makefile b/Makefile deleted file mode 100644 index 1927722..0000000 --- a/Makefile +++ /dev/null @@ -1,42 +0,0 @@ -PLATFORM ?= $(shell sh -c 'uname -s | tr "[A-Z]" "[a-z]"') - -ifeq ($(PLATFORM),darwin) -FLAGS_BOOST_THREAD = -lboost_thread-mt -else -FLAGS_BOOST_THREAD = -lboost_thread -FLAGS_DEBUG = -g -FLAGS_GCOV = -r -endif - -binaries=covtest example example_chat - -all: covtest example example_chat - -example_chat: example_chat.cpp settings.h crow.h http_server.h http_connection.h parser.h http_response.h routing.h common.h utility.h json.h datetime.h logging.h mustache.h - ${CXX} -Wall $(FLAGS_DEBUG) -std=c++1y -o example_chat example_chat.cpp http-parser/http_parser.c -pthread -lboost_system $(FLAGS_BOOST_THREAD) -I http-parser/ - - -example: example.cpp settings.h crow.h http_server.h http_connection.h parser.h http_response.h routing.h common.h utility.h json.h datetime.h logging.h mustache.h - ${CXX} -Wall $(FLAGS_DEBUG) -O3 -std=c++1y -o example example.cpp http-parser/http_parser.c -pthread -lboost_system $(FLAGS_BOOST_THREAD) -ltcmalloc_minimal -I http-parser/ - -test: covtest - -runtest: example - pkill example || exit 0 - ./example & - python test.py || exit 0 - pkill example - -unittest: unittest.cpp routing.h utility.h crow.h http_server.h http_connection.h parser.h http_response.h common.h json.h datetime.h logging.h - ${CXX} -Wall $(FLAGS_DEBUG) -std=c++1y -o unittest unittest.cpp http-parser/http_parser.c -pthread -lboost_system $(FLAGS_BOOST_THREAD) -I http-parser/ - ./unittest - -covtest: unittest.cpp routing.h utility.h crow.h http_server.h http_connection.h parser.h http_response.h common.h json.h datetime.h logging.h mustache.h - ${CXX} -Wall $(FLAGS_DEBUG) -std=c++1y -o covtest --coverage unittest.cpp http-parser/http_parser.c -pthread -lboost_system $(FLAGS_BOOST_THREAD) -I http-parser/ - ./covtest - gcov $(FLAGS_GCOV) unittest.cpp - -.PHONY: clean - -clean: - rm -f $(binaries) *.o diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..7313adf --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,21 @@ +cmake_minimum_required(VERSION 2.8) +project (crow_examples) + +add_executable(example example.cpp) +target_link_libraries(example crow) +target_link_libraries(example ${Boost_LIBRARIES} ) +set_target_properties(example PROPERTIES COMPILE_FLAGS "-Wall -std=c++1y") + +add_executable(example_chat example_chat.cpp) +target_link_libraries(example_chat crow) +target_link_libraries(example_chat ${Boost_LIBRARIES} ) +set_target_properties(example_chat PROPERTIES COMPILE_FLAGS "-Wall -std=c++1y") +message(${CMAKE_CURRENT_BINARY_DIR}) +message(${PROJECT_SOURCE_DIR}) +add_custom_command(OUTPUT example_chat.html + #TARGET example_chat + COMMAND ${CMAKE_COMMAND} -E + copy ${PROJECT_SOURCE_DIR}/example_chat.html ${CMAKE_CURRENT_BINARY_DIR}/example_chat.html + DEPENDS ${PROJECT_SOURCE_DIR}/example_chat.html + ) +add_custom_target(example_chat_copy ALL DEPENDS example_chat.html) diff --git a/example.cpp b/examples/example.cpp index f21a7d6..f21a7d6 100644 --- a/example.cpp +++ b/examples/example.cpp diff --git a/example.py b/examples/example.py index 1fd8fcd..1fd8fcd 100644 --- a/example.py +++ b/examples/example.py diff --git a/example_chat.cpp b/examples/example_chat.cpp index 994269e..b12926a 100644 --- a/example_chat.cpp +++ b/examples/example_chat.cpp @@ -3,11 +3,12 @@ #include "mustache.h" #include <string> #include <vector> +#include <chrono> using namespace std; vector<string> msgs; -vector<crow::response*> ress; +vector<pair<crow::response*, decltype(chrono::steady_clock::now())>> ress; void broadcast(const string& msg) { @@ -16,9 +17,10 @@ void broadcast(const string& msg) x["msgs"][0] = msgs.back(); x["last"] = msgs.size(); string body = crow::json::dump(x); - for(auto* res:ress) + for(auto p:ress) { - CROW_LOG_DEBUG << res->p << " replied: " << body; + auto* res = p.first; + CROW_LOG_DEBUG << res << " replied: " << body; res->end(body); } ress.clear(); @@ -39,8 +41,9 @@ int main() ([]{ CROW_LOG_INFO << "logs requested"; crow::json::wvalue x; - for(int i = max(0, (int)msgs.size()-100); i < (int)msgs.size(); i++) - x["msgs"][i] = msgs[i]; + int start = max(0, (int)msgs.size()-100); + for(int i = start; i < (int)msgs.size(); i++) + x["msgs"][i-start] = msgs[i]; x["last"] = msgs.size(); CROW_LOG_INFO << "logs completed"; return x; @@ -61,8 +64,17 @@ int main() } else { - ress.push_back(&res); - CROW_LOG_DEBUG << res.p << " stored"; + vector<pair<crow::response*, decltype(chrono::steady_clock::now())>> filtered; + for(auto p : ress) + { + if (p.first->is_alive() && chrono::steady_clock::now() - p.second < chrono::seconds(30)) + filtered.push_back(p); + else + p.first->end(); + } + ress.swap(filtered); + ress.push_back({&res, chrono::steady_clock::now()}); + CROW_LOG_DEBUG << &res << " stored " << ress.size(); } }); @@ -74,7 +86,7 @@ int main() return ""; }); - app.port(18080) + app.port(40080) //.multithreaded() .run(); } diff --git a/example_chat.html b/examples/example_chat.html index 0fe35dd..233e093 100644 --- a/example_chat.html +++ b/examples/example_chat.html @@ -30,9 +30,15 @@ $(document).ready(function(){ var lastLog = data.last*1; console.log("lastLog: " + lastLog); var s = ""; + function htmlEncode(s) + { + return s.replace(/&(?!\w+([;\s]|$))/g, "&") + .replace(/</g, "<").replace(/>/g, ">"); + } for(var x in data.msgs) { - s += data.msgs[x] + "<BR>"; + + s = htmlEncode(data.msgs[x]) + "<BR>" + s; } $("#logs").html(s+$("#logs").html()); var failFunction; diff --git a/common.h b/include/common.h index 048a7fc..048a7fc 100644 --- a/common.h +++ b/include/common.h diff --git a/datetime.h b/include/datetime.h index 0a47379..0a47379 100644 --- a/datetime.h +++ b/include/datetime.h diff --git a/http_connection.h b/include/http_connection.h index 0f23ab7..d73398e 100644 --- a/http_connection.h +++ b/include/http_connection.h @@ -98,6 +98,7 @@ namespace crow deadline_.cancel(); auto self = this->shared_from_this(); res.complete_request_handler_ = [self]{ self->complete_request(); }; + res.is_alive_helper_ = [this]()->bool{ return socket_.is_open(); }; handler_->handle(req, res); } else diff --git a/http_request.h b/include/http_request.h index 83b6059..83b6059 100644 --- a/http_request.h +++ b/include/http_request.h diff --git a/http_response.h b/include/http_response.h index 1a4cbd3..ae92543 100644 --- a/http_response.h +++ b/include/http_response.h @@ -73,9 +73,14 @@ namespace crow end(); } - void* p; + bool is_alive() + { + return is_alive_helper_ && is_alive_helper_(); + } + private: bool completed_{}; std::function<void()> complete_request_handler_; + std::function<bool()> is_alive_helper_; }; } diff --git a/http_server.h b/include/http_server.h index 9381fdb..9381fdb 100644 --- a/http_server.h +++ b/include/http_server.h @@ -29,10 +29,45 @@ namespace crow namespace json { + void escape(const std::string& str, std::string& ret) + { + ret.reserve(ret.size() + str.size()+str.size()/4); + for(char c:str) + { + switch(c) + { + case '"': ret += "\\\""; break; + case '\\': ret += "\\\\"; break; + case '\n': ret += "\\n"; break; + case '\b': ret += "\\b"; break; + case '\f': ret += "\\f"; break; + case '\r': ret += "\\r"; break; + case '\t': ret += "\\t"; break; + default: + if (0 <= c && c < 0x20) + { + ret += "\\u00"; + auto to_hex = [](char c) + { + c = c&0xf; + if (c < 10) + return '0' + c; + return 'a'+c-10; + }; + ret += to_hex(c/16); + ret += to_hex(c%16); + } + else + ret += c; + break; + } + } + } std::string escape(const std::string& str) { - // TODO - return str; + std::string ret; + escape(str, ret); + return ret; } enum class type : char @@ -1265,9 +1300,8 @@ namespace crow void dump_string(const std::string& str, std::string& out) { - // TODO escaping out.push_back('"'); - out += str; + escape(str, out); out.push_back('"'); } void dump_internal(const wvalue& v, std::string& out) diff --git a/logging.h b/include/logging.h index b43a969..b43a969 100644 --- a/logging.h +++ b/include/logging.h diff --git a/mustache.h b/include/mustache.h index 7218ae8..7218ae8 100644 --- a/mustache.h +++ b/include/mustache.h diff --git a/parser.h b/include/parser.h index f504248..f504248 100644 --- a/parser.h +++ b/include/parser.h diff --git a/routing.h b/include/routing.h index 28700c1..28700c1 100644 --- a/routing.h +++ b/include/routing.h diff --git a/settings.h b/include/settings.h index 563fb1b..563fb1b 100644 --- a/settings.h +++ b/include/settings.h diff --git a/utility.h b/include/utility.h index a46d577..a46d577 100644 --- a/utility.h +++ b/include/utility.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..bf74895 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 2.8) +project (crow) + +set(CROW_SRCS +#${PROJECT_SOURCE_DIR}/some.cpp +#${PROJECT_SOURCE_DIR}/someother.cpp +${PROJECT_SOURCE_DIR}/../http-parser/http_parser.c +) + +set_source_files_properties(${PROJECT_SOURCE_DIR}/../http-parser/http_parser.c PROPERTIES LANGUAGE C ) + +include_directories("${PROJECT_BINARY_DIR}") +include_directories("${PROJECT_INCLUDE_DIR}") + + +add_library(${PROJECT_NAME} SHARED ${CROW_SRCS}) +#target_link_libraries(${PROJECT_NAME} tcmalloc) +set_target_properties(${PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-Wall -std=c++1y") diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..45943ed --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,17 @@ +cmake_minimum_required(VERSION 2.8) +project (crow_test) + + +set(PROJECT_INCLUDE_DIR +${PROJECT_SOURCE_DIR}/include +${PROJECT_SOURCE_DIR}/http-parser +) + +set(TEST_SRCS +unittest.cpp +) + +add_executable(test ${TEST_SRCS}) +target_link_libraries(test crow) +target_link_libraries( test ${Boost_LIBRARIES} ) +set_target_properties(test PROPERTIES COMPILE_FLAGS "-Wall -std=c++1y") diff --git a/template_test/Makefile b/tests/template/Makefile index ad0d49b..ad0d49b 100644 --- a/template_test/Makefile +++ b/tests/template/Makefile diff --git a/template_test/README.template_test b/tests/template/README.template_test index 6307171..6307171 100644 --- a/template_test/README.template_test +++ b/tests/template/README.template_test diff --git a/template_test/comments.json b/tests/template/comments.json index 30cb927..30cb927 100644 --- a/template_test/comments.json +++ b/tests/template/comments.json diff --git a/template_test/comments.yml b/tests/template/comments.yml index 7b14c7f..7b14c7f 100644 --- a/template_test/comments.yml +++ b/tests/template/comments.yml diff --git a/template_test/delimiters.json b/tests/template/delimiters.json index fcf9588..fcf9588 100644 --- a/template_test/delimiters.json +++ b/tests/template/delimiters.json diff --git a/template_test/delimiters.yml b/tests/template/delimiters.yml index ce80b17..ce80b17 100644 --- a/template_test/delimiters.yml +++ b/tests/template/delimiters.yml diff --git a/template_test/interpolation.json b/tests/template/interpolation.json index d1a1a32..d1a1a32 100644 --- a/template_test/interpolation.json +++ b/tests/template/interpolation.json diff --git a/template_test/interpolation.yml b/tests/template/interpolation.yml index 1b6fff1..1b6fff1 100644 --- a/template_test/interpolation.yml +++ b/tests/template/interpolation.yml diff --git a/template_test/inverted.json b/tests/template/inverted.json index c9b550b..c9b550b 100644 --- a/template_test/inverted.json +++ b/tests/template/inverted.json diff --git a/template_test/inverted.yml b/tests/template/inverted.yml index 5f8e2b2..5f8e2b2 100644 --- a/template_test/inverted.yml +++ b/tests/template/inverted.yml diff --git a/template_test/mustachetest.cc b/tests/template/mustachetest.cc index c4ac2c9..c4ac2c9 100644 --- a/template_test/mustachetest.cc +++ b/tests/template/mustachetest.cc diff --git a/template_test/partials.json b/tests/template/partials.json index e5f21a2..e5f21a2 100644 --- a/template_test/partials.json +++ b/tests/template/partials.json diff --git a/template_test/partials.yml b/tests/template/partials.yml index 8c41543..8c41543 100644 --- a/template_test/partials.yml +++ b/tests/template/partials.yml diff --git a/template_test/sections.json b/tests/template/sections.json index b0aa352..b0aa352 100644 --- a/template_test/sections.json +++ b/tests/template/sections.json diff --git a/template_test/sections.yml b/tests/template/sections.yml index f62d9cb..f62d9cb 100644 --- a/template_test/sections.yml +++ b/tests/template/sections.yml diff --git a/template_test/test.py b/tests/template/test.py index 22fcca0..22fcca0 100755 --- a/template_test/test.py +++ b/tests/template/test.py diff --git a/template_test/~lambdas.json b/tests/template/~lambdas.json index 3c58bf8..3c58bf8 100644 --- a/template_test/~lambdas.json +++ b/tests/template/~lambdas.json diff --git a/template_test/~lambdas.yml b/tests/template/~lambdas.yml index b9fb4d0..b9fb4d0 100644 --- a/template_test/~lambdas.yml +++ b/tests/template/~lambdas.yml diff --git a/unittest.cpp b/tests/unittest.cpp index 918d027..918d027 100644 --- a/unittest.cpp +++ b/tests/unittest.cpp |