From 8321adf0bc5798bfd80e3099f7652bbf82c456eb Mon Sep 17 00:00:00 2001 From: Unbit Date: Wed, 1 May 2013 10:21:35 +0200 Subject: [PATCH] completed chunked transformation --- core/transformations.c | 4 ++++ plugins/transformation_chunked/chunked.c | 12 ++++++++++-- uwsgi.h | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/core/transformations.c b/core/transformations.c index 36b7c81c..fcf6c857 100644 --- a/core/transformations.c +++ b/core/transformations.c @@ -27,6 +27,8 @@ int uwsgi_apply_transformations(struct wsgi_request *wsgi_req, char *buf, size_t if (!ut->chunk) { ut->chunk = uwsgi_buffer_new(t_len); } + // skip final transformations before appending data + if (ut->is_final) goto next; if (uwsgi_buffer_append(ut->chunk, t_buf, t_len)) { return -1; } @@ -42,6 +44,7 @@ int uwsgi_apply_transformations(struct wsgi_request *wsgi_req, char *buf, size_t t_len = ut->chunk->pos; // we reset the buffer, so we do not waste memory ut->chunk->pos = 0; +next: ut = ut->next; } @@ -117,6 +120,7 @@ struct uwsgi_transformation *uwsgi_add_transformation(struct wsgi_request *wsgi_ ut = uwsgi_malloc(sizeof(struct uwsgi_transformation)); ut->func = func; + ut->is_final = 0; ut->next = NULL; ut->chunk = NULL; ut->can_stream = 0; diff --git a/plugins/transformation_chunked/chunked.c b/plugins/transformation_chunked/chunked.c index 2b2a80c8..5bc0c067 100644 --- a/plugins/transformation_chunked/chunked.c +++ b/plugins/transformation_chunked/chunked.c @@ -10,17 +10,25 @@ static int transform_chunked(struct wsgi_request *wsgi_req, struct uwsgi_transformation *ut) { struct uwsgi_buffer *ub = ut->chunk; - if (uwsgi_buffer_insert_chunked(ub, 0, ub->pos)) return -1; - if (uwsgi_buffer_append(ub, "\r\n", 2)) return -1; if (!wsgi_req->headers_sent) { if (uwsgi_response_add_header(wsgi_req, "Transfer-Encoding", 17, "chunked", 7)) return -1; } + if (ut->is_final) { + if (uwsgi_buffer_insert_chunked(ub, 0, 0)) return -1; + } + else { + if (uwsgi_buffer_insert_chunked(ub, 0, ub->pos)) return -1; + } + if (uwsgi_buffer_append(ub, "\r\n", 2)) return -1; return 0; } static int uwsgi_routing_func_chunked(struct wsgi_request *wsgi_req, struct uwsgi_route *ur) { struct uwsgi_transformation *ut = uwsgi_add_transformation(wsgi_req, transform_chunked, NULL); ut->can_stream = 1; + // add a "final" transformation to add the trailing chunk + ut = uwsgi_add_transformation(wsgi_req, transform_chunked, NULL); + ut->is_final = 1; return UWSGI_ROUTE_NEXT; } diff --git a/uwsgi.h b/uwsgi.h index 594a9c67..462b2c57 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -1198,6 +1198,7 @@ struct uwsgi_transformation { int (*func)(struct wsgi_request *, struct uwsgi_transformation *); struct uwsgi_buffer *chunk; uint8_t can_stream; + uint8_t is_final; void *data; struct uwsgi_transformation *next; };