Handle a Content-Length-framed message (LSP-style transport). * Reads headers, body, processes request, writes framed response. */
| 6201 | /* Handle a Content-Length-framed message (LSP-style transport). |
| 6202 | * Reads headers, body, processes request, writes framed response. */ |
| 6203 | static void handle_content_length_frame(cbm_mcp_server_t *srv, FILE *in, FILE *out, char **line, |
| 6204 | size_t *cap, int content_len) { |
| 6205 | /* Skip blank line(s) between header and body */ |
| 6206 | while (cbm_getline(line, cap, in) > 0) { |
| 6207 | size_t hlen = strlen(*line); |
| 6208 | while (hlen > 0 && ((*line)[hlen - SKIP_ONE] == '\n' || (*line)[hlen - SKIP_ONE] == '\r')) { |
| 6209 | (*line)[--hlen] = '\0'; |
| 6210 | } |
| 6211 | if (hlen == 0) { |
| 6212 | break; |
| 6213 | } |
| 6214 | } |
| 6215 | |
| 6216 | char *body = malloc((size_t)content_len + SKIP_ONE); |
| 6217 | if (!body) { |
| 6218 | return; |
| 6219 | } |
| 6220 | size_t nread = fread(body, SKIP_ONE, (size_t)content_len, in); |
| 6221 | body[nread] = '\0'; |
| 6222 | |
| 6223 | char *resp = cbm_mcp_server_handle(srv, body); |
| 6224 | free(body); |
| 6225 | |
| 6226 | if (resp) { |
| 6227 | size_t rlen = strlen(resp); |
| 6228 | (void)fprintf(out, "Content-Length: %zu\r\n\r\n%s", rlen, resp); |
| 6229 | (void)fflush(out); |
| 6230 | free(resp); |
| 6231 | } |
| 6232 | } |
| 6233 | |
| 6234 | #ifndef _WIN32 |
| 6235 | /* Unix 3-phase poll: non-blocking fd check, FILE* buffer peek, blocking poll. |
no test coverage detected