This commit is contained in:
源文雨 2022-11-23 14:21:23 +08:00
parent 0b6745c6f8
commit db29be7054
4 changed files with 73 additions and 0 deletions

2
.gitignore vendored
View File

@ -50,3 +50,5 @@ modules.order
Module.symvers
Mkfile.old
dkms.conf
/build

14
CMakeLists.txt Normal file
View File

@ -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)

30
c302.c Normal file
View File

@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <sys/uio.h>
#include <time.h>
#include <unistd.h>
#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;
}

27
c302.h Normal file
View File

@ -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<P>%s\r\n"
#define H404 "HTTP/1.0 404 NOT FOUND\r\nContent-Type: text/html\r\n\r\n<HTML><TITLE>Not Found</TITLE>\r\n<BODY><P>%s\r\n</BODY></HTML>\r\n"
#define H500 "HTTP/1.0 500 Internal Server Error\r\nContent-Type: text/html\r\n\r\n<P>%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