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