From 8d6020730a2a5dcbc3975939b5120c1ab44e13ca Mon Sep 17 00:00:00 2001 From: hou Date: Fri, 8 Sep 2017 23:22:50 +0800 Subject: [PATCH] Change function cat. The old function set maximum buffer length 1024. When request compressed file such as jquery.min.js, broswer cannot get complete content. --- httpd.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/httpd.c b/httpd.c index d4eb456..1cfaa2d 100644 --- a/httpd.c +++ b/httpd.c @@ -162,14 +162,15 @@ void bad_request(int client) /**********************************************************************/ void cat(int client, FILE *resource) { - char buf[1024]; - - fgets(buf, sizeof(buf), resource); - while (!feof(resource)) - { - send(client, buf, strlen(buf), 0); - fgets(buf, sizeof(buf), resource); - } + fseek(resource, 0, SEEK_END); + int end = ftell(resource); + fseek(resource, 0, SEEK_SET); + int start = ftell(resource); + int length = end - start; + unsigned char *buf = (unsigned char *)malloc(length); + int len = fread((void *)buf, length, 1, resource); + send(client, buf, length, 0); + free(buf); } /**********************************************************************/