Skip to content

Commit 62a5e14

Browse files
Problem: zhttp_client does not support authentication
Solution: Add functions to zhttp_request to set username and password, and send these to the client.
1 parent a166bb8 commit 62a5e14

File tree

4 files changed

+60
-7
lines changed

4 files changed

+60
-7
lines changed

api/zhttp_request.api

+10
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@
112112
Set the content to NULL
113113
</method>
114114

115+
<method name = "set username">
116+
Set the request username
117+
<argument name = "username" type = "string" />
118+
</method>
119+
120+
<method name = "set password">
121+
Set the request password
122+
<argument name = "password" type = "string" />
123+
</method>
124+
115125
<method name = "match">
116126
Match the path of the request.
117127
Support wildcards with '%s' symbol inside the match string.

include/zhttp_request.h

+10
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ CZMQ_EXPORT void
124124
CZMQ_EXPORT void
125125
zhttp_request_reset_content (zhttp_request_t *self);
126126

127+
// *** Draft method, for development use, may change without warning ***
128+
// Set the request username
129+
CZMQ_EXPORT void
130+
zhttp_request_set_username (zhttp_request_t *self, const char *username);
131+
132+
// *** Draft method, for development use, may change without warning ***
133+
// Set the request password
134+
CZMQ_EXPORT void
135+
zhttp_request_set_password (zhttp_request_t *self, const char *password);
136+
127137
// *** Draft method, for development use, may change without warning ***
128138
// Match the path of the request.
129139
// Support wildcards with '%s' symbol inside the match string.

src/zhttp_client.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,12 @@ static void zhttp_client_actor (zsock_t *pipe, void *args) {
183183
char *url;
184184
zhash_t *headers;
185185
byte free_content;
186-
char* content;
186+
char *content;
187+
char *username;
188+
char *password;
187189

188-
int rc = zsock_brecv (pipe, "4ppSp1p", &timeout, &arg, &arg2,
189-
&url, &headers, &free_content, &content);
190+
int rc = zsock_brecv (pipe, "4ppSp1pss", &timeout, &arg, &arg2,
191+
&url, &headers, &free_content, &content, &username, &password);
190192
assert (rc == 0);
191193

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

221+
if (*username)
222+
curl_easy_setopt (curl, CURLOPT_USERNAME, username);
223+
224+
if (*password)
225+
curl_easy_setopt (curl, CURLOPT_PASSWORD, password);
226+
219227
if (streq (command, "POST") || streq (command, "PUT") ||
220228
streq (command, "PATCH")) {
221229
curl_easy_setopt (curl, CURLOPT_POSTFIELDS, content);

src/zhttp_request.c

+29-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ struct _zhttp_request_t {
2626
char *url;
2727
char method[256];
2828
zhash_t *headers;
29-
char* content;
29+
char *content;
3030
bool free_content;
31+
char *username;
32+
char *password;
3133
};
3234

3335

@@ -46,6 +48,8 @@ zhttp_request_new (void)
4648
strcpy (self->method, "GET");
4749
self->content = NULL;
4850
self->free_content = false;
51+
self->username = NULL;
52+
self->password = NULL;
4953

5054
return self;
5155
}
@@ -70,6 +74,9 @@ zhttp_request_destroy (zhttp_request_t **self_p)
7074
self->content = NULL;
7175
self->free_content = false;
7276

77+
zstr_free (&self->username);
78+
zstr_free (&self->password);
79+
7380
// Free object itself
7481
free (self);
7582
*self_p = NULL;
@@ -118,8 +125,9 @@ zhttp_request_send (zhttp_request_t *self, zhttp_client_t *client, int timeout,
118125
if (rc == -1)
119126
return -1;
120127

121-
zsock_bsend (client, "4ppSp1p", timeout, arg, arg2, self->url,
122-
self->headers, self->free_content ? (byte)1 : (byte)0, self->content);
128+
zsock_bsend (client, "4ppSp1pss", timeout, arg, arg2, self->url,
129+
self->headers, self->free_content ? (byte)1 : (byte)0, self->content,
130+
self->username, self->password);
123131

124132
self->headers = zhash_new ();
125133
zhash_autofree (self->headers);
@@ -256,6 +264,23 @@ zhttp_request_reset_content (zhttp_request_t *self) {
256264
self->content = NULL;
257265
}
258266

267+
268+
void
269+
zhttp_request_set_username (zhttp_request_t *self, const char *username) {
270+
assert (self);
271+
zstr_free (&self->username);
272+
self->username = strdup (username);
273+
}
274+
275+
276+
void
277+
zhttp_request_set_password (zhttp_request_t *self, const char *password) {
278+
assert (self);
279+
zstr_free (&self->password);
280+
self->password = strdup (password);
281+
}
282+
283+
259284
bool
260285
zhttp_request_match (zhttp_request_t *self, const char *method, const char *match, ...) {
261286
if (strneq (method, self->method))
@@ -387,4 +412,4 @@ zhttp_request_test (bool verbose) {
387412
zhttp_request_destroy (&request);
388413

389414
printf ("OK\n");
390-
}
415+
}

0 commit comments

Comments
 (0)