Slight Refactoring
This commit is contained in:
@@ -137,16 +137,8 @@ struct http_header parse_http_header(char *header_str) {
|
||||
return header;
|
||||
}
|
||||
|
||||
// Construct HTTP Message from String
|
||||
struct req_http_message create_req_http_message_from_string(char *message_str) {
|
||||
struct req_http_message message;
|
||||
// Parse Start Line
|
||||
int start_line_len = REQ_HTTP_START_LINE_METHOD_LENGTH + REQ_HTTP_START_LINE_PATH_LENGTH + REQ_HTTP_START_LINE_VERSION_LENGTH + REQ_HTTP_START_LINE_EXTRA_CHARS;
|
||||
char start_line_unparsed[start_line_len];
|
||||
memcpy(start_line_unparsed, message_str, start_line_len);
|
||||
message.start_line = parse_req_http_start_line(start_line_unparsed);
|
||||
// Parse Headers
|
||||
|
||||
// Parse HTTP Headers from Message String
|
||||
int parse_http_headers(char *message_str, struct http_header *headers) {
|
||||
// Find start of headers (after first \r\n)
|
||||
char *headers_start = strstr(message_str, HTTP_HEADER_LINE_ENDING) + 2;
|
||||
char *headers_end = strstr(headers_start, HTTP_BODY_SEPARATOR);
|
||||
@@ -163,28 +155,52 @@ struct req_http_message create_req_http_message_from_string(char *message_str) {
|
||||
memcpy(current_header_unparsed, current_line, line_end - current_line);
|
||||
current_header_unparsed[line_end - current_line] = '\0';
|
||||
|
||||
message.headers[header_count] = parse_http_header(current_header_unparsed);
|
||||
headers[header_count] = parse_http_header(current_header_unparsed);
|
||||
if (DEBUG) {
|
||||
printf(" Parsed HTTP Header %d:\n", header_count + 1);
|
||||
printf(" Field Name: %s\n", message.headers[header_count].field_name);
|
||||
printf(" Field Value: %s\n", message.headers[header_count].field_value);
|
||||
printf(" Field Name: %s\n", headers[header_count].field_name);
|
||||
printf(" Field Value: %s\n", headers[header_count].field_value);
|
||||
}
|
||||
header_count++;
|
||||
|
||||
current_line = line_end + strlen(HTTP_HEADER_LINE_ENDING);
|
||||
}
|
||||
message.header_count = header_count;
|
||||
|
||||
return header_count;
|
||||
}
|
||||
|
||||
// Parse HTTP Body from Message String
|
||||
void parse_http_body(char *message_str, char *body) {
|
||||
// Find start of headers (after first \r\n)
|
||||
char *headers_start = strstr(message_str, HTTP_HEADER_LINE_ENDING) + 2;
|
||||
char *headers_end = strstr(headers_start, HTTP_BODY_SEPARATOR);
|
||||
|
||||
// Parse body
|
||||
char *body_start = headers_end + strlen(HTTP_BODY_SEPARATOR);
|
||||
int body_len = strlen(body_start);
|
||||
memcpy(message.body, body_start, body_len);
|
||||
message.body[body_len] = '\0';
|
||||
memcpy(body, body_start, body_len);
|
||||
body[body_len] = '\0';
|
||||
if (DEBUG) {
|
||||
printf(" Parsed HTTP Body:\n");
|
||||
printf(" Body Length: %d\n", body_len);
|
||||
printf(" Body Content: %s\n", message.body);
|
||||
printf(" Body Content: %s\n", body);
|
||||
}
|
||||
}
|
||||
|
||||
// Construct HTTP Message from String
|
||||
struct req_http_message create_req_http_message_from_string(char *message_str) {
|
||||
struct req_http_message message;
|
||||
// Parse Start Line
|
||||
int start_line_len = REQ_HTTP_START_LINE_METHOD_LENGTH + REQ_HTTP_START_LINE_PATH_LENGTH + REQ_HTTP_START_LINE_VERSION_LENGTH + REQ_HTTP_START_LINE_EXTRA_CHARS;
|
||||
char start_line_unparsed[start_line_len];
|
||||
memcpy(start_line_unparsed, message_str, start_line_len);
|
||||
message.start_line = parse_req_http_start_line(start_line_unparsed);
|
||||
|
||||
// Parse Headers
|
||||
message.header_count = parse_http_headers(message_str, message.headers);
|
||||
|
||||
// Parse Body
|
||||
parse_http_body(message_str, message.body);
|
||||
|
||||
return message;
|
||||
}
|
||||
@@ -329,46 +345,10 @@ struct res_http_message create_res_http_message_from_string(char *message_str) {
|
||||
message.start_line = parse_res_http_start_line(start_line_unparsed);
|
||||
|
||||
// Parse Headers
|
||||
|
||||
// Find start of headers (after first \r\n)
|
||||
char *headers_start = strstr(message_str, HTTP_HEADER_LINE_ENDING) + 2;
|
||||
char *headers_end = strstr(headers_start, HTTP_BODY_SEPARATOR);
|
||||
|
||||
// Parse each header line
|
||||
char *current_line = headers_start;
|
||||
int header_count = 0;
|
||||
while (current_line < headers_end && header_count < HTTP_MESSAGE_MAX_HEADERS) {
|
||||
|
||||
char *line_end = strstr(current_line, HTTP_HEADER_LINE_ENDING);
|
||||
if (line_end == NULL || line_end > headers_end) break;
|
||||
|
||||
char current_header_unparsed[HTTP_HEADER_FIELD_NAME_LENGTH + HTTP_HEADER_FIELD_VALUE_LENGTH + 4];
|
||||
memcpy(current_header_unparsed, current_line, line_end - current_line);
|
||||
current_header_unparsed[line_end - current_line] = '\0';
|
||||
|
||||
message.headers[header_count] = parse_http_header(current_header_unparsed);
|
||||
if (DEBUG) {
|
||||
printf(" Parsed HTTP Header %d:\n", header_count + 1);
|
||||
printf(" Field Name: %s\n", message.headers[header_count].field_name);
|
||||
printf(" Field Value: %s\n", message.headers[header_count].field_value);
|
||||
}
|
||||
header_count++;
|
||||
|
||||
current_line = line_end + strlen(HTTP_HEADER_LINE_ENDING);
|
||||
}
|
||||
message.header_count = header_count;
|
||||
|
||||
// Parse body
|
||||
char *body_start = headers_end + strlen(HTTP_BODY_SEPARATOR);
|
||||
int body_len = strlen(body_start);
|
||||
memcpy(message.body, body_start, body_len);
|
||||
message.body[body_len] = '\0';
|
||||
if (DEBUG) {
|
||||
printf(" Parsed HTTP Body:\n");
|
||||
printf(" Body Length: %d\n", body_len);
|
||||
printf(" Body Content: %s\n", message.body);
|
||||
}
|
||||
message.header_count = parse_http_headers(message_str, message.headers);
|
||||
|
||||
// Parse Body
|
||||
parse_http_body(message_str, message.body);
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user