From db29be70540bc5ddf16cf1db2c455eeafc4a367b 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 14:21:23 +0800 Subject: [PATCH] init --- .gitignore | 2 ++ CMakeLists.txt | 14 ++++++++++++++ c302.c | 30 ++++++++++++++++++++++++++++++ c302.h | 27 +++++++++++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 c302.c create mode 100644 c302.h diff --git a/.gitignore b/.gitignore index c6127b3..bd98235 100644 --- a/.gitignore +++ b/.gitignore @@ -50,3 +50,5 @@ modules.order Module.symvers Mkfile.old dkms.conf + +/build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..a8c12e6 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 2.6) +project(C302 C) +SET(CMAKE_BUILD_TYPE "Release") + +# include_directories("/usr/local/include") +# link_directories("/usr/local/lib") + +#在编译选项中加入c99支持 +add_compile_options(-std=c99) +message(STATUS "optional:-std=c99") + +add_executable(c302 c302.c) + +INSTALL(TARGETS c302 RUNTIME DESTINATION bin) diff --git a/c302.c b/c302.c new file mode 100644 index 0000000..d420e77 --- /dev/null +++ b/c302.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include +#include +#include +#include "c302.h" + +static void http_error(ERRCODE code, char* msg) { + uint32_t len = strlen(msg) + typel[code]; + char* str = malloc(len); + sprintf(str, types[code], msg); + const struct iovec iov[2] = {{&len, sizeof(uint32_t)}, {str, len}}; + writev(1, (const struct iovec *)&iov, 2); + free(str); +} + +// Usage: c302 method query_string +int main(int argc, char **argv) { + if (argc != 3) { + http_error(HTTP500, "Argument Count Error."); + return 1; + } + srand(time(NULL)); + unsigned int i = rand() % (sizeof(urls)/sizeof(char*)); + const struct iovec iov[3] = {{HTTP302HEAD, sizeof(HTTP302HEAD)}, {(void *)urls[i], urlsl[i]}, {"\r\n\r\n", 4}}; + writev(1, (const struct iovec *)&iov, 3); + return 0; +} diff --git a/c302.h b/c302.h new file mode 100644 index 0000000..9cb322a --- /dev/null +++ b/c302.h @@ -0,0 +1,27 @@ +#ifndef _C302_H_ +#define _C302_H_ + +#define SERVER_STRING "Server: C302 by Fumiama/1.0\r\n" +#define HTTP302 "HTTP/1.0 302 Moved Temporarily\r\n" +#define LOCATION "Location: " +#define HTTP302HEAD HTTP302 SERVER_STRING LOCATION + +#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" +#define H404 "HTTP/1.0 404 NOT FOUND\r\nContent-Type: text/html\r\n\r\nNot Found\r\n

%s\r\n\r\n" +#define H500 "HTTP/1.0 500 Internal Server Error\r\nContent-Type: text/html\r\n\r\n

%s\r\n" +const char* types[] = {H400, H404, H500}; +const unsigned char typel[] = {59, 111, 69}; +enum ERRCODE {HTTP400, HTTP404, HTTP500}; +typedef enum ERRCODE ERRCODE; + +static const char* urls[] = { + "https://bocchi.rocks/assets/img/page/character/hitori/main.png", // 後藤ひとり + "https://bocchi.rocks/assets/img/page/character/nijika/main.png", // 伊地知虹夏 + "https://bocchi.rocks/assets/img/page/character/ryo/main.png", // 山田リョウ + "https://bocchi.rocks/assets/img/page/character/ikuyo/main.png" // 喜多郁代 +}; + +static const unsigned short urlsl[] = {62, 62, 59, 61}; + +#endif