1
0
mirror of https://github.com/fumiama/CMoe-Counter.git synced 2024-11-24 06:10:11 +01:00

小修补

This commit is contained in:
源文雨 2021-09-12 00:05:52 +08:00
parent 0be8efff9b
commit 61b453f06c
2 changed files with 15 additions and 10 deletions

11
cmoe.c
View File

@ -22,7 +22,7 @@ static void headers(uint32_t content_len, const char* content_type) {
char buf[1024]; char buf[1024];
uint32_t offset = 0; uint32_t offset = 0;
ADD_HERDER(HTTP200 SERVER_STRING); ADD_HERDER(H200 SERVER_STRING);
ADD_HERDER(CACHE_CTRL); ADD_HERDER(CACHE_CTRL);
ADD_HERDER_PARAM(CONTENT_TYPE, content_type); ADD_HERDER_PARAM(CONTENT_TYPE, content_type);
ADD_HERDER_PARAM(CONTENT_LEN "\r\n", content_len); ADD_HERDER_PARAM(CONTENT_LEN "\r\n", content_len);
@ -31,11 +31,10 @@ static void headers(uint32_t content_len, const char* content_type) {
write(1, buf, offset); write(1, buf, offset);
} }
static void http_error(const char* type, const char* msg) { static void http_error(RESPCODE code, char* msg) {
char* str = malloc(strlen(type) + strlen(msg)); uint32_t len = strlen(msg) + typel[code];
uint32_t len = strlen(str); char* str = malloc(len);
sprintf(str, type, msg); sprintf(str, types[code], msg);
fflush(stdout);
write(1, (char*)&len, sizeof(uint32_t)); write(1, (char*)&len, sizeof(uint32_t));
write(1, str, len); write(1, str, len);
free(str); free(str);

14
cmoe.h
View File

@ -1,6 +1,8 @@
#ifndef _CMOE_H_ #ifndef _CMOE_H_
#define _CMOE_H_ #define _CMOE_H_
#include <stdint.h>
#define DATFILE "dat.sp" #define DATFILE "dat.sp"
#define TOKEN "fumiama" #define TOKEN "fumiama"
@ -13,10 +15,14 @@ typedef struct COUNTER COUNTER;
#define SERVER_STRING "Server: CMoe Counter by Fumiama/1.0\r\n" #define SERVER_STRING "Server: CMoe Counter by Fumiama/1.0\r\n"
#define HTTP200 "HTTP/1.0 200 OK\r\n" #define H200 "HTTP/1.0 200 OK\r\n"
#define HTTP400 "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"
#define HTTP404 "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 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 HTTP500 "HTTP/1.0 500 Internal Server Error\r\nContent-Type: text/html\r\n\r\n<P>%s\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[] = {H200, H400, H404, H500};
const uint8_t typel[] = {18, 59, 111, 69};
enum RESPCODE {HTTP200, HTTP400, HTTP404, HTTP500};
typedef enum RESPCODE RESPCODE;
#define CONTENT_TYPE "Content-Type: %s\r\n" #define CONTENT_TYPE "Content-Type: %s\r\n"
#define CONTENT_LEN "Content-Length: %d\r\n" #define CONTENT_LEN "Content-Length: %d\r\n"