MCPcopy Create free account
hub / github.com/apache/trafficserver / httpCall

Method httpCall

plugins/experimental/wasm/ats_context.cc:537–592  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

535}
536
537WasmResult
538Context::httpCall(std::string_view /* target ATS_UNUSED */, const Pairs &request_headers, std::string_view request_body,
539 const Pairs & /* request_trailers ATS_UNUSED */, int /* timeout_millisconds ATS_UNUSED */, uint32_t *token_ptr)
540{
541 Wasm *wasm = this->wasm();
542 Context *root_context = this->root_context();
543
544 TSCont contp;
545 std::string request, method, path, authority;
546
547 // setup local address for API call
548 struct sockaddr_in addr;
549 addr.sin_family = AF_INET;
550 addr.sin_addr.s_addr = LOCAL_IP_ADDRESS;
551 addr.sin_port = LOCAL_PORT;
552
553 for (const auto &p : request_headers) {
554 std::string key(p.first);
555 std::string value(p.second);
556
557 if (key == ":method") {
558 method = value;
559 } else if (key == ":path") {
560 path = value;
561 } else if (key == ":authority") {
562 authority = value;
563 }
564 }
565
566 /* request */
567 request = method + " https://" + authority + path + " HTTP/1.1\r\n";
568 for (const auto &p : request_headers) {
569 std::string key(p.first);
570 std::string value(p.second);
571 request += key + ": " + value + "\r\n";
572 }
573 request += "\r\n";
574 request += request_body;
575
576 TSFetchEvent event_ids;
577 event_ids.success_event_id = FETCH_EVENT_ID_BASE;
578 event_ids.failure_event_id = FETCH_EVENT_ID_BASE + 1;
579 event_ids.timeout_event_id = FETCH_EVENT_ID_BASE + 2;
580
581 contp = TSContCreate(async_handler, TSMutexCreate());
582 AsyncInfo *ai = new AsyncInfo();
583 ai->token = wasm->nextHttpCallId();
584 ai->root_context = root_context;
585 *token_ptr = ai->token; // to be returned to the caller
586 TSContDataSet(contp, ai);
587
588 // API call for async fetch
589 TSFetchUrl(request.c_str(), request.size(), reinterpret_cast<struct sockaddr const *>(&addr), contp, AFTER_BODY, event_ids);
590
591 return WasmResult::Ok;
592}
593
594// Metrics

Callers

nothing calls this directly

Calls 9

wasmMethod · 0.95
root_contextMethod · 0.95
TSContCreateFunction · 0.85
TSMutexCreateFunction · 0.85
TSContDataSetFunction · 0.85
TSFetchUrlFunction · 0.85
nextHttpCallIdMethod · 0.80
c_strMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected