From 62a5e1426020b44c89c99fae60a611b8ee1e7e3f Mon Sep 17 00:00:00 2001 From: Stephen Procter Date: Tue, 6 Aug 2024 10:20:29 +0400 Subject: [PATCH] Problem: zhttp_client does not support authentication Solution: Add functions to zhttp_request to set username and password, and send these to the client. --- api/zhttp_request.api | 10 ++++++++++ include/zhttp_request.h | 10 ++++++++++ src/zhttp_client.c | 14 +++++++++++--- src/zhttp_request.c | 33 +++++++++++++++++++++++++++++---- 4 files changed, 60 insertions(+), 7 deletions(-) diff --git a/api/zhttp_request.api b/api/zhttp_request.api index a70c7eb84..9bc39eff1 100644 --- a/api/zhttp_request.api +++ b/api/zhttp_request.api @@ -112,6 +112,16 @@ Set the content to NULL + + Set the request username + + + + + Set the request password + + + Match the path of the request. Support wildcards with '%s' symbol inside the match string. diff --git a/include/zhttp_request.h b/include/zhttp_request.h index e76803677..8affd216a 100644 --- a/include/zhttp_request.h +++ b/include/zhttp_request.h @@ -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. diff --git a/src/zhttp_client.c b/src/zhttp_client.c index 0164e8989..add0553f1 100644 --- a/src/zhttp_client.c +++ b/src/zhttp_client.c @@ -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); @@ -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); diff --git a/src/zhttp_request.c b/src/zhttp_request.c index e511c674b..2cac96319 100644 --- a/src/zhttp_request.c +++ b/src/zhttp_request.c @@ -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; }; @@ -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; } @@ -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; @@ -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); @@ -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)) @@ -387,4 +412,4 @@ zhttp_request_test (bool verbose) { zhttp_request_destroy (&request); printf ("OK\n"); -} \ No newline at end of file +}