Format code
This commit is contained in:
parent
260422b7d7
commit
58b2814fda
@ -44,14 +44,10 @@ int main(void) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Run servers
|
// Run servers
|
||||||
auto httpThread = std::thread([&]() {
|
auto httpThread = std::thread([&]() { http.listen("localhost", 8080); });
|
||||||
http.listen("localhost", 8080);
|
|
||||||
});
|
|
||||||
|
|
||||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||||
auto httpsThread = std::thread([&]() {
|
auto httpsThread = std::thread([&]() { https.listen("localhost", 8081); });
|
||||||
https.listen("localhost", 8081);
|
|
||||||
});
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
httpThread.join();
|
httpThread.join();
|
||||||
|
@ -5,9 +5,9 @@
|
|||||||
// MIT License
|
// MIT License
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
#include <httplib.h>
|
#include <httplib.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
|
||||||
using namespace httplib;
|
using namespace httplib;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@ -37,7 +37,8 @@ int main(void) {
|
|||||||
|
|
||||||
svr.Post("/post", [](const Request &req, Response &res) {
|
svr.Post("/post", [](const Request &req, Response &res) {
|
||||||
auto file = req.get_file_value("file");
|
auto file = req.get_file_value("file");
|
||||||
cout << "file length: " << file.content.length() << ":" << file.filename << endl;
|
cout << "file length: " << file.content.length() << ":" << file.filename
|
||||||
|
<< endl;
|
||||||
|
|
||||||
ofstream ofs(file.filename, ios::binary);
|
ofstream ofs(file.filename, ios::binary);
|
||||||
ofs << file.content;
|
ofs << file.content;
|
||||||
|
25
test/test.cc
25
test/test.cc
@ -30,10 +30,11 @@ const std::string JSON_DATA = "{\"hello\":\"world\"}";
|
|||||||
|
|
||||||
const string LARGE_DATA = string(1024 * 1024 * 100, '@'); // 100MB
|
const string LARGE_DATA = string(1024 * 1024 * 100, '@'); // 100MB
|
||||||
|
|
||||||
MultipartFormData& get_file_value(MultipartFormDataItems &files, const char *key) {
|
MultipartFormData &get_file_value(MultipartFormDataItems &files,
|
||||||
auto it = std::find_if(files.begin(), files.end(), [&](const MultipartFormData &file) {
|
const char *key) {
|
||||||
return file.name == key;
|
auto it = std::find_if(
|
||||||
});
|
files.begin(), files.end(),
|
||||||
|
[&](const MultipartFormData &file) { return file.name == key; });
|
||||||
if (it != files.end()) { return *it; }
|
if (it != files.end()) { return *it; }
|
||||||
throw std::runtime_error("invalid mulitpart form data name error");
|
throw std::runtime_error("invalid mulitpart form data name error");
|
||||||
}
|
}
|
||||||
@ -801,7 +802,8 @@ protected:
|
|||||||
EXPECT_EQ("5", req.get_header_value("Content-Length"));
|
EXPECT_EQ("5", req.get_header_value("Content-Length"));
|
||||||
})
|
})
|
||||||
.Post("/content_receiver",
|
.Post("/content_receiver",
|
||||||
[&](const Request & req, Response &res, const ContentReader &content_reader) {
|
[&](const Request &req, Response &res,
|
||||||
|
const ContentReader &content_reader) {
|
||||||
if (req.is_multipart_form_data()) {
|
if (req.is_multipart_form_data()) {
|
||||||
MultipartFormDataItems files;
|
MultipartFormDataItems files;
|
||||||
content_reader(
|
content_reader(
|
||||||
@ -1822,8 +1824,8 @@ static bool send_request(time_t read_timeout_sec, const std::string& req) {
|
|||||||
|
|
||||||
return detail::process_and_close_socket(
|
return detail::process_and_close_socket(
|
||||||
true, client_sock, 1, read_timeout_sec, 0,
|
true, client_sock, 1, read_timeout_sec, 0,
|
||||||
[&](Stream& strm, bool /*last_connection*/,
|
[&](Stream &strm, bool /*last_connection*/, bool &
|
||||||
bool &/*connection_close*/) -> bool {
|
/*connection_close*/) -> bool {
|
||||||
if (req.size() !=
|
if (req.size() !=
|
||||||
static_cast<size_t>(strm.write(req.data(), req.size()))) {
|
static_cast<size_t>(strm.write(req.data(), req.size()))) {
|
||||||
return false;
|
return false;
|
||||||
@ -1840,8 +1842,7 @@ static bool send_request(time_t read_timeout_sec, const std::string& req) {
|
|||||||
TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) {
|
TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) {
|
||||||
Server svr;
|
Server svr;
|
||||||
std::string header_value;
|
std::string header_value;
|
||||||
svr.Get("/validate-ws-in-headers",
|
svr.Get("/validate-ws-in-headers", [&](const Request &req, Response &res) {
|
||||||
[&](const Request &req, Response &res) {
|
|
||||||
header_value = req.get_header_value("foo");
|
header_value = req.get_header_value("foo");
|
||||||
res.set_content("ok", "text/plain");
|
res.set_content("ok", "text/plain");
|
||||||
});
|
});
|
||||||
@ -1853,8 +1854,7 @@ TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) {
|
|||||||
|
|
||||||
// Only space and horizontal tab are whitespace. Make sure other whitespace-
|
// Only space and horizontal tab are whitespace. Make sure other whitespace-
|
||||||
// like characters are not treated the same - use vertical tab and escape.
|
// like characters are not treated the same - use vertical tab and escape.
|
||||||
const std::string req =
|
const std::string req = "GET /validate-ws-in-headers HTTP/1.1\r\n"
|
||||||
"GET /validate-ws-in-headers HTTP/1.1\r\n"
|
|
||||||
"foo: \t \v bar \e\t \r\n"
|
"foo: \t \v bar \e\t \r\n"
|
||||||
"Connection: close\r\n"
|
"Connection: close\r\n"
|
||||||
"\r\n";
|
"\r\n";
|
||||||
@ -1867,8 +1867,7 @@ TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) {
|
|||||||
|
|
||||||
TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity) {
|
TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity) {
|
||||||
Server svr;
|
Server svr;
|
||||||
svr.Get("/hi",
|
svr.Get("/hi", [&](const Request & /*req*/, Response &res) {
|
||||||
[&](const Request & /*req*/, Response &res) {
|
|
||||||
res.set_content("ok", "text/plain");
|
res.set_content("ok", "text/plain");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user