diff --git a/.gitignore b/.gitignore index c8fcc1b..cb90f2a 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,7 @@ example/simplecli example/simplesvr example/benchmark example/redirect -example/sse +example/sse* example/upload example/*.pem test/test diff --git a/README.md b/README.md index daac71a..70360cc 100644 --- a/README.md +++ b/README.md @@ -243,7 +243,9 @@ svr.set_payload_max_length(1024 * 1024 * 512); // 512MB ### Server-Sent Events -Please check [here](https://github.com/yhirose/cpp-httplib/blob/master/example/sse.cc). +[Server example](https://github.com/yhirose/cpp-httplib/blob/master/example/ssesvr.cc) + +[Client example](https://github.com/yhirose/cpp-httplib/blob/master/example/ssecli.cc) ### Default thread pool support @@ -306,20 +308,6 @@ httplib::Headers headers = { auto res = cli.Get("/hi", headers); ``` -### GET with Content Receiver - -```c++ -std::string body; - -auto res = cli.Get("/large-data", - [&](const char *data, size_t data_length) { - body.append(data, data_length); - return true; - }); - -assert(res->body.empty()); -``` - ### POST ```c++ @@ -390,6 +378,16 @@ cli.set_write_timeout(5, 0); // 5 seconds ### Receive content with Content receiver +```c++ +std::string body; + +auto res = cli.Get("/large-data", + [&](const char *data, size_t data_length) { + body.append(data, data_length); + return true; + }); +``` + ```cpp std::string body; auto res = cli.Get( diff --git a/example/Makefile b/example/Makefile index 7e18250..b29b73c 100644 --- a/example/Makefile +++ b/example/Makefile @@ -5,7 +5,7 @@ OPENSSL_DIR = /usr/local/opt/openssl OPENSSL_SUPPORT = -DCPPHTTPLIB_OPENSSL_SUPPORT -I$(OPENSSL_DIR)/include -L$(OPENSSL_DIR)/lib -lssl -lcrypto ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz -all: server client hello simplecli simplesvr upload redirect sse benchmark +all: server client hello simplecli simplesvr upload redirect ssesvr ssecli benchmark server : server.cc ../httplib.h Makefile $(CXX) -o server $(CXXFLAGS) server.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) @@ -28,8 +28,11 @@ upload : upload.cc ../httplib.h Makefile redirect : redirect.cc ../httplib.h Makefile $(CXX) -o redirect $(CXXFLAGS) redirect.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) -sse : sse.cc ../httplib.h Makefile - $(CXX) -o sse $(CXXFLAGS) sse.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) +ssesvr : ssesvr.cc ../httplib.h Makefile + $(CXX) -o ssesvr $(CXXFLAGS) ssesvr.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) + +ssecli : ssecli.cc ../httplib.h Makefile + $(CXX) -o ssecli $(CXXFLAGS) ssecli.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) benchmark : benchmark.cc ../httplib.h Makefile $(CXX) -o benchmark $(CXXFLAGS) benchmark.cc $(OPENSSL_SUPPORT) $(ZLIB_SUPPORT) @@ -39,4 +42,4 @@ pem: openssl req -new -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem clean: - rm server client hello simplecli simplesvr upload redirect sse benchmark *.pem + rm server client hello simplecli simplesvr upload redirect ssesvr sselci benchmark *.pem diff --git a/example/ssecli.cc b/example/ssecli.cc new file mode 100644 index 0000000..c85c299 --- /dev/null +++ b/example/ssecli.cc @@ -0,0 +1,21 @@ +// +// ssecli.cc +// +// Copyright (c) 2019 Yuji Hirose. All rights reserved. +// MIT License +// + +#include +#include + +using namespace std; + +int main(void) { + httplib::Client2("http://localhost:1234") + .Get("/event1", [&](const char *data, size_t data_length) { + std::cout << string(data, data_length); + return true; + }); + + return 0; +} diff --git a/example/sse.cc b/example/ssesvr.cc similarity index 100% rename from example/sse.cc rename to example/ssesvr.cc