Skip to content

Commit

Permalink
Merge pull request #6 from STNS/support-http-header
Browse files Browse the repository at this point in the history
support http header
  • Loading branch information
pyama86 authored Feb 27, 2019
2 parents f7e25ed + 6de96dd commit 60c9d26
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 2 deletions.
46 changes: 45 additions & 1 deletion stns.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static void stns_force_create_cache_dir(stns_conf_t *c)
void stns_load_config(char *filename, stns_conf_t *c)
{
char errbuf[200];
const char *raw;
const char *raw, *key;
toml_table_t *in_tab;

FILE *fp = fopen(filename, "r");
Expand Down Expand Up @@ -98,6 +98,33 @@ void stns_load_config(char *filename, stns_conf_t *c)
TRIM_SLASH(api_endpoint)
TRIM_SLASH(cache_dir)

int header_size = 0;
stns_user_httpheader_t *http_headers = NULL;

if (0 != (in_tab = toml_table_in(tab, "http_headers"))) {
c->http_headers = (stns_user_httpheaders_t *)malloc(sizeof(stns_user_httpheaders_t));

while (1) {
if (0 != (key = toml_key_in(in_tab, header_size)) && 0 != (raw = toml_raw_in(in_tab, key))) {
if (header_size == 0)
http_headers = (stns_user_httpheader_t *)malloc(sizeof(stns_user_httpheader_t) * header_size);
else
http_headers = (stns_user_httpheader_t *)realloc(http_headers, sizeof(stns_user_httpheader_t) * header_size);

if (0 != toml_rtos(raw, &http_headers[header_size].value)) {
syslog(LOG_ERR, "%s(stns)[L%d] cannot parse toml file:%s key:%s", __func__, __LINE__, filename, key);
}
http_headers[header_size].key = strdup(key);
header_size++;
} else {
break;
}
}
c->http_headers->headers = http_headers;
c->http_headers->size = (size_t)header_size;
} else {
c->http_headers = NULL;
}
stns_force_create_cache_dir(c);
fclose(fp);
toml_free(tab);
Expand Down Expand Up @@ -185,6 +212,7 @@ static size_t response_callback(void *buffer, size_t size, size_t nmemb, void *u
static CURLcode inner_http_request(stns_conf_t *c, char *path, stns_response_t *res)
{
char *auth;
char *in_headers = NULL;
char *url;
CURL *curl;
CURLcode result;
Expand All @@ -208,6 +236,21 @@ static CURLcode inner_http_request(stns_conf_t *c, char *path, stns_response_t *
headers = curl_slist_append(headers, auth);
}

if (c->http_headers != NULL) {

int i, size = 0;
for (i = 0; i < c->http_headers->size; i++) {
size += strlen(c->http_headers->headers[i].key) + strlen(c->http_headers->headers[i].key) + 3;
if (in_headers == NULL)
in_headers = (char *)malloc(size);
else
in_headers = (char *)realloc(in_headers, size);

sprintf(in_headers, "%s: %s", c->http_headers->headers[i].key, c->http_headers->headers[i].value);
headers = curl_slist_append(headers, in_headers);
}
}

#ifdef DEBUG
syslog(LOG_DEBUG, "%s(stns)[L%d] send http request: %s", __func__, __LINE__, url);
#endif
Expand Down Expand Up @@ -256,6 +299,7 @@ static CURLcode inner_http_request(stns_conf_t *c, char *path, stns_response_t *
}

free(auth);
free(in_headers);
free(url);
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
Expand Down
12 changes: 12 additions & 0 deletions stns.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ struct stns_response_t {
long status_code;
};

typedef struct stns_user_httpheader_t stns_user_httpheader_t;
struct stns_user_httpheader_t {
char *key;
char *value;
};
typedef struct stns_user_httpheaders_t stns_user_httpheaders_t;
struct stns_user_httpheaders_t {
stns_user_httpheader_t *headers;
size_t size;
};

typedef struct stns_conf_t stns_conf_t;
struct stns_conf_t {
char *api_endpoint;
Expand All @@ -49,6 +60,7 @@ struct stns_conf_t {
char *cache_dir;
char *tls_cert;
char *tls_key;
stns_user_httpheaders_t *http_headers;
int uid_shift;
int gid_shift;
int ssl_verify;
Expand Down
47 changes: 46 additions & 1 deletion stns_test.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "stns.h"
#include "stns_test.h"

void readfile(char *file, char **result)
{
FILE *fp;
Expand Down Expand Up @@ -56,6 +55,9 @@ Test(stns_load_config, load_ok)
cr_assert_eq(c.negative_cache_ttl, 10);
cr_assert_str_eq(c.tls_cert, "example_cert");
cr_assert_str_eq(c.tls_key, "example_key");
cr_assert_eq(c.http_headers->size, 1);
cr_assert_str_eq(c.http_headers->headers[0].key, "X-API-TOKEN");
cr_assert_str_eq(c.http_headers->headers[0].value, "token");
}

Test(stns_request, http_request)
Expand All @@ -73,6 +75,7 @@ Test(stns_request, http_request)
c.query_wrapper = NULL;
c.tls_cert = NULL;
c.tls_key = NULL;
c.http_headers = NULL;
c.request_timeout = 3;
c.request_retry = 3;
c.auth_token = NULL;
Expand Down Expand Up @@ -101,6 +104,7 @@ Test(stns_request, http_cache)
c.request_timeout = 3;
c.request_retry = 3;
c.auth_token = NULL;
c.http_headers = NULL;

stns_request(&c, "get?example", &r);
cr_assert_eq(stat(path, &st), 0);
Expand Down Expand Up @@ -137,6 +141,47 @@ Test(stns_request, wrapper_request_ng)
res = stns_request(&c, NULL, &r);
cr_assert_eq(res, 22);
}

Test(stns_request, http_request_with_header)
{
stns_conf_t c;
stns_response_t r;

c.api_endpoint = "https://httpbin.org";
c.http_proxy = NULL;
c.cache_dir = "/var/cache/stns";
c.cache = 0;
c.user = NULL;
c.password = NULL;
c.query_wrapper = NULL;
c.tls_cert = NULL;
c.tls_key = NULL;

c.http_headers = (stns_user_httpheaders_t *)malloc(sizeof(stns_user_httpheaders_t));
stns_user_httpheader_t *http_headers = (stns_user_httpheader_t *)malloc(sizeof(stns_user_httpheader_t) * 2);
http_headers[0].key = "test_key1";
http_headers[0].value = "test_value1";
http_headers[1].key = "test_key2";
http_headers[1].value = "test_value2";
c.http_headers->headers = http_headers;
c.http_headers->size = 2;

c.request_timeout = 3;
c.request_retry = 3;
c.auth_token = NULL;
stns_request(&c, "headers", &r);

cr_assert_str_eq(r.data, "{\n\
\"headers\": {\n\
\"Accept\": \"*/*\", \n\
\"Host\": \"httpbin.org\", \n\
\"Test-Key1\": \"test_value1\", \n\
\"Test-Key2\": \"test_value2\", \n\
\"User-Agent\": \"stns/2.0.0\"\n\
}\n\
}\n");
}

Test(stns_request_available, ok)
{
char expect_body[1024];
Expand Down
3 changes: 3 additions & 0 deletions test/stns.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ negative_cache_ttl = 10
[tls]
cert = "example_cert"
key = "example_key"

[http_headers]
X-API-TOKEN = "token"

0 comments on commit 60c9d26

Please sign in to comment.