xs/proto_http.c

filter: Statementstmt 53.5% 190 / 355 Branchbran 37.6% 116 / 308 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 46.1% 306 / 663 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« ClickHouse.xs ↑ Summary xs/io.c »
1 /* --- HTTP request building --- */
2  
3 /* Tiny base64 encoder (RFC 4648). Writes ((src_len + 2) / 3) * 4 bytes plus
4 * a NUL into dst (caller-allocated). Used only for HTTP basic auth. */
5 0 static size_t base64_encode(const unsigned char *src, size_t src_len, char *dst) {
6 static const char ALPHA[] =
7 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
8 0 size_t i, o = 0;
9 0 ▶ for (i = 0; i + 3 <= src_len; i += 3) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 3 <= src_len; i += 3) { 0 0 0
10 0 dst[o++] = ALPHA[ src[i] >> 2];
11 0 dst[o++] = ALPHA[((src[i] & 0x03) << 4) | (src[i+1] >> 4)];
12 0 dst[o++] = ALPHA[((src[i+1] & 0x0f) << 2) | (src[i+2] >> 6)];
13 0 dst[o++] = ALPHA[ src[i+2] & 0x3f];
14 }
15 0 ▶ if (i < src_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (i < src_len) { 0 0 0
16 0 dst[o++] = ALPHA[src[i] >> 2];
17 0 ▶ if (i + 1 == src_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (i + 1 == src_len) { 0 0 0
18 0 dst[o++] = ALPHA[(src[i] & 0x03) << 4];
19 0 dst[o++] = '=';
20 } else {
21 0 dst[o++] = ALPHA[((src[i] & 0x03) << 4) | (src[i+1] >> 4)];
22 0 dst[o++] = ALPHA[ (src[i+1] & 0x0f) << 2];
23 }
24 0 dst[o++] = '=';
25 }
26 0 dst[o] = '\0';
27 0 return o;
28 }
29  
30 /*
31 * Build HTTP POST request. Used for both SELECT (sql in body, url_sql=NULL)
32 * and INSERT (url_sql="INSERT ... FORMAT TabSeparated" in URL, data in body).
33 * Returns malloc'd buffer with full request.
34 */
35 26 static char* build_http_post_request(ev_clickhouse_t *self,
36 const char *url_sql, size_t url_sql_len,
37 const char *body_data, size_t body_data_len,
38 HV *defaults, HV *overrides,
39 size_t *req_len) {
40 char *req;
41 size_t req_cap;
42 26 size_t pos = 0;
43 26 char *body = NULL;
44 26 size_t body_len = body_data_len;
45 26 const char *content_encoding = NULL;
46  
47 /* compress body if requested */
48 26 ▶ if (self->compress && body_data_len > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->compress && body_data_len > 0) { 0 26 26
if (self->compress && body_data_len > 0) { 0 0 0
49 size_t gz_len;
50 0 body = gzip_compress(body_data, body_data_len, &gz_len);
51 0 ▶ if (body) {
Branch: 1 branch
BranchTrueFalseTotal
if (body) { 0 0 0
52 0 body_len = gz_len;
53 0 content_encoding = "Content-Encoding: gzip\r\n";
54 }
55 }
56  
57 /* build URL params (growable buffer; static prefix first) */
58 26 const char *query_id = NULL;
59 26 STRLEN query_id_len = 0;
60 native_buf_t params;
61 size_t plen;
62 26 nbuf_init(&params);
63 26 nbuf_grow(&params, 128
64 26 ▶ + (self->database ? strlen(self->database) * 3 : 0)
Branch: 1 branch
BranchTrueFalseTotal
+ (self->database ? strlen(self->database) * 3 : 0) 26 0 26
65 26 ▶ + (self->session_id ? strlen(self->session_id) * 3 : 0)
Branch: 1 branch
BranchTrueFalseTotal
+ (self->session_id ? strlen(self->session_id) * 3 : 0) 0 26 26
66 26 + url_sql_len * 3);
67 26 ▶ if (self->database) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->database) { 26 0 26
68 26 nbuf_append(&params, "?database=", sizeof("?database=") - 1);
69 26 nbuf_grow(&params, strlen(self->database) * 3);
70 52 params.len += url_encode(self->database, strlen(self->database),
71 26 params.data + params.len);
72 26 nbuf_append(&params, "&wait_end_of_query=1",
73 sizeof("&wait_end_of_query=1") - 1);
74 } else {
75 0 nbuf_append(&params, "?wait_end_of_query=1",
76 sizeof("?wait_end_of_query=1") - 1);
77 }
78 26 ▶ if (self->session_id) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->session_id) { 0 26 26
79 0 nbuf_append(&params, "&session_id=", sizeof("&session_id=") - 1);
80 0 nbuf_grow(&params, strlen(self->session_id) * 3);
81 0 params.len += url_encode(self->session_id, strlen(self->session_id),
82 0 params.data + params.len);
83 }
84 26 ▶ if (url_sql) {
Branch: 1 branch
BranchTrueFalseTotal
if (url_sql) { 0 26 26
85 0 nbuf_append(&params, "&query=", sizeof("&query=") - 1);
86 0 nbuf_grow(&params, url_sql_len * 3);
87 0 params.len += url_encode(url_sql, url_sql_len, params.data + params.len);
88 }
89 26 append_settings_url_params(&params, defaults, overrides,
90 &query_id, &query_id_len);
91 26 ▶ if (query_id)
Branch: 1 branch
BranchTrueFalseTotal
if (query_id) 0 26 26
92 0 append_url_param(&params, "query_id", 8, query_id, query_id_len);
93 26 nbuf_u8(&params, '\0');
94 26 plen = params.len - 1;
95  
96 /* user/password are quoted as-is in the X-ClickHouse-* form, or
97 * base64-expanded ((n+2)/3)*4 in the Basic auth form. The 4/3 factor
98 * is enough either way. */
99 52 req_cap = 512 + body_len + plen
100 26 ▶ + (self->host ? strlen(self->host) : 0)
Branch: 1 branch
BranchTrueFalseTotal
+ (self->host ? strlen(self->host) : 0) 26 0 26
101 26 ▶ + (self->user ? strlen(self->user) * 2 : 0)
Branch: 1 branch
BranchTrueFalseTotal
+ (self->user ? strlen(self->user) * 2 : 0) 26 0 26
102 26 ▶ + (self->password ? strlen(self->password) * 2 : 0)
Branch: 1 branch
BranchTrueFalseTotal
+ (self->password ? strlen(self->password) * 2 : 0) 26 0 26
103 + 2; /* IPv6 Host brackets */
104 26 Newx(req, req_cap, char);
105  
106 /* request line + headers */
107 26 pos += snprintf(req + pos, req_cap - pos,
108 "POST /%s HTTP/1.1\r\n", params.data);
109 26 Safefree(params.data);
110 {
111 26 ▶ int v6 = self->host && strchr(self->host, ':') != NULL;
Branch: 2 branches
BranchTrueFalseTotal
int v6 = self->host && strchr(self->host, ':') != NULL; 26 0 26
int v6 = self->host && strchr(self->host, ':') != NULL; 0 26 26
112 26 ▶ pos += snprintf(req + pos, req_cap - pos, "Host: %s%s%s:%u\r\n",
Branch: 2 branches
BranchTrueFalseTotal
pos += snprintf(req + pos, req_cap - pos, "Host: %s%s%s:%u\r\n", 0 26 26
pos += snprintf(req + pos, req_cap - pos, "Host: %s%s%s:%u\r\n", 0 26 26
113 v6 ? "[" : "", self->host, v6 ? "]" : "",
114 self->port);
115 }
116 26 ▶ if (self->http_basic_auth && self->user) {
Branch: 2 branches
BranchTrueFalseTotal
if (self->http_basic_auth && self->user) { 0 26 26
if (self->http_basic_auth && self->user) { 0 0 0
117 /* "user:pass" → base64. Single allocation: [cred][b64]. */
118 0 size_t ul = strlen(self->user);
119 0 ▶ size_t pl = self->password ? strlen(self->password) : 0;
Branch: 1 branch
BranchTrueFalseTotal
size_t pl = self->password ? strlen(self->password) : 0; 0 0 0
120 0 size_t cred_len = ul + 1 + pl;
121 0 size_t b64_cap = ((cred_len + 2) / 3) * 4 + 1;
122 char *buf;
123 0 Newx(buf, cred_len + b64_cap, char);
124 0 memcpy(buf, self->user, ul);
125 0 buf[ul] = ':';
126 0 ▶ if (pl) memcpy(buf + ul + 1, self->password, pl);
Branch: 1 branch
BranchTrueFalseTotal
if (pl) memcpy(buf + ul + 1, self->password, pl); 0 0 0
127 0 base64_encode((const unsigned char *)buf, cred_len, buf + cred_len);
128 0 pos += snprintf(req + pos, req_cap - pos,
129 "Authorization: Basic %s\r\n", buf + cred_len);
130 0 Safefree(buf);
131 } else {
132 26 ▶ if (self->user)
Branch: 1 branch
BranchTrueFalseTotal
if (self->user) 26 0 26
133 26 pos += snprintf(req + pos, req_cap - pos,
134 "X-ClickHouse-User: %s\r\n", self->user);
135 26 ▶ if (self->password && self->password[0])
Branch: 2 branches
BranchTrueFalseTotal
if (self->password && self->password[0]) 26 0 26
if (self->password && self->password[0]) 0 26 26
136 0 pos += snprintf(req + pos, req_cap - pos,
137 "X-ClickHouse-Key: %s\r\n", self->password);
138 }
139 26 pos += snprintf(req + pos, req_cap - pos, "Connection: keep-alive\r\n");
140 26 ▶ if (self->compress)
Branch: 1 branch
BranchTrueFalseTotal
if (self->compress) 0 26 26
141 0 pos += snprintf(req + pos, req_cap - pos, "Accept-Encoding: gzip\r\n");
142 26 ▶ if (content_encoding)
Branch: 1 branch
BranchTrueFalseTotal
if (content_encoding) 0 26 26
143 0 pos += snprintf(req + pos, req_cap - pos, "%s", content_encoding);
144 26 pos += snprintf(req + pos, req_cap - pos,
145 "Content-Length: %lu\r\n\r\n", (unsigned long)body_len);
146  
147 /* body */
148 26 ▶ if (body_len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (body_len > 0) { 26 0 26
149 26 ▶ if (pos + body_len > req_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos + body_len > req_cap) { 0 26 26
150 0 req_cap = pos + body_len + 1;
151 0 Renew(req, req_cap, char);
152 }
153 26 ▶ Copy(body ? body : body_data, req + pos, body_len, char);
Branch: 1 branch
BranchTrueFalseTotal
Copy(body ? body : body_data, req + pos, body_len, char); 0 26 26
154 26 pos += body_len;
155 }
156  
157 26 ▶ if (body) Safefree(body);
Branch: 1 branch
BranchTrueFalseTotal
if (body) Safefree(body); 0 26 26
158  
159 26 *req_len = pos;
160 26 return req;
161 }
162  
163 /* Build HTTP GET /ping request */
164 4 static char* build_http_ping_request(ev_clickhouse_t *self, size_t *req_len) {
165 char *req;
166 4 ▶ size_t req_cap = 130 + (self->host ? strlen(self->host) : 0);
Branch: 1 branch
BranchTrueFalseTotal
size_t req_cap = 130 + (self->host ? strlen(self->host) : 0); 4 0 4
167 4 size_t pos = 0;
168 4 ▶ int v6 = self->host && strchr(self->host, ':') != NULL;
Branch: 2 branches
BranchTrueFalseTotal
int v6 = self->host && strchr(self->host, ':') != NULL; 4 0 4
int v6 = self->host && strchr(self->host, ':') != NULL; 0 4 4
169  
170 4 Newx(req, req_cap, char);
171 4 ▶ pos = snprintf(req, req_cap,
Branch: 2 branches
BranchTrueFalseTotal
pos = snprintf(req, req_cap, 0 4 4
pos = snprintf(req, req_cap, 0 4 4
172 "GET /ping HTTP/1.1\r\n"
173 "Host: %s%s%s:%u\r\n"
174 "Connection: keep-alive\r\n\r\n",
175 v6 ? "[" : "", self->host, v6 ? "]" : "",
176 self->port);
177 4 ▶ if (pos >= req_cap) pos = req_cap - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (pos >= req_cap) pos = req_cap - 1; 0 4 4
178 4 *req_len = pos;
179 4 return req;
180 }
181  
182 /* --- HTTP response parsing --- */
183  
184 /* Length-bounded uint parser. Stops at the first non-digit OR at `len`,
185 * whichever comes first. Used to parse X-ClickHouse-Summary values out
186 * of a non-NUL-terminated header buffer without letting strtoull scan
187 * into bytes past the header on malformed input. */
188 0 static uint64_t parse_uint_within(const char *p, size_t len) {
189 0 uint64_t n = 0;
190 size_t i;
191 0 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 0 0 0
192 0 unsigned char c = (unsigned char)p[i];
193 0 ▶ if (c < '0' || c > '9') break;
Branch: 2 branches
BranchTrueFalseTotal
if (c < '0' || c > '9') break; 0 0 0
if (c < '0' || c > '9') break; 0 0 0
194 0 n = n * 10 + (c - '0');
195 }
196 0 return n;
197 }
198  
199 /* Find \r\n\r\n in recv_buf. Returns offset past it, or 0 if not found. */
200 1 static size_t find_header_end(const char *buf, size_t len) {
201 size_t i;
202 1 ▶ if (len < 4) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 4) return 0; 0 1 1
203 50 ▶ for (i = 0; i <= len - 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= len - 4; i++) { 50 0 50
204 50 ▶ if (buf[i] == '\r' && buf[i+1] == '\n' &&
Branch: 2 branches
BranchTrueFalseTotal
if (buf[i] == '\r' && buf[i+1] == '\n' && 2 48 50
if (buf[i] == '\r' && buf[i+1] == '\n' && 2 0 2
205 2 ▶ buf[i+2] == '\r' && buf[i+3] == '\n') {
Branch: 2 branches
BranchTrueFalseTotal
buf[i+2] == '\r' && buf[i+3] == '\n') { 1 1 2
buf[i+2] == '\r' && buf[i+3] == '\n') { 1 0 1
206 1 return i + 4;
207 }
208 }
209 0 return 0;
210 }
211  
212 /* Extract ClickHouse error code from HTTP error body ("Code: NNN. ...") */
213 0 static int32_t parse_ch_error_code(const char *body, size_t len) {
214 0 ▶ if (len > 6 && memcmp(body, "Code: ", 6) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (len > 6 && memcmp(body, "Code: ", 6) == 0) 0 0 0
if (len > 6 && memcmp(body, "Code: ", 6) == 0) 0 0 0
215 0 return (int32_t)atoi(body + 6);
216 0 return 0;
217 }
218  
219 /* Format an HTTP error response into a Newx-allocated "HTTP NNN: ..." message
220 * and update self->last_error_code. Body may be gzip-compressed. Caller must
221 * Safefree the returned pointer. */
222 0 static char* format_http_error(ev_clickhouse_t *self, int status,
223 const char *body, size_t body_len, int is_gzip) {
224 char *errmsg;
225 0 char *err_body = (char *)body;
226 0 size_t err_len = body_len;
227 0 ▶ if (is_gzip && body_len > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_gzip && body_len > 0) { 0 0 0
if (is_gzip && body_len > 0) { 0 0 0
228 size_t dec_len;
229 0 char *dec = gzip_decompress(body, body_len, &dec_len);
230 0 ▶ if (dec) { err_body = dec; err_len = dec_len; }
Branch: 1 branch
BranchTrueFalseTotal
if (dec) { err_body = dec; err_len = dec_len; } 0 0 0
231 }
232 0 ▶ while (err_len > 0 && (err_body[err_len-1] == '\n' || err_body[err_len-1] == '\r'))
Branch: 3 branches
BranchTrueFalseTotal
while (err_len > 0 && (err_body[err_len-1] == '\n' || err_body[err_len-1] == '\r')) 0 0 0
while (err_len > 0 && (err_body[err_len-1] == '\n' || err_body[err_len-1] == '\r')) 0 0 0
while (err_len > 0 && (err_body[err_len-1] == '\n' || err_body[err_len-1] == '\r')) 0 0 0
233 0 err_len--;
234 0 self->last_error_code = parse_ch_error_code(err_body, err_len);
235 0 Newx(errmsg, err_len + 64, char);
236 0 snprintf(errmsg, err_len + 64, "HTTP %d: %.*s",
237 status, (int)err_len, err_body);
238 0 ▶ if (err_body != body) Safefree(err_body);
Branch: 1 branch
BranchTrueFalseTotal
if (err_body != body) Safefree(err_body); 0 0 0
239 0 return errmsg;
240 }
241  
242 /* Parse HTTP status line, extract status code */
243 1 static int parse_http_status(const char *buf, size_t len) {
244 /* HTTP/1.1 200 OK\r\n */
245 1 const char *p = buf;
246 1 const char *end = buf + len;
247 int status;
248  
249 /* skip "HTTP/1.x " */
250 9 ▶ while (p < end && *p != ' ') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != ' ') p++; 9 0 9
while (p < end && *p != ' ') p++; 8 1 9
251 1 ▶ if (p >= end) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) return 0; 0 1 1
252 1 p++;
253  
254 1 status = atoi(p);
255 1 ▶ if (status < 100 || status > 599) return 500; /* treat malformed as server error */
Branch: 2 branches
BranchTrueFalseTotal
if (status < 100 || status > 599) return 500; /* treat malformed as server error */ 1 0 1
if (status < 100 || status > 599) return 500; /* treat malformed as server error */ 0 1 1
256 1 return status;
257 }
258  
259 /* Find header value (case-insensitive). Returns pointer into buf or NULL. */
260 4 static const char* find_header(const char *headers, size_t headers_len,
261 const char *name, size_t *value_len) {
262 4 size_t name_len = strlen(name);
263 4 const char *p = headers;
264 4 const char *end = headers + headers_len;
265  
266 14 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 11 3 14
267 11 const char *line_end = p;
268 199 ▶ while (line_end < end && *line_end != '\r') line_end++;
Branch: 2 branches
BranchTrueFalseTotal
while (line_end < end && *line_end != '\r') line_end++; 199 0 199
while (line_end < end && *line_end != '\r') line_end++; 188 11 199
269  
270 11 ▶ if ((size_t)(line_end - p) > name_len + 1 && p[name_len] == ':') {
Branch: 2 branches
BranchTrueFalseTotal
if ((size_t)(line_end - p) > name_len + 1 && p[name_len] == ':') { 4 7 11
if ((size_t)(line_end - p) > name_len + 1 && p[name_len] == ':') { 1 3 4
271 1 int match = 1;
272 size_t i;
273 18 ▶ for (i = 0; i < name_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < name_len; i++) { 17 1 18
274 17 ▶ if (tolower((unsigned char)p[i]) != tolower((unsigned char)name[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (tolower((unsigned char)p[i]) != tolower((unsigned char)name[i])) { 0 17 17
275 0 match = 0;
276 0 break;
277 }
278 }
279 1 ▶ if (match) {
Branch: 1 branch
BranchTrueFalseTotal
if (match) { 1 0 1
280 1 const char *val = p + name_len + 1;
281 2 ▶ while (val < line_end && *val == ' ') val++;
Branch: 2 branches
BranchTrueFalseTotal
while (val < line_end && *val == ' ') val++; 2 0 2
while (val < line_end && *val == ' ') val++; 1 1 2
282 1 *value_len = line_end - val;
283 1 return val;
284 }
285 }
286  
287 /* advance past \r\n */
288 10 ▶ if (line_end + 2 <= end) p = line_end + 2;
Branch: 1 branch
BranchTrueFalseTotal
if (line_end + 2 <= end) p = line_end + 2; 10 0 10
289 0 else break;
290 }
291 3 return NULL;
292 }
293  
294 /* Case-insensitive comma-separated token match (Transfer-Encoding and
295 * Content-Encoding are lists, e.g. "gzip, chunked"). val is NOT
296 * NUL-terminated; surrounding whitespace is tolerated. */
297 2 static int header_has_token(const char *val, size_t vlen,
298 const char *tok, size_t tlen) {
299 2 size_t i = 0;
300 5 ▶ while (i < vlen) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < vlen) { 3 0 3
301 size_t start, end;
302 4 ▶ while (i < vlen && (val[i] == ' ' || val[i] == '\t')) i++;
Branch: 3 branches
BranchTrueFalseTotal
while (i < vlen && (val[i] == ' ' || val[i] == '\t')) i++; 4 0 4
while (i < vlen && (val[i] == ' ' || val[i] == '\t')) i++; 1 3 4
while (i < vlen && (val[i] == ' ' || val[i] == '\t')) i++; 0 3 3
303 3 start = i;
304 18 ▶ while (i < vlen && val[i] != ',') i++;
Branch: 2 branches
BranchTrueFalseTotal
while (i < vlen && val[i] != ',') i++; 17 1 18
while (i < vlen && val[i] != ',') i++; 15 2 17
305 3 end = i;
306 3 ▶ while (end > start && (val[end - 1] == ' ' || val[end - 1] == '\t')) end--;
Branch: 3 branches
BranchTrueFalseTotal
while (end > start && (val[end - 1] == ' ' || val[end - 1] == '\t')) end--; 3 0 3
while (end > start && (val[end - 1] == ' ' || val[end - 1] == '\t')) end--; 0 3 3
while (end > start && (val[end - 1] == ' ' || val[end - 1] == '\t')) end--; 0 3 3
307 3 ▶ if (end - start == tlen && strncasecmp(val + start, tok, tlen) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (end - start == tlen && strncasecmp(val + start, tok, tlen) == 0) 2 1 3
if (end - start == tlen && strncasecmp(val + start, tok, tlen) == 0) 2 0 2
308 2 return 1;
309 1 ▶ if (i < vlen) i++;
Branch: 1 branch
BranchTrueFalseTotal
if (i < vlen) i++; 1 0 1
310 }
311 0 return 0;
312 }
313  
314 /* Parse a complete HTTP response from recv_buf. */
315 1 static void process_http_response(ev_clickhouse_t *self) {
316 size_t hdr_end;
317 int status;
318 const char *val;
319 size_t val_len;
320 1 size_t content_length = 0;
321 1 int chunked = 0;
322 1 int is_gzip = 0;
323 const char *body;
324 size_t body_len;
325 1 char *decoded = NULL;
326 1 size_t decoded_len = 0;
327 1 size_t decoded_cap = 0;
328 1 const char *cp = NULL; /* chunk parse cursor; read at chunk_need_more */
329  
330 1 ▶ if (self->recv_len == 0 || self->send_count == 0) return;
Branch: 2 branches
BranchTrueFalseTotal
if (self->recv_len == 0 || self->send_count == 0) return; 1 0 1
if (self->recv_len == 0 || self->send_count == 0) return; 0 1 1
331  
332 /* find headers end */
333 1 hdr_end = find_header_end(self->recv_buf, self->recv_len);
334 1 ▶ if (hdr_end == 0) return; /* need more data */
Branch: 1 branch
BranchTrueFalseTotal
if (hdr_end == 0) return; /* need more data */ 0 1 1
335  
336 /* parse status */
337 1 status = parse_http_status(self->recv_buf, hdr_end);
338  
339 /* parse Content-Length */
340 1 val = find_header(self->recv_buf, hdr_end, "Content-Length", &val_len);
341 1 ▶ if (val) {
Branch: 1 branch
BranchTrueFalseTotal
if (val) { 0 1 1
342 0 content_length = (size_t)strtoul(val, NULL, 10);
343 }
344  
345 /* check Transfer-Encoding for the chunked token; a gzip token in
346 * the same list means the de-chunked body still needs gunzipping. */
347 1 val = find_header(self->recv_buf, hdr_end, "Transfer-Encoding", &val_len);
348 1 ▶ if (val) {
Branch: 1 branch
BranchTrueFalseTotal
if (val) { 1 0 1
349 1 ▶ if (header_has_token(val, val_len, "chunked", 7))
Branch: 1 branch
BranchTrueFalseTotal
if (header_has_token(val, val_len, "chunked", 7)) 1 0 1
350 1 chunked = 1;
351 1 ▶ if (header_has_token(val, val_len, "gzip", 4)
Branch: 1 branch
BranchTrueFalseTotal
if (header_has_token(val, val_len, "gzip", 4) 0 1 1
352 0 ▶ || header_has_token(val, val_len, "x-gzip", 6))
Branch: 1 branch
BranchTrueFalseTotal
|| header_has_token(val, val_len, "x-gzip", 6)) 0 0 0
353 1 is_gzip = 1;
354 }
355  
356 /* check Content-Encoding: gzip */
357 1 val = find_header(self->recv_buf, hdr_end, "Content-Encoding", &val_len);
358 1 ▶ if (val && (header_has_token(val, val_len, "gzip", 4)
Branch: 2 branches
BranchTrueFalseTotal
if (val && (header_has_token(val, val_len, "gzip", 4) 0 1 1
if (val && (header_has_token(val, val_len, "gzip", 4) 0 0 0
359 0 ▶ || header_has_token(val, val_len, "x-gzip", 6))) {
Branch: 1 branch
BranchTrueFalseTotal
|| header_has_token(val, val_len, "x-gzip", 6))) { 0 0 0
360 0 is_gzip = 1;
361 }
362  
363 /* parse X-ClickHouse-Summary: {"read_rows":"N","read_bytes":"N",...}
364 * to populate profile_rows/profile_bytes for HTTP, mirroring the
365 * native protocol's SERVER_PROFILE_INFO packet. */
366 1 val = find_header(self->recv_buf, hdr_end, "X-ClickHouse-Summary", &val_len);
367 1 ▶ if (val && val_len > 12) {
Branch: 2 branches
BranchTrueFalseTotal
if (val && val_len > 12) { 0 1 1
if (val && val_len > 12) { 0 0 0
368 size_t i;
369 0 ▶ for (i = 0; i + 12 < val_len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 12 < val_len; i++) { 0 0 0
370 0 ▶ if (val[i] != '"') continue;
Branch: 1 branch
BranchTrueFalseTotal
if (val[i] != '"') continue; 0 0 0
371 /* val isn't NUL-terminated (it points into recv_buf), so use
372 * parse_uint_within which takes an explicit length cap rather
373 * than scanning until a non-digit/NUL like strtoull would. */
374 0 ▶ if (i + 13 < val_len
Branch: 1 branch
BranchTrueFalseTotal
if (i + 13 < val_len 0 0 0
375 0 ▶ && memcmp(val + i + 1, "read_rows\":\"", 12) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(val + i + 1, "read_rows\":\"", 12) == 0) 0 0 0
376 0 self->profile_rows = parse_uint_within(val + i + 13,
377 0 val_len - (i + 13));
378 0 ▶ else if (i + 14 < val_len
Branch: 1 branch
BranchTrueFalseTotal
else if (i + 14 < val_len 0 0 0
379 0 ▶ && memcmp(val + i + 1, "read_bytes\":\"", 13) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(val + i + 1, "read_bytes\":\"", 13) == 0) 0 0 0
380 0 self->profile_bytes = parse_uint_within(val + i + 14,
381 0 val_len - (i + 14));
382 }
383 }
384  
385 size_t consumed;
386 1 ▶ if (chunked) {
Branch: 1 branch
BranchTrueFalseTotal
if (chunked) { 1 0 1
387 /* decode chunked transfer encoding */
388 1 const char *cp_end = self->recv_buf + self->recv_len;
389  
390 1 ▶ if (self->http_chunk_active) {
Branch: 1 branch
BranchTrueFalseTotal
if (self->http_chunk_active) { 0 1 1
391 /* resume partial decode; offset survives recv_buf realloc */
392 0 decoded = self->http_decoded;
393 0 decoded_len = self->http_decoded_len;
394 0 decoded_cap = self->http_decoded_cap;
395 0 cp = self->recv_buf + self->http_chunk_off;
396 } else {
397 1 cp = self->recv_buf + hdr_end;
398 }
399  
400 {
401 1 int chunked_complete = 0;
402 3 ▶ while (cp < cp_end) {
Branch: 1 branch
BranchTrueFalseTotal
while (cp < cp_end) { 3 0 3
403 3 const char *chunk_start = cp;
404 /* read chunk size */
405 3 const char *nl = cp;
406 unsigned long chunk_size;
407 8 ▶ while (nl < cp_end && *nl != '\r') nl++;
Branch: 2 branches
BranchTrueFalseTotal
while (nl < cp_end && *nl != '\r') nl++; 8 0 8
while (nl < cp_end && *nl != '\r') nl++; 5 3 8
408 /* resume at the header of the chunk not yet copied */
409 3 ▶ if (nl + 2 > cp_end) { cp = chunk_start; goto chunk_need_more; }
Branch: 1 branch
BranchTrueFalseTotal
if (nl + 2 > cp_end) { cp = chunk_start; goto chunk_need_more; } 0 3 3
410  
411 3 chunk_size = strtoul(cp, NULL, 16);
412 3 cp = nl + 2; /* skip \r\n */
413  
414 3 ▶ if (chunk_size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (chunk_size == 0) { 1 2 3
415 /* terminal chunk; skip trailing \r\n */
416 1 ▶ if (cp + 2 > cp_end) { cp = chunk_start; goto chunk_need_more; }
Branch: 1 branch
BranchTrueFalseTotal
if (cp + 2 > cp_end) { cp = chunk_start; goto chunk_need_more; } 0 1 1
417 1 cp += 2;
418 1 chunked_complete = 1;
419 1 break;
420 }
421  
422 2 ▶ if ((size_t)(cp_end - cp) < 2
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)(cp_end - cp) < 2 2 0 2
423 2 ▶ || chunk_size > (size_t)(cp_end - cp) - 2) {
Branch: 1 branch
BranchTrueFalseTotal
|| chunk_size > (size_t)(cp_end - cp) - 2) { 0 2 2
424 0 cp = chunk_start;
425 0 goto chunk_need_more;
426 }
427  
428 /* guard against overflow and unbounded growth —
429 * close connection since remaining chunks would
430 * corrupt the stream for subsequent requests.
431 * Apply both the hard ceiling and the user's
432 * opt-in max_recv_buffer (when set) to the
433 * post-decode body size. */
434 2 size_t cap = CH_MAX_DECOMPRESS_SIZE;
435 2 ▶ if (self->max_recv_buffer > 0
Branch: 1 branch
BranchTrueFalseTotal
if (self->max_recv_buffer > 0 0 2 2
436 0 ▶ && self->max_recv_buffer < cap)
Branch: 1 branch
BranchTrueFalseTotal
&& self->max_recv_buffer < cap) 0 0 0
437 0 cap = self->max_recv_buffer;
438 2 ▶ if (decoded_len + chunk_size < decoded_len
Branch: 1 branch
BranchTrueFalseTotal
if (decoded_len + chunk_size < decoded_len 2 0 2
439 2 ▶ || decoded_len + chunk_size > cap) {
Branch: 1 branch
BranchTrueFalseTotal
|| decoded_len + chunk_size > cap) { 0 2 2
440 0 ▶ if (decoded) Safefree(decoded);
Branch: 1 branch
BranchTrueFalseTotal
if (decoded) Safefree(decoded); 0 0 0
441 0 self->http_decoded = NULL;
442 0 self->http_decoded_len = 0;
443 0 self->http_decoded_cap = 0;
444 0 self->http_chunk_off = 0;
445 0 self->http_chunk_active = 0;
446 0 self->send_count--;
447 {
448 0 int ogen = self->connect_gen;
449 0 ▶ if (teardown_after_deliver(self,
Branch: 1 branch
BranchTrueFalseTotal
if (teardown_after_deliver(self, 0 0 0
450 "chunked response too large",
451 0 "connection closed")) return;
452 0 ▶ if (self->connect_gen == ogen
Branch: 1 branch
BranchTrueFalseTotal
if (self->connect_gen == ogen 0 0 0
453 0 ▶ && self->auto_reconnect && self->host)
Branch: 2 branches
BranchTrueFalseTotal
&& self->auto_reconnect && self->host) 0 0 0
&& self->auto_reconnect && self->host) 0 0 0
454 0 schedule_reconnect(self);
455 }
456 0 return;
457 }
458 2 ▶ if (decoded == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (decoded == NULL) { 1 1 2
459 1 decoded_cap = chunk_size + 256;
460 1 Newx(decoded, decoded_cap, char);
461 1 ▶ } else if (decoded_len + chunk_size > decoded_cap) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (decoded_len + chunk_size > decoded_cap) { 0 1 1
462 0 decoded_cap = (decoded_len + chunk_size) * 2;
463 0 Renew(decoded, decoded_cap, char);
464 }
465 2 Copy(cp, decoded + decoded_len, chunk_size, char);
466 2 decoded_len += chunk_size;
467 2 cp += chunk_size + 2; /* skip chunk data + \r\n */
468 }
469  
470 /* loop exited at cp_end, so all present chunks were copied;
471 * resume at cp — chunk_start would duplicate the last chunk */
472 1 ▶ if (!chunked_complete) goto chunk_need_more;
Branch: 1 branch
BranchTrueFalseTotal
if (!chunked_complete) goto chunk_need_more; 0 1 1
473 }
474  
475 1 body = decoded;
476 1 body_len = decoded_len;
477 1 consumed = cp - self->recv_buf;
478 /* ownership moved to local decoded; drop saved state, no free */
479 1 self->http_decoded = NULL;
480 1 self->http_decoded_len = 0;
481 1 self->http_decoded_cap = 0;
482 1 self->http_chunk_off = 0;
483 1 self->http_chunk_active = 0;
484 } else {
485 /* Content-Length based — bound content_length before any pointer
486 * arithmetic so a malicious huge value cannot overflow
487 * hdr_end + content_length and slip past the need-more check. */
488 0 size_t cl_cap = CH_MAX_DECOMPRESS_SIZE;
489 0 ▶ if (self->max_recv_buffer > 0 && self->max_recv_buffer < cl_cap)
Branch: 2 branches
BranchTrueFalseTotal
if (self->max_recv_buffer > 0 && self->max_recv_buffer < cl_cap) 0 0 0
if (self->max_recv_buffer > 0 && self->max_recv_buffer < cl_cap) 0 0 0
490 0 cl_cap = self->max_recv_buffer;
491 0 ▶ if (content_length > cl_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (content_length > cl_cap) { 0 0 0
492 int ogen;
493 0 self->send_count--;
494 0 ogen = self->connect_gen;
495 0 ▶ if (teardown_after_deliver(self,
Branch: 1 branch
BranchTrueFalseTotal
if (teardown_after_deliver(self, 0 0 0
496 0 "Content-Length exceeds limit", "connection closed")) return;
497 0 ▶ if (self->connect_gen == ogen
Branch: 1 branch
BranchTrueFalseTotal
if (self->connect_gen == ogen 0 0 0
498 0 ▶ && self->auto_reconnect && self->host)
Branch: 2 branches
BranchTrueFalseTotal
&& self->auto_reconnect && self->host) 0 0 0
&& self->auto_reconnect && self->host) 0 0 0
499 0 schedule_reconnect(self);
500 0 return;
501 }
502 0 ▶ if (self->recv_len < hdr_end + content_length) return; /* need more data */
Branch: 1 branch
BranchTrueFalseTotal
if (self->recv_len < hdr_end + content_length) return; /* need more data */ 0 0 0
503 0 body = self->recv_buf + hdr_end;
504 0 body_len = content_length;
505 0 consumed = hdr_end + content_length;
506 }
507  
508 /* deliver response (body, body_len, consumed are set; decoded may be NULL) */
509 1 self->send_count--;
510 1 ▶ if (status == 200) {
Branch: 1 branch
BranchTrueFalseTotal
if (status == 200) { 1 0 1
511 1 char *final_body = (char *)body;
512 1 size_t final_len = body_len;
513  
514 1 ▶ if (is_gzip && body_len > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_gzip && body_len > 0) { 1 0 1
if (is_gzip && body_len > 0) { 1 0 1
515 size_t dec_len;
516 1 char *dec = gzip_decompress(body, body_len, &dec_len);
517 1 ▶ if (!dec) {
Branch: 1 branch
BranchTrueFalseTotal
if (!dec) { 0 1 1
518 0 ▶ if (decoded) Safefree(decoded);
Branch: 1 branch
BranchTrueFalseTotal
if (decoded) Safefree(decoded); 0 0 0
519 0 self->http_decoded = NULL;
520 0 self->http_decoded_len = 0;
521 0 self->http_decoded_cap = 0;
522 0 self->http_chunk_off = 0;
523 0 self->http_chunk_active = 0;
524 0 recv_consume(self, consumed);
525 /* Stop the timer BEFORE delivering: the handler may issue
526 * a chained query (arming a fresh timer) that the old
527 * post-deliver stop would kill. */
528 0 stop_timing(self);
529 0 int destroyed = deliver_error(self, "gzip decompression failed");
530 0 ▶ if (destroyed) return;
Branch: 1 branch
BranchTrueFalseTotal
if (destroyed) return; 0 0 0
531 0 goto done;
532 }
533 /* Apply user's opt-in max_recv_buffer to the gzip-decoded
534 * body too — same trip-wire semantics as the chunked path
535 * and the on_readable raw-recv check. Tear the connection
536 * down on overflow so subsequent queries can't slip past
537 * the cap on the same socket (matches the chunked path's
538 * behaviour and the POD contract). */
539 1 ▶ if (self->max_recv_buffer > 0
Branch: 1 branch
BranchTrueFalseTotal
if (self->max_recv_buffer > 0 0 1 1
540 0 ▶ && dec_len > self->max_recv_buffer) {
Branch: 1 branch
BranchTrueFalseTotal
&& dec_len > self->max_recv_buffer) { 0 0 0
541 0 Safefree(dec);
542 0 ▶ if (decoded) Safefree(decoded);
Branch: 1 branch
BranchTrueFalseTotal
if (decoded) Safefree(decoded); 0 0 0
543 {
544 0 int ogen = self->connect_gen;
545 0 ▶ if (teardown_after_deliver(self,
Branch: 1 branch
BranchTrueFalseTotal
if (teardown_after_deliver(self, 0 0 0
546 "gzip body exceeds max_recv_buffer",
547 0 "connection closed")) return;
548 0 ▶ if (self->connect_gen == ogen
Branch: 1 branch
BranchTrueFalseTotal
if (self->connect_gen == ogen 0 0 0
549 0 ▶ && self->auto_reconnect && self->host)
Branch: 2 branches
BranchTrueFalseTotal
&& self->auto_reconnect && self->host) 0 0 0
&& self->auto_reconnect && self->host) 0 0 0
550 0 schedule_reconnect(self);
551 }
552 0 return;
553 }
554 1 final_body = dec;
555 1 final_len = dec_len;
556 }
557  
558 {
559 1 int is_raw = peek_cb_raw(self);
560 int destroyed;
561 /* Stop the timer BEFORE delivering (native paths do the
562 * same): a chained query from the handler arms a fresh
563 * timer that a post-deliver stop would kill. */
564 1 stop_timing(self);
565 1 ▶ if (is_raw) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_raw) { 0 1 1
566 /* raw mode — deliver body as scalar, skip TSV parsing */
567 0 destroyed = deliver_raw_body(self, final_body, final_len);
568 } else {
569 1 AV *rows = NULL;
570 1 ▶ if (final_len > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (final_len > 0) 1 0 1
571 1 rows = parse_tab_separated(final_body, final_len);
572 1 destroyed = deliver_rows(self, rows);
573 }
574 1 ▶ if (final_body != body) Safefree(final_body);
Branch: 1 branch
BranchTrueFalseTotal
if (final_body != body) Safefree(final_body); 1 0 1
575 1 ▶ if (decoded) Safefree(decoded);
Branch: 1 branch
BranchTrueFalseTotal
if (decoded) Safefree(decoded); 1 0 1
576 /* deliver_* may have freed self (DESTROY when the handler
577 * dropped the last ref) — never touch self afterwards. */
578 1 ▶ if (destroyed) return;
Branch: 1 branch
BranchTrueFalseTotal
if (destroyed) return; 0 1 1
579 1 recv_consume(self, consumed);
580 }
581 } else {
582 /* error */
583 0 char *errmsg = format_http_error(self, status, body, body_len, is_gzip);
584 0 ▶ if (decoded) Safefree(decoded);
Branch: 1 branch
BranchTrueFalseTotal
if (decoded) Safefree(decoded); 0 0 0
585 0 recv_consume(self, consumed);
586 0 stop_timing(self);
587 0 int destroyed = deliver_error(self, errmsg);
588 0 Safefree(errmsg);
589 0 ▶ if (destroyed) return;
Branch: 1 branch
BranchTrueFalseTotal
if (destroyed) return; 0 0 0
590 }
591  
592 1 ▶ if (self->magic != EV_CH_MAGIC) return;
Branch: 1 branch
BranchTrueFalseTotal
if (self->magic != EV_CH_MAGIC) return; 0 1 1
593  
594 1 done:
595 /* The query timer was stopped before delivering (see above) so a
596 * chained query's fresh timer survives; just pipeline onwards. */
597 1 pipeline_advance(self);
598 1 return;
599  
600 0 chunk_need_more:
601 /* incomplete chunked body — persist the decode across read events;
602 * cp carries the resume offset, so recv_buf may be realloc'd */
603 0 self->http_decoded = decoded;
604 0 self->http_decoded_len = decoded_len;
605 0 self->http_decoded_cap = decoded_cap;
606 0 self->http_chunk_off = (size_t)(cp - self->recv_buf);
607 0 self->http_chunk_active = 1;
608 0 return;
609 }
610  
« ClickHouse.xs ↑ Summary xs/io.c »