Repository navigation
Use UrlLib transport fixes branch #263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
CedricGuillemet
wants to merge
4
commits into
BabylonJS:main
Choose a base branch
from
CedricGuillemet:use-urllib-transport-fixes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b96e90a
Use UrlLib transport fixes branch
CedricGuillemet a6c6fb8
Add UrlLib consumer transport regressions
CedricGuillemet 851229e
Remove test README from transport regression PR
CedricGuillemet 8f2fae2
Pin UrlLib Android POST byte fix
CedricGuillemet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Tests/UnitTests/Source/Android/app/src/main/AndroidManifest.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,347 @@ | ||
| #include "HttpTestServer.h" | ||
|
|
||
| #if defined(_WIN32) | ||
| #include <winsock2.h> | ||
| #include <ws2tcpip.h> | ||
| #else | ||
| #include <netinet/in.h> | ||
| #include <sys/select.h> | ||
| #include <sys/socket.h> | ||
| #include <unistd.h> | ||
| #endif | ||
|
|
||
| #include <algorithm> | ||
| #include <array> | ||
| #include <atomic> | ||
| #include <charconv> | ||
| #include <chrono> | ||
| #include <cctype> | ||
| #include <cstdint> | ||
| #include <exception> | ||
| #include <map> | ||
| #include <stdexcept> | ||
| #include <string_view> | ||
| #include <thread> | ||
|
|
||
| namespace | ||
| { | ||
| #if defined(_WIN32) | ||
| using NativeSocket = SOCKET; | ||
| using SocketLength = int; | ||
| constexpr auto InvalidSocket = INVALID_SOCKET; | ||
| #else | ||
| using NativeSocket = int; | ||
| using SocketLength = socklen_t; | ||
| constexpr auto InvalidSocket = -1; | ||
| #endif | ||
|
|
||
| struct Socket | ||
| { | ||
| NativeSocket Value; | ||
|
|
||
| explicit Socket(NativeSocket value) | ||
| : Value{value} | ||
| { | ||
| } | ||
| Socket(const Socket&) = delete; | ||
| Socket& operator=(const Socket&) = delete; | ||
|
|
||
| ~Socket() | ||
| { | ||
| if (Value != InvalidSocket) | ||
| { | ||
| #if defined(_WIN32) | ||
| ::closesocket(Value); | ||
| #else | ||
| ::close(Value); | ||
| #endif | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| std::string Lowercase(std::string value) | ||
| { | ||
| std::transform(value.begin(), value.end(), value.begin(), | ||
| [](unsigned char c) { return static_cast<char>(std::tolower(c)); }); | ||
| return value; | ||
| } | ||
|
|
||
| bool Wait(NativeSocket socket, bool writing, const std::atomic<bool>& stop, | ||
| std::chrono::steady_clock::time_point deadline) | ||
| { | ||
| while (!stop.load()) | ||
| { | ||
| if (std::chrono::steady_clock::now() >= deadline) | ||
| { | ||
| throw std::runtime_error{"HTTP test server: connection timed out"}; | ||
| } | ||
| fd_set descriptors; | ||
| FD_ZERO(&descriptors); | ||
| FD_SET(socket, &descriptors); | ||
| timeval timeout{0, 100000}; | ||
| const auto ready = ::select( | ||
| #if defined(_WIN32) | ||
| 0, | ||
| #else | ||
| socket + 1, | ||
| #endif | ||
| writing ? nullptr : &descriptors, writing ? &descriptors : nullptr, nullptr, &timeout); | ||
| if (ready < 0) | ||
| { | ||
| throw std::runtime_error{"HTTP test server: select failed"}; | ||
| } | ||
| if (ready > 0) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| void Send(NativeSocket socket, std::string_view bytes, const std::atomic<bool>& stop) | ||
| { | ||
| const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds{5}; | ||
| while (!bytes.empty() && Wait(socket, true, stop, deadline)) | ||
| { | ||
| const auto count = ::send(socket, bytes.data(), static_cast<int>(bytes.size()), | ||
| #if defined(MSG_NOSIGNAL) | ||
| MSG_NOSIGNAL | ||
| #else | ||
| 0 | ||
| #endif | ||
| ); | ||
| if (count <= 0) | ||
| { | ||
| throw std::runtime_error{"HTTP test server: send failed"}; | ||
| } | ||
| bytes.remove_prefix(static_cast<size_t>(count)); | ||
| } | ||
| } | ||
|
|
||
| void Respond(NativeSocket socket, const std::atomic<bool>& stop) | ||
| { | ||
| #if defined(_WIN32) | ||
| const DWORD timeout = 1000; | ||
| #else | ||
| const timeval timeout{1, 0}; | ||
| #endif | ||
| if (::setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<const char*>(&timeout), sizeof(timeout)) != 0 || | ||
| ::setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<const char*>(&timeout), sizeof(timeout)) != 0) | ||
| { | ||
| throw std::runtime_error{"HTTP test server: could not set socket timeouts"}; | ||
| } | ||
| #if defined(__APPLE__) | ||
| const int noSignal = 1; | ||
| if (::setsockopt(socket, SOL_SOCKET, SO_NOSIGPIPE, &noSignal, sizeof(noSignal)) != 0) | ||
| { | ||
| throw std::runtime_error{"HTTP test server: SO_NOSIGPIPE failed"}; | ||
| } | ||
| #endif | ||
| const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds{5}; | ||
| std::string request; | ||
| size_t bodyStart = std::string::npos; | ||
| size_t bodySize{}; | ||
| std::map<std::string, std::string> headers; | ||
| while (Wait(socket, false, stop, deadline)) | ||
| { | ||
| std::array<char, 4096> buffer; | ||
| const auto count = ::recv(socket, buffer.data(), static_cast<int>(buffer.size()), 0); | ||
| if (count <= 0) | ||
| { | ||
| throw std::runtime_error{"HTTP test server: incomplete request"}; | ||
| } | ||
| request.append(buffer.data(), static_cast<size_t>(count)); | ||
| if (request.size() > 65536) | ||
| { | ||
| throw std::runtime_error{"HTTP test server: request exceeds test fixture limit"}; | ||
| } | ||
| if (bodyStart == std::string::npos) | ||
| { | ||
| const auto end = request.find("\r\n\r\n"); | ||
| if (end == std::string::npos) | ||
| { | ||
| continue; | ||
| } | ||
| bodyStart = end + 4; | ||
| for (auto line = request.find("\r\n") + 2; line < end;) | ||
| { | ||
| const auto next = request.find("\r\n", line); | ||
| const auto colon = request.find(':', line); | ||
| if (colon == std::string::npos || colon >= next) | ||
| { | ||
| throw std::runtime_error{"HTTP test server: malformed request header"}; | ||
| } | ||
| const auto value = request.find_first_not_of(" \t", colon + 1); | ||
| headers.emplace(Lowercase(request.substr(line, colon - line)), | ||
| value < next ? request.substr(value, next - value) : ""); | ||
| line = next + 2; | ||
| } | ||
| if (headers.contains("transfer-encoding")) | ||
| { | ||
| throw std::runtime_error{"HTTP test server expects length-delimited string bodies"}; | ||
| } | ||
| if (const auto length = headers.find("content-length"); length != headers.end()) | ||
| { | ||
| const auto& value = length->second; | ||
| const auto parsed = std::from_chars(value.data(), value.data() + value.size(), bodySize); | ||
| if (parsed.ec != std::errc{} || parsed.ptr != value.data() + value.size() || bodySize > 32768) | ||
| { | ||
| throw std::runtime_error{"HTTP test server: invalid Content-Length"}; | ||
| } | ||
| } | ||
| if (const auto expect = headers.find("expect"); | ||
| expect != headers.end() && Lowercase(expect->second) == "100-continue") | ||
| { | ||
| Send(socket, "HTTP/1.1 100 Continue\r\n\r\n", stop); | ||
| } | ||
| } | ||
| if (request.size() >= bodyStart + bodySize) | ||
| { | ||
| break; | ||
| } | ||
| } | ||
| if (stop.load()) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| const auto methodEnd = request.find(' '); | ||
| const auto pathEnd = request.find(' ', methodEnd + 1); | ||
| if (methodEnd == std::string::npos || pathEnd == std::string::npos) | ||
| { | ||
| throw std::runtime_error{"HTTP test server: malformed request line"}; | ||
| } | ||
| const auto method = request.substr(0, methodEnd); | ||
| const auto path = request.substr(methodEnd + 1, pathEnd - methodEnd - 1); | ||
| std::string status{"200 OK"}; | ||
| std::string body; | ||
| std::string responseHeaders; | ||
| if (path == "/echo") | ||
| { | ||
| body = request.substr(bodyStart, bodySize); | ||
| responseHeaders = "Content-Type: text/plain; charset=utf-8\r\n"; | ||
| responseHeaders += "x-request-method: " + method + "\r\n"; | ||
| responseHeaders += "x-request-body-length: " + std::to_string(body.size()) + "\r\n"; | ||
| responseHeaders += "x-request-content-type: " + (headers.contains("content-type") ? headers.at("content-type") : "<absent>") + "\r\n"; | ||
| responseHeaders += "x-request-test: " + (headers.contains("x-test") ? headers.at("x-test") : "<absent>") + "\r\n"; | ||
| } | ||
| else if (path == "/no-content-type") | ||
| { | ||
| body = "{\"message\":\"hello\"}"; | ||
| } | ||
| else if (path == "/empty") | ||
| { | ||
| } | ||
| else if (path == "/no-content") | ||
| { | ||
| status = "204 No Content"; | ||
| } | ||
| else if (path == "/not-found") | ||
| { | ||
| status = "404 Not Found"; | ||
| } | ||
| else if (path == "/server-error") | ||
| { | ||
| status = "500 Internal Server Error"; | ||
| } | ||
| else if (path == "/not-modified") | ||
| { | ||
| status = "304 Not Modified"; | ||
| } | ||
| else | ||
| { | ||
| throw std::runtime_error{"HTTP test server: unexpected path " + path}; | ||
| } | ||
|
|
||
| Send(socket, "HTTP/1.1 " + status + "\r\n" + responseHeaders + "Content-Length: " + std::to_string(body.size()) + "\r\nConnection: close\r\n\r\n" + body, stop); | ||
| } | ||
| } | ||
|
|
||
| struct HttpTestServer::Impl | ||
| { | ||
| #if defined(_WIN32) | ||
| struct Winsock | ||
| { | ||
| Winsock() | ||
| { | ||
| WSADATA data{}; | ||
| if (::WSAStartup(MAKEWORD(2, 2), &data) != 0) | ||
| { | ||
| throw std::runtime_error{"HTTP test server: WSAStartup failed"}; | ||
| } | ||
| } | ||
| ~Winsock() { ::WSACleanup(); } | ||
| } Sockets; | ||
| #endif | ||
| Socket Listener{::socket(AF_INET, SOCK_STREAM, 0)}; | ||
| uint16_t Port{}; | ||
| std::exception_ptr Error; | ||
| std::atomic<bool> Stopping{}; | ||
| std::thread Worker; | ||
|
|
||
| void Stop() | ||
| { | ||
| Stopping.store(true); | ||
| if (Worker.joinable()) | ||
| { | ||
| Worker.join(); | ||
| } | ||
| } | ||
|
|
||
| ~Impl() { Stop(); } | ||
|
|
||
| Impl() | ||
| { | ||
| sockaddr_in address{}; | ||
| address.sin_family = AF_INET; | ||
| address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | ||
| SocketLength length = sizeof(address); | ||
| if (Listener.Value == InvalidSocket || | ||
| ::bind(Listener.Value, reinterpret_cast<const sockaddr*>(&address), sizeof(address)) != 0 || | ||
| ::getsockname(Listener.Value, reinterpret_cast<sockaddr*>(&address), &length) != 0 || | ||
| ::listen(Listener.Value, 8) != 0) | ||
| { | ||
| throw std::runtime_error{"HTTP test server: could not listen on loopback"}; | ||
| } | ||
| Port = ntohs(address.sin_port); | ||
| Worker = std::thread{[this] { | ||
| try | ||
| { | ||
| while (Wait(Listener.Value, false, Stopping, (std::chrono::steady_clock::time_point::max)())) | ||
| { | ||
| const Socket connection{::accept(Listener.Value, nullptr, nullptr)}; | ||
| if (connection.Value == InvalidSocket) | ||
| { | ||
| throw std::runtime_error{"HTTP test server: accept failed"}; | ||
| } | ||
| Respond(connection.Value, Stopping); | ||
| } | ||
| } | ||
| catch (...) | ||
| { | ||
| Error = std::current_exception(); | ||
| } | ||
| }}; | ||
| } | ||
| }; | ||
|
|
||
| HttpTestServer::HttpTestServer() | ||
| : m_impl{std::make_unique<Impl>()} | ||
| { | ||
| } | ||
|
|
||
| HttpTestServer::~HttpTestServer() = default; | ||
|
|
||
| std::string HttpTestServer::Url() const | ||
| { | ||
| return "http://127.0.0.1:" + std::to_string(m_impl->Port); | ||
| } | ||
|
|
||
| void HttpTestServer::Stop() | ||
| { | ||
| m_impl->Stop(); | ||
| if (m_impl->Error) | ||
| { | ||
| std::rethrow_exception(m_impl->Error); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.