Skip to content

Commit

Permalink
Problem: zhttp_client does not support authentication
Browse files Browse the repository at this point in the history
Solution: Add functions to zhttp_request to set username and password, and
send these to the client.
  • Loading branch information
stephenprocter committed Aug 6, 2024
1 parent a166bb8 commit 62a5e14
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 7 deletions.
10 changes: 10 additions & 0 deletions api/zhttp_request.api
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@
Set the content to NULL
</method>

<method name = "set username">
Set the request username
<argument name = "username" type = "string" />
</method>

<method name = "set password">
Set the request password
<argument name = "password" type = "string" />
</method>

<method name = "match">
Match the path of the request.
Support wildcards with '%s' symbol inside the match string.
Expand Down
10 changes: 10 additions & 0 deletions include/zhttp_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ CZMQ_EXPORT void
CZMQ_EXPORT void
zhttp_request_reset_content (zhttp_request_t *self);

// *** Draft method, for development use, may change without warning ***
// Set the request username
CZMQ_EXPORT void
zhttp_request_set_username (zhttp_request_t *self, const char *username);

// *** Draft method, for development use, may change without warning ***
// Set the request password
CZMQ_EXPORT void
zhttp_request_set_password (zhttp_request_t *self, const char *password);

// *** Draft method, for development use, may change without warning ***
// Match the path of the request.
// Support wildcards with '%s' symbol inside the match string.
Expand Down
14 changes: 11 additions & 3 deletions src/zhttp_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,12 @@ static void zhttp_client_actor (zsock_t *pipe, void *args) {
char *url;
zhash_t *headers;
byte free_content;
char* content;
char *content;
char *username;
char *password;

int rc = zsock_brecv (pipe, "4ppSp1p", &timeout, &arg, &arg2,
&url, &headers, &free_content, &content);
int rc = zsock_brecv (pipe, "4ppSp1pss", &timeout, &arg, &arg2,
&url, &headers, &free_content, &content, &username, &password);
assert (rc == 0);

struct curl_slist *curl_headers = zhash_to_slist (headers);
Expand Down Expand Up @@ -216,6 +218,12 @@ static void zhttp_client_actor (zsock_t *pipe, void *args) {
curl_easy_setopt (curl, CURLOPT_HEADERDATA, request);
curl_easy_setopt (curl, CURLOPT_PRIVATE, request);

if (*username)
curl_easy_setopt (curl, CURLOPT_USERNAME, username);

if (*password)
curl_easy_setopt (curl, CURLOPT_PASSWORD, password);

if (streq (command, "POST") || streq (command, "PUT") ||
streq (command, "PATCH")) {
curl_easy_setopt (curl, CURLOPT_POSTFIELDS, content);
Expand Down
33 changes: 29 additions & 4 deletions src/zhttp_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ struct _zhttp_request_t {
char *url;
char method[256];
zhash_t *headers;
char* content;
char *content;
bool free_content;
char *username;
char *password;
};


Expand All @@ -46,6 +48,8 @@ zhttp_request_new (void)
strcpy (self->method, "GET");
self->content = NULL;
self->free_content = false;
self->username = NULL;
self->password = NULL;

return self;
}
Expand All @@ -70,6 +74,9 @@ zhttp_request_destroy (zhttp_request_t **self_p)
self->content = NULL;
self->free_content = false;

zstr_free (&self->username);
zstr_free (&self->password);

// Free object itself
free (self);
*self_p = NULL;
Expand Down Expand Up @@ -118,8 +125,9 @@ zhttp_request_send (zhttp_request_t *self, zhttp_client_t *client, int timeout,
if (rc == -1)
return -1;

zsock_bsend (client, "4ppSp1p", timeout, arg, arg2, self->url,
self->headers, self->free_content ? (byte)1 : (byte)0, self->content);
zsock_bsend (client, "4ppSp1pss", timeout, arg, arg2, self->url,
self->headers, self->free_content ? (byte)1 : (byte)0, self->content,
self->username, self->password);

self->headers = zhash_new ();
zhash_autofree (self->headers);
Expand Down Expand Up @@ -256,6 +264,23 @@ zhttp_request_reset_content (zhttp_request_t *self) {
self->content = NULL;
}


void
zhttp_request_set_username (zhttp_request_t *self, const char *username) {
assert (self);
zstr_free (&self->username);
self->username = strdup (username);
}


void
zhttp_request_set_password (zhttp_request_t *self, const char *password) {
assert (self);
zstr_free (&self->password);
self->password = strdup (password);
}


bool
zhttp_request_match (zhttp_request_t *self, const char *method, const char *match, ...) {
if (strneq (method, self->method))
Expand Down Expand Up @@ -387,4 +412,4 @@ zhttp_request_test (bool verbose) {
zhttp_request_destroy (&request);

printf ("OK\n");
}
}

0 comments on commit 62a5e14

Please sign in to comment.