| 1233 | } |
| 1234 | |
| 1235 | HttpClient::RequestId HttpClient::postAsync(String url, std::span<Slice> headers, String json, float timeout, const ContentPartHandler& partCallback, const ContentHandler& callback) { |
| 1236 | if (_stopped.load(std::memory_order_relaxed)) { |
| 1237 | callback(std::nullopt); |
| 1238 | return 0; |
| 1239 | } |
| 1240 | auto request = register_http_client_request(); |
| 1241 | if (!request) { |
| 1242 | callback(std::nullopt); |
| 1243 | return 0; |
| 1244 | } |
| 1245 | std::vector<std::pair<std::string, std::string>> postHeaders; |
| 1246 | for (const auto& header : headers) { |
| 1247 | auto parts = header.split(":"sv); |
| 1248 | if (parts.size() == 2) { |
| 1249 | postHeaders.emplace_back(parts.front().toString(), parts.back().toString()); |
| 1250 | } |
| 1251 | } |
| 1252 | postHeaders.emplace_back("Content-Type"s, "application/json"s); |
| 1253 | auto callbackFunc = std::make_shared<ContentHandler>(callback); |
| 1254 | auto partCallbackFunc = std::make_shared<ContentPartHandler>(partCallback); |
| 1255 | SharedAsyncThread.run([request, json = json.toString(), timeout, urlStr = url.toString(), partCallbackFunc, callbackFunc, headers = std::move(postHeaders)]() { |
| 1256 | std::vector<std::string> headerNames; |
| 1257 | std::vector<std::string> headerValues; |
| 1258 | std::vector<const char*> headerNamePtrs; |
| 1259 | std::vector<const char*> headerValuePtrs; |
| 1260 | DoraXrtHttpResponse response; |
| 1261 | prepare_xrt_http_headers(headerNames, headerValues, headerNamePtrs, headerValuePtrs, headers); |
| 1262 | if (*partCallbackFunc) { |
| 1263 | auto stopped = std::make_shared<std::atomic<bool>>(false); |
| 1264 | auto completion = std::make_shared<XrtPostStreamCompletion>(); |
| 1265 | completion->callback = callbackFunc; |
| 1266 | XrtPostStreamContext streamContext{request, partCallbackFunc, stopped, completion}; |
| 1267 | int statusCode = 0; |
| 1268 | auto status = DoraXrtHttpExecuteStream( |
| 1269 | "POST", |
| 1270 | urlStr.c_str(), |
| 1271 | headerNamePtrs.data(), |
| 1272 | headerValuePtrs.data(), |
| 1273 | headerNamePtrs.size(), |
| 1274 | json.data(), |
| 1275 | json.size(), |
| 1276 | to_timeout_ms(timeout), |
| 1277 | 0, |
| 1278 | should_cancel_xrt_http, |
| 1279 | request.get(), |
| 1280 | on_xrt_post_stream_chunk, |
| 1281 | &streamContext, |
| 1282 | &statusCode); |
| 1283 | if (status != 0 || statusCode < 200 || statusCode >= 400 || stopped->load(std::memory_order_relaxed)) { |
| 1284 | request_finish_xrt_post_stream_in_logic(completion, false); |
| 1285 | } else { |
| 1286 | request_finish_xrt_post_stream_in_logic(completion, true); |
| 1287 | } |
| 1288 | } else { |
| 1289 | auto status = DoraXrtHttpExecute( |
| 1290 | "POST", |
| 1291 | urlStr.c_str(), |
| 1292 | headerNamePtrs.data(), |
nothing calls this directly
no test coverage detected