Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Commit 75053bf

Browse files
authored
* Fix yhirose#1498 * Fixed build error
1 parent ae3a6dd commit 75053bf

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

httplib.h

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,34 @@ struct ci {
311311
}
312312
};
313313

314+
// This is based on
315+
// "http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4189".
316+
317+
template <typename EF> struct scope_exit {
318+
explicit scope_exit(EF &&f)
319+
: exit_function(std::move(f)), execute_on_destruction{true} {}
320+
321+
scope_exit(scope_exit &&rhs)
322+
: exit_function(std::move(rhs.exit_function)),
323+
execute_on_destruction{rhs.execute_on_destruction} {
324+
rhs.release();
325+
}
326+
327+
~scope_exit() {
328+
if (execute_on_destruction) { this->exit_function(); }
329+
}
330+
331+
void release() { this->execute_on_destruction = false; }
332+
333+
private:
334+
scope_exit(const scope_exit &) = delete;
335+
void operator=(const scope_exit &) = delete;
336+
scope_exit &operator=(scope_exit &&) = delete;
337+
338+
EF exit_function;
339+
bool execute_on_destruction;
340+
};
341+
314342
} // namespace detail
315343

316344
using Headers = std::multimap<std::string, std::string, detail::ci>;
@@ -4785,13 +4813,14 @@ inline MultipartFormData Request::get_file_value(const std::string &key) const {
47854813
return MultipartFormData();
47864814
}
47874815

4788-
inline std::vector<MultipartFormData> Request::get_file_values(const std::string &key) const {
4789-
std::vector<MultipartFormData> values;
4790-
auto rng = files.equal_range(key);
4791-
for (auto it = rng.first; it != rng.second; it++) {
4792-
values.push_back(it->second);
4793-
}
4794-
return values;
4816+
inline std::vector<MultipartFormData>
4817+
Request::get_file_values(const std::string &key) const {
4818+
std::vector<MultipartFormData> values;
4819+
auto rng = files.equal_range(key);
4820+
for (auto it = rng.first; it != rng.second; it++) {
4821+
values.push_back(it->second);
4822+
}
4823+
return values;
47954824
}
47964825

47974826
// Response implementation
@@ -6307,13 +6336,11 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) {
63076336
}
63086337
}
63096338

6339+
auto ret = false;
63106340
auto close_connection = !keep_alive_;
6311-
auto ret = process_socket(socket_, [&](Stream &strm) {
6312-
return handle_request(strm, req, res, close_connection, error);
6313-
});
63146341

6315-
// Briefly lock mutex in order to mark that a request is no longer ongoing
6316-
{
6342+
auto se = detail::scope_exit<std::function<void (void)>>([&]() {
6343+
// Briefly lock mutex in order to mark that a request is no longer ongoing
63176344
std::lock_guard<std::mutex> guard(socket_mutex_);
63186345
socket_requests_in_flight_ -= 1;
63196346
if (socket_requests_in_flight_ <= 0) {
@@ -6327,7 +6354,11 @@ inline bool ClientImpl::send(Request &req, Response &res, Error &error) {
63276354
shutdown_socket(socket_);
63286355
close_socket(socket_);
63296356
}
6330-
}
6357+
});
6358+
6359+
ret = process_socket(socket_, [&](Stream &strm) {
6360+
return handle_request(strm, req, res, close_connection, error);
6361+
});
63316362

63326363
if (!ret) {
63336364
if (error == Error::Success) { error = Error::Unknown; }

0 commit comments

Comments
 (0)