diff options
Diffstat (limited to 'src/emu/http.cpp')
-rw-r--r-- | src/emu/http.cpp | 69 |
1 files changed, 37 insertions, 32 deletions
diff --git a/src/emu/http.cpp b/src/emu/http.cpp index 3a28fa270c0..2a30a13dba3 100644 --- a/src/emu/http.cpp +++ b/src/emu/http.cpp @@ -2,17 +2,22 @@ // copyright-holders:Miodrag Milanovic /*************************************************************************** -http.cpp + http.cpp -HTTP server handling + HTTP server handling ***************************************************************************/ + +#include "emu.h" +#include "http.h" +#include "server_http.hpp" +#include "server_ws.hpp" + #ifdef __sun #define ASIO_DISABLE_DEV_POLL #define ASIO_HAS_EPOLL #endif -#include "emu.h" #include "server_ws_impl.hpp" #include "server_http_impl.hpp" #include <fstream> @@ -126,7 +131,7 @@ public: std::size_t m_path_end; std::size_t m_query_end; - http_request_impl(std::shared_ptr<webpp::Request> request) : m_request(request) { + http_request_impl(std::shared_ptr<webpp::Request> request) : m_request(std::move(request)) { std::size_t len = m_request->path.length(); m_fragment = m_request->path.find('#'); @@ -139,28 +144,28 @@ public: virtual ~http_request_impl() = default; /** Retrieves the requested resource. */ - virtual const std::string get_resource() { + virtual const std::string get_resource() override { // The entire resource: path, query and fragment. return m_request->path; } /** Returns the path part of the requested resource. */ - virtual const std::string get_path() { + virtual const std::string get_path() override { return m_request->path.substr(0, m_path_end); } /** Returns the query part of the requested resource. */ - virtual const std::string get_query() { + virtual const std::string get_query() override { return m_query == std::string::npos ? "" : m_request->path.substr(m_query, m_query_end); } /** Returns the fragment part of the requested resource. */ - virtual const std::string get_fragment() { + virtual const std::string get_fragment() override { return m_fragment == std::string::npos ? "" : m_request->path.substr(m_fragment); } /** Retrieves a header from the HTTP request. */ - virtual const std::string get_header(const std::string &header_name) { + virtual const std::string get_header(const std::string &header_name) override { auto i = m_request->header.find(header_name); if (i != m_request->header.end()) { return (*i).second; @@ -170,7 +175,7 @@ public: } /** Retrieves a header from the HTTP request. */ - virtual const std::list<std::string> get_headers(const std::string &header_name) { + virtual const std::list<std::string> get_headers(const std::string &header_name) override { std::list<std::string> result; auto range = m_request->header.equal_range(header_name); for (auto i = range.first; i != range.second; i++) { @@ -180,7 +185,7 @@ public: } /** Returns the body that was submitted with the HTTP request. */ - virtual const std::string get_body() { + virtual const std::string get_body() override { // TODO(cbrunschen): What to return here - http_server::Request has a 'content' feld that is never filled in! return ""; } @@ -194,28 +199,28 @@ struct http_response_impl : public http_manager::http_response { std::stringstream m_headers; std::stringstream m_body; - http_response_impl(std::shared_ptr<webpp::Response> response) : m_response(response) { } + http_response_impl(std::shared_ptr<webpp::Response> response) : m_response(std::move(response)) { } virtual ~http_response_impl() = default; /** Sets the HTTP status to be returned to the client. */ - virtual void set_status(int status) { + virtual void set_status(int status) override { m_status = status; } /** Sets the HTTP content type to be returned to the client. */ - virtual void set_content_type(const std::string &content_type) { + virtual void set_content_type(const std::string &content_type) override { m_content_type = content_type; } /** Sets the body to be sent to the client. */ - virtual void set_body(const std::string &body) { + virtual void set_body(const std::string &body) override { m_body.str(""); append_body(body); } /** Appends something to the body to be sent to the client. */ - virtual void append_body(const std::string &body) { + virtual void append_body(const std::string &body) override { m_body << body; } @@ -237,10 +242,10 @@ struct websocket_endpoint_impl : public http_manager::websocket_endpoint { http_manager::websocket_close_handler on_close, http_manager::websocket_error_handler on_error) : m_endpoint(endpoint) { - this->on_open = on_open; - this->on_message = on_message; - this->on_close = on_close; - this->on_error = on_error; + this->on_open = std::move(on_open); + this->on_message = std::move(on_message); + this->on_close = std::move(on_close); + this->on_error = std::move(on_error); } }; @@ -253,7 +258,7 @@ struct websocket_connection_impl : public http_manager::websocket_connection { : m_wsserver(server), m_connection(connection) { } /** Sends a message to the client that is connected on the other end of this Websocket connection. */ - virtual void send_message(const std::string &payload, int opcode) { + virtual void send_message(const std::string &payload, int opcode) override { if (auto connection = m_connection.lock()) { std::shared_ptr<webpp::ws_server::SendStream> message_stream = std::make_shared<webpp::ws_server::SendStream>(); (*message_stream) << payload; @@ -262,7 +267,7 @@ struct websocket_connection_impl : public http_manager::websocket_connection { } /** Closes this open Websocket connection. */ - virtual void close() { + virtual void close() override { if (auto connection = m_connection.lock()) { m_wsserver->send_close(connection, 1000 /* normal close */); } @@ -294,8 +299,8 @@ http_manager::http_manager(bool active, short port, const char *root) } // Determine the file extension. - std::size_t last_slash_pos = path.find_last_of("/"); - std::size_t last_dot_pos = path.find_last_of("."); + std::size_t last_slash_pos = path.find_last_of('/'); + std::size_t last_dot_pos = path.find_last_of('.'); std::string extension; if (last_dot_pos != std::string::npos && last_dot_pos > last_slash_pos) { @@ -329,7 +334,7 @@ http_manager::http_manager(bool active, short port, const char *root) }; m_server->on_upgrade = [this](auto socket, auto request) { - auto connection = std::make_shared<webpp::ws_server::Connection>(socket); + auto connection = std::make_shared<webpp::ws_server::Connection>(*m_io_context, socket); connection->method = std::move(request->method); connection->path = std::move(request->path); connection->http_version = std::move(request->http_version); @@ -420,8 +425,8 @@ bool http_manager::read_file(std::ostream &os, const std::string &path) { std::ostringstream full_path; full_path << m_root << path; util::core_file::ptr f; - osd_file::error e = util::core_file::open(full_path.str(), OPEN_FLAG_READ, f); - if (e == osd_file::error::NONE) + std::error_condition const e = util::core_file::open(full_path.str(), OPEN_FLAG_READ, f); + if (!e) { int c; while ((c = f->getc()) >= 0) @@ -429,7 +434,7 @@ bool http_manager::read_file(std::ostream &os, const std::string &path) { os.put(c); } } - return e == osd_file::error::NONE; + return !e; } void http_manager::serve_document(http_request_ptr request, http_response_ptr response, const std::string &filename) { @@ -544,21 +549,21 @@ http_manager::websocket_endpoint_ptr http_manager::add_endpoint(const std::strin auto endpoint_impl = std::make_shared<websocket_endpoint_impl>(endpoint_ptr, on_open, on_message, on_close, on_error); endpoint.on_open = [&, this, endpoint_impl](std::shared_ptr<webpp::Connection> connection) { - this->on_open(endpoint_impl, connection); + this->on_open(endpoint_impl, std::move(connection)); }; endpoint.on_message = [&, this, endpoint_impl](std::shared_ptr<webpp::Connection> connection, std::shared_ptr<webpp::ws_server::Message> message) { std::string payload = message->string(); int opcode = message->fin_rsv_opcode & 0x0f; - this->on_message(endpoint_impl, connection, payload, opcode); + this->on_message(endpoint_impl, std::move(connection), payload, opcode); }; endpoint.on_close = [&, this, endpoint_impl](std::shared_ptr<webpp::Connection> connection, int status, const std::string& reason) { - this->on_close(endpoint_impl, connection, status, reason); + this->on_close(endpoint_impl, std::move(connection), status, reason); }; endpoint.on_error = [&, this, endpoint_impl](std::shared_ptr<webpp::Connection> connection, const std::error_code& error_code) { - this->on_error(endpoint_impl, connection, error_code); + this->on_error(endpoint_impl, std::move(connection), error_code); }; m_endpoints[path] = endpoint_impl; |