Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3f00e1b

Browse files
committed
Revert "Changed set_file_content to accept only a regular file path."
This reverts commit 7ab9c11.
1 parent 7ab9c11 commit 3f00e1b

File tree

3 files changed

+91
-90
lines changed

3 files changed

+91
-90
lines changed

‎README.md

Lines changed: 88 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -97,33 +97,37 @@ int main(void)
9797

9898
Server svr;
9999

100-
svr.Get("/hi", [](const Request &req, Response &res) {
100+
svr.Get("/hi", [](const Request& req, Response& res) {
101101
res.set_content("Hello World!", "text/plain");
102102
});
103103

104104
// Match the request path against a regular expression
105105
// and extract its captures
106-
svr.Get(R"(/numbers/(\d+))", [&](const Request &req, Response &res) {
106+
svr.Get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) {
107107
auto numbers = req.matches[1];
108108
res.set_content(numbers, "text/plain");
109109
});
110110

111111
// Capture the second segment of the request path as "id" path param
112-
svr.Get("/users/:id", [&](const Request &req, Response &res) {
112+
svr.Get("/users/:id", [&](const Request& req, Response& res) {
113113
auto user_id = req.path_params.at("id");
114114
res.set_content(user_id, "text/plain");
115115
});
116116

117117
// Extract values from HTTP headers and URL query params
118-
svr.Get("/body-header-param", [](const Request &req, Response &res) {
118+
svr.Get("/body-header-param", [](const Request& req, Response& res) {
119119
if (req.has_header("Content-Length")) {
120120
auto val = req.get_header_value("Content-Length");
121121
}
122-
if (req.has_param("key")) { auto val = req.get_param_value("key"); }
122+
if (req.has_param("key")) {
123+
auto val = req.get_param_value("key");
124+
}
123125
res.set_content(req.body, "text/plain");
124126
});
125127

126-
svr.Get("/stop", [&](const Request &req, Response &res) { svr.stop(); });
128+
svr.Get("/stop", [&](const Request& req, Response& res) {
129+
svr.stop();
130+
});
127131

128132
svr.listen("localhost", 1234);
129133
}
@@ -272,7 +276,7 @@ svr.set_post_routing_handler([](const auto& req, auto& res) {
272276
svr.Post("/multipart", [&](const auto& req, auto& res) {
273277
auto size = req.files.size();
274278
auto ret = req.has_file("name1");
275-
const auto &file = req.get_file_value("name1");
279+
const auto& file = req.get_file_value("name1");
276280
// file.filename;
277281
// file.content_type;
278282
// file.content;
@@ -284,10 +288,10 @@ svr.Post("/multipart", [&](const auto& req, auto& res) {
284288
```cpp
285289
svr.Post("/content_receiver",
286290
[&](const Request &req, Response &res, const ContentReader &content_reader) {
287-
if (req.is_multipart_form_data()) {
288-
// NOTE: `content_reader` is blocking until every form data field is read
289-
MultipartFormDataItems files;
290-
content_reader(
291+
if (req.is_multipart_form_data()) {
292+
// NOTE: `content_reader` is blocking until every form data field is read
293+
MultipartFormDataItems files;
294+
content_reader(
291295
[&](const MultipartFormData &file) {
292296
files.push_back(file);
293297
return true;
@@ -296,13 +300,13 @@ svr.Post("/content_receiver",
296300
files.back().content.append(data, data_length);
297301
return true;
298302
});
299-
} else {
300-
std::string body;
301-
content_reader([&](const char *data, size_t data_length) {
302-
body.append(data, data_length);
303-
return true;
304-
});
305-
}
303+
} else {
304+
std::string body;
305+
content_reader([&](const char *data, size_t data_length) {
306+
body.append(data, data_length);
307+
return true;
308+
});
309+
}
306310
});
307311
```
308312
@@ -315,14 +319,14 @@ svr.Get("/stream", [&](const Request &req, Response &res) {
315319
auto data = new std::string("abcdefg");
316320
317321
res.set_content_provider(
318-
data->size(), // Content length
319-
"text/plain", // Content type
320-
[&, data](size_t offset, size_t length, DataSink &sink) {
321-
const auto &d = *data;
322-
sink.write(&d[offset], std::min(length, DATA_CHUNK_SIZE));
323-
return true; // return 'false' if you want to cancel the process.
324-
},
325-
[data](bool success) { delete data; });
322+
data->size(), // Content length
323+
"text/plain", // Content type
324+
[&, data](size_t offset, size_t length, DataSink &sink) {
325+
const auto &d = *data;
326+
sink.write(&d[offset], std::min(length, DATA_CHUNK_SIZE));
327+
return true; // return 'false' if you want to cancel the process.
328+
},
329+
[data](bool success) { delete data; });
326330
});
327331
```
328332

@@ -331,17 +335,17 @@ Without content length:
331335
```cpp
332336
svr.Get("/stream", [&](const Request &req, Response &res) {
333337
res.set_content_provider(
334-
"text/plain", // Content type
335-
[&](size_t offset, DataSink &sink) {
336-
if (/* there is still data */) {
337-
std::vector<char> data;
338-
// prepare data...
339-
sink.write(data.data(), data.size());
340-
} else {
341-
sink.done(); // No more data
342-
}
343-
return true; // return 'false' if you want to cancel the process.
344-
});
338+
"text/plain", // Content type
339+
[&](size_t offset, DataSink &sink) {
340+
if (/* there is still data */) {
341+
std::vector<char> data;
342+
// prepare data...
343+
sink.write(data.data(), data.size());
344+
} else {
345+
sink.done(); // No more data
346+
}
347+
return true; // return 'false' if you want to cancel the process.
348+
});
345349
});
346350
```
347351

@@ -350,13 +354,15 @@ svr.Get("/stream", [&](const Request &req, Response &res) {
350354
```cpp
351355
svr.Get("/chunked", [&](const Request& req, Response& res) {
352356
res.set_chunked_content_provider(
353-
"text/plain", [](size_t offset, DataSink &sink) {
354-
sink.write("123", 3);
355-
sink.write("345", 3);
356-
sink.write("789", 3);
357-
sink.done(); // No more data
358-
return true; // return 'false' if you want to cancel the process.
359-
});
357+
"text/plain",
358+
[](size_t offset, DataSink &sink) {
359+
sink.write("123", 3);
360+
sink.write("345", 3);
361+
sink.write("789", 3);
362+
sink.done(); // No more data
363+
return true; // return 'false' if you want to cancel the process.
364+
}
365+
);
360366
});
361367
```
362368

@@ -365,21 +371,24 @@ With trailer:
365371
```cpp
366372
svr.Get("/chunked", [&](const Request& req, Response& res) {
367373
res.set_header("Trailer", "Dummy1, Dummy2");
368-
res.set_chunked_content_provider("text/plain", [](size_t offset,
369-
DataSink &sink) {
370-
sink.write("123", 3);
371-
sink.write("345", 3);
372-
sink.write("789", 3);
373-
sink.done_with_trailer({{"Dummy1", "DummyVal1"}, {"Dummy2", "DummyVal2"}});
374-
return true;
375-
});
374+
res.set_chunked_content_provider(
375+
"text/plain",
376+
[](size_t offset, DataSink &sink) {
377+
sink.write("123", 3);
378+
sink.write("345", 3);
379+
sink.write("789", 3);
380+
sink.done_with_trailer({
381+
{"Dummy1", "DummyVal1"},
382+
{"Dummy2", "DummyVal2"}
383+
});
384+
return true;
385+
}
386+
);
376387
});
377388
```
378389

379390
### Send file content
380391

381-
We can set a file path for the response body. It's a user's responsibility to pass a valid regular file path. If the path doesn't exist, or a directory path, cpp-httplib throws an exception.
382-
383392
```cpp
384393
svr.Get("/content", [&](const Request &req, Response &res) {
385394
res.set_file_content("./path/to/conent.html");
@@ -443,17 +452,15 @@ Please see [Server example](https://github.com/yhirose/cpp-httplib/blob/master/e
443452
If you want to set the thread count at runtime, there is no convenient way... But here is how.
444453

445454
```cpp
446-
svr.new_task_queue = [] {
447-
return new ThreadPool(12); };
455+
svr.new_task_queue = [] { return new ThreadPool(12); };
448456
```
449457
450458
You can also provide an optional parameter to limit the maximum number
451459
of pending requests, i.e. requests `accept()`ed by the listener but
452460
still waiting to be serviced by worker threads.
453461
454462
```cpp
455-
svr.new_task_queue = [] {
456-
return new ThreadPool(/*num_threads=*/12, /*max_queued_requests=*/18); };
463+
svr.new_task_queue = [] { return new ThreadPool(/*num_threads=*/12, /*max_queued_requests=*/18); };
457464
```
458465

459466
Default limit is 0 (unlimited). Once the limit is reached, the listener
@@ -466,15 +473,19 @@ You can supply your own thread pool implementation according to your need.
466473
```cpp
467474
class YourThreadPoolTaskQueue : public TaskQueue {
468475
public:
469-
YourThreadPoolTaskQueue(size_t n) { pool_.start_with_thread_count(n); }
476+
YourThreadPoolTaskQueue(size_t n) {
477+
pool_.start_with_thread_count(n);
478+
}
470479

471480
virtual bool enqueue(std::function<void()> fn) override {
472481
/* Return true if the task was actually enqueued, or false
473482
* if the caller must drop the corresponding connection. */
474483
return pool_.enqueue(fn);
475484
}
476485

477-
virtual void shutdown() override { pool_.shutdown_gracefully(); }
486+
virtual void shutdown() override {
487+
pool_.shutdown_gracefully();
488+
}
478489

479490
private:
480491
YourThreadPool pool_;
@@ -637,8 +648,8 @@ std::string body;
637648

638649
auto res = cli.Get("/large-data",
639650
[&](const char *data, size_t data_length) {
640-
body.append(data, data_length);
641-
return true;
651+
body.append(data, data_length);
652+
return true;
642653
});
643654
```
644655
@@ -648,12 +659,12 @@ std::string body;
648659
auto res = cli.Get(
649660
"/stream", Headers(),
650661
[&](const Response &response) {
651-
EXPECT_EQ(StatusCode::OK_200, response.status);
652-
return true; // return 'false' if you want to cancel the request.
662+
EXPECT_EQ(StatusCode::OK_200, response.status);
663+
return true; // return 'false' if you want to cancel the request.
653664
},
654665
[&](const char *data, size_t data_length) {
655-
body.append(data, data_length);
656-
return true; // return 'false' if you want to cancel the request.
666+
body.append(data, data_length);
667+
return true; // return 'false' if you want to cancel the request.
657668
});
658669
```
659670

@@ -665,8 +676,8 @@ std::string body = ...;
665676
auto res = cli.Post(
666677
"/stream", body.size(),
667678
[](size_t offset, size_t length, DataSink &sink) {
668-
sink.write(body.data() + offset, length);
669-
return true; // return 'false' if you want to cancel the request.
679+
sink.write(body.data() + offset, length);
680+
return true; // return 'false' if you want to cancel the request.
670681
},
671682
"text/plain");
672683
```
@@ -677,11 +688,11 @@ auto res = cli.Post(
677688
auto res = cli.Post(
678689
"/stream",
679690
[](size_t offset, DataSink &sink) {
680-
sink.os << "chunked data 1";
681-
sink.os << "chunked data 2";
682-
sink.os << "chunked data 3";
683-
sink.done();
684-
return true; // return 'false' if you want to cancel the request.
691+
sink.os << "chunked data 1";
692+
sink.os << "chunked data 2";
693+
sink.os << "chunked data 3";
694+
sink.done();
695+
return true; // return 'false' if you want to cancel the request.
685696
},
686697
"text/plain");
687698
```
@@ -693,8 +704,9 @@ httplib::Client cli(url, port);
693704

694705
// prints: 0 / 000 bytes => 50% complete
695706
auto res = cli.Get("/", [](uint64_t len, uint64_t total) {
696-
printf("%lld / %lld bytes => %d%% complete\n", len, total,
697-
(int)(len * 100 / total));
707+
printf("%lld / %lld bytes => %d%% complete\n",
708+
len, total,
709+
(int)(len*100/total));
698710
return true; // return 'false' if you want to cancel the request.
699711
}
700712
);
@@ -892,8 +904,8 @@ g++ 4.8 and below cannot build this library since `<regex>` in the versions are
892904
Include `httplib.h` before `Windows.h` or include `Windows.h` by defining `WIN32_LEAN_AND_MEAN` beforehand.
893905

894906
```cpp
895-
#include <Windows.h>
896907
#include <httplib.h>
908+
#include <Windows.h>
897909
```
898910

899911
```cpp

‎httplib.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5752,21 +5752,12 @@ inline void Response::set_chunked_content_provider(
57525752

57535753
inline void Response::set_file_content(const std::string &path,
57545754
const std::string &content_type) {
5755-
detail::FileStat stat(dir);
5756-
if (stat.is_file(path)) {
5757-
file_content_path_ = path;
5758-
file_content_content_type_ = content_type;
5759-
return;
5760-
}
5761-
5762-
#ifndef CPPHTTPLIB_NO_EXCEPTIONS
5763-
std::string msg = "'" + path + "' is not a regular file.";
5764-
throw std::invalid_argument(msg);
5765-
#endif
5755+
file_content_path_ = path;
5756+
file_content_content_type_ = content_type;
57665757
}
57675758

57685759
inline void Response::set_file_content(const std::string &path) {
5769-
returnset_file_content(path, std::string());
5760+
file_content_path_ = path;
57705761
}
57715762

57725763
// Result implementation

‎test/test.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,8 +2288,6 @@ class ServerTest : public ::testing::Test {
22882288
{
22892289
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
22902290
cli_.enable_server_certificate_verification(false);
2291-
#else
2292-
#error no ssl
22932291
#endif
22942292
}
22952293

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /