|
4 | 4 |
|
5 | 5 | #include "TServerSocket.h" |
6 | 6 |
|
| 7 | +#include <algorithm> |
| 8 | +#include <cctype> |
7 | 9 | #include <cstdint> |
8 | 10 | #include <cstring> |
9 | 11 | #include <string> |
10 | 12 | #include <thread> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +/// Return a lower-cased copy of the input string. |
| 16 | +static std::string ToLower(std::string s) |
| 17 | +{ |
| 18 | + std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); }); |
| 19 | + return s; |
| 20 | +} |
| 21 | + |
| 22 | +/// Accept a PUT request: read headers + body, optionally respond to Expect: 100-continue, send 200 OK. |
| 23 | +static void TaskRecvPut(TServerSocket *serverSocket, std::string *requestHeaders, std::string *requestBody) |
| 24 | +{ |
| 25 | + requestHeaders->clear(); |
| 26 | + requestBody->clear(); |
| 27 | + auto sock = serverSocket->Accept(); |
| 28 | + |
| 29 | + const char *eof = "\r\n\r\n"; |
| 30 | + const std::size_t eofLen = strlen(eof); |
| 31 | + std::size_t nextInEof = 0; |
| 32 | + char c; |
| 33 | + while (sock->RecvRaw(&c, 1)) { |
| 34 | + requestHeaders->push_back(c); |
| 35 | + if (c == eof[nextInEof]) { |
| 36 | + if (++nextInEof == eofLen) |
| 37 | + break; |
| 38 | + } else { |
| 39 | + nextInEof = 0; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // If the client sent Expect: 100-continue, respond with HTTP 100 before reading the body |
| 44 | + std::string headersLower = ToLower(*requestHeaders); |
| 45 | + if (headersLower.find("expect: 100-continue") != std::string::npos) { |
| 46 | + const char *continueResponse = "HTTP/1.1 100 Continue\r\n\r\n"; |
| 47 | + sock->SendRaw(continueResponse, strlen(continueResponse)); |
| 48 | + } |
| 49 | + |
| 50 | + // Parse content-length (case-insensitive) |
| 51 | + std::size_t contentLength = 0; |
| 52 | + auto pos = headersLower.find("content-length: "); |
| 53 | + if (pos != std::string::npos) { |
| 54 | + auto valStart = pos + strlen("content-length: "); |
| 55 | + auto valEnd = headersLower.find("\r\n", valStart); |
| 56 | + contentLength = std::stoul(headersLower.substr(valStart, valEnd - valStart)); |
| 57 | + } |
| 58 | + |
| 59 | + if (contentLength > 0) { |
| 60 | + requestBody->resize(contentLength); |
| 61 | + sock->RecvRaw(&(*requestBody)[0], contentLength); |
| 62 | + } |
| 63 | + |
| 64 | + const char *response = "HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"; |
| 65 | + sock->SendRaw(response, strlen(response)); |
| 66 | + sock->Close(); |
| 67 | +} |
11 | 68 |
|
12 | 69 | static void TaskRecv(TServerSocket *serverSocket, std::string *request) |
13 | 70 | { |
@@ -63,3 +120,64 @@ TEST(RCurlConnection, Cred) |
63 | 120 | threadRecv.join(); |
64 | 121 | EXPECT_EQ(std::string::npos, request.find("\r\nAuthorization: ")); |
65 | 122 | } |
| 123 | + |
| 124 | +TEST(RCurlConnection, Put) |
| 125 | +{ |
| 126 | + TServerSocket sock(0, false, TServerSocket::kDefaultBacklog, -1, ESocketBindOption::kInaddrLoopback); |
| 127 | + const std::string url = |
| 128 | + std::string("http://") + sock.GetLocalInetAddress().GetHostAddress() + ":" + std::to_string(sock.GetLocalPort()); |
| 129 | + |
| 130 | + const unsigned char payload[] = "Hello, S3!"; |
| 131 | + const std::size_t payloadLen = sizeof(payload) - 1; // exclude null terminator |
| 132 | + |
| 133 | + std::string headers; |
| 134 | + std::string body; |
| 135 | + std::thread threadRecv(TaskRecvPut, &sock, &headers, &body); |
| 136 | + |
| 137 | + ROOT::Internal::RCurlConnection conn(url); |
| 138 | + auto status = conn.SendPutReq(payload, payloadLen); |
| 139 | + |
| 140 | + threadRecv.join(); |
| 141 | + EXPECT_TRUE(static_cast<bool>(status)); |
| 142 | + EXPECT_EQ(0u, headers.find("PUT ")); |
| 143 | + |
| 144 | + // Normalize headers to lower-case for case-insensitive matching |
| 145 | + std::string headersLower = ToLower(headers); |
| 146 | + auto clPos = headersLower.find("content-length: " + std::to_string(payloadLen)); |
| 147 | + ASSERT_NE(std::string::npos, clPos) << "content-length header not found in request"; |
| 148 | + |
| 149 | + EXPECT_EQ(std::string(reinterpret_cast<const char *>(payload), payloadLen), body); |
| 150 | +} |
| 151 | + |
| 152 | +/// PUT with a payload larger than libcurl's internal Expect: 100-continue threshold (1 MB since curl 7.69). |
| 153 | +/// Verifies that the server-side 100 Continue handshake works and all bytes arrive correctly. |
| 154 | +TEST(RCurlConnection, PutLargeExpect100) |
| 155 | +{ |
| 156 | + TServerSocket sock(0, false, TServerSocket::kDefaultBacklog, -1, ESocketBindOption::kInaddrLoopback); |
| 157 | + const std::string url = |
| 158 | + std::string("http://") + sock.GetLocalInetAddress().GetHostAddress() + ":" + std::to_string(sock.GetLocalPort()); |
| 159 | + |
| 160 | + // 2 MB payload with a known repeating pattern |
| 161 | + const std::size_t payloadLen = 2 * 1024 * 1024; |
| 162 | + std::vector<unsigned char> payload(payloadLen); |
| 163 | + for (std::size_t i = 0; i < payloadLen; ++i) |
| 164 | + payload[i] = static_cast<unsigned char>(i & 0xFF); |
| 165 | + |
| 166 | + std::string headers; |
| 167 | + std::string body; |
| 168 | + std::thread threadRecv(TaskRecvPut, &sock, &headers, &body); |
| 169 | + |
| 170 | + ROOT::Internal::RCurlConnection conn(url); |
| 171 | + auto status = conn.SendPutReq(payload.data(), payloadLen); |
| 172 | + |
| 173 | + threadRecv.join(); |
| 174 | + EXPECT_TRUE(static_cast<bool>(status)); |
| 175 | + EXPECT_EQ(0u, headers.find("PUT ")); |
| 176 | + |
| 177 | + std::string headersLower = ToLower(headers); |
| 178 | + EXPECT_NE(std::string::npos, headersLower.find("expect: 100-continue")) |
| 179 | + << "large upload should include Expect: 100-continue header"; |
| 180 | + EXPECT_NE(std::string::npos, headersLower.find("content-length: " + std::to_string(payloadLen))); |
| 181 | + ASSERT_EQ(payloadLen, body.size()); |
| 182 | + EXPECT_EQ(0, memcmp(body.data(), payload.data(), payloadLen)); |
| 183 | +} |
0 commit comments