ssize_t client_socket_sendall(struct client_socket *socket, const void *buf, size_t len) { /* Keep track of how much we've sent so far. */ size_t numsent = 0; do { ssize_t newsent = client_socket_send(socket, buf + numsent, len - numsent); check(newsent != -1, "Error sending data"); numsent += newsent; } while (numsent < len); return numsent; error: return -1; }
void webserver_send_response(ClientSocket* client, enum EHttpStatus status, const char* body, const char* content_type) { // Build the Content-Length string char content_length[20] = {0}; snprintf(content_length, 20, "%zu", (body ? strlen(body) : 0)); if (!content_type) { content_type = "text/plain"; } // Build the response object HttpResponse* res = http_response_new(); http_response_set_status(res, HTTP_VERSION_1_0, status); http_response_add_header(res, "Server", "webserver"); http_response_add_header(res, "Content-Length", content_length); if (body) { http_response_add_header(res, "Content-Type", content_type); } if (body) { http_response_set_body(res, body); } // Send the response and clean up client_socket_send(client, http_response_string(res), http_response_length(res)); http_response_free(res); }