rework reinitialization of http_response

This commit is contained in:
F. Duncanh
2024-07-10 16:55:09 -04:00
parent 53ac57dc42
commit 75d64e6b1e
4 changed files with 35 additions and 26 deletions

View File

@@ -51,10 +51,29 @@ http_response_add_data(http_response_t *response, const char *data, int datalen)
response->data_length += datalen;
}
http_response_t *
http_response_init(const char *protocol, int code, const char *message)
http_response_create()
{
http_response_t *response;
http_response_t *response = (http_response_t *) calloc(1, sizeof(http_response_t));
if (!response) {
return NULL;
}
/* Allocate response data */
response->data_size = 1024;
response->data = (char *) malloc(response->data_size);
if (!response->data) {
free(response);
return NULL;
}
return response;
}
void
http_response_init(http_response_t *response, const char *protocol, int code, const char *message)
{
assert(response);
response->data_length = 0; /* can be used to reinitialize a previously-initialized response */
char codestr[4];
assert(code >= 100 && code < 1000);
@@ -63,19 +82,6 @@ http_response_init(const char *protocol, int code, const char *message)
memset(codestr, 0, sizeof(codestr));
snprintf(codestr, sizeof(codestr), "%u", code);
response = calloc(1, sizeof(http_response_t));
if (!response) {
return NULL;
}
/* Allocate response data */
response->data_size = 1024;
response->data = malloc(response->data_size);
if (!response->data) {
free(response);
return NULL;
}
/* Add first line of response to the data array */
http_response_add_data(response, protocol, strlen(protocol));
http_response_add_data(response, " ", 1);
@@ -83,8 +89,6 @@ http_response_init(const char *protocol, int code, const char *message)
http_response_add_data(response, " ", 1);
http_response_add_data(response, message, strlen(message));
http_response_add_data(response, "\r\n", 2);
return response;
}
void