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.

extract response body setting logic

+16 -47
+16 -47
src/modules/server.c
··· 74 74 struct response_ctx_s *next; 75 75 } response_ctx_t; 76 76 77 + static void res_set_body(response_ctx_t *ctx, const char *src, size_t len) { 78 + char *copy = malloc(len + 1); 79 + if (copy) { 80 + memcpy(copy, src, len); 81 + copy[len] = '\0'; 82 + } 83 + ctx->body = copy; 84 + ctx->body_len = len; 85 + ctx->should_free_body = 1; 86 + } 87 + 77 88 typedef struct http_server_s { 78 89 struct js *js; 79 90 jsval_t handler; ··· 440 451 if (vtype(args[0]) == T_STR) { 441 452 size_t len; 442 453 const char *src = js_getstr(js, args[0], &len); 443 - char *copy = malloc(len + 1); 444 - if (copy) { 445 - memcpy(copy, src, len); 446 - copy[len] = '\0'; 447 - } 448 - ctx->body = copy; 449 - ctx->body_len = len; 450 - ctx->should_free_body = 1; 454 + res_set_body(ctx, src, len); 451 455 } 452 456 453 457 if (nargs >= 2 && vtype(args[1]) == T_NUM) { ··· 476 480 if (vtype(args[0]) == T_STR) { 477 481 size_t len; 478 482 const char *src = js_getstr(js, args[0], &len); 479 - char *copy = malloc(len + 1); 480 - if (copy) { 481 - memcpy(copy, src, len); 482 - copy[len] = '\0'; 483 - } 484 - ctx->body = copy; 485 - ctx->body_len = len; 486 - ctx->should_free_body = 1; 483 + res_set_body(ctx, src, len); 487 484 } 488 485 489 486 if (nargs >= 2 && vtype(args[1]) == T_NUM) { ··· 512 509 if (vtype(result) == T_STR) { 513 510 size_t len; 514 511 const char *src = js_getstr(js, result, &len); 515 - char *copy = malloc(len + 1); 516 - if (copy) { 517 - memcpy(copy, src, len); 518 - copy[len] = '\0'; 519 - } 520 - ctx->body = copy; 521 - ctx->body_len = len; 522 - ctx->should_free_body = 1; 512 + res_set_body(ctx, src, len); 523 513 } else if (vtype(result) == T_ERR) { 524 514 const char *json_str = js_str(js, args[0]); 525 - if (json_str) { 526 - size_t len = strlen(json_str); 527 - char *copy = malloc(len + 1); 528 - if (copy) { 529 - memcpy(copy, json_str, len); 530 - copy[len] = '\0'; 531 - } 532 - ctx->body = copy; 533 - ctx->body_len = len; 534 - ctx->should_free_body = 1; 535 - } 515 + if (json_str) res_set_body(ctx, json_str, strlen(json_str)); 536 516 } 537 517 538 518 if (nargs >= 2 && vtype(args[1]) == T_NUM) { ··· 792 772 if (vtype(result) == T_ERR) { 793 773 const char *error_msg = js_str(server->js, result); 794 774 fprintf(stderr, "Handler error: %s\n", error_msg); 795 - 796 775 char *clean_error = strip_ansi(error_msg); 797 776 if (clean_error) { 798 777 res_ctx->body = clean_error; 799 778 res_ctx->body_len = strlen(clean_error); 800 779 res_ctx->should_free_body = 1; 801 - } else { 802 - size_t err_len = strlen(error_msg); 803 - char *err_copy = malloc(err_len + 1); 804 - if (err_copy) { 805 - memcpy(err_copy, error_msg, err_len); 806 - err_copy[err_len] = '\0'; 807 - } 808 - res_ctx->body = err_copy; 809 - res_ctx->body_len = err_len; 810 - res_ctx->should_free_body = 1; 811 - } 780 + } else res_set_body(res_ctx, error_msg, strlen(error_msg)); 812 781 res_ctx->status = 500; 813 782 res_ctx->content_type = "text/plain"; 814 783 res_ctx->sent = 1;