Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions ixwebsocket/IXHttp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ namespace ix
char* p_end {};
errno = 0;
long val = std::strtol(p, &p_end, 10);
if (p_end == p // invalid argument
|| errno == ERANGE // out of range
)
if (p[0] < '0' || p[0] > '9' || *p_end != '\0' || errno == ERANGE)
{
return std::make_tuple(
false, "Error parsing HTTP Header 'Content-Length'", httpRequest);
Expand Down Expand Up @@ -213,6 +211,10 @@ namespace ix
ss << "Content-Length: " << response->body.size() << "\r\n";
for (auto&& it : response->headers)
{
// The generated length must match the actual body and appear only once.
if (!CaseInsensitiveLess::cmp(it.first, "Content-Length") &&
!CaseInsensitiveLess::cmp("Content-Length", it.first))
continue;
ss << it.first << ": " << it.second << "\r\n";
}
ss << "\r\n";
Expand Down
8 changes: 8 additions & 0 deletions ixwebsocket/IXSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ namespace ix
#endif
}

// Darwin TSan can confuse reused per-thread errno storage with shared memory.
// These accessors touch only OS thread-local error state; keep callers instrumented.
#if defined(__APPLE__) && defined(__clang__)
__attribute__((no_sanitize("thread")))
#endif
int Socket::getErrno()
{
int err;
Expand All @@ -319,6 +324,9 @@ namespace ix
return err;
}

#if defined(__APPLE__) && defined(__clang__)
__attribute__((no_sanitize("thread")))
#endif
void Socket::setErrno(int err)
{
#ifdef _WIN32
Expand Down
3 changes: 2 additions & 1 deletion ixwebsocket/IXWebSocketHttpHeaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ namespace ix
std::string lineStr(line);

int start = colon + 1;
while (start < (int) lineStr.size() && lineStr[start] == ' ')
while (start < (int) lineStr.size() &&
(lineStr[start] == ' ' || lineStr[start] == '\t'))
{
start++;
}
Expand Down
66 changes: 66 additions & 0 deletions test/IXHttpTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#include <algorithm>
#include <catch_amalgamated.hpp>
#include <clocale>
#include <cstring>
#include <iostream>
#include <ixwebsocket/IXHttp.h>
#include <ixwebsocket/IXHttpClient.h>
#include <ixwebsocket/IXSocket.h>
#include <string.h>
#include <thread>
#include <vector>
Expand Down Expand Up @@ -100,3 +102,67 @@ namespace ix
}

} // namespace ix

namespace
{
class HttpMemorySocket : public ix::Socket
{
public:
explicit HttpMemorySocket(const std::string& input = std::string())
: _input(input)
{
}
std::ptrdiff_t recv(void* buffer, size_t length) override
{
const size_t size = (std::min) (length, _input.size() - _offset);
std::memcpy(buffer, _input.data() + _offset, size);
_offset += size;
return static_cast<std::ptrdiff_t>(size);
}
std::ptrdiff_t send(char* buffer, size_t length) override
{
output.append(buffer, length);
return static_cast<std::ptrdiff_t>(length);
}
std::string output;

private:
std::string _input;
size_t _offset = 0;
};
} // namespace

TEST_CASE("HTTP requests reject partially parsed content lengths", "[http][framing]")
{
const std::string length =
GENERATE(std::string("1x"), std::string("+1"), std::string("1, 2"), std::string("\v1"));
std::unique_ptr<ix::Socket> socket(
new HttpMemorySocket("POST / HTTP/1.1\r\nContent-Length: " + length + "\r\n\r\nx"));
auto result = ix::Http::parseRequest(socket, 1);
CHECK_FALSE(std::get<0>(result));
CHECK_FALSE(std::get<1>(result).empty());
}

TEST_CASE("HTTP requests accept exact decimal content lengths", "[http][framing]")
{
const std::string length = GENERATE(std::string("1"), std::string("0001"), std::string("\t1"));
std::unique_ptr<ix::Socket> socket(
new HttpMemorySocket("POST / HTTP/1.1\r\nContent-Length: " + length + "\r\n\r\nx"));
auto result = ix::Http::parseRequest(socket, 1);
REQUIRE(std::get<0>(result));
CHECK(std::get<2>(result)->body == "x");
}

TEST_CASE("HTTP responses send exactly one accurate content length", "[http][framing]")
{
const std::string supplied = GENERATE(std::string("999"), std::string("3"));
std::unique_ptr<ix::Socket> socket(new HttpMemorySocket());
ix::WebSocketHttpHeaders headers;
headers["content-length"] = supplied;
headers["X-Custom"] = "preserved";
auto response =
std::make_shared<ix::HttpResponse>(200, "OK", ix::HttpErrorCode::Ok, headers, "abc");
REQUIRE(ix::Http::sendResponse(response, socket));
const auto& wire = static_cast<HttpMemorySocket&>(*socket).output;
CHECK(wire == "HTTP/1.1 200 OK\r\nContent-Length: 3\r\nX-Custom: preserved\r\n\r\nabc");
}
24 changes: 24 additions & 0 deletions test/IXSocketTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <ixwebsocket/IXSocket.h>
#include <ixwebsocket/IXSocketFactory.h>
#include <string.h>
#include <thread>

using namespace ix;

Expand Down Expand Up @@ -107,6 +108,29 @@ TEST_CASE("Empty and cancelled socket reads do not receive data", "[socket][read
CHECK(socket.reads == 0);
}

TEST_CASE("Socket error state remains local when threads are reused", "[socket][errno]")
{
std::atomic<bool> isolated {true};
auto churn = [&](int error)
{
for (int i = 0; i < 16; ++i)
{
std::thread worker(
[&, error]
{
Socket::setErrno(error);
if (Socket::getErrno() != error) isolated = false;
});
worker.join();
}
};
std::thread a(churn, EWOULDBLOCK);
std::thread b(churn, EINVAL);
a.join();
b.join();
CHECK(isolated.load());
}

namespace ix
{
void testSocket(const std::string& host,
Expand Down
Loading