#include "ai/socket_transport.hpp" #if !defined(_WIN32) #include #include #include #include #endif #include namespace molo::ai { SocketAiTransport::ParsedUrl SocketAiTransport::parse(const std::string& url) { ParsedUrl out; std::string rest = url; if (rest.rfind("http://", 0) == 0) { rest = rest.substr(7); } else if (rest.rfind("https://", 0) == 0) { out.https = true; rest = rest.substr(8); } else { return out; } auto slash = rest.find('/'); std::string hostport = slash == std::string::npos ? rest : rest.substr(0, slash); out.path = slash == std::string::npos ? "/" : rest.substr(slash); auto colon = hostport.find(':'); if (colon != std::string::npos) { out.host = hostport.substr(0, colon); try { out.port = std::stoi(hostport.substr(colon + 1)); } catch (...) { return out; } } else { out.host = hostport; out.port = out.https ? 443 : 80; } if (!out.host.empty()) out.ok = true; return out; } AiHttpResponse SocketAiTransport::post_stream(const AiHttpRequest& request) { AiHttpResponse out; ParsedUrl url = parse(request.url); if (!url.ok) { out.error = "bad URL: " + request.url; return out; } if (url.https) { out.error = TlsStub::unavailable_reason(); return out; } #if defined(_WIN32) out.error = "socket transport is POSIX only"; return out; #else addrinfo hints{}; hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; addrinfo* list = nullptr; if (getaddrinfo(url.host.c_str(), std::to_string(url.port).c_str(), &hints, &list) != 0) { out.error = "dns failed for " + url.host; return out; } int fd = -1; for (addrinfo* ai = list; ai != nullptr; ai = ai->ai_next) { fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (fd < 0) continue; struct timeval tv; tv.tv_sec = request.connect_timeout_sec; tv.tv_usec = 0; setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); if (connect(fd, ai->ai_addr, ai->ai_addrlen) == 0) break; close(fd); fd = -1; } freeaddrinfo(list); if (fd < 0) { out.error = "connect failed to " + url.host; return out; } std::string head = "POST " + url.path + " HTTP/1.1\r\nHost: " + url.host + "\r\nContent-Type: application/json\r\nAccept: text/event-stream\r\n" + "Authorization: Bearer " + request.bearer_key + "\r\nContent-Length: " + std::to_string(request.body_json.size()) + "\r\nConnection: close\r\n\r\n"; std::string wire = head + request.body_json; std::size_t sent = 0; while (sent < wire.size()) { ssize_t n = send(fd, wire.data() + sent, wire.size() - sent, 0); if (n <= 0) { close(fd); out.error = "send failed"; return out; } sent += static_cast(n); } std::string raw; char buf[8192]; while (true) { ssize_t n = recv(fd, buf, sizeof(buf), 0); if (n <= 0) break; raw.append(buf, static_cast(n)); } close(fd); auto eol = raw.find("\r\n"); if (eol == std::string::npos) { out.error = "bad response"; return out; } std::string status_line = raw.substr(0, eol); auto sp = status_line.find(' '); if (sp != std::string::npos) { try { out.status = std::stoi(status_line.substr(sp + 1)); } catch (...) { out.status = 0; } } auto hdr_end = raw.find("\r\n\r\n"); out.sse_body = hdr_end == std::string::npos ? "" : raw.substr(hdr_end + 4); return out; #endif } } // namespace molo::ai