Skip to content

Commit

Permalink
extends http interface with headers param
Browse files Browse the repository at this point in the history
  • Loading branch information
estkme committed Nov 26, 2023
1 parent f28f871 commit 6626749
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
9 changes: 8 additions & 1 deletion euicc/es9p.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@

#include <cjson/cJSON.h>

static const char *lpa_header[] = {
"User-Agent: gsma-rsp-lpad",
"X-Admin-Protocol: gsma/rsp/v2.2.0",
"Content-Type: application/json",
NULL,
};

static int es9p_trans_ex(struct euicc_ctx *ctx, const char *url, const char *url_postfix, unsigned int *rcode, char **str_rx, const char *str_tx)
{
int fret = 0;
Expand All @@ -32,7 +39,7 @@ static int es9p_trans_ex(struct euicc_ctx *ctx, const char *url, const char *url
strcat(full_url, url_postfix);
// printf("url: %s\n", full_url);
// printf("tx: %s\n", str_tx);
if (ctx->interface.http->transmit(ctx, full_url, &rcode_mearged, &rbuf, &rlen, str_tx, strlen(str_tx)) < 0)
if (ctx->interface.http->transmit(ctx, full_url, &rcode_mearged, &rbuf, &rlen, str_tx, strlen(str_tx), lpa_header) < 0)
{
goto err;
}
Expand Down
2 changes: 1 addition & 1 deletion euicc/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ struct euicc_apdu_interface

struct euicc_http_interface
{
int (*transmit)(struct euicc_ctx *ctx, const char *url, uint32_t *rcode, uint8_t **rx, uint32_t *rx_len, const uint8_t *tx, uint32_t tx_len);
int (*transmit)(struct euicc_ctx *ctx, const char *url, uint32_t *rcode, uint8_t **rx, uint32_t *rx_len, const uint8_t *tx, uint32_t tx_len, const char **headers);
void *userdata;
};
8 changes: 1 addition & 7 deletions interface/http/curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,14 @@ static size_t http_trans_write_callback(void *contents, size_t size, size_t nmem
return realsize;
}

static int http_interface_transmit(struct euicc_ctx *ctx, const char *url, uint32_t *rcode, uint8_t **rx, uint32_t *rx_len, const uint8_t *tx, uint32_t tx_len)
static int http_interface_transmit(struct euicc_ctx *ctx, const char *url, uint32_t *rcode, uint8_t **rx, uint32_t *rx_len, const uint8_t *tx, uint32_t tx_len, const char **h)
{
int fret = 0;
CURL *curl;
CURLcode res;
struct http_trans_response_data responseData = {0};
struct curl_slist *headers = NULL, *nheaders = NULL;
long response_code;
const char *h[] = {
"User-Agent: gsma-rsp-lpad",
"X-Admin-Protocol: gsma/rsp/v2.2.0",
"Content-Type: application/json",
NULL,
};

(*rx) = NULL;
(*rcode) = 0;
Expand Down
23 changes: 20 additions & 3 deletions interface/http/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,12 @@ static int json_print(cJSON *jpayload)
return -1;
}

static int json_request(const char *url, const uint8_t *tx, uint32_t tx_len)
static int json_request(const char *url, const uint8_t *tx, uint32_t tx_len, const char **headers)
{
int fret = 0;
char *tx_hex = NULL;
cJSON *jpayload = NULL;
cJSON *jheaders = NULL;

tx_hex = malloc((2 * tx_len) + 1);
if (tx_hex == NULL)
Expand Down Expand Up @@ -216,6 +217,22 @@ static int json_request(const char *url, const uint8_t *tx, uint32_t tx_len)
free(tx_hex);
tx_hex = NULL;

jheaders = cJSON_AddArrayToObject(jpayload, "headers");
if (jheaders == NULL)
{
goto err;
}

for (int i = 0; headers[i] != NULL; i++)
{
cJSON *jh = cJSON_CreateString(headers[i]);
if (jh == NULL)
{
goto err;
}
cJSON_AddItemToArray(jheaders, jh);
}

fret = json_print(jpayload);
cJSON_Delete(jpayload);
jpayload = NULL;
Expand All @@ -230,7 +247,7 @@ static int json_request(const char *url, const uint8_t *tx, uint32_t tx_len)
}

// {"type":"http","payload":{"rcode":404,"rx":"333435"}}
static int http_interface_transmit(struct euicc_ctx *ctx, const char *url, uint32_t *rcode, uint8_t **rx, uint32_t *rx_len, const uint8_t *tx, uint32_t tx_len)
static int http_interface_transmit(struct euicc_ctx *ctx, const char *url, uint32_t *rcode, uint8_t **rx, uint32_t *rx_len, const uint8_t *tx, uint32_t tx_len, const char **headers)
{
int fret = 0;
char *rx_json;
Expand All @@ -240,7 +257,7 @@ static int http_interface_transmit(struct euicc_ctx *ctx, const char *url, uint3

*rx = NULL;

json_request(url, tx, tx_len);
json_request(url, tx, tx_len, headers);
if (afgets(&rx_json, stdin) < 0)
{
return -1;
Expand Down

0 comments on commit 6626749

Please sign in to comment.