diff --git a/Makefile b/Makefile index 41d330c..dfd822c 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ EXAMPLE_FILES := HelloWorld EchoServer BroadcastingEchoServer THREADED_EXAMPLE_FILES := HelloWorldThreaded EchoServerThreaded -override CXXFLAGS += -lpthread -std=c++17 -Isrc -IuSockets/src +override CXXFLAGS += -lpthread -Wconversion -std=c++17 -Isrc -IuSockets/src override LDFLAGS += uSockets/*.o -lz # WITH_OPENSSL=1 enables OpenSSL 1.1+ support diff --git a/src/AsyncSocket.h b/src/AsyncSocket.h index 61ee312..3957fed 100644 --- a/src/AsyncSocket.h +++ b/src/AsyncSocket.h @@ -82,7 +82,7 @@ protected: LoopData *loopData = getLoopData(); if (loopData->corkedSocket == this && loopData->corkOffset + size < LoopData::CORK_BUFFER_SIZE) { char *sendBuffer = loopData->corkBuffer + loopData->corkOffset; - loopData->corkOffset += size; + loopData->corkOffset += (int) size; return {sendBuffer, false}; } else { /* Slow path for now, we want to always be corked if possible */ @@ -92,7 +92,7 @@ protected: /* Returns the user space backpressure. */ int getBufferedAmount() { - return getAsyncSocketData()->buffer.size(); + return (int) getAsyncSocketData()->buffer.size(); } /* Returns the remote IP address or empty string on failure */ @@ -118,7 +118,7 @@ protected: /* We are limited if we have a per-socket buffer */ if (asyncSocketData->buffer.length()) { /* Write off as much as we can */ - int written = us_socket_write(SSL, (us_socket_t *) this, asyncSocketData->buffer.data(), asyncSocketData->buffer.length(), /*nextLength != 0 | */length); + int written = us_socket_write(SSL, (us_socket_t *) this, asyncSocketData->buffer.data(), (int) asyncSocketData->buffer.length(), /*nextLength != 0 | */length); /* On failure return, otherwise continue down the function */ if ((unsigned int) written < asyncSocketData->buffer.length()) { diff --git a/src/HttpParser.h b/src/HttpParser.h index 62333a9..a1c21e7 100644 --- a/src/HttpParser.h +++ b/src/HttpParser.h @@ -150,7 +150,7 @@ private: if (*postPaddedBuffer == '\r') { if ((postPaddedBuffer != end) & (postPaddedBuffer[1] == '\n') & (i > 0)) { headers->key = std::string_view(nullptr, 0); - return (postPaddedBuffer + 2) - start; + return (unsigned int) ((postPaddedBuffer + 2) - start); } else { return 0; } @@ -182,11 +182,11 @@ private: length -= consumed; consumedTotal += consumed; - req->headers->value = std::string_view(req->headers->value.data(), std::max(0, req->headers->value.length() - 9)); + req->headers->value = std::string_view(req->headers->value.data(), std::max(0, (int) req->headers->value.length() - 9)); /* Parse query */ const char *querySeparatorPtr = (const char *) memchr(req->headers->value.data(), '?', req->headers->value.length()); - req->querySeparator = (querySeparatorPtr ? querySeparatorPtr : req->headers->value.data() + req->headers->value.length()) - req->headers->value.data(); + req->querySeparator = (int) ((querySeparatorPtr ? querySeparatorPtr : req->headers->value.data() + req->headers->value.length()) - req->headers->value.data()); /* If returned socket is not what we put in we need * to break here as we either have upgraded to @@ -257,16 +257,16 @@ public: } } else if (fallback.length()) { - int had = fallback.length(); + int had = (int) fallback.length(); - int maxCopyDistance = std::min(MAX_FALLBACK_SIZE - fallback.length(), (size_t) length); + int maxCopyDistance = (int) std::min(MAX_FALLBACK_SIZE - fallback.length(), (size_t) length); /* We don't want fallback to be short string optimized, since we want to move it */ fallback.reserve(fallback.length() + maxCopyDistance + std::max(MINIMUM_HTTP_POST_PADDING, sizeof(std::string))); fallback.append(data, maxCopyDistance); // break here on break - std::pair consumed = fenceAndConsumePostPadded(fallback.data(), fallback.length(), user, &req, requestHandler, dataHandler); + std::pair consumed = fenceAndConsumePostPadded(fallback.data(), (int) fallback.length(), user, &req, requestHandler, dataHandler); if (consumed.second != user) { return consumed.second; } diff --git a/src/HttpResponse.h b/src/HttpResponse.h index 4053a86..76a027a 100644 --- a/src/HttpResponse.h +++ b/src/HttpResponse.h @@ -88,7 +88,7 @@ private: /* If no total size given then assume this chunk is everything */ if (!totalSize) { - totalSize = data.length(); + totalSize = (int) data.length(); } HttpResponseData *httpResponseData = getHttpResponseData(); @@ -99,11 +99,11 @@ private: /* Do not allow sending 0 chunk here */ if (data.length()) { Super::write("\r\n", 2); - writeUnsignedHex(data.length()); + writeUnsignedHex((unsigned int) data.length()); Super::write("\r\n", 2); /* Ignoring optional for now */ - Super::write(data.data(), data.length()); + Super::write(data.data(), (int) data.length()); } /* Terminating 0 chunk */ @@ -138,7 +138,7 @@ private: * if it failed to drain any prior failed header writes */ /* Write as much as possible without causing backpressure */ - auto [written, failed] = Super::write(data.data(), data.length(), optional); + auto [written, failed] = Super::write(data.data(), (int) data.length(), optional); httpResponseData->offset += written; /* Success is when we wrote the entire thing without any failures */ @@ -185,7 +185,7 @@ public: httpResponseData->state |= HttpResponseData::HTTP_STATUS_CALLED; Super::write("HTTP/1.1 ", 9); - Super::write(status.data(), status.length()); + Super::write(status.data(), (int) status.length()); Super::write("\r\n", 2); return this; } @@ -194,16 +194,16 @@ public: HttpResponse *writeHeader(std::string_view key, std::string_view value) { writeStatus(HTTP_200_OK); - Super::write(key.data(), key.length()); + Super::write(key.data(), (int) key.length()); Super::write(": ", 2); - Super::write(value.data(), value.length()); + Super::write(value.data(), (int) value.length()); Super::write("\r\n", 2); return this; } /* Write an HTTP header with unsigned int value */ HttpResponse *writeHeader(std::string_view key, unsigned int value) { - Super::write(key.data(), key.length()); + Super::write(key.data(), (int) key.length()); Super::write(": ", 2); writeUnsigned(value); Super::write("\r\n", 2); @@ -212,7 +212,7 @@ public: /* End the response with an optional data chunk. Always starts a timeout. */ void end(std::string_view data = {}) { - internalEnd(data, data.length(), false); + internalEnd(data, (int) data.length(), false); } /* Try and end the response. Returns [true, true] on success. @@ -242,10 +242,10 @@ public: } Super::write("\r\n", 2); - writeUnsignedHex(data.length()); + writeUnsignedHex((unsigned int) data.length()); Super::write("\r\n", 2); - auto [written, failed] = Super::write(data.data(), data.length()); + auto [written, failed] = Super::write(data.data(), (int) data.length()); if (failed) { Super::timeout(HTTP_TIMEOUT_S); } diff --git a/src/HttpRouter.h b/src/HttpRouter.h index 3bd2aad..9ab8032 100644 --- a/src/HttpRouter.h +++ b/src/HttpRouter.h @@ -107,7 +107,7 @@ private: /* Set URL for router. Will reset any URL cache */ inline void setUrl(std::string_view url) { /* Remove / from input URL */ - currentUrl = url.substr(std::min(url.length(), 1)); + currentUrl = url.substr(std::min((unsigned int) url.length(), 1)); urlSegmentTop = -1; } @@ -230,7 +230,7 @@ public: node = getNode(node, std::string(getUrlSegment(i))); } /* Insert handler in order sorted by priority (most significant 1 byte) */ - node->handlers.insert(std::upper_bound(node->handlers.begin(), node->handlers.end(), priority | handlers.size()), priority | handlers.size()); + node->handlers.insert(std::upper_bound(node->handlers.begin(), node->handlers.end(), (uint32_t) (priority | handlers.size())), (uint32_t) (priority | handlers.size())); } /* Alloate this handler */ diff --git a/src/Utilities.h b/src/Utilities.h index 123bbfc..c84029e 100644 --- a/src/Utilities.h +++ b/src/Utilities.h @@ -34,7 +34,7 @@ inline int u32toaHex(uint32_t value, char *dst) { value /= 16; } while (value > 0); - int ret = p - temp; + int ret = (int) (p - temp); do { *dst++ = *--p; @@ -47,11 +47,11 @@ inline int u32toa(uint32_t value, char *dst) { char temp[10]; char *p = temp; do { - *p++ = (char) (value % 10) + '0'; + *p++ = (char) ((value % 10) + '0'); value /= 10; } while (value > 0); - int ret = p - temp; + int ret = (int) (p - temp); do { *dst++ = *--p; diff --git a/src/WebSocket.h b/src/WebSocket.h index eb24526..9c36e32 100644 --- a/src/WebSocket.h +++ b/src/WebSocket.h @@ -84,7 +84,7 @@ public: auto[sendBuffer, requiresWrite] = Super::getSendBuffer(messageFrameSize); protocol::formatMessage(sendBuffer, message.data(), message.length(), opCode, message.length(), compress); if (requiresWrite) { - auto[written, failed] = Super::write(sendBuffer, messageFrameSize); + auto[written, failed] = Super::write(sendBuffer, (int) messageFrameSize); /* For now, we are slow here (fix!) */ free(sendBuffer); @@ -110,9 +110,9 @@ public: /* Format and send the close frame */ static const int MAX_CLOSE_PAYLOAD = 123; - int length = std::min(MAX_CLOSE_PAYLOAD, message.length()); + int length = (int) std::min(MAX_CLOSE_PAYLOAD, message.length()); char closePayload[MAX_CLOSE_PAYLOAD + 2]; - int closePayloadLength = protocol::formatClosePayload(closePayload, code, message.data(), length); + int closePayloadLength = (int) protocol::formatClosePayload(closePayload, (uint16_t) code, message.data(), length); bool ok = send(std::string_view(closePayload, closePayloadLength), OpCode::CLOSE); /* FIN if we are ok and not corked */ diff --git a/src/WebSocketContext.h b/src/WebSocketContext.h index 0e17ec9..fb96fa9 100644 --- a/src/WebSocketContext.h +++ b/src/WebSocketContext.h @@ -179,7 +179,7 @@ private: } else { /* Here we never mind any size optimizations as we are in the worst possible path */ webSocketData->fragmentBuffer.append(data, length); - webSocketData->controlTipLength += length; + webSocketData->controlTipLength += (int) length; if (!remainingBytes && fin) { char *controlBuffer = (char *) webSocketData->fragmentBuffer.data() + webSocketData->fragmentBuffer.length() - webSocketData->controlTipLength; diff --git a/src/WebSocketContextData.h b/src/WebSocketContextData.h index d7e0af8..51b4f7f 100644 --- a/src/WebSocketContextData.h +++ b/src/WebSocketContextData.h @@ -57,7 +57,7 @@ struct WebSocketContextData { /* We rely on writing to regular asyncSockets */ auto *asyncSocket = (AsyncSocket *) s->user; - auto [written, failed] = asyncSocket->write(data.data(), data.length()); + auto [written, failed] = asyncSocket->write(data.data(), (int) data.length()); if (!failed) { asyncSocket->timeout(this->idleTimeout); } else { diff --git a/src/WebSocketHandshake.h b/src/WebSocketHandshake.h index 453473c..a539c75 100644 --- a/src/WebSocketHandshake.h +++ b/src/WebSocketHandshake.h @@ -120,10 +120,10 @@ public: for (int i = 0; i < 5; i++) { uint32_t tmp = b_output[i]; char *bytes = (char *) &b_output[i]; - bytes[3] = tmp & 0xff; - bytes[2] = (tmp >> 8) & 0xff; - bytes[1] = (tmp >> 16) & 0xff; - bytes[0] = (tmp >> 24) & 0xff; + bytes[3] = (char) (tmp & 0xff); + bytes[2] = (char) ((tmp >> 8) & 0xff); + bytes[1] = (char) ((tmp >> 16) & 0xff); + bytes[0] = (char) ((tmp >> 24) & 0xff); } base64((unsigned char *) b_output, output); } diff --git a/src/WebSocketProtocol.h b/src/WebSocketProtocol.h index d3c4159..ea69d1c 100644 --- a/src/WebSocketProtocol.h +++ b/src/WebSocketProtocol.h @@ -189,23 +189,23 @@ static inline size_t formatMessage(char *dst, const char *src, size_t length, Op size_t headerLength; if (reportedLength < 126) { headerLength = 2; - dst[1] = reportedLength; + dst[1] = (char) reportedLength; } else if (reportedLength <= UINT16_MAX) { headerLength = 4; dst[1] = 126; - uint16_t tmp = cond_byte_swap(reportedLength); + uint16_t tmp = cond_byte_swap((uint16_t) reportedLength); memcpy(&dst[2], &tmp, sizeof(uint16_t)); } else { headerLength = 10; dst[1] = 127; - uint64_t tmp = cond_byte_swap(reportedLength); + uint64_t tmp = cond_byte_swap((uint64_t) reportedLength); memcpy(&dst[2], &tmp, sizeof(uint64_t)); } int flags = 0; - dst[0] = (flags & SND_NO_FIN ? 0 : 128) | (compressed ? SND_COMPRESSED : 0); + dst[0] = (char) ((flags & SND_NO_FIN ? 0 : 128) | (compressed ? SND_COMPRESSED : 0)); if (!(flags & SND_CONTINUATION)) { - dst[0] |= opCode; + dst[0] |= (char) opCode; } char mask[4]; @@ -320,7 +320,7 @@ protected: } src += payLength + MESSAGE_HEADER; - length -= payLength + MESSAGE_HEADER; + length -= (unsigned int) (payLength + MESSAGE_HEADER); wState->state.spillLength = 0; return false; } else { @@ -419,7 +419,7 @@ public: } if (length) { memcpy(wState->state.spill, src, length); - wState->state.spillLength = length; + wState->state.spillLength = length & 0xf; } } else if (consumeContinuation(src, length, wState, user)) { goto parseNext;