| 147 | private: |
| 148 | SocketHandle socket_ = kInvalidSocket; |
| 149 | }; |
| 150 | |
| 151 | void send_all(SocketHandle socket, const std::string & data) { |
| 152 | size_t offset = 0; |
| 153 | while (offset < data.size()) { |
| 154 | const auto remaining = data.size() - offset; |
| 155 | #ifdef _WIN32 |
| 156 | const int written = send( |
| 157 | socket, |
| 158 | data.data() + offset, |
| 159 | static_cast<int>(std::min<size_t>(remaining, std::numeric_limits<int>::max())), |
| 160 | 0); |
| 161 | #else |
| 162 | const ssize_t written = send(socket, data.data() + offset, remaining, 0); |
| 163 | #endif |
| 164 | if (written <= 0) { |
| 165 | throw std::runtime_error("socket send failed"); |
| 166 | } |
| 167 | offset += static_cast<size_t>(written); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // An SSE body is written while a model lock is held, so an unbounded blocking |
| 172 | // send() lets a client that uploads but never reads fill the kernel send buffer |
| 173 | // and pin that model indefinitely. A send timeout turns it into a failed write. |
| 174 | void set_send_timeout(SocketHandle socket, int timeout_ms) { |
| 175 | #ifdef _WIN32 |
| 176 | const DWORD timeout = static_cast<DWORD>(timeout_ms); |
| 177 | setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, reinterpret_cast<const char *>(&timeout), sizeof(timeout)); |
| 178 | #else |
| 179 | timeval timeout{}; |
| 180 | timeout.tv_sec = timeout_ms / 1000; |
| 181 | timeout.tv_usec = (timeout_ms % 1000) * 1000; |
| 182 | setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)); |
| 183 | #endif |
| 184 | } |
| 185 | |
| 186 | // The endpoints that consume their body incrementally. Gating on the path as |
| 187 | // well as the encoding keeps every other endpoint's request handling bit-for-bit |
| 188 | // unchanged, instead of silently altering how any chunked request is read. |
| 189 | constexpr std::string_view kLiveTranscriptionPath = "/v1/audio/transcriptions/live"; |
| 190 | constexpr std::string_view kLiveTranscriptionDetailsPath = "/v1/audio/transcriptions/live/details"; |
| 191 | constexpr std::string_view kLiveSpeechPath = "/v1/audio/speech/live"; |
| 192 | |
| 193 | // True only when the header names exactly one transfer-coding and that coding is |
| 194 | // "chunked". A substring test would accept "notchunked" as well as chains like |
| 195 | // "gzip, chunked" — and for the latter, stripping the chunk framing would hand the |
| 196 | // PCM decoder compressed bytes, which it would happily interpret as audio. |
| 197 | bool is_chunked_only(std::string_view value) { |
| 198 | bool saw_coding = false; |
| 199 | size_t start = 0; |
| 200 | while (start <= value.size()) { |
| 201 | const size_t comma = value.find(',', start); |
| 202 | const auto end = comma == std::string_view::npos ? value.size() : comma; |
| 203 | const std::string coding = trim(std::string(value.substr(start, end - start))); |
| 204 | if (!coding.empty()) { |
| 205 | if (saw_coding || lower_ascii(coding) != "chunked") { |
| 206 | return false; |
no test coverage detected