diff --git a/bak/encoding.cc b/bak/encoding.cc new file mode 100644 index 0000000..fd5ebae --- /dev/null +++ b/bak/encoding.cc @@ -0,0 +1 @@ +#include "webcc/encoding.h" diff --git a/bak/encoding.h b/bak/encoding.h new file mode 100644 index 0000000..8b3cf96 --- /dev/null +++ b/bak/encoding.h @@ -0,0 +1,21 @@ +#ifndef WEBCC_ENCODING_H_ +#define WEBCC_ENCODING_H_ + +#include +#include + +namespace webcc { + +std::string Utf16ToUtf8(const std::wstring& utf16_string) { + std::wstring_convert> converter; + return converter.to_bytes(utf16_string); +} + +std::wstring Utf8ToUtf16(const std::string& utf8_string) { + std::wstring_convert> converter; + return converter.from_bytes(utf8_string); +} + +} // namespace webcc + +#endif // WEBCC_ENCODING_H_ diff --git a/bak/endpoint.cc b/bak/endpoint.cc new file mode 100644 index 0000000..16ce664 --- /dev/null +++ b/bak/endpoint.cc @@ -0,0 +1,41 @@ + +#include "boost/asio/ip/tcp.hpp" + + +using Endpoint = boost::asio::ip::tcp::endpoint; +using Endpoints = boost::asio::ip::tcp::resolver::results_type; + +void PrintEndpoint(std::ostream& ostream, const Endpoint& endpoint); + +void PrintEndpoints(std::ostream& ostream, const Endpoints& endpoints); + +std::string EndpointToString(const Endpoint& endpoint); + + + +using tcp = boost::asio::ip::tcp; + +void PrintEndpoint(std::ostream& ostream, const Endpoint& endpoint) { + ostream << endpoint; + if (endpoint.protocol() == tcp::v4()) { + ostream << ", v4"; + } else if (endpoint.protocol() == tcp::v6()) { + ostream << ", v6"; + } +} + +void PrintEndpoints(std::ostream& ostream, const Endpoints& endpoints) { + ostream << "Endpoints: " << endpoints.size() << std::endl; + tcp::resolver::results_type::iterator it = endpoints.begin(); + for (; it != endpoints.end(); ++it) { + ostream << " - "; + PrintEndpoint(ostream, it->endpoint()); + ostream << std::endl; + } +} + +std::string EndpointToString(const Endpoint& endpoint) { + std::stringstream ss; + PrintEndpoint(ss, endpoint); + return ss.str(); +} diff --git a/bak/js_encode_uri_component.js b/bak/js_encode_uri_component.js new file mode 100644 index 0000000..aad7e5c --- /dev/null +++ b/bak/js_encode_uri_component.js @@ -0,0 +1,16 @@ +function fixedEncodeURIComponent(str) { + return encodeURIComponent(str).replace(/[!'()*]/g, function(c) { + return '%' + c.charCodeAt(0).toString(16); + }); +} + +console.log(encodeURIComponent('[!\'()*]')) +console.log(fixedEncodeURIComponent('[!\'()*]')) + +const subDelims = '!$&\\()*+,;='; +console.log(encodeURIComponent(subDelims)) +console.log(fixedEncodeURIComponent(subDelims)) + +const unreserved = 'aAzZ09-._~'; +console.log(encodeURIComponent(unreserved)) +console.log(fixedEncodeURIComponent(unreserved)) diff --git a/bak/service_manager_unittest.cc b/bak/service_manager_unittest.cc new file mode 100644 index 0000000..e519aa3 --- /dev/null +++ b/bak/service_manager_unittest.cc @@ -0,0 +1,77 @@ +#include "gtest/gtest.h" + +#include "webcc/service_manager.h" + +// ----------------------------------------------------------------------------- + +class MyService : public webcc::Service { +public: + webcc::ResponsePtr Handle(webcc::RequestPtr request, + const webcc::UrlArgs& args) override { + return webcc::ResponseBuilder{}.OK()(); + } +}; + +// ----------------------------------------------------------------------------- +/* +TEST(ServiceManagerTest, URL_RegexBasic) { + webcc::ServiceManager service_manager; + + service_manager.Add(std::make_shared(), "/instance/(\\d+)", true); + + std::string url = "/instance/12345"; + webcc::UrlArgs args; + + webcc::ServicePtr service = service_manager.Get(url, &args); + + EXPECT_TRUE(!!service); + + EXPECT_EQ(1, args.size()); + EXPECT_EQ("12345", args[0]); + + url = "/instance/abcde"; + args.clear(); + service = service_manager.Get(url, &args); + + EXPECT_FALSE(!!service); +} + +TEST(RestServiceManagerTest, URL_RegexMultiple) { + webcc::ServiceManager service_manager; + + service_manager.Add(std::make_shared(), + "/study/(\\d+)/series/(\\d+)/instance/(\\d+)", true); + + std::string url = "/study/1/series/2/instance/3"; + webcc::UrlArgs args; + + webcc::ServicePtr service = service_manager.Get(url, &args); + + EXPECT_TRUE(!!service); + + EXPECT_EQ(3, args.size()); + EXPECT_EQ("1", args[0]); + EXPECT_EQ("2", args[1]); + EXPECT_EQ("3", args[2]); + + url = "/study/a/series/b/instance/c"; + args.clear(); + service = service_manager.Get(url, &args); + + EXPECT_FALSE(!!service); +} + +TEST(RestServiceManagerTest, URL_NonRegex) { + webcc::ServiceManager service_manager; + + service_manager.Add(std::make_shared(), "/instances", false); + + std::string url = "/instances"; + webcc::UrlArgs args; + + webcc::ServicePtr service = service_manager.Get(url, &args); + + EXPECT_TRUE(!!service); + EXPECT_TRUE(args.empty()); +} +*/ \ No newline at end of file diff --git a/bak/sleep.cpp b/bak/sleep.cpp new file mode 100644 index 0000000..5056c5d --- /dev/null +++ b/bak/sleep.cpp @@ -0,0 +1,11 @@ +static void Sleep(int seconds) { + if (seconds > 0) { + LOG_INFO("Sleep %d seconds...", seconds); + std::this_thread::sleep_for(std::chrono::seconds(seconds)); + } +} + +std::cout << std::endl; + std::cout << "If |seconds| is provided, the server will sleep, for testing " + << "timeout, before " << std::endl + << "send back each response." << std::endl; \ No newline at end of file diff --git a/data/README.md b/data/README.md index 2f1ac46..6e421e8 100644 --- a/data/README.md +++ b/data/README.md @@ -1,2 +1,2 @@ - -**upload**: files for testing upload. +**upload**: files for testing upload using multipart form data. (`examples/form_client.cc`) +**www**: doc root for testing static file server. (`examples/static_file_server.cc`) diff --git a/data/upload/记住我.txt b/data/upload/记住我.txt new file mode 100644 index 0000000..f563cc7 --- /dev/null +++ b/data/upload/记住我.txt @@ -0,0 +1,17 @@ +Remember +BY CHRISTINA ROSSETTI + +Remember me when I am gone away, + Gone far away into the silent land; + When you can no more hold me by the hand, +Nor I half turn to go yet turning stay. +Remember me when no more day by day + You tell me of our future that you plann'd: + Only remember me; you understand +It will be late to counsel then or pray. +Yet if you should forget me for a while + And afterwards remember, do not grieve: + For if the darkness and corruption leave + A vestige of the thoughts that once I had, +Better by far you should forget and smile + Than that you should remember and be sad. diff --git a/data/www/deer.jpg b/data/www/deer.jpg new file mode 100644 index 0000000..bc6ba92 Binary files /dev/null and b/data/www/deer.jpg differ diff --git a/data/www/index.html b/data/www/index.html new file mode 100644 index 0000000..b45ef6f --- /dev/null +++ b/data/www/index.html @@ -0,0 +1 @@ +Hello, World! \ No newline at end of file diff --git a/webcc/url.h b/webcc/url.h index 57032d1..fa342e3 100644 --- a/webcc/url.h +++ b/webcc/url.h @@ -102,6 +102,8 @@ class UrlQuery { public: using Parameter = std::pair; + UrlQuery() = default; + // The query string should be key-value pairs separated by '&'. explicit UrlQuery(const std::string& encoded_str);