| 243 | } |
| 244 | |
| 245 | static char *web_http_request(const char *method, int port, const char *path, |
| 246 | char *err, size_t err_len) { |
| 247 | int fd = web_tcp_connect("127.0.0.1", port, DS4_WEB_CONNECT_TIMEOUT_MS, |
| 248 | err, err_len); |
| 249 | if (fd < 0) return NULL; |
| 250 | web_buf req = {0}; |
| 251 | char line[512]; |
| 252 | snprintf(line, sizeof(line), |
| 253 | "%s %s HTTP/1.1\r\nHost: 127.0.0.1:%d\r\nConnection: close\r\n\r\n", |
| 254 | method, path, port); |
| 255 | web_buf_puts(&req, line); |
| 256 | if (web_write_all(fd, req.ptr, req.len) != 0) { |
| 257 | web_set_err(err, err_len, "write HTTP request failed: %s", strerror(errno)); |
| 258 | close(fd); |
| 259 | free(req.ptr); |
| 260 | return NULL; |
| 261 | } |
| 262 | free(req.ptr); |
| 263 | |
| 264 | web_buf resp = {0}; |
| 265 | char tmp[4096]; |
| 266 | for (;;) { |
| 267 | ssize_t n = web_read_some(fd, tmp, sizeof(tmp), DS4_WEB_CONNECT_TIMEOUT_MS); |
| 268 | if (n < 0) { |
| 269 | web_set_err(err, err_len, "read HTTP response failed: %s", strerror(errno)); |
| 270 | close(fd); |
| 271 | free(resp.ptr); |
| 272 | return NULL; |
| 273 | } |
| 274 | if (n == 0) break; |
| 275 | web_buf_append(&resp, tmp, (size_t)n); |
| 276 | } |
| 277 | close(fd); |
| 278 | if (!resp.ptr) { |
| 279 | web_set_err(err, err_len, "empty HTTP response"); |
| 280 | return NULL; |
| 281 | } |
| 282 | char *body = strstr(resp.ptr, "\r\n\r\n"); |
| 283 | if (!body) { |
| 284 | web_set_err(err, err_len, "malformed HTTP response"); |
| 285 | free(resp.ptr); |
| 286 | return NULL; |
| 287 | } |
| 288 | body += 4; |
| 289 | char *out = web_xstrdup(body); |
| 290 | free(resp.ptr); |
| 291 | return out; |
| 292 | } |
| 293 | |
| 294 | static bool web_cdp_alive(ds4_web *web) { |
| 295 | char err[160] = {0}; |
no test coverage detected