in AZ3166/src/cores/arduino/httpserver/httpd_wsgi.c [420:496]
int httpd_send_response(httpd_request_t *req, const char *first_line,
char *content, int length, const char *content_type)
{
int ret;
/* Parse the header tags. This is valid only for GET or HEAD request */
if (req->type == HTTPD_REQ_TYPE_GET ||
req->type == HTTPD_REQ_TYPE_HEAD) {
ret = httpd_purge_headers(req->sock);
if (ret != kNoErr) {
httpd_d("Unable to purge headers");
return ret;
}
}
ret = httpd_send(req->sock, first_line, strlen(first_line));
if (ret != kNoErr) {
httpd_d("Error in sending the first line");
return ret;
}
/* Send default headers */
httpd_d("HTTP Req for URI %s", req->wsgi->uri);
if (req->wsgi->hdr_fields) {
ret = httpd_send_default_headers(req->sock,
req->wsgi->hdr_fields);
if (ret != kNoErr) {
httpd_d("Error in sending default headers");
return ret;
}
}
ret = httpd_send_header(req->sock, "Content-Type", content_type);
if (ret != kNoErr) {
httpd_d("Error in sending Content-Type");
return ret;
}
/* Send Content-Length if non-chunked */
if (!chunked_encoding(req)) {
/* 6 should be more than enough */
char con_len[6];
snprintf(con_len, sizeof(con_len), "%d", length);
ret = httpd_send_header(req->sock, "Content-Length", con_len);
if (ret != kNoErr) {
httpd_d("Error in sending Content-Length");
return ret;
}
}
httpd_send_crlf(req->sock);
/* HTTP Head response does not require any content. It should
* contain identical headers as per the corresponding GET request */
if (req->type != HTTPD_REQ_TYPE_HEAD) {
if (chunked_encoding(req)) {
ret = httpd_send_chunk(req->sock, content, length);
if (ret != kNoErr) {
httpd_d("Error in sending response content");
return ret;
}
ret = httpd_send_chunk(req->sock, NULL, 0);
if (ret != kNoErr) {
httpd_d("Error in sending last chunk");
}
} else {
/* Send our data */
ret = httpd_send(req->sock, content, length);
if (ret != kNoErr) {
httpd_d("Error sending response");
}
}
}
return ret;
}