Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
yhirose committed Dec 15, 2019
1 parent 260422b commit 58b2814
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 46 deletions.
8 changes: 2 additions & 6 deletions example/redirect.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,10 @@ int main(void) {
#endif

// Run servers
auto httpThread = std::thread([&]() {
http.listen("localhost", 8080);
});
auto httpThread = std::thread([&]() { http.listen("localhost", 8080); });

#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
auto httpsThread = std::thread([&]() {
https.listen("localhost", 8081);
});
auto httpsThread = std::thread([&]() { https.listen("localhost", 8081); });
#endif

httpThread.join();
Expand Down
9 changes: 5 additions & 4 deletions example/upload.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
// MIT License
//

#include <fstream>
#include <httplib.h>
#include <iostream>
#include <fstream>
using namespace httplib;
using namespace std;

const char* html = R"(
const char *html = R"(
<form id="formElem">
<input type="file" name="file" accept="image/*">
<input type="submit">
Expand All @@ -35,9 +35,10 @@ int main(void) {
res.set_content(html, "text/html");
});

svr.Post("/post", [](const Request & req, Response &res) {
svr.Post("/post", [](const Request &req, Response &res) {
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);
ofs << file.content;
Expand Down
71 changes: 35 additions & 36 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ const std::string JSON_DATA = "{\"hello\":\"world\"}";

const string LARGE_DATA = string(1024 * 1024 * 100, '@'); // 100MB

MultipartFormData& get_file_value(MultipartFormDataItems &files, const char *key) {
auto it = std::find_if(files.begin(), files.end(), [&](const MultipartFormData &file) {
return file.name == key;
});
MultipartFormData &get_file_value(MultipartFormDataItems &files,
const char *key) {
auto it = std::find_if(
files.begin(), files.end(),
[&](const MultipartFormData &file) { return file.name == key; });
if (it != files.end()) { return *it; }
throw std::runtime_error("invalid mulitpart form data name error");
}
Expand Down Expand Up @@ -496,15 +497,15 @@ TEST(DigestAuthTest, FromHTTPWatch) {

{
std::vector<std::string> paths = {
"/digest-auth/auth/hello/world/MD5",
"/digest-auth/auth/hello/world/SHA-256",
"/digest-auth/auth/hello/world/SHA-512",
"/digest-auth/auth-init/hello/world/MD5",
"/digest-auth/auth-int/hello/world/MD5",
"/digest-auth/auth/hello/world/MD5",
"/digest-auth/auth/hello/world/SHA-256",
"/digest-auth/auth/hello/world/SHA-512",
"/digest-auth/auth-init/hello/world/MD5",
"/digest-auth/auth-int/hello/world/MD5",
};

cli.set_auth("hello", "world");
for (auto path: paths) {
for (auto path : paths) {
auto res = cli.Get(path.c_str());
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(res->body,
Expand Down Expand Up @@ -801,18 +802,19 @@ class ServerTest : public ::testing::Test {
EXPECT_EQ("5", req.get_header_value("Content-Length"));
})
.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()) {
MultipartFormDataItems files;
content_reader(
[&](const MultipartFormData &file) {
files.push_back(file);
return true;
},
[&](const char *data, size_t data_length) {
files.back().content.append(data, data_length);
return true;
});
[&](const MultipartFormData &file) {
files.push_back(file);
return true;
},
[&](const char *data, size_t data_length) {
files.back().content.append(data, data_length);
return true;
});

EXPECT_EQ(5u, files.size());

Expand Down Expand Up @@ -1814,16 +1816,16 @@ TEST_F(ServerTest, MultipartFormDataGzip) {
#endif

// Sends a raw request to a server listening at HOST:PORT.
static bool send_request(time_t read_timeout_sec, const std::string& req) {
static bool send_request(time_t read_timeout_sec, const std::string &req) {
auto client_sock =
detail::create_client_socket(HOST, PORT, /*timeout_sec=*/5);

if (client_sock == INVALID_SOCKET) { return false; }

return detail::process_and_close_socket(
true, client_sock, 1, read_timeout_sec, 0,
[&](Stream& strm, bool /*last_connection*/,
bool &/*connection_close*/) -> bool {
[&](Stream &strm, bool /*last_connection*/, bool &
/*connection_close*/) -> bool {
if (req.size() !=
static_cast<size_t>(strm.write(req.data(), req.size()))) {
return false;
Expand All @@ -1840,11 +1842,10 @@ static bool send_request(time_t read_timeout_sec, const std::string& req) {
TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) {
Server svr;
std::string header_value;
svr.Get("/validate-ws-in-headers",
[&](const Request &req, Response &res) {
header_value = req.get_header_value("foo");
res.set_content("ok", "text/plain");
});
svr.Get("/validate-ws-in-headers", [&](const Request &req, Response &res) {
header_value = req.get_header_value("foo");
res.set_content("ok", "text/plain");
});

thread t = thread([&] { svr.listen(HOST, PORT); });
while (!svr.is_running()) {
Expand All @@ -1853,11 +1854,10 @@ TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) {

// Only space and horizontal tab are whitespace. Make sure other whitespace-
// like characters are not treated the same - use vertical tab and escape.
const std::string req =
"GET /validate-ws-in-headers HTTP/1.1\r\n"
"foo: \t \v bar \e\t \r\n"
"Connection: close\r\n"
"\r\n";
const std::string req = "GET /validate-ws-in-headers HTTP/1.1\r\n"
"foo: \t \v bar \e\t \r\n"
"Connection: close\r\n"
"\r\n";

ASSERT_TRUE(send_request(5, req));
svr.stop();
Expand All @@ -1867,10 +1867,9 @@ TEST(ServerRequestParsingTest, TrimWhitespaceFromHeaderValues) {

TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity) {
Server svr;
svr.Get("/hi",
[&](const Request & /*req*/, Response &res) {
res.set_content("ok", "text/plain");
});
svr.Get("/hi", [&](const Request & /*req*/, Response &res) {
res.set_content("ok", "text/plain");
});

// Server read timeout must be longer than the client read timeout for the
// bug to reproduce, probably to force the server to process a request
Expand Down

0 comments on commit 58b2814

Please sign in to comment.