From a2ad48f6cbe4de4da01d375a2c7f990d29f3792a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Wed, 23 Nov 2022 15:37:12 +0800 Subject: [PATCH] fix --- c302.c | 12 +++++++++--- c302.h | 6 +++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/c302.c b/c302.c index d5d01ac..3bc399f 100644 --- a/c302.c +++ b/c302.c @@ -23,7 +23,13 @@ int main(int argc, char **argv) { } srand(time(NULL)); uint32_t i = rand() % (sizeof(urls)/sizeof(char*)); - uint32_t len = sizeof(HTTP302HEAD) - 1 + urlsl[i]; - const struct iovec iov[3] = {{(void *)&len, sizeof(uint32_t)}, {HTTP302HEAD, sizeof(HTTP302HEAD)-1}, {(void *)urls[i], urlsl[i]}}; - return writev(1, (const struct iovec *)&iov, 3) != len+sizeof(uint32_t); + uint32_t len = sizeof(HTTP302HEAD)-1 + urlsl[i] + 4 + sizeof(CONTENT)-1; + const struct iovec iov[5] = { + {(void *)&len, sizeof(uint32_t)}, + {HTTP302HEAD, sizeof(HTTP302HEAD)-1}, + {(void *)urls[i], urlsl[i]}, + {"\r\n\r\n", 4}, + {CONTENT, sizeof(CONTENT)-1} + }; + return writev(1, (const struct iovec *)&iov, 5) != len+sizeof(uint32_t); } diff --git a/c302.h b/c302.h index 6da1e13..24215d8 100644 --- a/c302.h +++ b/c302.h @@ -5,8 +5,12 @@ #define SERVER_STRING "Server: C302 by Fumiama/1.0\r\n" #define HTTP302 "HTTP/1.1 302 Found\r\n" +#define CONTENT_TYPE "Content-Type: text/html; charset=utf-8\r\n" +#define CONTENT_LENGTH "Content-Length: 11\r\n" #define LOCATION "Location: " -#define HTTP302HEAD HTTP302 SERVER_STRING LOCATION +#define HTTP302HEAD HTTP302 SERVER_STRING CONTENT_TYPE CONTENT_LENGTH LOCATION + +#define CONTENT "

Found.\r\n" #define HTTP200 "HTTP/1.0 200 OK\r\n" #define H400 "HTTP/1.0 400 BAD REQUEST\r\nContent-Type: text/html\r\n\r\n

%s\r\n"