| 1 | |
| 2 | typedef struct phr_header phr_header_t; |
| 3 | |
| 4 | #include "src/picohttpparser.h" |
| 5 | #include <stdint.h> |
| 6 | |
| 7 | #define ADVANCE_TOKEN2(buf, tok, toklen, max_len) \ |
| 8 | do {\ |
| 9 | for (u32 i = 0; i < max_len; i++) {\ |
| 10 | if (buf[i] == ' ') {\ |
| 11 | tok = buf;\ |
| 12 | toklen = i++;\ |
| 13 | while (buf[i] == ' ') i++;\ |
| 14 | buf += i;\ |
| 15 | break;\ |
| 16 | }\ |
| 17 | }\ |
| 18 | } while (0) |
| 19 | |
| 20 | static inline int phr_parse_request_path(const char *buf_start, size_t len, const char **method, size_t *method_len, const char **path, size_t *path_len) { |
| 21 | if (len < 14) return -2; |
| 22 | const char *buf = buf_start, *buf_end = buf_start + len; |
| 23 | |
| 24 | ADVANCE_TOKEN2(buf, *method, *method_len, 9); |
| 25 | len -= buf-buf_start; |
| 26 | ADVANCE_TOKEN2(buf, *path, *path_len, len); |
| 27 | if (*method_len == 0 || *path_len == 0) return -1; |
| 28 | |
| 29 | return buf-buf_start; |
| 30 | } |
| 31 | |
| 32 | static inline int phr_parse_request_path_pipeline(const char *buf_start, size_t len, const char **method, size_t *method_len, const char **path, size_t *path_len) { |
| 33 | if (len < 14) return -2; |
| 34 | const char *buf = buf_start, *buf_end = buf_start + len; |
| 35 | |
| 36 | ADVANCE_TOKEN2(buf, *method, *method_len, 9); |
| 37 | len -= buf-buf_start; |
| 38 | ADVANCE_TOKEN2(buf, *path, *path_len, len); |
| 39 | if (*method_len == 0 || *path_len == 0) return -1; |
| 40 | |
| 41 | while (buf < buf_end) { |
| 42 | ++buf; |
| 43 | if (*(uint32_t*)buf == 0x0a0d0a0d) { |
| 44 | buf += 4; |
| 45 | return (int)(buf - buf_start); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | return -1; |
| 50 | } |
| 51 | |
| 52 | // date |
| 53 | const char* get_date(); |
| 54 | |
| 55 | // branchlut |
| 56 | const char gDigitsLut[200] = { |
| 57 | '0','0','0','1','0','2','0','3','0','4','0','5','0','6','0','7','0','8','0','9', |
| 58 | '1','0','1','1','1','2','1','3','1','4','1','5','1','6','1','7','1','8','1','9', |
| 59 | '2','0','2','1','2','2','2','3','2','4','2','5','2','6','2','7','2','8','2','9', |
| 60 | '3','0','3','1','3','2','3','3','3','4','3','5','3','6','3','7','3','8','3','9', |
| 61 | '4','0','4','1','4','2','4','3','4','4','4','5','4','6','4','7','4','8','4','9', |
| 62 | '5','0','5','1','5','2','5','3','5','4','5','5','5','6','5','7','5','8','5','9', |
| 63 | '6','0','6','1','6','2','6','3','6','4','6','5','6','6','6','7','6','8','6','9', |
| 64 | '7','0','7','1','7','2','7','3','7','4','7','5','7','6','7','7','7','8','7','9', |
| 65 | '8','0','8','1','8','2','8','3','8','4','8','5','8','6','8','7','8','8','8','9', |
| 66 | '9','0','9','1','9','2','9','3','9','4','9','5','9','6','9','7','9','8','9','9' |
| 67 | }; |
| 68 | |
| 69 | static inline int u64toa(char* buf, uint64_t value) { |
| 70 | const char* b = buf; |
| 71 | if (value < 100000000) { |
| 72 | uint32_t v = (uint32_t)(value); |
| 73 | if (v < 10000) { |
| 74 | const uint32_t d1 = (v / 100) << 1; |
| 75 | const uint32_t d2 = (v % 100) << 1; |
| 76 | |
| 77 | if (v >= 1000) |
| 78 | *buf++ = gDigitsLut[d1]; |
| 79 | if (v >= 100) |
| 80 | *buf++ = gDigitsLut[d1 + 1]; |
| 81 | if (v >= 10) |
| 82 | *buf++ = gDigitsLut[d2]; |
| 83 | *buf++ = gDigitsLut[d2 + 1]; |
| 84 | } |
| 85 | else { |
| 86 | // value = bbbbcccc |
| 87 | const uint32_t b = v / 10000; |
| 88 | const uint32_t c = v % 10000; |
| 89 | |
| 90 | const uint32_t d1 = (b / 100) << 1; |
| 91 | const uint32_t d2 = (b % 100) << 1; |
| 92 | |
| 93 | const uint32_t d3 = (c / 100) << 1; |
| 94 | const uint32_t d4 = (c % 100) << 1; |
| 95 | |
| 96 | if (value >= 10000000) |
| 97 | *buf++ = gDigitsLut[d1]; |
| 98 | if (value >= 1000000) |
| 99 | *buf++ = gDigitsLut[d1 + 1]; |
| 100 | if (value >= 100000) |
| 101 | *buf++ = gDigitsLut[d2]; |
| 102 | *buf++ = gDigitsLut[d2 + 1]; |
| 103 | |
| 104 | *buf++ = gDigitsLut[d3]; |
| 105 | *buf++ = gDigitsLut[d3 + 1]; |
| 106 | *buf++ = gDigitsLut[d4]; |
| 107 | *buf++ = gDigitsLut[d4 + 1]; |
| 108 | } |
| 109 | } |
| 110 | // *buf = '\0'; |
| 111 | return buf - b; |
| 112 | } |
| 113 | |