diff options
Diffstat (limited to 'src/lib/util')
-rw-r--r-- | src/lib/util/chd.cpp | 14 | ||||
-rw-r--r-- | src/lib/util/client_http.hpp | 12 | ||||
-rw-r--r-- | src/lib/util/client_https.hpp | 2 | ||||
-rw-r--r-- | src/lib/util/hashing.h | 54 | ||||
-rw-r--r-- | src/lib/util/server_http.hpp | 11 |
5 files changed, 73 insertions, 20 deletions
diff --git a/src/lib/util/chd.cpp b/src/lib/util/chd.cpp index 0db64344861..dc7c334b1bf 100644 --- a/src/lib/util/chd.cpp +++ b/src/lib/util/chd.cpp @@ -885,7 +885,7 @@ chd_error chd_file::read_hunk(uint32_t hunknum, void *buffer) // get a pointer to the map entry uint64_t blockoffs; uint32_t blocklen; - uint32_t blockcrc; + util::crc32_t blockcrc; uint8_t *rawmap; uint8_t *dest = reinterpret_cast<uint8_t *>(buffer); switch (m_version) @@ -2147,12 +2147,12 @@ void chd_file::decompress_v5_map() // read the reader uint8_t rawbuf[16]; file_read(m_mapoffset, rawbuf, sizeof(rawbuf)); - uint32_t mapbytes = be_read(&rawbuf[0], 4); - uint64_t firstoffs = be_read(&rawbuf[4], 6); - uint16_t mapcrc = be_read(&rawbuf[10], 2); - uint8_t lengthbits = rawbuf[12]; - uint8_t selfbits = rawbuf[13]; - uint8_t parentbits = rawbuf[14]; + uint32_t const mapbytes = be_read(&rawbuf[0], 4); + uint64_t const firstoffs = be_read(&rawbuf[4], 6); + util::crc16_t const mapcrc = be_read(&rawbuf[10], 2); + uint8_t const lengthbits = rawbuf[12]; + uint8_t const selfbits = rawbuf[13]; + uint8_t const parentbits = rawbuf[14]; // now read the map std::vector<uint8_t> compressed(mapbytes); diff --git a/src/lib/util/client_http.hpp b/src/lib/util/client_http.hpp index df188090f87..63bd60c1f32 100644 --- a/src/lib/util/client_http.hpp +++ b/src/lib/util/client_http.hpp @@ -70,6 +70,8 @@ namespace webpp { public: /// Set timeout on requests in seconds. Default value: 0 (no timeout). size_t timeout = 0; + /// Set connect timeout in seconds. Default value: 0 (Config::timeout is then used instead). + size_t timeout_connect=0; /// Set proxy server (server:port) std::string proxy_server; }; @@ -213,12 +215,14 @@ namespace webpp { virtual void connect()=0; - std::shared_ptr<asio::system_timer> get_timeout_timer() { - if (config.timeout == 0) + std::shared_ptr<asio::system_timer> get_timeout_timer(size_t timeout=0) { + if(timeout==0) + timeout=config.timeout; + if (timeout == 0) return nullptr; auto timer = std::make_shared<asio::system_timer>(io_context); - timer->expires_from_now(std::chrono::seconds(config.timeout)); + timer->expires_from_now(std::chrono::seconds(timeout)); timer->async_wait([this](const std::error_code& ec) { if (!ec) { close(); @@ -401,7 +405,7 @@ namespace webpp { socket = std::make_unique<HTTP>(io_context); } - auto timer = get_timeout_timer(); + auto timer = get_timeout_timer(config.timeout_connect); asio::async_connect(*socket, it, [this,timer] (const std::error_code &ec, asio::ip::tcp::resolver::iterator /*it*/){ if (timer) diff --git a/src/lib/util/client_https.hpp b/src/lib/util/client_https.hpp index fc1f12bb075..3300ea6865f 100644 --- a/src/lib/util/client_https.hpp +++ b/src/lib/util/client_https.hpp @@ -56,7 +56,7 @@ namespace webpp { socket = std::make_unique<HTTPS>(io_context, m_context); } - auto timer = get_timeout_timer(); + auto timer = get_timeout_timer(config.timeout_connect); asio::async_connect(socket->lowest_layer(), it, [this, timer] (const std::error_code &ec, asio::ip::tcp::resolver::iterator /*it*/) { if (timer) diff --git a/src/lib/util/hashing.h b/src/lib/util/hashing.h index 906bad047b4..5e9c9134c97 100644 --- a/src/lib/util/hashing.h +++ b/src/lib/util/hashing.h @@ -15,10 +15,12 @@ #include "osdcore.h" #include "corestr.h" -#include <string> #include "md5.h" #include "sha1.h" +#include <functional> +#include <string> + namespace util { //************************************************************************** @@ -132,13 +134,23 @@ protected: // final digest struct crc32_t { - bool operator==(const crc32_t &rhs) const { return m_raw == rhs.m_raw; } - bool operator!=(const crc32_t &rhs) const { return m_raw != rhs.m_raw; } + crc32_t() { } + constexpr crc32_t(const crc32_t &rhs) = default; + constexpr crc32_t(const uint32_t crc) : m_raw(crc) { } + + constexpr bool operator==(const crc32_t &rhs) const { return m_raw == rhs.m_raw; } + constexpr bool operator!=(const crc32_t &rhs) const { return m_raw != rhs.m_raw; } + + crc32_t &operator=(const crc32_t &rhs) = default; crc32_t &operator=(const uint32_t crc) { m_raw = crc; return *this; } - operator uint32_t() const { return m_raw; } + + constexpr operator uint32_t() const { return m_raw; } + bool from_string(const char *string, int length = -1); std::string as_string() const; + uint32_t m_raw; + static const crc32_t null; }; @@ -178,13 +190,23 @@ protected: // final digest struct crc16_t { - bool operator==(const crc16_t &rhs) const { return m_raw == rhs.m_raw; } - bool operator!=(const crc16_t &rhs) const { return m_raw != rhs.m_raw; } + crc16_t() { } + constexpr crc16_t(const crc16_t &rhs) = default; + constexpr crc16_t(const uint16_t crc) : m_raw(crc) { } + + constexpr bool operator==(const crc16_t &rhs) const { return m_raw == rhs.m_raw; } + constexpr bool operator!=(const crc16_t &rhs) const { return m_raw != rhs.m_raw; } + + crc16_t &operator=(const crc16_t &rhs) = default; crc16_t &operator=(const uint16_t crc) { m_raw = crc; return *this; } - operator uint16_t() const { return m_raw; } + + constexpr operator uint16_t() const { return m_raw; } + bool from_string(const char *string, int length = -1); std::string as_string() const; + uint16_t m_raw; + static const crc16_t null; }; @@ -220,4 +242,22 @@ protected: } // namespace util +namespace std { + +template <> struct hash<::util::crc32_t> +{ + typedef ::util::crc32_t argument_type; + typedef std::size_t result_type; + result_type operator()(argument_type const & s) const { return std::hash<std::uint32_t>()(s); } +}; + +template <> struct hash<::util::crc16_t> +{ + typedef ::util::crc16_t argument_type; + typedef std::size_t result_type; + result_type operator()(argument_type const & s) const { return std::hash<std::uint16_t>()(s); } +}; + +} // namespace std + #endif // __HASHING_H__ diff --git a/src/lib/util/server_http.hpp b/src/lib/util/server_http.hpp index 57fd57e6993..978e9e3ce40 100644 --- a/src/lib/util/server_http.hpp +++ b/src/lib/util/server_http.hpp @@ -89,6 +89,12 @@ namespace webpp { void send(std::string str) { m_ostream << m_header.str() << "Content-Length: " << str.length() << "\r\n\r\n" << str; } size_t size() const { return m_streambuf.size(); } std::shared_ptr<socket_type> socket() { return m_socket; } + + /// If true, force server to close the connection after the response have been sent. + /// + /// This is useful when implementing a HTTP/1.0-server sending content + /// without specifying the content length. + bool close_connection_after_response = false; }; class Content : public std::istream { @@ -182,7 +188,7 @@ namespace webpp { void remove_handler(std::string regex) { std::lock_guard<std::mutex> lock(m_resource_mutex); - for (auto &it = m_resource.begin(); it != m_resource.end(); ++it) + for (auto it = m_resource.begin(); it != m_resource.end(); ++it) { if (it->first.getstr() == regex) { @@ -428,6 +434,9 @@ namespace webpp { return; } + if (response->close_connection_after_response) + return; + auto range = request->header.equal_range("Connection"); case_insensitive_equals check; for (auto it = range.first; it != range.second; ++it) { |