mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Refactor beast core, http, tests, and examples:
* Fix warnings * Port cmake scripts to linux * Add command line options for running test suites * Add examples to CMakeLists * Return std::uint64_t from writer::content_length * basic_parser::write takes asio::const_buffer instead of pointer and size * Turn message test back on now that it passes * Rename to http::headers, use std::allocator, remove http_headers * http::message::method is now a string * Refactor to_string for ConstBufferSequence * Remove chunk_encode from the public interface * Initialize members for default constructed iterators * Disallow default construction for dependent buffer sequences Refactor http::message serialization: * Serialization no longer creates a copy of the headers and modifies them * New function prepare(), sets Connection, Transfer-Encoding, Content-Length based on the body attributes and caller options. Callers can use prepare() to have the fields set automatically, or they can set the fields manually. * Use write for operator<< * Tests for serialization
This commit is contained in:
@@ -5,26 +5,56 @@ GroupSources(examples)
|
||||
|
||||
add_executable (http-crawl
|
||||
${BEAST_INCLUDES}
|
||||
http_crawl.cpp
|
||||
urls_large_data.hpp
|
||||
urls_large_data.cpp
|
||||
http_crawl.cpp
|
||||
)
|
||||
|
||||
if (NOT WIN32)
|
||||
target_link_libraries(http-crawl ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
|
||||
endif()
|
||||
|
||||
add_executable (http-server
|
||||
${BEAST_INCLUDES}
|
||||
file_body.hpp
|
||||
http_async_server.hpp
|
||||
http_stream.hpp
|
||||
http_stream.ipp
|
||||
http_sync_server.hpp
|
||||
sig_wait.hpp
|
||||
http_server.cpp
|
||||
)
|
||||
|
||||
if (NOT WIN32)
|
||||
target_link_libraries(http-server ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
|
||||
endif()
|
||||
|
||||
add_executable (http-example
|
||||
${BEAST_INCLUDES}
|
||||
http_example.cpp
|
||||
)
|
||||
|
||||
if (NOT WIN32)
|
||||
target_link_libraries(http-example ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
|
||||
endif()
|
||||
|
||||
add_executable (websocket-echo
|
||||
${BEAST_INCLUDES}
|
||||
sig_wait.hpp
|
||||
websocket_async_echo_peer.hpp
|
||||
websocket_sync_echo_peer.hpp
|
||||
websocket_echo.cpp
|
||||
)
|
||||
|
||||
if (NOT WIN32)
|
||||
target_link_libraries(websocket-echo ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
|
||||
endif()
|
||||
|
||||
add_executable (websocket-example
|
||||
${BEAST_INCLUDES}
|
||||
websocket_example.cpp
|
||||
)
|
||||
|
||||
if (NOT WIN32)
|
||||
target_link_libraries(websocket-example ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
|
||||
endif()
|
||||
|
||||
@@ -36,8 +36,8 @@ struct file_body
|
||||
|
||||
class writer
|
||||
{
|
||||
std::size_t size_;
|
||||
std::size_t offset_ = 0;
|
||||
std::uint64_t size_;
|
||||
std::uint64_t offset_ = 0;
|
||||
std::string const& path_;
|
||||
FILE* file_ = nullptr;
|
||||
char buf_[4096];
|
||||
@@ -69,7 +69,7 @@ struct file_body
|
||||
size_ = boost::filesystem::file_size(path_);
|
||||
}
|
||||
|
||||
std::size_t
|
||||
std::uint64_t
|
||||
content_length() const
|
||||
{
|
||||
return size_;
|
||||
@@ -79,7 +79,11 @@ struct file_body
|
||||
boost::tribool
|
||||
operator()(resume_context&&, error_code&, Write&& write)
|
||||
{
|
||||
buf_len_ = std::min(size_ - offset_, sizeof(buf_));
|
||||
if(size_ - offset_ < sizeof(buf_))
|
||||
buf_len_ = static_cast<std::size_t>(
|
||||
size_ - offset_);
|
||||
else
|
||||
buf_len_ = sizeof(buf_);
|
||||
auto const nread = fread(buf_, 1, sizeof(buf_), file_);
|
||||
(void)nread;
|
||||
offset_ += buf_len_;
|
||||
@@ -20,8 +20,8 @@
|
||||
#ifndef BEAST_EXAMPLE_HTTP_ASYNC_SERVER_H_INCLUDED
|
||||
#define BEAST_EXAMPLE_HTTP_ASYNC_SERVER_H_INCLUDED
|
||||
|
||||
#include "file_body.h"
|
||||
#include "http_stream.h"
|
||||
#include "file_body.hpp"
|
||||
#include "http_stream.hpp"
|
||||
|
||||
#include <beast/placeholders.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
@@ -131,6 +131,7 @@ private:
|
||||
{404, "Not Found", req_.version});
|
||||
resp.headers.replace("Server", "http_async_server");
|
||||
resp.body = "The file '" + path + "' was not found";
|
||||
prepare(resp);
|
||||
stream_.async_write(std::move(resp),
|
||||
std::bind(&peer::on_write, shared_from_this(),
|
||||
asio::placeholders::error));
|
||||
@@ -141,6 +142,7 @@ private:
|
||||
resp.headers.replace("Server", "http_async_server");
|
||||
resp.headers.replace("Content-Type", "text/html");
|
||||
resp.body = path;
|
||||
prepare(resp);
|
||||
stream_.async_write(std::move(resp),
|
||||
std::bind(&peer::on_write, shared_from_this(),
|
||||
asio::placeholders::error));
|
||||
@@ -17,8 +17,8 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include "http_stream.h"
|
||||
#include "urls_large_data.h"
|
||||
#include "http_stream.hpp"
|
||||
#include "urls_large_data.hpp"
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <iostream>
|
||||
@@ -46,10 +46,11 @@ int main(int, char const*[])
|
||||
stream<ip::tcp::socket> hs(ios);
|
||||
connect(hs.lowest_layer(), it);
|
||||
auto ep = hs.lowest_layer().remote_endpoint();
|
||||
request<empty_body> req({method_t::http_get, "/", 11});
|
||||
request<empty_body> req({"GET", "/", 11});
|
||||
req.headers.insert("Host", host +
|
||||
std::string(":") + std::to_string(ep.port()));
|
||||
req.headers.insert("User-Agent", "beast/http");
|
||||
prepare(req);
|
||||
hs.write(req);
|
||||
response<string_body> resp;
|
||||
hs.read(resp);
|
||||
|
||||
@@ -23,9 +23,10 @@ int main()
|
||||
using namespace beast::http;
|
||||
|
||||
// Send HTTP request using beast
|
||||
request<empty_body> req({method_t::http_get, "/", 11});
|
||||
request<empty_body> req({"GET", "/", 11});
|
||||
req.headers.replace("Host", host + ":" + std::to_string(sock.remote_endpoint().port()));
|
||||
req.headers.replace("User-Agent", "Beast");
|
||||
prepare(req);
|
||||
write(sock, req);
|
||||
|
||||
// Receive and print HTTP response using beast
|
||||
|
||||
@@ -17,9 +17,9 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include "http_async_server.h"
|
||||
#include "http_sync_server.h"
|
||||
#include "sig_wait.h"
|
||||
#include "http_async_server.hpp"
|
||||
#include "http_sync_server.hpp"
|
||||
#include "sig_wait.hpp"
|
||||
|
||||
#include <boost/program_options.hpp>
|
||||
|
||||
|
||||
@@ -20,8 +20,8 @@
|
||||
#ifndef BEAST_EXAMPLE_HTTP_SYNC_SERVER_H_INCLUDED
|
||||
#define BEAST_EXAMPLE_HTTP_SYNC_SERVER_H_INCLUDED
|
||||
|
||||
#include "file_body.h"
|
||||
#include "http_stream.h"
|
||||
#include "file_body.hpp"
|
||||
#include "http_stream.hpp"
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <cstdint>
|
||||
@@ -159,6 +159,7 @@ public:
|
||||
{404, "Not Found", req.version});
|
||||
resp.headers.replace("Server", "http_sync_server");
|
||||
resp.body = "The file '" + path + "' was not found";
|
||||
prepare(resp);
|
||||
hs.write(resp, ec);
|
||||
if(ec)
|
||||
break;
|
||||
@@ -168,6 +169,7 @@ public:
|
||||
resp.headers.replace("Server", "http_sync_server");
|
||||
resp.headers.replace("Content-Type", "text/html");
|
||||
resp.body = path;
|
||||
prepare(resp);
|
||||
hs.write(resp, ec);
|
||||
if(ec)
|
||||
break;
|
||||
@@ -17,7 +17,7 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include "urls_large_data.h"
|
||||
#include "urls_large_data.hpp"
|
||||
|
||||
// Data from Alexa top 1 million sites
|
||||
// http://s3.amazonaws.com/alexa-static/top-1m.csv.zip
|
||||
|
||||
@@ -17,9 +17,9 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include "websocket_async_echo_peer.h"
|
||||
#include "websocket_sync_echo_peer.h"
|
||||
#include "sig_wait.h"
|
||||
#include "websocket_async_echo_peer.hpp"
|
||||
#include "websocket_sync_echo_peer.hpp"
|
||||
#include "sig_wait.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#include <beast/to_string.hpp>
|
||||
#include <beast/websocket.hpp>
|
||||
#include <beast/buffers_debug.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
@@ -33,6 +33,5 @@ int main()
|
||||
opcode op;
|
||||
ws.read(op, sb);
|
||||
ws.close(close_code::normal);
|
||||
std::cout <<
|
||||
beast::debug::buffers_to_string(sb.data()) << "\n";
|
||||
std::cout << to_string(sb.data()) << "\n";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user