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 fallback headers if none exist

+44 -1
+44 -1
src/modules/response.c
··· 274 274 return extract_string_body(js, body_val, out_data, out_size, out_type, err_out); 275 275 } 276 276 277 + static bool response_content_type_has_charset(const char *value) { 278 + const char *p = NULL; 279 + 280 + if (!value) return false; 281 + p = strchr(value, ';'); 282 + 283 + while (p) { 284 + p++; 285 + while (*p == ' ' || *p == '\t') p++; 286 + if (strncasecmp(p, "charset", 7) == 0) { 287 + p += 7; 288 + while (*p == ' ' || *p == '\t') p++; 289 + if (*p == '=') return true; 290 + } 291 + p = strchr(p, ';'); 292 + } 293 + 294 + return false; 295 + } 296 + 297 + static void response_maybe_normalize_text_content_type( 298 + ant_t *js, ant_value_t headers, ant_value_t current_type, const char *body_type 299 + ) { 300 + const char *current = NULL; 301 + 302 + if (!body_type || !headers_is_headers(headers)) return; 303 + if (vtype(current_type) != T_STR) return; 304 + 305 + current = js_getstr(js, current_type, NULL); 306 + if (!current) return; 307 + if (strncasecmp(current, "text/", 5) != 0) return; 308 + if (response_content_type_has_charset(current)) return; 309 + if (!response_content_type_has_charset(body_type)) return; 310 + 311 + headers_set_literal(js, headers, "content-type", body_type); 312 + } 313 + 277 314 enum { 278 315 BODY_TEXT = 0, 279 316 BODY_JSON, ··· 643 680 644 681 if (resp->body_is_stream) js_set_slot_wb(js, resp_obj, SLOT_RESPONSE_BODY_STREAM, body_stream); 645 682 current_type = headers_get_value(js, headers, "content-type"); 683 + 646 684 if (body_type && !is_err(current_type) && vtype(current_type) == T_NULL) 647 685 headers_append_if_missing(headers, "content-type", body_type); 686 + else if (!is_err(current_type)) 687 + response_maybe_normalize_text_content_type(js, headers, current_type, body_type); 648 688 649 689 return js_mkundef(); 650 690 } ··· 1036 1076 1037 1077 headers_set_guard(headers, guard); 1038 1078 headers_apply_guard(headers); 1079 + 1039 1080 ant_value_t current_type = headers_get_value(js, headers, "content-type"); 1040 1081 if (body_type && !is_err(current_type) && vtype(current_type) == T_NULL) { 1041 1082 headers_append_if_missing(headers, "content-type", body_type); 1042 - } 1083 + } else if (!is_err(current_type)) response_maybe_normalize_text_content_type( 1084 + js, headers, current_type, body_type 1085 + ); 1043 1086 js_set_slot_wb(js, obj, SLOT_RESPONSE_HEADERS, headers); 1044 1087 return obj; 1045 1088 }