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

View File

@@ -10,6 +10,9 @@
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*==============================================================
* modified fduncanh 2024
*/
#ifndef HTTP_RESPONSE_H
@@ -17,7 +20,8 @@
typedef struct http_response_s http_response_t;
http_response_t *http_response_init(const char *protocol, int code, const char *message);
http_response_t *http_response_create();
void http_response_init(http_response_t *response, const char *protocol, int code, const char *message);
void http_response_add_header(http_response_t *response, const char *name, const char *value);
void http_response_finish(http_response_t *response, const char *data, int datalen);

View File

@@ -174,8 +174,9 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response) {
if (httpd_count_connection_type(conn->raop->httpd, CONNECTION_TYPE_RAOP)) {
char ipaddr[40];
utils_ipaddress_to_string(conn->remotelen, conn->remote, conn->zone_id, ipaddr, (int) (sizeof(ipaddr)));
logger_log(conn->raop->logger, LOGGER_WARNING, "rejecting new connection request from %s", ipaddr);
*response = http_response_init(protocol, 409, "Conflict: Server is connected to another client");
logger_log(conn->raop->logger, LOGGER_WARNING, "rejecting new connection request from %s", ipaddr);
*response = http_response_create();
http_response_init(*response, protocol, 409, "Conflict: Server is connected to another client");
goto finish;
}
httpd_set_connection_type(conn->raop->httpd, ptr, CONNECTION_TYPE_RAOP);
@@ -236,8 +237,8 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response) {
}
}
*response = http_response_init(protocol, 200, "OK");
*response = http_response_create();
http_response_init(*response, protocol, 200, "OK");
//http_response_add_header(*response, "Apple-Jack-Status", "connected; type=analog");

View File

@@ -354,8 +354,7 @@ raop_handler_pairsetup_pin(raop_conn_t *conn,
return;
}
authentication_failed:;
http_response_destroy(response);
response = http_response_init("RTSP/1.0", 470, "Client Authentication Failure");
http_response_init(response, "RTSP/1.0", 470, "Client Authentication Failure");
}
static void
@@ -748,12 +747,13 @@ raop_handler_setup(raop_conn_t *conn,
conn->raop_rtp_mirror = raop_rtp_mirror_init(conn->raop->logger, &conn->raop->callbacks,
conn->raop_ntp, remote, conn->remotelen, aeskey);
plist_t res_event_port_node = plist_new_uint(conn->raop->port);
// plist_t res_event_port_node = plist_new_uint(conn->raop->port);
plist_t res_event_port_node = plist_new_uint(0);
plist_t res_timing_port_node = plist_new_uint(timing_lport);
plist_dict_set_item(res_root_node, "timingPort", res_timing_port_node);
plist_dict_set_item(res_root_node, "eventPort", res_event_port_node);
logger_log(conn->raop->logger, LOGGER_DEBUG, "eport = %d, tport = %d", conn->raop->port, timing_lport);
logger_log(conn->raop->logger, LOGGER_DEBUG, "eport = %d, tport = %d", 0, timing_lport);
}
// Process stream setup requests