From dbbd22f792ca46f94da348fb9b855821af3f021c Mon Sep 17 00:00:00 2001 From: Philipp Schafft Date: Mon, 2 Feb 2015 00:50:56 +0000 Subject: [PATCH] Cleanup: Set Connection:-Header via client->reuse --- src/client.c | 16 +++++++++------- src/util.c | 12 +++++++++++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/client.c b/src/client.c index dd099f2e..654b13b0 100644 --- a/src/client.c +++ b/src/client.c @@ -201,6 +201,8 @@ void client_send_error(client_t *client, int status, int plain, const char *mess return; } + client->reuse = ICECAST_REUSE_KEEPALIVE; + ret = util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0, 0, status, NULL, plain ? "text/plain" : "text/html", "utf-8", @@ -223,15 +225,13 @@ void client_send_error(client_t *client, int status, int plain, const char *mess data->len = strlen(data->data); snprintf(client->refbuf->data + ret, PER_CLIENT_REFBUF_SIZE - ret, - "Content-Length: %llu\r\nConnection: Keep-Alive\r\n\r\n", + "Content-Length: %llu\r\n\r\n", (long long unsigned int)data->len); client->respcode = status; client->refbuf->len = strlen (client->refbuf->data); client->refbuf->next = data; - client->reuse = ICECAST_REUSE_KEEPALIVE; - fserve_add_client (client, NULL); } @@ -253,19 +253,19 @@ void client_send_101(client_t *client, reuse_t reuse) return; } + client->reuse = reuse; + ret = util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0, 0, 101, NULL, "text/plain", "utf-8", NULL, NULL, client); snprintf(client->refbuf->data + ret, PER_CLIENT_REFBUF_SIZE - ret, - "Content-Length: 0\r\nUpgrade: TLS/1.0, HTTP/1.0\r\nConnection: Upgrade\r\n\r\n"); + "Content-Length: 0\r\nUpgrade: TLS/1.0, HTTP/1.0\r\n\r\n"); client->respcode = 101; client->refbuf->len = strlen(client->refbuf->data); - client->reuse = reuse; - fserve_add_client(client, NULL); } @@ -281,13 +281,15 @@ void client_send_426(client_t *client, reuse_t reuse) return; } + client->reuse = reuse; + ret = util_http_build_header(client->refbuf->data, PER_CLIENT_REFBUF_SIZE, 0, 0, 426, NULL, "text/plain", "utf-8", NULL, NULL, client); snprintf(client->refbuf->data + ret, PER_CLIENT_REFBUF_SIZE - ret, - "Content-Length: 0\r\nUpgrade: TLS/1.0, HTTP/1.0\r\nConnection: Upgrade\r\n\r\n"); + "Content-Length: 0\r\nUpgrade: TLS/1.0, HTTP/1.0\r\n\r\n"); client->respcode = 426; client->refbuf->len = strlen(client->refbuf->data); diff --git a/src/util.c b/src/util.c index 55131673..2a32b798 100644 --- a/src/util.c +++ b/src/util.c @@ -632,10 +632,19 @@ ssize_t util_http_build_header(char * out, size_t len, ssize_t offset, char contenttype_buffer[80]; ssize_t ret; char * extra_headers; + const char *connection_header = "Close"; if (!out) return -1; + if (client) { + switch (client->reuse) { + case ICECAST_REUSE_CLOSE: connection_header = "Close"; break; + case ICECAST_REUSE_KEEPALIVE: connection_header = "Keep-Alive"; break; + case ICECAST_REUSE_UPGRADETLS: connection_header = "Upgrade"; break; + } + } + if (offset == -1) offset = strlen (out); @@ -698,9 +707,10 @@ ssize_t util_http_build_header(char * out, size_t len, ssize_t offset, config = config_get_config(); extra_headers = _build_headers(status, config, source); - ret = snprintf (out, len, "%sServer: %s\r\nAccept-Encoding: identity\r\nAllow: %s\r\n%s%s%s%s%s%s%s", + ret = snprintf (out, len, "%sServer: %s\r\nConnection: %s\r\nAccept-Encoding: identity\r\nAllow: %s\r\n%s%s%s%s%s%s%s", status_buffer, config->server_id, + connection_header, (client->admin_command == ADMIN_COMMAND_ERROR ? "GET, SOURCE" : "GET"), currenttime_buffer,