@@ -227,23 +227,23 @@ using MultipartContentHeader =
227
227
std::function<bool (const MultipartFormData &file)>;
228
228
229
229
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)>;
233
234
234
- ContentReader (Reader reader, MultipartReader muitlpart_reader)
235
+ ContentReader (Reader reader, MultipartReader muitlpart_reader)
235
236
: reader_(reader), muitlpart_reader_(muitlpart_reader) {}
236
237
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
+ }
240
242
241
- bool operator ()(ContentReceiver receiver) const {
242
- return reader_ (receiver);
243
- }
243
+ bool operator ()(ContentReceiver receiver) const { return reader_ (receiver); }
244
244
245
- Reader reader_;
246
- MultipartReader muitlpart_reader_;
245
+ Reader reader_;
246
+ MultipartReader muitlpart_reader_;
247
247
};
248
248
249
249
using Range = std::pair<ssize_t , ssize_t >;
@@ -583,14 +583,12 @@ class Server {
583
583
const std::string &content_type);
584
584
bool read_content (Stream &strm, bool last_connection, Request &req,
585
585
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,
594
592
MultipartContentHeader mulitpart_header,
595
593
ContentReceiver multipart_receiver);
596
594
@@ -767,12 +765,10 @@ class Client {
767
765
void write_request (Stream &strm, const Request &req, bool last_connection);
768
766
bool redirect (const Request &req, Response &res);
769
767
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);
776
772
777
773
virtual bool process_and_close_socket (
778
774
socket_t sock, size_t request_count,
@@ -1352,8 +1348,8 @@ inline bool is_connection_error() {
1352
1348
#endif
1353
1349
}
1354
1350
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) {
1357
1353
return create_socket (
1358
1354
host, port, [=](socket_t sock, struct addrinfo &ai) -> bool {
1359
1355
set_nonblocking (sock, true );
@@ -1914,8 +1910,7 @@ inline bool parse_multipart_boundary(const std::string &content_type,
1914
1910
}
1915
1911
1916
1912
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*)*))" );
1919
1914
std::smatch m;
1920
1915
if (std::regex_match (s, m, re_first_range)) {
1921
1916
auto pos = m.position (1 );
@@ -1953,9 +1948,7 @@ class MultipartFormDataParser {
1953
1948
public:
1954
1949
MultipartFormDataParser () {}
1955
1950
1956
- void set_boundary (const std::string &boundary) {
1957
- boundary_ = boundary;
1958
- }
1951
+ void set_boundary (const std::string &boundary) { boundary_ = boundary; }
1959
1952
1960
1953
bool is_valid () const { return is_valid_; }
1961
1954
@@ -2028,9 +2021,7 @@ class MultipartFormDataParser {
2028
2021
{
2029
2022
auto pattern = crlf_ + dash_;
2030
2023
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 (); }
2034
2025
if (!content_callback (buf_.data (), pos)) {
2035
2026
is_valid_ = false ;
2036
2027
is_done_ = false ;
@@ -2354,10 +2345,9 @@ make_basic_authentication_header(const std::string &username,
2354
2345
2355
2346
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
2356
2347
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) {
2361
2351
using namespace std ;
2362
2352
2363
2353
string nc;
@@ -2385,26 +2375,25 @@ inline std::pair<std::string, std::string> make_digest_authentication_header(
2385
2375
auto A1 = username + " :" + auth.at (" realm" ) + " :" + password;
2386
2376
2387
2377
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 ); }
2391
2379
2392
2380
response = H (H (A1) + " :" + auth.at (" nonce" ) + " :" + nc + " :" + cnonce +
2393
2381
" :" + qop + " :" + H (A2));
2394
2382
}
2395
2383
2396
2384
auto field = " Digest username=\" hello\" , realm=\" " + auth.at (" realm" ) +
2397
2385
" \" , 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 + " \" " ;
2401
2389
2402
2390
return make_pair (" Authorization" , field);
2403
2391
}
2404
2392
#endif
2405
2393
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) {
2408
2397
if (res.has_header (" WWW-Authenticate" )) {
2409
2398
static auto re = std::regex (R"~( (?:(?:,\s*)?(.+?)=(?:"(.*?)"|([^,]*))))~" );
2410
2399
auto s = res.get_header_value (" WWW-Authenticate" );
@@ -2957,25 +2946,25 @@ Server::write_content_with_provider(Stream &strm, const Request &req,
2957
2946
inline bool Server::read_content (Stream &strm, bool last_connection,
2958
2947
Request &req, Response &res) {
2959
2948
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
+ } );
2979
2968
2980
2969
const auto &content_type = req.get_header_value (" Content-Type" );
2981
2970
if (!content_type.find (" application/x-www-form-urlencoded" )) {
@@ -2985,22 +2974,19 @@ inline bool Server::read_content(Stream &strm, bool last_connection,
2985
2974
return ret;
2986
2975
}
2987
2976
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);
2996
2983
}
2997
2984
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) {
3004
2990
detail::MultipartFormDataParser multipart_form_data_parser;
3005
2991
ContentReceiver out;
3006
2992
@@ -3014,7 +3000,8 @@ Server::read_content_core(Stream &strm, bool last_connection,
3014
3000
3015
3001
multipart_form_data_parser.set_boundary (boundary);
3016
3002
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);
3018
3005
};
3019
3006
} else {
3020
3007
out = receiver;
@@ -3159,15 +3146,14 @@ inline bool Server::routing(Request &req, Response &res, Stream &strm,
3159
3146
// Content reader handler
3160
3147
{
3161
3148
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
+ });
3171
3157
3172
3158
if (req.method == " POST" ) {
3173
3159
if (dispatch_request_for_content_reader (
@@ -3319,8 +3305,7 @@ inline Client::Client(const char *host, int port, time_t timeout_sec)
3319
3305
keep_alive_max_count_(CPPHTTPLIB_KEEPALIVE_MAX_COUNT),
3320
3306
read_timeout_sec_(CPPHTTPLIB_READ_TIMEOUT_SECOND),
3321
3307
read_timeout_usec_(CPPHTTPLIB_READ_TIMEOUT_USECOND),
3322
- follow_location_(false ),
3323
- compress_(false ) {}
3308
+ follow_location_(false ), compress_(false ) {}
3324
3309
3325
3310
inline Client::~Client () {}
3326
3311
@@ -3760,15 +3745,16 @@ inline std::shared_ptr<Response> Client::Post(const char *path,
3760
3745
return Post (path, Headers (), body, content_type);
3761
3746
}
3762
3747
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) {
3766
3752
return send_with_content_provider (" POST" , path, headers, body, 0 , nullptr ,
3767
3753
content_type);
3768
3754
}
3769
3755
3770
- inline std::shared_ptr<Response>
3771
- Client::Post ( const char *path, const Params ¶ms) {
3756
+ inline std::shared_ptr<Response> Client::Post ( const char *path,
3757
+ const Params ¶ms) {
3772
3758
return Post (path, Headers (), params);
3773
3759
}
3774
3760
@@ -3787,9 +3773,8 @@ Client::Post(const char *path, const Headers &headers, size_t content_length,
3787
3773
content_type);
3788
3774
}
3789
3775
3790
- inline std::shared_ptr<Response> Client::Post (const char *path,
3791
- const Headers &headers,
3792
- const Params ¶ms) {
3776
+ inline std::shared_ptr<Response>
3777
+ Client::Post (const char *path, const Headers &headers, const Params ¶ms) {
3793
3778
std::string query;
3794
3779
for (auto it = params.begin (); it != params.end (); ++it) {
3795
3780
if (it != params.begin ()) { query += " &" ; }
@@ -3839,9 +3824,10 @@ inline std::shared_ptr<Response> Client::Put(const char *path,
3839
3824
return Put (path, Headers (), body, content_type);
3840
3825
}
3841
3826
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) {
3845
3831
return send_with_content_provider (" PUT" , path, headers, body, 0 , nullptr ,
3846
3832
content_type);
3847
3833
}
@@ -3867,9 +3853,10 @@ inline std::shared_ptr<Response> Client::Patch(const char *path,
3867
3853
return Patch (path, Headers (), body, content_type);
3868
3854
}
3869
3855
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) {
3873
3860
return send_with_content_provider (" PATCH" , path, headers, body, 0 , nullptr ,
3874
3861
content_type);
3875
3862
}
0 commit comments