From f0586366daf8d5c0732727a8012f1adcd4f343e1 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Sun, 23 Oct 2022 03:41:03 +0200 Subject: [PATCH] Take some from jarred --- benchmarks/load_test.c | 22 ++++++++++++++++++++++ src/ChunkedEncoding.h | 18 +++++++++--------- src/HttpParser.h | 4 ++++ src/HttpResponse.h | 11 +++++++++-- 4 files changed, 44 insertions(+), 11 deletions(-) diff --git a/benchmarks/load_test.c b/benchmarks/load_test.c index 6f382ea..82b64da 100644 --- a/benchmarks/load_test.c +++ b/benchmarks/load_test.c @@ -1,7 +1,29 @@ /* This is a simple yet efficient WebSocket server benchmark much like WRK */ #define _BSD_SOURCE + +#ifdef __APPLE__ +#include + +#define htobe16(x) OSSwapHostToBigInt16(x) +#define htole16(x) OSSwapHostToLittleInt16(x) +#define be16toh(x) OSSwapBigToHostInt16(x) +#define le16toh(x) OSSwapLittleToHostInt16(x) + +#define htobe32(x) OSSwapHostToBigInt32(x) +#define htole32(x) OSSwapHostToLittleInt32(x) +#define be32toh(x) OSSwapBigToHostInt32(x) +#define le32toh(x) OSSwapLittleToHostInt32(x) + +#define htobe64(x) OSSwapHostToBigInt64(x) +#define htole64(x) OSSwapHostToLittleInt64(x) +#define be64toh(x) OSSwapBigToHostInt64(x) +#define le64toh(x) OSSwapLittleToHostInt64(x) +#else #include +#endif + + #include #include diff --git a/src/ChunkedEncoding.h b/src/ChunkedEncoding.h index 5b2e091..803f62c 100644 --- a/src/ChunkedEncoding.h +++ b/src/ChunkedEncoding.h @@ -29,12 +29,12 @@ namespace uWS { - uint32_t STATE_HAS_SIZE = 0x80000000; - uint32_t STATE_IS_CHUNKED = 0x40000000; - uint32_t STATE_SIZE_MASK = 0x3FFFFFFF; + constexpr uint32_t STATE_HAS_SIZE = 0x80000000; + constexpr uint32_t STATE_IS_CHUNKED = 0x40000000; + constexpr uint32_t STATE_SIZE_MASK = 0x3FFFFFFF; /* Reads hex number until CR or out of data to consume. Updates state. Returns bytes consumed. */ - void consumeHexNumber(std::string_view &data, unsigned int &state) { + inline void consumeHexNumber(std::string_view &data, unsigned int &state) { /* Consume everything higher than 32 */ while (data.length() && data.data()[0] > 32) { @@ -63,11 +63,11 @@ namespace uWS { } } - unsigned int chunkSize(unsigned int state) { + inline unsigned int chunkSize(unsigned int state) { return state & STATE_SIZE_MASK; } - void decChunkSize(unsigned int &state, unsigned int by) { + inline void decChunkSize(unsigned int &state, unsigned int by) { //unsigned int bits = state & STATE_IS_CHUNKED; @@ -76,17 +76,17 @@ namespace uWS { //state |= bits; } - bool hasChunkSize(unsigned int state) { + inline bool hasChunkSize(unsigned int state) { return state & STATE_HAS_SIZE; } /* Are we in the middle of parsing chunked encoding? */ - bool isParsingChunkedEncoding(unsigned int state) { + inline bool isParsingChunkedEncoding(unsigned int state) { return state & ~STATE_SIZE_MASK; } /* Returns next chunk (empty or not), or if all data was consumed, nullopt is returned. */ - std::optional getNextChunk(std::string_view &data, unsigned int &state, bool trailer = false) { + static std::optional getNextChunk(std::string_view &data, unsigned int &state, bool trailer = false) { while (data.length()) { diff --git a/src/HttpParser.h b/src/HttpParser.h index 0ec530a..d85a86a 100644 --- a/src/HttpParser.h +++ b/src/HttpParser.h @@ -113,6 +113,10 @@ public: return std::string_view(headers->value.data(), querySeparator); } + std::string_view getFullUrl() { + return std::string_view(headers->value.data(), headers->value.length()); + } + std::string_view getMethod() { return std::string_view(headers->key.data(), headers->key.length()); } diff --git a/src/HttpResponse.h b/src/HttpResponse.h index f7337db..9b3d505 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -401,8 +401,8 @@ public: /* Try and end the response. Returns [true, true] on success. * Starts a timeout in some cases. Returns [ok, hasResponded] */ - std::pair tryEnd(std::string_view data, uintmax_t totalSize = 0) { - return {internalEnd(data, totalSize, true), hasResponded()}; + std::pair tryEnd(std::string_view data, uintmax_t totalSize = 0, bool closeConnection = false) { + return {internalEnd(data, totalSize, true, true, closeConnection), hasResponded()}; } /* Write parts of the response in chunking fashion. Starts timeout if failed. */ @@ -445,6 +445,13 @@ public: return httpResponseData->offset; } + /* If you are messing around with sendfile you might want to override the offset. */ + void overrideWriteOffset(uintmax_t offset) { + HttpResponseData *httpResponseData = getHttpResponseData(); + + httpResponseData->offset = offset; + } + /* Checking if we have fully responded and are ready for another request */ bool hasResponded() { HttpResponseData *httpResponseData = getHttpResponseData();