diff --git a/cmoe.c b/cmoe.c index 10ef3a9..c934a97 100644 --- a/cmoe.c +++ b/cmoe.c @@ -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 = "

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."); diff --git a/cmoe.h b/cmoe.h index c3ebcd0..f97b58e 100644 --- a/cmoe.h +++ b/cmoe.h @@ -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[] = "\n" " Moe Count\n" - " \n" - " \n"; + " \n"; const char svg_big[] = "\n" " Moe Count\n" - " \n" - " \n"; + " \n"; const char img_slot_front[] = " \n"; const char svg_tail[] =