hw_string* create_response_buffer(hw_http_response* response) { http_response* resp = (http_response*)response; hw_string* response_string = malloc(sizeof(hw_string)); hw_string* cached_entry = get_cached_request(resp->status_code.value); hw_string content_length; int i = 0; response_string->value = calloc(1024, 1); response_string->length = 0; append_string(response_string, cached_entry); for (i=0; i< resp->number_of_headers; i++) { http_header header = resp->headers[i]; append_string(response_string, &header.name); APPENDSTRING(response_string, ": "); append_string(response_string, &header.value); APPENDSTRING(response_string, CRLF); } /* Add the body */ APPENDSTRING(response_string, "Content-Length: "); content_length.value = itoa(resp->body.length + 3, 10); content_length.length = strlen(content_length.value); append_string(response_string, &content_length); APPENDSTRING(response_string, CRLF CRLF); append_string(response_string, &resp->body); APPENDSTRING(response_string, CRLF); return response_string; }
hw_string* create_response_buffer(hw_http_response* response) { http_response* resp = (http_response*)response; hw_string* response_string = malloc(sizeof(hw_string)); hw_string* cached_entry = get_cached_request(resp->status_code.value); hw_string content_length; int i = 0; int length_plus = 3; const char *text_lead = "text/html"; response_string->value = calloc(1024 * 1024, 1); response_string->length = 0; append_string(response_string, cached_entry); for (i=0; i< resp->number_of_headers; i++) { http_header header = resp->headers[i]; append_string(response_string, &header.name); APPENDSTRING(response_string, ": "); append_string(response_string, &header.value); APPENDSTRING(response_string, CRLF); } //Check the plus length by checking header<content-type> for(i=0; i< resp->number_of_headers; ++i) { http_header header = resp->headers[i]; if ( ! strcmp(header.name.value, "Content-Type") ) { //~ A text based page.(content) //if ( strncmp(header.value.value, text_lead, strlen(text_lead))) if (strcmp(header.value.value, text_lead)) { // Not a text-lead length_plus = 0; } break; } } /* Add the body */ APPENDSTRING(response_string, "Content-Length: "); string_from_int(&content_length, resp->body.length + length_plus, 10); append_string(response_string, &content_length); APPENDSTRING(response_string, CRLF CRLF); if (resp->body.length > 0) { append_string(response_string, &resp->body); } APPENDSTRING(response_string, CRLF); return response_string; }
hw_string* create_response_buffer(hw_http_response* response) { http_response* resp = (http_response*)response; hw_string* response_string = malloc(sizeof(hw_string)); hw_string* cached_entry = get_cached_request(resp->status_code.value); hw_string content_length; int i = 0; char length_header[] = "Content-Length: "; int line_sep_size = sizeof(CRLF); int header_buffer_incr = 512; int body_size = resp->body.length + line_sep_size; int header_size_remaining = header_buffer_incr; int response_size = header_size_remaining + sizeof(length_header) + num_chars(resp->body.length) + 2 * line_sep_size + body_size + line_sep_size; response_string->value = malloc(response_size); response_string->length = 0; append_string(response_string, cached_entry); for (i=0; i< resp->number_of_headers; i++) { http_header header = resp->headers[i]; header_size_remaining -= header.name.length + 2 + header.value.length + line_sep_size; if (header_size_remaining < 0) { header_size_remaining += header_buffer_incr * ((-header_size_remaining/header_buffer_incr) + 1); response_size += header_size_remaining; response_string->value = realloc(response_string->value, response_size); } append_string(response_string, &header.name); APPENDSTRING(response_string, ": "); append_string(response_string, &header.value); APPENDSTRING(response_string, CRLF); } /* Add the body */ APPENDSTRING(response_string, length_header); string_from_int(&content_length, body_size, 10); append_string(response_string, &content_length); APPENDSTRING(response_string, CRLF CRLF); if (resp->body.length > 0) { append_string(response_string, &resp->body); } APPENDSTRING(response_string, CRLF); response_string->value[response_string->length] = '\0'; return response_string; }
hw_string* create_response_file_header_buffer(hw_http_response* response, const char *file) { http_response* resp = (http_response*)response; hw_string* response_string = malloc(sizeof(hw_string)); hw_string* cached_entry = get_cached_request(resp->status_code.value); hw_string content_length; int file_length = get_file_length(file); int i = 0; response_string->value = calloc(1024 * 1024, 1); response_string->length = 0; append_string(response_string, cached_entry); for (i=0; i< resp->number_of_headers; i++) { http_header header = resp->headers[i]; append_string(response_string, &header.name); APPENDSTRING(response_string, ": "); append_string(response_string, &header.value); APPENDSTRING(response_string, CRLF); } /* Add the body */ APPENDSTRING(response_string, "Content-Length: "); //string_from_int(&content_length, file_length, 10); string_from_int_10base(&content_length, file_length); append_string(response_string, &content_length); APPENDSTRING(response_string, CRLF CRLF); //if (resp->body.length > 0) //{ // append_string(response_string, &resp->body); //} //APPENDSTRING(response_string, CRLF); return response_string; }