MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

add method and body support to fetch request

+21 -12
+21 -12
src/modules/fetch.c
··· 35 35 int failed; 36 36 char *error_msg; 37 37 jsval_t headers_obj; 38 + char *method; 39 + char *body; 40 + size_t body_len; 38 41 } fetch_request_t; 39 42 40 43 static uv_loop_t *fetch_loop = NULL; ··· 45 48 46 49 if (req->response_buffer.data) free(req->response_buffer.data); 47 50 if (req->error_msg) free(req->error_msg); 51 + if (req->method) free(req->method); 52 + if (req->body) free(req->body); 48 53 49 54 free(req); 50 55 } ··· 267 272 268 273 req->http_client.data = req; 269 274 270 - char *method = "GET"; 271 - char *body = NULL; 272 - size_t body_len = 0; 275 + req->method = NULL; 276 + req->body = NULL; 277 + req->body_len = 0; 273 278 274 - int options_type = vtype(options_val); 275 279 if (is_special_object(options_val)) { 276 280 jsval_t method_val = js_get(js, options_val, "method"); 277 281 if (vtype(method_val) == T_STR) { 278 - char *method_str = js_getstr(js, method_val, NULL); 279 - if (method_str) method = method_str; 282 + char *str = js_getstr(js, method_val, NULL); 283 + if (str) req->method = strdup(str); 280 284 } 281 285 282 286 jsval_t body_val = js_get(js, options_val, "body"); 283 287 if (vtype(body_val) == T_STR) { 284 - body = js_getstr(js, body_val, NULL); 285 - if (body) body_len = strlen(body); 288 + size_t len; 289 + char *str = js_getstr(js, body_val, &len); 290 + if (str) { 291 + req->body = memcpy(malloc(len), str, len); 292 + req->body_len = len; 293 + } 286 294 } 287 295 } 288 296 289 - req->http_req = tlsuv_http_req(&req->http_client, method, path, resp_cb, req); 297 + if (!req->method) req->method = strdup("GET"); 298 + req->http_req = tlsuv_http_req(&req->http_client, req->method, path, resp_cb, req); 290 299 291 300 if (!req->http_req) { 292 301 jsval_t err = js_mkstr(js, "Failed to create HTTP request", 30); ··· 303 312 snprintf(user_agent, sizeof(user_agent), "ant/%s", ANT_VERSION); 304 313 tlsuv_http_req_header(req->http_req, "User-Agent", user_agent); 305 314 306 - if ((TYPE_FLAG(options_type) & T_SPECIAL_OBJECT_MASK) != 0) { 315 + if (is_special_object(options_val)) { 307 316 jsval_t headers_val = js_get(js, options_val, "headers"); 308 317 if (is_special_object(headers_val)) { 309 318 ant_iter_t iter = js_prop_iter_begin(js, headers_val); ··· 322 331 } 323 332 } 324 333 325 - if (body && body_len > 0) { 326 - tlsuv_http_req_data(req->http_req, body, body_len, body_cb); 334 + if (req->body && req->body_len > 0) { 335 + tlsuv_http_req_data(req->http_req, req->body, req->body_len, body_cb); 327 336 } 328 337 329 338 return js_mkundef();