diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/CMakeLists.txt | 14 | ||||
-rw-r--r-- | examples/example.cpp | 26 | ||||
-rw-r--r-- | examples/example_chat.cpp | 4 | ||||
-rw-r--r-- | examples/example_test.py | 18 | ||||
-rw-r--r-- | examples/example_vs.cpp | 1 | ||||
-rw-r--r-- | examples/example_with_all.cpp | 6 | ||||
-rw-r--r-- | examples/websocket/example_ws.cpp | 48 | ||||
-rw-r--r-- | examples/websocket/templates/ws.html | 42 |
8 files changed, 145 insertions, 14 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 22139ad..3c9b0b0 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -11,9 +11,21 @@ add_executable(helloworld helloworld.cpp) target_link_libraries(helloworld ${Boost_LIBRARIES}) target_link_libraries(helloworld ${CMAKE_THREAD_LIBS_INIT}) +if (OPENSSL_FOUND) add_executable(example_ssl ssl/example_ssl.cpp) target_link_libraries(example_ssl ${Boost_LIBRARIES}) -target_link_libraries(example_ssl ${CMAKE_THREAD_LIBS_INIT} ssl crypto) +target_link_libraries(example_ssl ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES}) +endif() + +add_executable(example_websocket websocket/example_ws.cpp) +target_link_libraries(example_websocket ${Boost_LIBRARIES}) +target_link_libraries(example_websocket ${CMAKE_THREAD_LIBS_INIT} ${OPENSSL_LIBRARIES}) +add_custom_command(OUTPUT ws.html + COMMAND ${CMAKE_COMMAND} -E + copy ${PROJECT_SOURCE_DIR}/websocket/templates/ws.html ${CMAKE_CURRENT_BINARY_DIR}/templates/ws.html + DEPENDS ${PROJECT_SOURCE_DIR}/websocket/templates/ws.html + ) +add_custom_target(example_ws_copy ALL DEPENDS ws.html) add_executable(example example.cpp) #target_link_libraries(example crow) diff --git a/examples/example.cpp b/examples/example.cpp index dec57cd..87231a2 100644 --- a/examples/example.cpp +++ b/examples/example.cpp @@ -1,11 +1,10 @@ #include "crow.h" -#include "json.h" #include <sstream> class ExampleLogHandler : public crow::ILogHandler { public: - void log(std::string message, crow::LogLevel level) override { + void log(std::string /*message*/, crow::LogLevel /*level*/) override { // cerr << "ExampleLogHandler -> " << message; } }; @@ -28,12 +27,12 @@ struct ExampleMiddleware { }; - void before_handle(crow::request& req, crow::response& res, context& ctx) + void before_handle(crow::request& /*req*/, crow::response& /*res*/, context& /*ctx*/) { CROW_LOG_DEBUG << " - MESSAGE: " << message; } - void after_handle(crow::request& req, crow::response& res, context& ctx) + void after_handle(crow::request& /*req*/, crow::response& /*res*/, context& /*ctx*/) { // no-op } @@ -85,7 +84,7 @@ int main() // To see it in action submit {ip}:18080/add/1/2 and you should receive 3 (exciting, isn't it) CROW_ROUTE(app,"/add/<int>/<int>") - ([](const crow::request& req, crow::response& res, int a, int b){ + ([](const crow::request& /*req*/, crow::response& res, int a, int b){ std::ostringstream os; os << a+b; res.write(os.str()); @@ -106,6 +105,9 @@ int main() // * Select 'raw' and then JSON // * Add {"a": 1, "b": 1} // * Send and you should receive 2 + + // A simpler way for json example: + // * curl -d '{"a":1,"b":2}' {ip}:18080/add_json CROW_ROUTE(app, "/add_json") .methods("POST"_method) ([](const crow::request& req){ @@ -144,9 +146,23 @@ int main() for(const auto& countVal : count) { os << " - " << countVal << '\n'; } + + // To get a dictionary from the request + // You have to submit something like '/params?mydict[a]=b&mydict[abcd]=42' to have a list of pairs ((a, b) and (abcd, 42)) + auto mydict = req.url_params.get_dict("mydict"); + os << "The key 'dict' contains " << mydict.size() << " value(s).\n"; + for(const auto& mydictVal : mydict) { + os << " - " << mydictVal.first << " -> " << mydictVal.second << '\n'; + } + return crow::response{os.str()}; }); + CROW_ROUTE(app, "/large") + ([]{ + return std::string(512*1024, ' '); + }); + // ignore all log crow::logger::setLogLevel(crow::LogLevel::DEBUG); //crow::logger::setHandler(std::make_shared<ExampleLogHandler>()); diff --git a/examples/example_chat.cpp b/examples/example_chat.cpp index ae031b7..d187f51 100644 --- a/examples/example_chat.cpp +++ b/examples/example_chat.cpp @@ -1,6 +1,4 @@ #include "crow.h" -#include "json.h" -#include "mustache.h" #include <string> #include <vector> #include <chrono> @@ -50,7 +48,7 @@ int main() }); CROW_ROUTE(app, "/logs/<int>") - ([](const crow::request& req, crow::response& res, int after){ + ([](const crow::request& /*req*/, crow::response& res, int after){ CROW_LOG_INFO << "logs with last " << after; if (after < (int)msgs.size()) { diff --git a/examples/example_test.py b/examples/example_test.py index e2bf651..d252df0 100644 --- a/examples/example_test.py +++ b/examples/example_test.py @@ -18,11 +18,27 @@ for i in xrange(10): Host: localhost\r\n\r\n'''); assert 'Hello World!' in s.recv(1024) +# test large +s = socket.socket() +s.connect(('localhost', 18080)) +s.send('''GET /large HTTP/1.1 +Host: localhost\r\nConnection: close\r\n\r\n''') +r = '' +while True: + d = s.recv(1024*1024) + if not d: + break; + r += d + print len(r), len(d) +print len(r), r[:100] +assert len(r) > 512*1024 + # test timeout s = socket.socket() s.connect(('localhost', 18080)) -print 'ERROR REQUEST' +# invalid request, connection will be closed after timeout s.send('''GET / HTTP/1.1 hHhHHefhwjkefhklwejfklwejf ''') print s.recv(1024) + diff --git a/examples/example_vs.cpp b/examples/example_vs.cpp index 95f46b0..dbc4d31 100644 --- a/examples/example_vs.cpp +++ b/examples/example_vs.cpp @@ -1,5 +1,4 @@ #include "crow.h" -#include "json.h" #include <sstream> diff --git a/examples/example_with_all.cpp b/examples/example_with_all.cpp index 55e2715..56ac4cd 100644 --- a/examples/example_with_all.cpp +++ b/examples/example_with_all.cpp @@ -4,7 +4,7 @@ class ExampleLogHandler : public crow::ILogHandler { public: - void log(std::string message, crow::LogLevel level) override { + void log(std::string /*message*/, crow::LogLevel /*level*/) override { // cerr << "ExampleLogHandler -> " << message; } }; @@ -42,7 +42,7 @@ int main() }); CROW_ROUTE(app,"/add/<int>/<int>") - ([](const crow::request& req, crow::response& res, int a, int b){ + ([](const crow::request& /*req*/, crow::response& res, int a, int b){ std::ostringstream os; os << a+b; res.write(os.str()); @@ -85,7 +85,7 @@ int main() }); // ignore all log - crow::logger::setLogLevel(crow::LogLevel::DEBUG); + crow::logger::setLogLevel(crow::LogLevel::Debug); //crow::logger::setHandler(std::make_shared<ExampleLogHandler>()); app.port(18080) diff --git a/examples/websocket/example_ws.cpp b/examples/websocket/example_ws.cpp new file mode 100644 index 0000000..173d062 --- /dev/null +++ b/examples/websocket/example_ws.cpp @@ -0,0 +1,48 @@ +#include "crow.h" +#include <unordered_set> +#include <mutex> + + +int main() +{ + crow::SimpleApp app; + + std::mutex mtx;; + std::unordered_set<crow::websocket::connection*> users; + + CROW_ROUTE(app, "/ws") + .websocket() + .onopen([&](crow::websocket::connection& conn){ + CROW_LOG_INFO << "new websocket connection"; + std::lock_guard<std::mutex> _(mtx); + users.insert(&conn); + }) + .onclose([&](crow::websocket::connection& conn, const std::string& reason){ + CROW_LOG_INFO << "websocket connection closed: " << reason; + std::lock_guard<std::mutex> _(mtx); + users.erase(&conn); + }) + .onmessage([&](crow::websocket::connection& /*conn*/, const std::string& data, bool is_binary){ + std::lock_guard<std::mutex> _(mtx); + for(auto u:users) + if (is_binary) + u->send_binary(data); + else + u->send_text(data); + }); + + CROW_ROUTE(app, "/") + ([]{ + char name[256]; + gethostname(name, 256); + crow::mustache::context x; + x["servername"] = name; + + auto page = crow::mustache::load("ws.html"); + return page.render(x); + }); + + app.port(40080) + .multithreaded() + .run(); +} diff --git a/examples/websocket/templates/ws.html b/examples/websocket/templates/ws.html new file mode 100644 index 0000000..2d38fdf --- /dev/null +++ b/examples/websocket/templates/ws.html @@ -0,0 +1,42 @@ +<!doctype html> +<html> +<head> + <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script> +</head> +<body> + <input id="msg" type="text"></input> + <button id="send"> + Send + </button><BR> + <textarea id="log" cols=100 rows=50> + </textarea> + <script> +var sock = new WebSocket("ws://{{servername}}:40080/ws"); + +sock.onopen = ()=>{ + console.log('open') +} +sock.onerror = (e)=>{ + console.log('error',e) +} +sock.onclose = ()=>{ + console.log('close') +} +sock.onmessage = (e)=>{ + $("#log").val( + e.data +"\n" + $("#log").val()); +} +$("#msg").keypress(function(e){ + if (e.which == 13) + { + sock.send($("#msg").val()); + $("#msg").val(""); + } +}); +$("#send").click(()=>{ + sock.send($("#msg").val()); + $("#msg").val(""); +}); + </script> +</body> +</html> |