1
0
mirror of https://github.com/fumiama/CMoe-Counter.git synced 2024-11-27 23:20:49 +01:00

fix: headers

This commit is contained in:
源文雨 2022-03-05 14:14:28 +08:00
parent 84bc015003
commit 99fe42e45e
2 changed files with 18 additions and 26 deletions

36
cmoe.c
View File

@ -10,25 +10,19 @@
static uint32_t* items_len; static uint32_t* items_len;
static COUNTER counter; static COUNTER counter;
#define ADD_HEADER(h) {\ #define ADD_HEADER_PARAM(buf, offset, h, p) sprintf(buf + offset, (h), (p))
strcpy(buf + offset, (h));\ #define HEADER(content_type) HTTP200 SERVER_STRING CACHE_CTRL CONTENT_TYPE(content_type)
offset += sizeof((h)) - 1;\ #define headers(content_len, content_type) (_headers(content_len, HEADER(content_type), sizeof(HEADER(content_type))-1))
} static void _headers(uint32_t content_len, const char* h, size_t hlen) {
#define ADD_HEADER_PARAM(h, p) {\ char buf[64];
sprintf(buf + offset, (h), (p));\ size_t offset = ADD_HEADER_PARAM(buf, 0, CONTENT_LEN "\r\n", content_len);
offset += strlen(buf + offset);\ if(offset <= 0) {
} write(1, "\0\0\0\0", 4);
exit(EXIT_FAILURE);
static void headers(uint32_t content_len, const char* content_type) { }
char buf[1024]; content_len += offset+hlen;
uint32_t offset = 0; struct iovec iov[3] = {{&content_len, sizeof(uint32_t)}, {h, hlen}, {buf, offset}};
writev(1, &iov, 3);
ADD_HEADER(HTTP200 SERVER_STRING CACHE_CTRL);
ADD_HEADER_PARAM(CONTENT_TYPE, content_type);
ADD_HEADER_PARAM(CONTENT_LEN "\r\n", content_len);
content_len += offset;
struct iovec iov[2] = {{&content_len, sizeof(uint32_t)}, {buf, offset}};
writev(1, &iov, 2);
} }
static void http_error(ERRCODE code, char* msg) { static void http_error(ERRCODE code, char* msg) {
@ -145,7 +139,7 @@ static void return_count(char* name, char* theme) {
h = H_SMALL; h = H_SMALL;
head = svg_small; head = svg_small;
} }
headers(get_content_len(isbig, len_type, cntstr), "image/svg+xml"); headers(get_content_len(isbig, len_type, cntstr), image/svg+xml);
printf(head, w*(10+cntstrbuf-cntstr)); printf(head, w*(10+cntstrbuf-cntstr));
for(int i = 0; cntstr[i]; i++) { for(int i = 0; cntstr[i]; i++) {
printf(img_slot_front, w * i, w, h); printf(img_slot_front, w * i, w, h);
@ -211,7 +205,7 @@ int main(int argc, char **argv) {
add_user(name, 0, fp); add_user(name, 0, fp);
fclose(fp); fclose(fp);
char* msg = "<P>Success.\r\n"; char* msg = "<P>Success.\r\n";
headers(strlen(msg), "text/html"); headers(strlen(msg), text/html);
write(1, msg, strlen(msg)); write(1, msg, strlen(msg));
} else http_error(HTTP500, "Open File Error."); } else http_error(HTTP500, "Open File Error.");
} else http_error(HTTP400, "Name Exist."); } else http_error(HTTP400, "Name Exist.");

8
cmoe.h
View File

@ -24,7 +24,7 @@ const uint8_t typel[] = {59, 111, 69};
enum ERRCODE {HTTP400, HTTP404, HTTP500}; enum ERRCODE {HTTP400, HTTP404, HTTP500};
typedef enum ERRCODE ERRCODE; typedef enum ERRCODE ERRCODE;
#define CONTENT_TYPE "Content-Type: %s\r\n" #define CONTENT_TYPE(x) "Content-Type: " #x "\r\n"
#define CONTENT_LEN "Content-Length: %d\r\n" #define CONTENT_LEN "Content-Length: %d\r\n"
#define CACHE_CTRL "Cache-Control: max-age=0, no-cache, no-store, must-revalidate\r\n" #define CACHE_CTRL "Cache-Control: max-age=0, no-cache, no-store, must-revalidate\r\n"
@ -42,13 +42,11 @@ const uint16_t gbhl[] = {1550, 1702, 1870, 1778, 1618, 2370, 1830, 2234, 1914, 1
const char svg_small[] = const char svg_small[] =
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"%d\" height=\"100\" version=\"1.1\">\n" "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"%d\" height=\"100\" version=\"1.1\">\n"
" <title>Moe Count</title>\n" " <title>Moe Count</title>\n"
" <g>\n" " <g>\n";
" \n";
const char svg_big[] = const char svg_big[] =
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"%d\" height=\"150\" version=\"1.1\">\n" "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"%d\" height=\"150\" version=\"1.1\">\n"
" <title>Moe Count</title>\n" " <title>Moe Count</title>\n"
" <g>\n" " <g>\n";
" \n";
const char img_slot_front[] = " <image x=\"%d\" y=\"0\" width=\"%d\" height=\"%d\" xlink:href=\""; const char img_slot_front[] = " <image x=\"%d\" y=\"0\" width=\"%d\" height=\"%d\" xlink:href=\"";
const char img_slot_rear[] = "\"></image>\n"; const char img_slot_rear[] = "\"></image>\n";
const char svg_tail[] = const char svg_tail[] =