From d576d7162a94833e526332aba5b82ee0def9d3f9 Mon Sep 17 00:00:00 2001 From: fumiama Date: Tue, 8 Jun 2021 14:33:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=95=BF=E5=BA=A6=E5=A4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmoe.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/cmoe.c b/cmoe.c index a8eeb7d..306b64f 100644 --- a/cmoe.c +++ b/cmoe.c @@ -8,6 +8,13 @@ static uint32_t* items_len; static COUNTER counter; +static void write2stdo(char* buf, uint32_t size, uint32_t content_len) { + content_len += size; + fwrite((char*)&content_len, sizeof(uint32_t), 1, stdout); + fwrite(buf, size, 1, stdout); + fflush(stdout); +} + #define ADD_HERDER(h)\ strcpy(buf + offset, (h));\ offset += sizeof((h)) - 1; @@ -24,11 +31,14 @@ static void headers(uint32_t content_len, const char* content_type, int no_cache if(no_cache) ADD_HERDER(CACHE_CTRL); ADD_HERDER_PARAM(CONTENT_TYPE, content_type); ADD_HERDER_PARAM(CONTENT_LEN "\r\n", content_len); - fwrite(buf, offset, 1, stdout); + write2stdo(buf, offset, content_len); } static void http_error(const char* type, const char* msg) { - fprintf(stdout, type, msg); + char* str = malloc(strlen(type) + strlen(msg)); + sprintf(str, type, msg); + write2stdo(str, strlen(str), 0); + free(str); exit(EXIT_FAILURE); }