I modified the sdk-nrf https_client sample to make a GET request to a web server and retrieve a JSON response. Unfortunately the rec_buf
always ends up with an HTML response <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
. I’m not sure what’s going wrong here, it seems like the headers aren’t being sent correctly.
Here’s my code:
#include <string.h>
#include <zephyr.h>
#include <stdlib.h>
#include <net/socket.h>
#include <modem/nrf_modem_lib.h>
#include <net/tls_credentials.h>
#include <modem/lte_lc.h>
#include <modem/at_cmd.h>
#include <modem/at_notif.h>
#include <modem/modem_key_mgmt.h>
#define SLEEP_TIME_MS 1000
#define HTTPS_PORT 443
#define HTTPS_HOSTNAME "bustracker.pvta.com"
#define STOP_ID "73"
#define HTTP_HEAD \
"GET /InfoPoint/rest/stopdepartures/get/" STOP_ID "HTTP/1.1 \r\n" \
"Host: " HTTPS_HOSTNAME ":443 \r\n" \
"Accept: text/json, application/json; charset=utf-8 \r\n" \
"Connection: close \r\n\r\n"
#define HTTP_HEAD_LEN (sizeof(HTTP_HEAD) - 1)
#define HTTP_HDR_END "\r\n\r\n"
#define RECV_BUF_SIZE 2048
#define TLS_SEC_TAG 42
static const char send_buf[] = HTTP_HEAD;
static char recv_buf[RECV_BUF_SIZE];
/* Certificate for godaddy */
static const char cert[] = {
#include "../cert/gdig2.crt.pem"
};
BUILD_ASSERT(sizeof(cert) < KB(4), "Certificate too large");
/* Initialize AT communications */
int at_comms_init(void) {
int err;
err = at_cmd_init();
if (err) {
printk("Failed to initialize AT commands, err %d\n", err);
return err;
}
err = at_notif_init();
if (err) {
printk("Failed to initialize AT notifications, err %d\n", err);
return err;
}
return 0;
}
/* Provision certificate to modem */
int cert_provision(void) {
int err;
bool exists;
uint8_t unused;
err = modem_key_mgmt_exists(TLS_SEC_TAG,
MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN,
&exists, &unused);
if (err) {
printk("Failed to check for certificates err %d\n", err);
return err;
}
if (exists) {
/* For the sake of simplicity we delete what is provisioned
* with our security tag and reprovision our certificate.
*/
err = modem_key_mgmt_delete(TLS_SEC_TAG,
MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN);
if (err) {
printk("Failed to delete existing certificate, err %d\n",
err);
}
}
printk("Provisioning certificate\n");
/* Provision certificate to the modem */
err = modem_key_mgmt_write(TLS_SEC_TAG,
MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN,
cert, sizeof(cert) - 1);
if (err) {
printk("Failed to provision certificate, err %d\n", err);
return err;
}
return 0;
}
/* Setup TLS options on a given socket */
int tls_setup(int fd) {
int err;
int verify;
/* Security tag that we have provisioned the certificate with */
const sec_tag_t tls_sec_tag[] = {
TLS_SEC_TAG,
};
/* Set up TLS peer verification */
enum {
NONE = 0,
OPTIONAL = 1,
REQUIRED = 2,
};
verify = REQUIRED;
err = setsockopt(fd, SOL_TLS, TLS_PEER_VERIFY, &verify, sizeof(verify));
if (err) {
printk("Failed to setup peer verification, err %d\n", errno);
return err;
}
/* Associate the socket with the security tag
* we have provisioned the certificate with.
*/
err = setsockopt(fd, SOL_TLS, TLS_SEC_TAG_LIST, tls_sec_tag,
sizeof(tls_sec_tag));
if (err) {
printk("Failed to setup TLS sec tag, err %d\n", errno);
return err;
}
err = setsockopt(fd, SOL_TLS, TLS_HOSTNAME, HTTPS_HOSTNAME, sizeof(HTTPS_HOSTNAME) - 1);
if (err) {
printk("Failed to setup TLS hostname, err %d\n", errno);
return err;
}
return 0;
}
int skip_headers(int sock) {
int state = 0;
while (1) {
char c;
int st;
st = recv(sock, &c, 1, 0);
if (st <= 0) {
return st;
}
if (state == 0 && c == '\r') {
state++;
} else if (state == 1 && c == '\n') {
state++;
} else if (state == 2 && c == '\r') {
state++;
} else if (state == 3 && c == '\n') {
break;
} else {
state = 0;
}
}
return 1;
}
char* https_request(void) {
int err;
int fd;
char *p;
int bytes;
size_t off;
struct addrinfo *res;
struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM,
};
k_msleep(SLEEP_TIME_MS);
printk("HTTPS client started\n\r");
printk(HTTP_HEAD);
printk("\n");
err = nrf_modem_lib_init(NORMAL_MODE);
if (err) {
printk("Failed to initialize modem library!");
return;
}
/* Initialize AT comms in order to provision the certificate */
err = at_comms_init();
if (err) {
return;
}
/* Provision certificates before connecting to the LTE network */
err = cert_provision();
if (err) {
return;
}
printk("Waiting for network.. ");
err = lte_lc_init_and_connect();
if (err) {
printk("Failed to connect to the LTE network, err %d\n", err);
return;
}
printk("OK\n");
err = getaddrinfo(HTTPS_HOSTNAME, NULL, &hints, &res);
if (err) {
printk("getaddrinfo() failed, err %d\n", errno);
return;
}
((struct sockaddr_in *)res->ai_addr)->sin_port = htons(HTTPS_PORT);
fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TLS_1_2);
if (fd == -1) {
printk("Failed to open socket!\n");
goto clean_up;
}
/* Setup TLS socket options */
err = tls_setup(fd);
if (err) {
goto clean_up;
}
printk("Connecting to %s\n", HTTPS_HOSTNAME);
err = connect(fd, res->ai_addr, sizeof(struct sockaddr_in));
if (err) {
printk("connect() failed, err: %d\n", errno);
goto clean_up;
}
off = 0;
do {
bytes = send(fd, &send_buf[off], HTTP_HEAD_LEN - off, 0);
if (bytes < 0) {
printk("send() failed, err %d\n", errno);
goto clean_up;
}
off += bytes;
} while (off < HTTP_HEAD_LEN);
printk("Sent %d bytes\n", off);
off = 0;
do {
bytes = recv(fd, &recv_buf[off], RECV_BUF_SIZE - off, 0);
if (bytes < 0) {
printk("recv() failed, err %d\n", errno);
goto clean_up;
}
off += bytes;
} while (bytes != 0 /* peer closed connection */);
printk("Received %d bytes\n", off);
/* Print HTTP response */
p = strstr(recv_buf, "\r\n");
if (p) {
off = p - recv_buf;
recv_buf[off + 1] = '\0';
}
printk("Finished, closing socket.\n");
clean_up:
freeaddrinfo(res);
(void)close(fd);
return(recv_buf);
}