From 5d0ea0066ad4997dd4fa424f92b6679fe96022fe Mon Sep 17 00:00:00 2001 From: Unbit Date: Sun, 2 Jun 2013 12:52:13 +0200 Subject: [PATCH] fixed fastcgi body read --- proto/fastcgi.c | 14 +++++++++++++- uwsgi.h | 1 + 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/proto/fastcgi.c b/proto/fastcgi.c index 53f87bb3..eeb98c6a 100644 --- a/proto/fastcgi.c +++ b/proto/fastcgi.c @@ -155,6 +155,11 @@ ssize_t uwsgi_proto_fastcgi_read_body(struct wsgi_request *wsgi_req, char *buf, memcpy(buf, wsgi_req->proto_parser_remains_buf, remains); wsgi_req->proto_parser_remains -= remains; wsgi_req->proto_parser_remains_buf += remains; + // we consumed all of the body, we can safely move the memory + if (wsgi_req->proto_parser_remains == 0 && wsgi_req->proto_parser_move) { + memmove(wsgi_req->proto_parser_buf, wsgi_req->proto_parser_buf + wsgi_req->proto_parser_move, wsgi_req->proto_parser_pos); + wsgi_req->proto_parser_move = 0; + } return remains; } @@ -176,7 +181,14 @@ ssize_t uwsgi_proto_fastcgi_read_body(struct wsgi_request *wsgi_req, char *buf, // copy remaining wsgi_req->proto_parser_remains = fcgi_len - remains; wsgi_req->proto_parser_remains_buf = wsgi_req->proto_parser_buf + sizeof(struct fcgi_record) + remains; - memmove(wsgi_req->proto_parser_buf, wsgi_req->proto_parser_buf + fcgi_all_len, wsgi_req->proto_parser_pos - fcgi_all_len); + // we consumed all of the body, we can safely move the memory + if (wsgi_req->proto_parser_remains == 0) { + memmove(wsgi_req->proto_parser_buf, wsgi_req->proto_parser_buf + fcgi_all_len, wsgi_req->proto_parser_pos - fcgi_all_len); + } + else { + // postpone memory move + wsgi_req->proto_parser_move = fcgi_all_len; + } wsgi_req->proto_parser_pos -= fcgi_all_len; return remains; } diff --git a/uwsgi.h b/uwsgi.h index fa4adcf7..a81f22e7 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -1402,6 +1402,7 @@ struct wsgi_request { int headers_hvec; uint64_t proto_parser_pos; + uint64_t proto_parser_move; int64_t proto_parser_status; void *proto_parser_buf; uint64_t proto_parser_buf_size;