@@ -17,24 +17,24 @@ Server Example
1717
1818int main (void)
1919{
20- using namespace httplib;
20+ using namespace httplib;
2121
22- Server svr;
22+ Server svr;
2323
24- svr.Get("/hi", [](const Request& req, Response& res) {
25- res.set_content("Hello World!", "text/plain");
26- });
24+ svr.Get("/hi", [ ] (const Request& req, Response& res) {
25+ res.set_content("Hello World!", "text/plain");
26+ });
2727
28- svr.Get(R"(/numbers/(\d+))", [&](const Request& req, Response& res) {
29- auto numbers = req.matches[1];
30- res.set_content(numbers, "text/plain");
31- });
28+ svr.Get(R"(/numbers/(\d+))", [ &] (const Request& req, Response& res) {
29+ auto numbers = req.matches[ 1] ;
30+ res.set_content(numbers, "text/plain");
31+ });
3232
33- svr.Get("/stop", [&](const Request& req, Response& res) {
34- svr.stop();
35- });
33+ svr.Get("/stop", [ &] (const Request& req, Response& res) {
34+ svr.stop();
35+ });
3636
37- svr.listen("localhost", 1234);
37+ svr.listen("localhost", 1234);
3838}
3939```
4040
@@ -102,50 +102,31 @@ NOTE: These the static file server methods are not thread safe.
102102
103103``` cpp
104104svr.set_logger([](const auto & req, const auto & res) {
105- your_logger (req, res);
105+ your_logger (req, res);
106106});
107107```
108108
109109### Error handler
110110
111111``` cpp
112112svr.set_error_handler([](const auto & req, auto & res) {
113- auto fmt = "<p>Error Status: <span style='color:red;'>%d</span></p>";
114- char buf[BUFSIZ];
115- snprintf (buf, sizeof(buf), fmt, res.status);
116- res.set_content(buf, "text/html");
113+ auto fmt = "<p >Error Status: <span style =' color :red ;' >%d</span ></p >";
114+ char buf[ BUFSIZ] ;
115+ snprintf (buf, sizeof(buf), fmt, res.status);
116+ res.set_content(buf, "text/html");
117117});
118118```
119119
120120### 'multipart/form-data' POST data
121121
122122``` cpp
123123svr.Post(" /multipart" , [&](const auto & req, auto & res) {
124- auto size = req.files.size();
125- auto ret = req.has_file("name1");
126- const auto& file = req.get_file_value("name1");
127- // file.filename;
128- // file.content_type;
129- // file.content;
130- });
131-
132- ```
133-
134- ### Send content with Content provider
135-
136- ``` cpp
137- const uint64_t DATA_CHUNK_SIZE = 4 ;
138-
139- svr.Get(" /stream" , [&](const Request &req, Response &res) {
140- auto data = new std::string("abcdefg");
141-
142- res.set_content_provider(
143- data->size(), // Content length
144- [ data] (uint64_t offset, uint64_t length, DataSink &sink) {
145- const auto &d = * data;
146- sink.write(&d[ offset] , std::min(length, DATA_CHUNK_SIZE));
147- },
148- [ data] { delete data; });
124+ auto size = req.files.size();
125+ auto ret = req.has_file("name1");
126+ const auto& file = req.get_file_value("name1");
127+ // file.filename;
128+ // file.content_type;
129+ // file.content;
149130});
150131```
151132
@@ -176,6 +157,24 @@ svr.Post("/content_receiver",
176157 });
177158```
178159
160+ ### Send content with Content provider
161+
162+ ```cpp
163+ const uint64_t DATA_CHUNK_SIZE = 4;
164+
165+ svr.Get("/stream", [&](const Request &req, Response &res) {
166+ auto data = new std::string("abcdefg");
167+
168+ res.set_content_provider(
169+ data->size(), // Content length
170+ [data](uint64_t offset, uint64_t length, DataSink &sink) {
171+ const auto &d = *data;
172+ sink.write(&d[offset], std::min(length, DATA_CHUNK_SIZE));
173+ },
174+ [data] { delete data; });
175+ });
176+ ```
177+
179178### Chunked transfer encoding
180179
181180``` cpp
@@ -261,36 +260,36 @@ Client Example
261260
262261int main (void)
263262{
264- httplib::Client cli("localhost", 1234);
263+ httplib::Client cli("localhost", 1234);
265264
266- auto res = cli.Get("/hi");
267- if (res && res->status == 200) {
268- std::cout << res->body << std::endl;
269- }
265+ auto res = cli.Get("/hi");
266+ if (res && res->status == 200) {
267+ std::cout << res->body << std::endl;
268+ }
270269}
271270```
272271
273272### GET with HTTP headers
274273
275274```c++
276- httplib::Headers headers = {
277- { "Accept-Encoding", "gzip, deflate" }
278- };
279- auto res = cli.Get("/hi", headers);
275+ httplib::Headers headers = {
276+ { "Accept-Encoding", "gzip, deflate" }
277+ };
278+ auto res = cli.Get("/hi", headers);
280279```
281280
282281### GET with Content Receiver
283282
284283``` c++
285- std::string body;
284+ std::string body;
286285
287- auto res = cli.Get(" /large-data" ,
288- [&](const char *data, uint64_t data_length) {
289- body.append(data, data_length);
290- return true;
291- });
286+ auto res = cli.Get(" /large-data" ,
287+ [&](const char *data, uint64_t data_length) {
288+ body.append(data, data_length);
289+ return true;
290+ });
292291
293- assert (res->body.empty());
292+ assert (res->body.empty());
294293```
295294
296295### POST
@@ -323,15 +322,15 @@ auto res = cli.Post("/post", params);
323322### POST with Multipart Form Data
324323
325324``` c++
326- httplib::MultipartFormDataItems items = {
327- { "text1", "text default", "", "" },
328- { "text2", "aωb", "", "" },
329- { "file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain" },
330- { "file2", "{\n \"world\", true\n}\n", "world.json", "application/json" },
331- { "file3", "", "", "application/octet-stream" },
332- };
325+ httplib::MultipartFormDataItems items = {
326+ { "text1", "text default", "", "" },
327+ { "text2", "aωb", "", "" },
328+ { "file1", "h\ne\n\nl\nl\no\n", "hello.txt", "text/plain" },
329+ { "file2", "{\n \" world\" , true\n}\n", "world.json", "application/json" },
330+ { "file3", "", "", "application/octet-stream" },
331+ };
333332
334- auto res = cli.Post("/multipart", items);
333+ auto res = cli.Post(" /multipart" , items);
335334```
336335
337336### PUT
@@ -365,12 +364,12 @@ httplib::Client client(url, port);
365364
366365// prints: 0 / 000 bytes => 50% complete
367366std::shared_ptr< httplib::Response > res =
368- cli.Get("/", [ ] (uint64_t len, uint64_t total) {
369- printf("%lld / %lld bytes => %d%% complete\n",
370- len, total,
371- (int)((len/total)* 100));
372- return true; // return 'false' if you want to cancel the request.
373- }
367+ cli.Get("/", [ ] (uint64_t len, uint64_t total) {
368+ printf("%lld / %lld bytes => %d%% complete\n",
369+ len, total,
370+ (int)((len/total)* 100));
371+ return true; // return 'false' if you want to cancel the request.
372+ }
374373);
375374```
376375
0 commit comments