forked from cory/tildefriends
Saw a websocket message go across the wire with this.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4693 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
24
src/http.c
24
src/http.c
@ -446,13 +446,16 @@ void tf_http_respond(tf_http_request_t* request, int status, const char** header
|
||||
/* HTTP/1.x 200 OK\r\n */
|
||||
bool sent_content_length = false;
|
||||
int headers_length = 8 + 1 + 3 + 1 + strlen(status_text) + 2;
|
||||
for (int i = 0; i < headers_count; i += 2)
|
||||
if (headers)
|
||||
{
|
||||
/* Key: Value\r\n */
|
||||
headers_length += strlen(headers[i]) + 2 + strlen(headers[i + 1]) + 2;
|
||||
if (strcasecmp(headers[i], "content-length") == 0)
|
||||
for (int i = 0; i < headers_count * 2; i += 2)
|
||||
{
|
||||
sent_content_length = true;
|
||||
/* Key: Value\r\n */
|
||||
headers_length += strlen(headers[i]) + 2 + strlen(headers[i + 1]) + 2;
|
||||
if (strcasecmp(headers[i], "content-length") == 0)
|
||||
{
|
||||
sent_content_length = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* \r\n */
|
||||
@ -470,9 +473,12 @@ void tf_http_respond(tf_http_request_t* request, int status, const char** header
|
||||
*write = (uv_write_t) { .data = request->connection };
|
||||
char* buffer = (char*)(write + 1);
|
||||
int offset = snprintf(buffer, headers_length + 1, "HTTP/1.%d %03d %s\r\n", request->connection->minor_version, status, status_text);
|
||||
for (int i = 0; i < headers_count; i += 2)
|
||||
if (headers)
|
||||
{
|
||||
offset += snprintf(buffer + offset, headers_length + 1 - offset, "%s: %s\r\n", headers[i], headers[i + 1]);
|
||||
for (int i = 0; i < headers_count * 2; i += 2)
|
||||
{
|
||||
offset += snprintf(buffer + offset, headers_length + 1 - offset, "%s: %s\r\n", headers[i], headers[i + 1]);
|
||||
}
|
||||
}
|
||||
if (!sent_content_length)
|
||||
{
|
||||
@ -485,6 +491,10 @@ void tf_http_respond(tf_http_request_t* request, int status, const char** header
|
||||
{
|
||||
memcpy(buffer + offset, body, content_length);
|
||||
}
|
||||
if (status == 101)
|
||||
{
|
||||
tf_printf("WRITE [%.*s]\n", (int)(headers_length + content_length), buffer);
|
||||
}
|
||||
int r = uv_write(write, (uv_stream_t*)&request->connection->tcp, &(uv_buf_t) { .base = buffer, .len = headers_length + content_length }, 1, _http_on_write);
|
||||
if (r)
|
||||
{
|
||||
|
Reference in New Issue
Block a user