This commit is contained in:
源文雨 2022-11-23 15:37:12 +08:00
parent 6298cfc645
commit a2ad48f6cb
2 changed files with 14 additions and 4 deletions

12
c302.c
View File

@ -23,7 +23,13 @@ int main(int argc, char **argv) {
} }
srand(time(NULL)); srand(time(NULL));
uint32_t i = rand() % (sizeof(urls)/sizeof(char*)); uint32_t i = rand() % (sizeof(urls)/sizeof(char*));
uint32_t len = sizeof(HTTP302HEAD) - 1 + urlsl[i]; uint32_t len = sizeof(HTTP302HEAD)-1 + urlsl[i] + 4 + sizeof(CONTENT)-1;
const struct iovec iov[3] = {{(void *)&len, sizeof(uint32_t)}, {HTTP302HEAD, sizeof(HTTP302HEAD)-1}, {(void *)urls[i], urlsl[i]}}; const struct iovec iov[5] = {
return writev(1, (const struct iovec *)&iov, 3) != len+sizeof(uint32_t); {(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);
} }

6
c302.h
View File

@ -5,8 +5,12 @@
#define SERVER_STRING "Server: C302 by Fumiama/1.0\r\n" #define SERVER_STRING "Server: C302 by Fumiama/1.0\r\n"
#define HTTP302 "HTTP/1.1 302 Found\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 LOCATION "Location: "
#define HTTP302HEAD HTTP302 SERVER_STRING LOCATION #define HTTP302HEAD HTTP302 SERVER_STRING CONTENT_TYPE CONTENT_LENGTH LOCATION
#define CONTENT "<P>Found.\r\n"
#define HTTP200 "HTTP/1.0 200 OK\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<P>%s\r\n" #define H400 "HTTP/1.0 400 BAD REQUEST\r\nContent-Type: text/html\r\n\r\n<P>%s\r\n"