1
0
mirror of https://github.com/fumiama/CMoe-Counter.git synced 2024-11-23 22:00:56 +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 COUNTER counter;
#define ADD_HEADER(h) {\
strcpy(buf + offset, (h));\
offset += sizeof((h)) - 1;\
}
#define ADD_HEADER_PARAM(h, p) {\
sprintf(buf + offset, (h), (p));\
offset += strlen(buf + offset);\
}
static void headers(uint32_t content_len, const char* content_type) {
char buf[1024];
uint32_t offset = 0;
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);
#define ADD_HEADER_PARAM(buf, offset, h, p) sprintf(buf + offset, (h), (p))
#define HEADER(content_type) HTTP200 SERVER_STRING CACHE_CTRL CONTENT_TYPE(content_type)
#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) {
char buf[64];
size_t offset = ADD_HEADER_PARAM(buf, 0, CONTENT_LEN "\r\n", content_len);
if(offset <= 0) {
write(1, "\0\0\0\0", 4);
exit(EXIT_FAILURE);
}
content_len += offset+hlen;
struct iovec iov[3] = {{&content_len, sizeof(uint32_t)}, {h, hlen}, {buf, offset}};
writev(1, &iov, 3);
}
static void http_error(ERRCODE code, char* msg) {
@ -145,7 +139,7 @@ static void return_count(char* name, char* theme) {
h = H_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));
for(int i = 0; cntstr[i]; i++) {
printf(img_slot_front, w * i, w, h);
@ -211,7 +205,7 @@ int main(int argc, char **argv) {
add_user(name, 0, fp);
fclose(fp);
char* msg = "<P>Success.\r\n";
headers(strlen(msg), "text/html");
headers(strlen(msg), text/html);
write(1, msg, strlen(msg));
} else http_error(HTTP500, "Open File Error.");
} 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};
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 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[] =
"<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"
" <g>\n"
" \n";
" <g>\n";
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"
" <title>Moe Count</title>\n"
" <g>\n"
" \n";
" <g>\n";
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 svg_tail[] =