Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for PUT requests using aeron_http_util. #1561

Merged
merged 3 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 39 additions & 10 deletions aeron-client/src/main/c/util/aeron_http_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,24 @@ int aeron_http_parse_response(aeron_http_response_t *response)
return 0;
}

#define AERON_HTTP_UTIL_EMPTY ""

static char aeron_http_request_format[] =
"GET %s HTTP/1.1\r\n"
"%s %s HTTP/1.1\r\n"
"Host: %s\r\n"
"Accept: text/plain, text/*, */*\r\n"
"Accept-Encoding: identity\r\n"
"%s"
"%s"
"\r\n";

int aeron_http_retrieve(aeron_http_response_t **response, const char *url, int64_t timeout_ns)
static int aeron_http_send_request(
aeron_http_response_t **response,
const char *url,
int64_t timeout_ns,
const char *method,
const char *headers,
const char *body)
{
aeron_http_parsed_url_t parsed_url;
aeron_socket_t sock;
Expand Down Expand Up @@ -283,7 +293,9 @@ int aeron_http_retrieve(aeron_http_response_t **response, const char *url, int64

char request[sizeof(parsed_url.path_and_query) + sizeof(aeron_http_request_format) + 1];
int length = snprintf(
request, sizeof(request) - 1, aeron_http_request_format, parsed_url.path_and_query, parsed_url.host_and_port);
request, sizeof(request) - 1,
aeron_http_request_format,
method, parsed_url.path_and_query, parsed_url.host_and_port, headers, body);
ssize_t sent_length = 0;

if (length < 0 || (sent_length = send(sock, request, length, 0)) < length)
Expand Down Expand Up @@ -370,15 +382,32 @@ int aeron_http_retrieve(aeron_http_response_t **response, const char *url, int64
*response = _response;
return 0;

error:
if (-1 != sock)
{
aeron_close_socket(sock);
}
error:
if (-1 != sock)
{
aeron_close_socket(sock);
}

aeron_http_response_delete(_response);
aeron_http_response_delete(_response);

return -1;
return -1;
}

int aeron_http_retrieve(aeron_http_response_t **response, const char *url, int64_t timeout_ns)
{
return aeron_http_send_request(response, url, timeout_ns, "GET", AERON_HTTP_UTIL_EMPTY, AERON_HTTP_UTIL_EMPTY);
}

int aeron_http_get(
aeron_http_response_t **response, const char *url, int64_t timeout_ns, const char *headers)
{
return aeron_http_send_request(response, url, timeout_ns, "GET", headers, AERON_HTTP_UTIL_EMPTY);
}

int aeron_http_put(
aeron_http_response_t **response, const char *url, int64_t timeout_ns, const char *headers, const char *body)
{
return aeron_http_send_request(response, url, timeout_ns, "PUT", headers, body);
}

int aeron_http_header_get(aeron_http_response_t *response, const char *header_name, char *line, size_t max_length)
Expand Down
6 changes: 6 additions & 0 deletions aeron-client/src/main/c/util/aeron_http_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ inline void aeron_http_response_delete(aeron_http_response_t *response)

int aeron_http_retrieve(aeron_http_response_t **response, const char *url, int64_t timeout_ns);

int aeron_http_get(
aeron_http_response_t **response, const char *url, int64_t timeout_ns, const char *header);

int aeron_http_put(
aeron_http_response_t **response, const char *url, int64_t timeout_ns, const char *header, const char *body);

int aeron_http_header_get(aeron_http_response_t *response, const char *header_name, char *line, size_t max_length);

#endif //AERON_HTTP_UTIL_H