| 114 | } |
| 115 | |
| 116 | SC::Result SC::HttpTestClient::put(AsyncEventLoop& loop, StringSpan url, StringSpan body, TimeMs delay) |
| 117 | { |
| 118 | bodyDelay = delay; |
| 119 | eventLoop = &loop; |
| 120 | responseHasNoBody = false; |
| 121 | |
| 122 | uint16_t port; |
| 123 | HttpURLParser urlParser; |
| 124 | SC_TRY(urlParser.parse(url)); |
| 125 | SC_TRY_MSG(urlParser.protocol == "http", "Invalid protocol"); |
| 126 | // TODO: Make DNS Resolution asynchronous |
| 127 | char buffer[256]; |
| 128 | Span<char> ipAddress = {buffer}; |
| 129 | SC_TRY(SocketDNS::resolveDNS(urlParser.hostname, ipAddress)) |
| 130 | port = urlParser.port; |
| 131 | SocketIPAddress localHost; |
| 132 | SC_TRY(localHost.fromAddressPort({ipAddress, true, StringEncoding::Ascii}, port)); |
| 133 | SC_TRY(eventLoop->createAsyncTCPSocket(localHost.getAddressFamily(), clientSocket)); |
| 134 | |
| 135 | { |
| 136 | GrowableBuffer<decltype(content)> gb = {content}; |
| 137 | |
| 138 | HttpStringAppend& sb = static_cast<HttpStringAppend&>(static_cast<IGrowableBuffer&>(gb)); |
| 139 | sb.clear(); |
| 140 | SC_TRY(sb.append("PUT ")); |
| 141 | SC_TRY(sb.append(urlParser.path)); |
| 142 | SC_TRY(sb.append(" HTTP/1.1\r\n")); |
| 143 | SC_TRY(sb.append("User-agent: SC\r\n")); |
| 144 | SC_TRY(sb.append("Host: 127.0.0.1\r\n")); |
| 145 | char contentLengthBuffer[32]; |
| 146 | ::snprintf(contentLengthBuffer, sizeof(contentLengthBuffer), "%zu", body.sizeInBytes()); |
| 147 | StringSpan cl({contentLengthBuffer, ::strlen(contentLengthBuffer)}, false, StringEncoding::Ascii); |
| 148 | SC_TRY(sb.append("Content-Length: ")); |
| 149 | SC_TRY(sb.append(cl)); |
| 150 | SC_TRY(sb.append("\r\n\r\n")); |
| 151 | headerBytes = content.size(); |
| 152 | SC_TRY(sb.append(body)); |
| 153 | } |
| 154 | |
| 155 | connectAsync.callback.bind<HttpTestClient, &HttpTestClient::startSendingHeaders>(*this); |
| 156 | parser = {}; |
| 157 | parser.type = HttpParser::Type::Response; |
| 158 | responseHasNoBody = false; |
| 159 | return connectAsync.start(*eventLoop, clientSocket, localHost); |
| 160 | } |
| 161 | |
| 162 | SC::Result SC::HttpTestClient::postMultipart(AsyncEventLoop& loop, StringSpan url, StringSpan fieldName, |
| 163 | StringSpan fileName, StringSpan fileContent, TimeMs delay) |
no test coverage detected