forked from boostorg/beast
Add HTTPS example
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
1.0.0-b15
|
1.0.0-b15
|
||||||
|
|
||||||
* rfc7230 section 3.3.2 compliance
|
* rfc7230 section 3.3.2 compliance
|
||||||
|
* Add HTTPS example
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@@ -23,6 +23,17 @@ else()
|
|||||||
"${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wpedantic")
|
"${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wpedantic")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if (APPLE AND NOT DEFINED ENV{OPENSSL_ROOT_DIR})
|
||||||
|
find_program(HOMEBREW brew)
|
||||||
|
if (NOT HOMEBREW STREQUAL "HOMEBREW-NOTFOUND")
|
||||||
|
execute_process(COMMAND brew --prefix openssl
|
||||||
|
OUTPUT_VARIABLE OPENSSL_ROOT_DIR
|
||||||
|
OUTPUT_STRIP_TRAILING_WHITESPACE)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
find_package(OpenSSL)
|
||||||
|
|
||||||
if (MINGW)
|
if (MINGW)
|
||||||
link_libraries(${Boost_LIBRARIES} ws2_32 mswsock)
|
link_libraries(${Boost_LIBRARIES} ws2_32 mswsock)
|
||||||
endif()
|
endif()
|
||||||
@@ -82,6 +93,11 @@ file(GLOB_RECURSE EXTRAS_INCLUDES
|
|||||||
)
|
)
|
||||||
|
|
||||||
add_subdirectory (examples)
|
add_subdirectory (examples)
|
||||||
|
if (NOT OPENSSL_FOUND)
|
||||||
|
message("OpenSSL not found. Not building examples/ssl")
|
||||||
|
else()
|
||||||
|
add_subdirectory (examples/ssl)
|
||||||
|
endif()
|
||||||
add_subdirectory (test)
|
add_subdirectory (test)
|
||||||
add_subdirectory (test/core)
|
add_subdirectory (test/core)
|
||||||
add_subdirectory (test/http)
|
add_subdirectory (test/http)
|
||||||
|
5
Jamroot
5
Jamroot
@@ -37,7 +37,7 @@ else if [ os.name ] = HAIKU
|
|||||||
if [ os.name ] = NT
|
if [ os.name ] = NT
|
||||||
{
|
{
|
||||||
lib ssl : : <name>ssleay32 ;
|
lib ssl : : <name>ssleay32 ;
|
||||||
lib crypto : : <name>libeay32 ;
|
lib crypto : : <name>libeay32 ;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -87,12 +87,9 @@ project beast
|
|||||||
<library>/boost/coroutine//boost_coroutine
|
<library>/boost/coroutine//boost_coroutine
|
||||||
<library>/boost/filesystem//boost_filesystem
|
<library>/boost/filesystem//boost_filesystem
|
||||||
<library>/boost/program_options//boost_program_options
|
<library>/boost/program_options//boost_program_options
|
||||||
# <library>ssl
|
|
||||||
# <library>crypto
|
|
||||||
<define>BOOST_ALL_NO_LIB=1
|
<define>BOOST_ALL_NO_LIB=1
|
||||||
<define>BOOST_SYSTEM_NO_DEPRECATED=1
|
<define>BOOST_SYSTEM_NO_DEPRECATED=1
|
||||||
<threading>multi
|
<threading>multi
|
||||||
<link>static
|
|
||||||
<runtime-link>shared
|
<runtime-link>shared
|
||||||
<debug-symbols>on
|
<debug-symbols>on
|
||||||
<toolset>gcc:<cxxflags>-std=c++11
|
<toolset>gcc:<cxxflags>-std=c++11
|
||||||
|
@@ -51,7 +51,12 @@ explicit callout ;
|
|||||||
|
|
||||||
install examples
|
install examples
|
||||||
:
|
:
|
||||||
[ glob ../examples/*.* ]
|
[ glob
|
||||||
|
../examples/*.cpp
|
||||||
|
../examples/*.hpp
|
||||||
|
../examples/ssl/*.cpp
|
||||||
|
../examples/ssl/*.hpp
|
||||||
|
]
|
||||||
:
|
:
|
||||||
<location>$(here)/html/examples
|
<location>$(here)/html/examples
|
||||||
;
|
;
|
||||||
|
@@ -83,6 +83,13 @@ int main()
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
[heading HTTPS GET]
|
||||||
|
|
||||||
|
This example demonstrates sending and receiving HTTP messages
|
||||||
|
over a TLS connection. Requires OpenSSL to build.
|
||||||
|
|
||||||
|
* [@examples/http_ssl_example.cpp]
|
||||||
|
|
||||||
[heading HTTP Crawl]
|
[heading HTTP Crawl]
|
||||||
|
|
||||||
This example retrieves the page at each of the most popular domains
|
This example retrieves the page at each of the most popular domains
|
||||||
|
@@ -5,8 +5,6 @@
|
|||||||
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
#
|
#
|
||||||
|
|
||||||
import os ;
|
|
||||||
|
|
||||||
exe http-crawl :
|
exe http-crawl :
|
||||||
http_crawl.cpp
|
http_crawl.cpp
|
||||||
urls_large_data.cpp
|
urls_large_data.cpp
|
||||||
@@ -23,4 +21,3 @@ exe http-example :
|
|||||||
exe websocket-example :
|
exe websocket-example :
|
||||||
websocket_example.cpp
|
websocket_example.cpp
|
||||||
;
|
;
|
||||||
|
|
32
examples/ssl/CMakeLists.txt
Normal file
32
examples/ssl/CMakeLists.txt
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# Part of Beast
|
||||||
|
|
||||||
|
GroupSources(extras/beast extras)
|
||||||
|
GroupSources(include/beast beast)
|
||||||
|
|
||||||
|
GroupSources(examples/ssl "/")
|
||||||
|
|
||||||
|
include_directories(${OPENSSL_INCLUDE_DIR})
|
||||||
|
|
||||||
|
add_executable (http-ssl-example
|
||||||
|
${BEAST_INCLUDES}
|
||||||
|
${EXTRAS_INCLUDES}
|
||||||
|
http_ssl_example.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(http-ssl-example ${OPENSSL_LIBRARIES})
|
||||||
|
|
||||||
|
if (NOT WIN32)
|
||||||
|
target_link_libraries(http-ssl-example ${Boost_LIBRARIES} Threads::Threads)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
add_executable (websocket-ssl-example
|
||||||
|
${BEAST_INCLUDES}
|
||||||
|
${EXTRAS_INCLUDES}
|
||||||
|
websocket_ssl_example.cpp
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(websocket-ssl-example ${OPENSSL_LIBRARIES})
|
||||||
|
|
||||||
|
if (NOT WIN32)
|
||||||
|
target_link_libraries(websocket-ssl-example ${Boost_LIBRARIES} Threads::Threads)
|
||||||
|
endif()
|
49
examples/ssl/Jamfile.v2
Normal file
49
examples/ssl/Jamfile.v2
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
#
|
||||||
|
# Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||||
|
#
|
||||||
|
# Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
#
|
||||||
|
|
||||||
|
import os ;
|
||||||
|
|
||||||
|
if [ os.name ] = SOLARIS
|
||||||
|
{
|
||||||
|
lib socket ;
|
||||||
|
lib nsl ;
|
||||||
|
}
|
||||||
|
else if [ os.name ] = NT
|
||||||
|
{
|
||||||
|
lib ws2_32 ;
|
||||||
|
lib mswsock ;
|
||||||
|
}
|
||||||
|
else if [ os.name ] = HPUX
|
||||||
|
{
|
||||||
|
lib ipv6 ;
|
||||||
|
}
|
||||||
|
else if [ os.name ] = HAIKU
|
||||||
|
{
|
||||||
|
lib network ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if [ os.name ] = NT
|
||||||
|
{
|
||||||
|
lib ssl : : <name>ssleay32 ;
|
||||||
|
lib crypto : : <name>libeay32 ;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lib ssl ;
|
||||||
|
lib crypto ;
|
||||||
|
}
|
||||||
|
|
||||||
|
project
|
||||||
|
: requirements
|
||||||
|
<library>ssl
|
||||||
|
<library>crypto
|
||||||
|
;
|
||||||
|
|
||||||
|
exe http-ssl-example
|
||||||
|
:
|
||||||
|
http_ssl_example.cpp
|
||||||
|
;
|
57
examples/ssl/http_ssl_example.cpp
Normal file
57
examples/ssl/http_ssl_example.cpp
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
//
|
||||||
|
// Copyright (c) 2013-2016 Vinnie Falco (vinnie dot falco at gmail dot com)
|
||||||
|
//
|
||||||
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||||
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <beast/http.hpp>
|
||||||
|
#include <boost/asio.hpp>
|
||||||
|
#include <boost/asio/ssl.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
using boost::asio::connect;
|
||||||
|
using socket = boost::asio::ip::tcp::socket;
|
||||||
|
using resolver = boost::asio::ip::tcp::resolver;
|
||||||
|
using io_service = boost::asio::io_service;
|
||||||
|
namespace ssl = boost::asio::ssl;
|
||||||
|
|
||||||
|
// Normal boost::asio setup
|
||||||
|
std::string const host = "github.com";
|
||||||
|
io_service ios;
|
||||||
|
resolver r{ios};
|
||||||
|
socket sock{ios};
|
||||||
|
connect(sock, r.resolve(resolver::query{host, "https"}));
|
||||||
|
|
||||||
|
// Perform SSL handshaking
|
||||||
|
ssl::context ctx{ssl::context::sslv23};
|
||||||
|
ssl::stream<socket&> stream{sock, ctx};
|
||||||
|
stream.set_verify_mode(ssl::verify_none);
|
||||||
|
stream.handshake(ssl::stream_base::client);
|
||||||
|
|
||||||
|
// Send HTTP request over SSL using Beast
|
||||||
|
beast::http::request_v1<beast::http::empty_body> req;
|
||||||
|
req.method = "GET";
|
||||||
|
req.url = "/";
|
||||||
|
req.version = 11;
|
||||||
|
req.headers.insert("Host", host + ":" +
|
||||||
|
std::to_string(sock.remote_endpoint().port()));
|
||||||
|
req.headers.insert("User-Agent", "Beast");
|
||||||
|
beast::http::prepare(req);
|
||||||
|
beast::http::write(stream, req);
|
||||||
|
|
||||||
|
// Receive and print HTTP response using Beast
|
||||||
|
beast::streambuf sb;
|
||||||
|
beast::http::response_v1<beast::http::streambuf_body> resp;
|
||||||
|
beast::http::read(stream, sb, resp);
|
||||||
|
std::cout << resp;
|
||||||
|
|
||||||
|
// Shut down SSL on the stream
|
||||||
|
boost::system::error_code ec;
|
||||||
|
stream.shutdown(ec);
|
||||||
|
if(ec && ec != boost::asio::error::eof)
|
||||||
|
std::cout << "error: " << ec.message();
|
||||||
|
}
|
Reference in New Issue
Block a user