Skip to content

Commit 260422b

Browse files
committed
Format code
1 parent d2c7b44 commit 260422b

File tree

3 files changed

+100
-107
lines changed

3 files changed

+100
-107
lines changed

.clang-format

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
BasedOnStyle: LLVM
2+
AllowShortBlocksOnASingleLine: true
3+
AllowShortCaseLabelsOnASingleLine: true
4+
AllowShortIfStatementsOnASingleLine: true
5+
Cpp11BracedListStyle: true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ example/hello
66
example/simplesvr
77
example/benchmark
88
example/redirect
9+
example/upload
910
example/*.pem
1011
test/test
1112
test/test.xcodeproj/xcuser*

httplib.h

Lines changed: 94 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -227,23 +227,23 @@ using MultipartContentHeader =
227227
std::function<bool(const MultipartFormData &file)>;
228228

229229
class ContentReader {
230-
public:
231-
using Reader = std::function<bool(ContentReceiver receiver)>;
232-
using MultipartReader = std::function<bool(MultipartContentHeader header, ContentReceiver receiver)>;
230+
public:
231+
using Reader = std::function<bool(ContentReceiver receiver)>;
232+
using MultipartReader = std::function<bool(MultipartContentHeader header,
233+
ContentReceiver receiver)>;
233234

234-
ContentReader(Reader reader, MultipartReader muitlpart_reader)
235+
ContentReader(Reader reader, MultipartReader muitlpart_reader)
235236
: reader_(reader), muitlpart_reader_(muitlpart_reader) {}
236237

237-
bool operator()(MultipartContentHeader header, ContentReceiver receiver) const {
238-
return muitlpart_reader_(header, receiver);
239-
}
238+
bool operator()(MultipartContentHeader header,
239+
ContentReceiver receiver) const {
240+
return muitlpart_reader_(header, receiver);
241+
}
240242

241-
bool operator()(ContentReceiver receiver) const {
242-
return reader_(receiver);
243-
}
243+
bool operator()(ContentReceiver receiver) const { return reader_(receiver); }
244244

245-
Reader reader_;
246-
MultipartReader muitlpart_reader_;
245+
Reader reader_;
246+
MultipartReader muitlpart_reader_;
247247
};
248248

249249
using Range = std::pair<ssize_t, ssize_t>;
@@ -583,14 +583,12 @@ class Server {
583583
const std::string &content_type);
584584
bool read_content(Stream &strm, bool last_connection, Request &req,
585585
Response &res);
586-
bool read_content_with_content_receiver(Stream &strm, bool last_connection,
587-
Request &req, Response &res,
588-
ContentReceiver receiver,
589-
MultipartContentHeader multipart_header,
590-
ContentReceiver multipart_receiver);
591-
bool read_content_core(Stream &strm, bool last_connection,
592-
Request &req, Response &res,
593-
ContentReceiver receiver,
586+
bool read_content_with_content_receiver(
587+
Stream &strm, bool last_connection, Request &req, Response &res,
588+
ContentReceiver receiver, MultipartContentHeader multipart_header,
589+
ContentReceiver multipart_receiver);
590+
bool read_content_core(Stream &strm, bool last_connection, Request &req,
591+
Response &res, ContentReceiver receiver,
594592
MultipartContentHeader mulitpart_header,
595593
ContentReceiver multipart_receiver);
596594

@@ -767,12 +765,10 @@ class Client {
767765
void write_request(Stream &strm, const Request &req, bool last_connection);
768766
bool redirect(const Request &req, Response &res);
769767

770-
std::shared_ptr<Response>
771-
send_with_content_provider(const char *method, const char *path,
772-
const Headers &headers, const std::string &body,
773-
size_t content_length,
774-
ContentProvider content_provider,
775-
const char *content_type);
768+
std::shared_ptr<Response> send_with_content_provider(
769+
const char *method, const char *path, const Headers &headers,
770+
const std::string &body, size_t content_length,
771+
ContentProvider content_provider, const char *content_type);
776772

777773
virtual bool process_and_close_socket(
778774
socket_t sock, size_t request_count,
@@ -1352,8 +1348,8 @@ inline bool is_connection_error() {
13521348
#endif
13531349
}
13541350

1355-
inline socket_t create_client_socket(
1356-
const char *host, int port, time_t timeout_sec) {
1351+
inline socket_t create_client_socket(const char *host, int port,
1352+
time_t timeout_sec) {
13571353
return create_socket(
13581354
host, port, [=](socket_t sock, struct addrinfo &ai) -> bool {
13591355
set_nonblocking(sock, true);
@@ -1914,8 +1910,7 @@ inline bool parse_multipart_boundary(const std::string &content_type,
19141910
}
19151911

19161912
inline bool parse_range_header(const std::string &s, Ranges &ranges) {
1917-
static auto re_first_range =
1918-
std::regex(R"(bytes=(\d*-\d*(?:,\s*\d*-\d*)*))");
1913+
static auto re_first_range = std::regex(R"(bytes=(\d*-\d*(?:,\s*\d*-\d*)*))");
19191914
std::smatch m;
19201915
if (std::regex_match(s, m, re_first_range)) {
19211916
auto pos = m.position(1);
@@ -1953,9 +1948,7 @@ class MultipartFormDataParser {
19531948
public:
19541949
MultipartFormDataParser() {}
19551950

1956-
void set_boundary(const std::string &boundary) {
1957-
boundary_ = boundary;
1958-
}
1951+
void set_boundary(const std::string &boundary) { boundary_ = boundary; }
19591952

19601953
bool is_valid() const { return is_valid_; }
19611954

@@ -2028,9 +2021,7 @@ class MultipartFormDataParser {
20282021
{
20292022
auto pattern = crlf_ + dash_;
20302023
auto pos = buf_.find(pattern);
2031-
if (pos == std::string::npos) {
2032-
pos = buf_.size();
2033-
}
2024+
if (pos == std::string::npos) { pos = buf_.size(); }
20342025
if (!content_callback(buf_.data(), pos)) {
20352026
is_valid_ = false;
20362027
is_done_ = false;
@@ -2354,10 +2345,9 @@ make_basic_authentication_header(const std::string &username,
23542345

23552346
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
23562347
inline std::pair<std::string, std::string> make_digest_authentication_header(
2357-
const Request &req,
2358-
const std::map<std::string, std::string> &auth,
2359-
size_t cnonce_count, const std::string &cnonce,
2360-
const std::string &username, const std::string &password) {
2348+
const Request &req, const std::map<std::string, std::string> &auth,
2349+
size_t cnonce_count, const std::string &cnonce, const std::string &username,
2350+
const std::string &password) {
23612351
using namespace std;
23622352

23632353
string nc;
@@ -2385,26 +2375,25 @@ inline std::pair<std::string, std::string> make_digest_authentication_header(
23852375
auto A1 = username + ":" + auth.at("realm") + ":" + password;
23862376

23872377
auto A2 = req.method + ":" + req.path;
2388-
if (qop == "auth-int") {
2389-
A2 += ":" + H(req.body);
2390-
}
2378+
if (qop == "auth-int") { A2 += ":" + H(req.body); }
23912379

23922380
response = H(H(A1) + ":" + auth.at("nonce") + ":" + nc + ":" + cnonce +
23932381
":" + qop + ":" + H(A2));
23942382
}
23952383

23962384
auto field = "Digest username=\"hello\", realm=\"" + auth.at("realm") +
23972385
"\", nonce=\"" + auth.at("nonce") + "\", uri=\"" + req.path +
2398-
"\", algorithm=" + auth.at("algorithm") + ", qop=" + qop + ", nc=\"" +
2399-
nc + "\", cnonce=\"" + cnonce + "\", response=\"" + response +
2400-
"\"";
2386+
"\", algorithm=" + auth.at("algorithm") + ", qop=" + qop +
2387+
", nc=\"" + nc + "\", cnonce=\"" + cnonce + "\", response=\"" +
2388+
response + "\"";
24012389

24022390
return make_pair("Authorization", field);
24032391
}
24042392
#endif
24052393

2406-
inline int parse_www_authenticate(const httplib::Response &res,
2407-
std::map<std::string, std::string> &digest_auth) {
2394+
inline int
2395+
parse_www_authenticate(const httplib::Response &res,
2396+
std::map<std::string, std::string> &digest_auth) {
24082397
if (res.has_header("WWW-Authenticate")) {
24092398
static auto re = std::regex(R"~((?:(?:,\s*)?(.+?)=(?:"(.*?)"|([^,]*))))~");
24102399
auto s = res.get_header_value("WWW-Authenticate");
@@ -2957,25 +2946,25 @@ Server::write_content_with_provider(Stream &strm, const Request &req,
29572946
inline bool Server::read_content(Stream &strm, bool last_connection,
29582947
Request &req, Response &res) {
29592948
MultipartFormDataMap::iterator cur;
2960-
auto ret = read_content_core(strm, last_connection, req, res,
2961-
// Regular
2962-
[&](const char *buf, size_t n) {
2963-
if (req.body.size() + n > req.body.max_size()) { return false; }
2964-
req.body.append(buf, n);
2965-
return true;
2966-
},
2967-
// Multipart
2968-
[&](const MultipartFormData &file) {
2969-
cur = req.files.emplace(file.name, file);
2970-
return true;
2971-
},
2972-
[&](const char *buf, size_t n) {
2973-
auto &content = cur->second.content;
2974-
if (content.size() + n > content.max_size()) { return false; }
2975-
content.append(buf, n);
2976-
return true;
2977-
}
2978-
);
2949+
auto ret = read_content_core(
2950+
strm, last_connection, req, res,
2951+
// Regular
2952+
[&](const char *buf, size_t n) {
2953+
if (req.body.size() + n > req.body.max_size()) { return false; }
2954+
req.body.append(buf, n);
2955+
return true;
2956+
},
2957+
// Multipart
2958+
[&](const MultipartFormData &file) {
2959+
cur = req.files.emplace(file.name, file);
2960+
return true;
2961+
},
2962+
[&](const char *buf, size_t n) {
2963+
auto &content = cur->second.content;
2964+
if (content.size() + n > content.max_size()) { return false; }
2965+
content.append(buf, n);
2966+
return true;
2967+
});
29792968

29802969
const auto &content_type = req.get_header_value("Content-Type");
29812970
if (!content_type.find("application/x-www-form-urlencoded")) {
@@ -2985,22 +2974,19 @@ inline bool Server::read_content(Stream &strm, bool last_connection,
29852974
return ret;
29862975
}
29872976

2988-
inline bool
2989-
Server::read_content_with_content_receiver(Stream &strm, bool last_connection,
2990-
Request &req, Response &res,
2991-
ContentReceiver receiver,
2992-
MultipartContentHeader multipart_header,
2993-
ContentReceiver multipart_receiver) {
2994-
return read_content_core(strm, last_connection, req, res,
2995-
receiver, multipart_header, multipart_receiver);
2977+
inline bool Server::read_content_with_content_receiver(
2978+
Stream &strm, bool last_connection, Request &req, Response &res,
2979+
ContentReceiver receiver, MultipartContentHeader multipart_header,
2980+
ContentReceiver multipart_receiver) {
2981+
return read_content_core(strm, last_connection, req, res, receiver,
2982+
multipart_header, multipart_receiver);
29962983
}
29972984

2998-
inline bool
2999-
Server::read_content_core(Stream &strm, bool last_connection,
3000-
Request &req, Response &res,
3001-
ContentReceiver receiver,
3002-
MultipartContentHeader mulitpart_header,
3003-
ContentReceiver multipart_receiver) {
2985+
inline bool Server::read_content_core(Stream &strm, bool last_connection,
2986+
Request &req, Response &res,
2987+
ContentReceiver receiver,
2988+
MultipartContentHeader mulitpart_header,
2989+
ContentReceiver multipart_receiver) {
30042990
detail::MultipartFormDataParser multipart_form_data_parser;
30052991
ContentReceiver out;
30062992

@@ -3014,7 +3000,8 @@ Server::read_content_core(Stream &strm, bool last_connection,
30143000

30153001
multipart_form_data_parser.set_boundary(boundary);
30163002
out = [&](const char *buf, size_t n) {
3017-
return multipart_form_data_parser.parse(buf, n, multipart_receiver, mulitpart_header);
3003+
return multipart_form_data_parser.parse(buf, n, multipart_receiver,
3004+
mulitpart_header);
30183005
};
30193006
} else {
30203007
out = receiver;
@@ -3159,15 +3146,14 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm,
31593146
// Content reader handler
31603147
{
31613148
ContentReader reader(
3162-
[&](ContentReceiver receiver) {
3163-
return read_content_with_content_receiver(strm, last_connection, req, res,
3164-
receiver, nullptr, nullptr);
3165-
},
3166-
[&](MultipartContentHeader header, ContentReceiver receiver) {
3167-
return read_content_with_content_receiver(strm, last_connection, req, res,
3168-
nullptr, header, receiver);
3169-
}
3170-
);
3149+
[&](ContentReceiver receiver) {
3150+
return read_content_with_content_receiver(
3151+
strm, last_connection, req, res, receiver, nullptr, nullptr);
3152+
},
3153+
[&](MultipartContentHeader header, ContentReceiver receiver) {
3154+
return read_content_with_content_receiver(
3155+
strm, last_connection, req, res, nullptr, header, receiver);
3156+
});
31713157

31723158
if (req.method == "POST") {
31733159
if (dispatch_request_for_content_reader(
@@ -3319,8 +3305,7 @@ inline Client::Client(const char *host, int port, time_t timeout_sec)
33193305
keep_alive_max_count_(CPPHTTPLIB_KEEPALIVE_MAX_COUNT),
33203306
read_timeout_sec_(CPPHTTPLIB_READ_TIMEOUT_SECOND),
33213307
read_timeout_usec_(CPPHTTPLIB_READ_TIMEOUT_USECOND),
3322-
follow_location_(false),
3323-
compress_(false) {}
3308+
follow_location_(false), compress_(false) {}
33243309

33253310
inline Client::~Client() {}
33263311

@@ -3760,15 +3745,16 @@ inline std::shared_ptr<Response> Client::Post(const char *path,
37603745
return Post(path, Headers(), body, content_type);
37613746
}
37623747

3763-
inline std::shared_ptr<Response>
3764-
Client::Post(const char *path, const Headers &headers, const std::string &body,
3765-
const char *content_type) {
3748+
inline std::shared_ptr<Response> Client::Post(const char *path,
3749+
const Headers &headers,
3750+
const std::string &body,
3751+
const char *content_type) {
37663752
return send_with_content_provider("POST", path, headers, body, 0, nullptr,
37673753
content_type);
37683754
}
37693755

3770-
inline std::shared_ptr<Response>
3771-
Client::Post(const char *path, const Params &params) {
3756+
inline std::shared_ptr<Response> Client::Post(const char *path,
3757+
const Params &params) {
37723758
return Post(path, Headers(), params);
37733759
}
37743760

@@ -3787,9 +3773,8 @@ Client::Post(const char *path, const Headers &headers, size_t content_length,
37873773
content_type);
37883774
}
37893775

3790-
inline std::shared_ptr<Response> Client::Post(const char *path,
3791-
const Headers &headers,
3792-
const Params &params) {
3776+
inline std::shared_ptr<Response>
3777+
Client::Post(const char *path, const Headers &headers, const Params &params) {
37933778
std::string query;
37943779
for (auto it = params.begin(); it != params.end(); ++it) {
37953780
if (it != params.begin()) { query += "&"; }
@@ -3839,9 +3824,10 @@ inline std::shared_ptr<Response> Client::Put(const char *path,
38393824
return Put(path, Headers(), body, content_type);
38403825
}
38413826

3842-
inline std::shared_ptr<Response>
3843-
Client::Put(const char *path, const Headers &headers, const std::string &body,
3844-
const char *content_type) {
3827+
inline std::shared_ptr<Response> Client::Put(const char *path,
3828+
const Headers &headers,
3829+
const std::string &body,
3830+
const char *content_type) {
38453831
return send_with_content_provider("PUT", path, headers, body, 0, nullptr,
38463832
content_type);
38473833
}
@@ -3867,9 +3853,10 @@ inline std::shared_ptr<Response> Client::Patch(const char *path,
38673853
return Patch(path, Headers(), body, content_type);
38683854
}
38693855

3870-
inline std::shared_ptr<Response>
3871-
Client::Patch(const char *path, const Headers &headers, const std::string &body,
3872-
const char *content_type) {
3856+
inline std::shared_ptr<Response> Client::Patch(const char *path,
3857+
const Headers &headers,
3858+
const std::string &body,
3859+
const char *content_type) {
38733860
return send_with_content_provider("PATCH", path, headers, body, 0, nullptr,
38743861
content_type);
38753862
}

0 commit comments

Comments
 (0)