Add a base class for http async clients; refine examples.
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
HttpBin (http://httpbin.org/) client example.
|
||||
|
||||
You request to different endpoints, and it returns information about what was in the request.
|
||||
|
||||
E.g., request:
|
||||
```plain
|
||||
GET /get HTTP/1.1
|
||||
Host: httpbin.org:80
|
||||
User-Agent: Webcc/0.1.0
|
||||
|
||||
```
|
||||
|
||||
Response:
|
||||
```plain
|
||||
HTTP/1.1 200 OK
|
||||
Connection: keep-alive
|
||||
Server: gunicorn/19.9.0
|
||||
Content-Type: application/json
|
||||
Content-Length: 191
|
||||
Access-Control-Allow-Origin: *
|
||||
Access-Control-Allow-Credentials: true
|
||||
Via: 1.1 vegur
|
||||
|
||||
{
|
||||
"args": {},
|
||||
"headers": {
|
||||
"Connection": "close",
|
||||
"Host": "httpbin.org",
|
||||
"User-Agent": "Webcc/0.1.0"
|
||||
},
|
||||
"origin": "198.55.94.81",
|
||||
"url": "http://httpbin.org/get"
|
||||
}
|
||||
```
|
||||
|
||||
As you can see, the request information is returned in JSON format.
|
||||
@@ -0,0 +1,4 @@
|
||||
add_executable(http_async_client main.cc)
|
||||
|
||||
target_link_libraries(http_async_client webcc ${Boost_LIBRARIES})
|
||||
target_link_libraries(http_async_client "${CMAKE_THREAD_LIBS_INIT}")
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "boost/asio/io_context.hpp"
|
||||
|
||||
#include "webcc/http_async_client.h"
|
||||
#include "webcc/logger.h"
|
||||
|
||||
// TODO: The program blocks during read response.
|
||||
// Only HttpBin.org has this issue.
|
||||
|
||||
static void Test(boost::asio::io_context& io_context) {
|
||||
auto request = webcc::HttpRequest::Make(webcc::kHttpGet, "/get",
|
||||
"httpbin.org");
|
||||
|
||||
webcc::HttpAsyncClientPtr client{
|
||||
new webcc::HttpAsyncClient(io_context)
|
||||
};
|
||||
|
||||
client->SetTimeout(3);
|
||||
|
||||
// Response callback.
|
||||
auto callback = [](webcc::HttpResponsePtr response, webcc::Error error,
|
||||
bool timed_out) {
|
||||
if (error == webcc::kNoError) {
|
||||
std::cout << response->content() << std::endl;
|
||||
} else {
|
||||
std::cout << DescribeError(error);
|
||||
if (timed_out) {
|
||||
std::cout << " (timed out)";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
client->Request(request, callback);
|
||||
}
|
||||
|
||||
int main() {
|
||||
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
|
||||
|
||||
boost::asio::io_context io_context;
|
||||
|
||||
//Test(io_context);
|
||||
Test(io_context);
|
||||
|
||||
io_context.run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
add_executable(http_bin_client main.cc)
|
||||
|
||||
target_link_libraries(http_bin_client webcc ${Boost_LIBRARIES})
|
||||
target_link_libraries(http_bin_client "${CMAKE_THREAD_LIBS_INIT}")
|
||||
@@ -1,61 +0,0 @@
|
||||
// HttpBin (http://httpbin.org/) client example.
|
||||
//
|
||||
// You request to different endpoints, and it returns information about what
|
||||
// was in the request.
|
||||
//
|
||||
// E.g., request:
|
||||
// > GET /get HTTP/1.1
|
||||
// > Host: httpbin.org:80
|
||||
// > User-Agent: Webcc/0.1.0
|
||||
// >
|
||||
// Response:
|
||||
// > HTTP/1.1 200 OK
|
||||
// > Connection: keep-alive
|
||||
// > Server: gunicorn/19.9.0
|
||||
// > Content-Type: application/json
|
||||
// > Content-Length: 191
|
||||
// > Access-Control-Allow-Origin: *
|
||||
// > Access-Control-Allow-Credentials: true
|
||||
// > Via: 1.1 vegur
|
||||
// >
|
||||
// > {
|
||||
// > "args": {},
|
||||
// > "headers": {
|
||||
// > "Connection": "close",
|
||||
// > "Host": "httpbin.org",
|
||||
// > "User-Agent": "Webcc/0.1.0"
|
||||
// > },
|
||||
// > "origin": "198.55.94.81",
|
||||
// > "url": "http://httpbin.org/get"
|
||||
// > }
|
||||
// >
|
||||
// As you can see, the request information is returned in JSON format.
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "webcc/http_client.h"
|
||||
#include "webcc/logger.h"
|
||||
|
||||
void Test() {
|
||||
webcc::HttpRequest request(webcc::kHttpGet, "/get", "httpbin.org"/*, "80"*/);
|
||||
request.Make();
|
||||
|
||||
webcc::HttpClient client;
|
||||
if (client.Request(request)) {
|
||||
std::cout << client.response()->content() << std::endl;
|
||||
} else {
|
||||
std::cout << webcc::DescribeError(client.error());
|
||||
if (client.timed_out()) {
|
||||
std::cout << " (timed out)";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
|
||||
|
||||
Test();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
add_executable(http_client main.cc)
|
||||
|
||||
target_link_libraries(http_client webcc ${Boost_LIBRARIES})
|
||||
target_link_libraries(http_client "${CMAKE_THREAD_LIBS_INIT}")
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "webcc/http_client.h"
|
||||
#include "webcc/logger.h"
|
||||
|
||||
static void PrintError(const webcc::HttpClient& client) {
|
||||
std::cout << webcc::DescribeError(client.error());
|
||||
if (client.timed_out()) {
|
||||
std::cout << " (timed out)";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
|
||||
|
||||
auto request = webcc::HttpRequest::Make(webcc::kHttpGet, "/get",
|
||||
"httpbin.org");
|
||||
|
||||
webcc::HttpClient client;
|
||||
|
||||
if (client.Request(*request)) {
|
||||
std::cout << client.response_content() << std::endl;
|
||||
} else {
|
||||
PrintError(client);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
add_executable(http_hello_async_client main.cc)
|
||||
|
||||
target_link_libraries(http_hello_async_client webcc ${Boost_LIBRARIES})
|
||||
target_link_libraries(http_hello_async_client "${CMAKE_THREAD_LIBS_INIT}")
|
||||
@@ -1,50 +0,0 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "boost/asio/io_context.hpp"
|
||||
|
||||
#include "webcc/http_async_client.h"
|
||||
#include "webcc/logger.h"
|
||||
|
||||
// In order to test this client, create a file index.html whose content is
|
||||
// simply "Hello, World!", then start a HTTP server with Python 3:
|
||||
// $ python -m http.server
|
||||
// The default port number should be 8000.
|
||||
|
||||
void Test(boost::asio::io_context& io_context) {
|
||||
webcc::HttpRequestPtr request = std::make_shared<webcc::HttpRequest>(
|
||||
webcc::kHttpGet, "/index.html", "localhost", "8000");
|
||||
|
||||
request->Make();
|
||||
|
||||
webcc::HttpAsyncClientPtr client(new webcc::HttpAsyncClient(io_context));
|
||||
|
||||
// Response handler.
|
||||
auto handler = [](webcc::HttpResponsePtr response, webcc::Error error,
|
||||
bool timed_out) {
|
||||
if (error == webcc::kNoError) {
|
||||
std::cout << response->content() << std::endl;
|
||||
} else {
|
||||
std::cout << webcc::DescribeError(error);
|
||||
if (timed_out) {
|
||||
std::cout << " (timed out)";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
client->Request(request, handler);
|
||||
}
|
||||
|
||||
int main() {
|
||||
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
|
||||
|
||||
boost::asio::io_context io_context;
|
||||
|
||||
Test(io_context);
|
||||
Test(io_context);
|
||||
Test(io_context);
|
||||
|
||||
io_context.run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
add_executable(http_hello_client main.cc)
|
||||
|
||||
target_link_libraries(http_hello_client webcc ${Boost_LIBRARIES})
|
||||
target_link_libraries(http_hello_client "${CMAKE_THREAD_LIBS_INIT}")
|
||||
@@ -1,36 +0,0 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "webcc/http_client.h"
|
||||
#include "webcc/logger.h"
|
||||
|
||||
// In order to test this client, create a file index.html whose content is
|
||||
// simply "Hello, World!", then start a HTTP server with Python 3:
|
||||
// $ python -m http.server
|
||||
// The default port number should be 8000.
|
||||
|
||||
void Test() {
|
||||
webcc::HttpRequest request(webcc::kHttpGet, "/index.html", "localhost",
|
||||
"8000");
|
||||
request.Make();
|
||||
|
||||
webcc::HttpClient client;
|
||||
if (client.Request(request)) {
|
||||
std::cout << client.response()->content() << std::endl;
|
||||
} else {
|
||||
std::cout << webcc::DescribeError(client.error());
|
||||
if (client.timed_out()) {
|
||||
std::cout << " (timed out)";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
|
||||
|
||||
Test();
|
||||
Test();
|
||||
Test();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
add_executable(http_ssl_async_client main.cc)
|
||||
|
||||
# TODO
|
||||
set(SSL_LIBS ${OPENSSL_LIBRARIES})
|
||||
if(WIN32)
|
||||
set(SSL_LIBS ${SSL_LIBS} crypt32)
|
||||
endif()
|
||||
|
||||
target_link_libraries(http_ssl_async_client webcc ${Boost_LIBRARIES})
|
||||
target_link_libraries(http_ssl_async_client "${CMAKE_THREAD_LIBS_INIT}")
|
||||
target_link_libraries(http_ssl_async_client ${SSL_LIBS})
|
||||
@@ -0,0 +1,58 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "boost/asio/io_context.hpp"
|
||||
|
||||
#include "webcc/http_ssl_async_client.h"
|
||||
#include "webcc/logger.h"
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
std::string host;
|
||||
std::string url;
|
||||
|
||||
if (argc != 3) {
|
||||
host = "www.boost.org";
|
||||
url = "/LICENSE_1_0.txt";
|
||||
} else {
|
||||
host = argv[1];
|
||||
url = argv[2];
|
||||
}
|
||||
|
||||
std::cout << "Host: " << host << std::endl;
|
||||
std::cout << "URL: " << url << std::endl;
|
||||
std::cout << std::endl;
|
||||
|
||||
WEBCC_LOG_INIT("", webcc::LOG_CONSOLE);
|
||||
|
||||
boost::asio::io_context io_context;
|
||||
|
||||
// Leave port to default value.
|
||||
auto request = webcc::HttpRequest::Make(webcc::kHttpGet, url, host);
|
||||
|
||||
// Verify the certificate of the peer or not.
|
||||
// See HttpSslClient::Request() for more details.
|
||||
bool ssl_verify = false;
|
||||
|
||||
webcc::HttpSslAsyncClientPtr client{
|
||||
new webcc::HttpSslAsyncClient{ io_context, 2000, ssl_verify }
|
||||
};
|
||||
|
||||
// Response callback.
|
||||
auto callback = [](webcc::HttpResponsePtr response, webcc::Error error,
|
||||
bool timed_out) {
|
||||
if (error == webcc::kNoError) {
|
||||
std::cout << response->content() << std::endl;
|
||||
} else {
|
||||
std::cout << DescribeError(error);
|
||||
if (timed_out) {
|
||||
std::cout << " (timed out)";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
client->Request(request, callback);
|
||||
|
||||
io_context.run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -24,7 +24,7 @@ int main(int argc, char* argv[]) {
|
||||
// Leave port to default value.
|
||||
webcc::HttpRequest request(webcc::kHttpGet, url, host);
|
||||
|
||||
request.Make();
|
||||
request.Prepare();
|
||||
|
||||
// Verify the certificate of the peer or not.
|
||||
// See HttpSslClient::Request() for more details.
|
||||
|
||||
Reference in New Issue
Block a user