esp_tls_conn_read is adding extra characters to the data I receive
Posted: Thu Oct 04, 2018 10:12 pm
I'm POST'ing data to a server and getting a response from it. Here's what I expect to get:
But here's what I'm actually getting:
As far as I can tell, esp_tls_conn_read is stuffing 10b and 0 into the data stream. It also seems to appear that 10b is, in hex, the number of bytes of the following chunk of received data. I've sometimes received the data broken into smaller pieces, and that hex number always indicates the size of the following chunk.
How do I get it to not stuff my data with those byte lengths?
Here's my full code for this function:
Code: Select all
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Server: Kestrel
X-Powered-By: ASP.NET
Set-Cookie: ARRAffinity=0c1a6972157af12ca575fb7a0ce73c735c8fb0151334f0283fcc042a338a2cb6;Path=/;HttpOnly;Domain=mydomain.azurewebsites.net
Date: Thu, 04 Oct 2018 21:56:30 GMT
Connection: close
{"id":9,"deviceID":12,"addressID":0,"userID":0,"deviceSerialNumber":0,"firmwareVersion":0,"lastFillUp":"0001-01-01T00:00:00","currentLevel":0,"status":"Checked Device","firstConnected":"2018-10-04T16:03:33.7168214","lastConnected":"2018-10-04T21:56:30.0917821+00:00"}
Code: Select all
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Server: Kestrel
X-Powered-By: ASP.NET
Set-Cookie: ARRAffinity=0c1a6972157af12ca575fb7a0ce73c735c8fb0151334f0283fcc042a338a2cb6;Path=/;HttpOnly;Domain=mydomain.azurewebsites.net
Date: Thu, 04 Oct 2018 21:56:30 GMT
Connection: close
10b
{"id":9,"deviceID":12,"addressID":0,"userID":0,"deviceSerialNumber":0,"firmwareVersion":0,"lastFillUp":"0001-01-01T00:00:00","currentLevel":0,"status":"Checked Device","firstConnected":"2018-10-04T16:03:33.7168214","lastConnected":"2018-10-04T21:56:30.0917821+00:00"}
0
How do I get it to not stuff my data with those byte lengths?
Here's my full code for this function:
Code: Select all
static int EthernetPost( char* url, char* content, char* rsp, int max_rsp_len )
{
int ret = -1;
static char header[512];
int bytes_read = 0;
sprintf(header, "POST %s HTTP/1.1\r\n", url);
sprintf(header, "%sHost: %s\r\n", header, WEB_SERVER);
strcat(header, "Connection: close\r\n");
strcat(header, "Content-Type: application/x-www-form-urlencoded\r\n");
sprintf(header, "%sContent-Length: %u\r\n\r\n", header, strlen(content));
assert(strlen(header) < sizeof(header));
esp_tls_cfg_t cfg = {
.cacert_pem_buf = server_root_cert_pem_start,
.cacert_pem_bytes = server_root_cert_pem_end - server_root_cert_pem_start,
};
struct esp_tls *tls = esp_tls_conn_http_new(url, &cfg);
if (tls)
{
bool error = false;
size_t written_bytes = 0;
while ((written_bytes < strlen(header)) && !error)
{
ret = esp_tls_conn_write(tls,
header + written_bytes,
strlen(header) - written_bytes);
if (ret >= 0)
{
ESP_LOGI(TAG, "%d bytes written", ret);
written_bytes += ret;
}
else if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE)
{
ESP_LOGE(TAG, "esp_tls_conn_write returned 0x%x", ret);
error = true;
}
}
written_bytes = 0;
while ((written_bytes < strlen(content)) && !error)
{
ret = esp_tls_conn_write(tls,
content + written_bytes,
strlen(content) - written_bytes);
if (ret >= 0)
{
ESP_LOGI(TAG, "%d bytes written", ret);
written_bytes += ret;
}
else if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE)
{
ESP_LOGE(TAG, "esp_tls_conn_write returned 0x%x", ret);
error = true;
}
}
// Get the response
bool done = false;
while (!done && !error && (bytes_read < max_rsp_len))
{
ret = esp_tls_conn_read(tls, (rsp + bytes_read), (max_rsp_len - bytes_read));
if (ret == MBEDTLS_ERR_SSL_WANT_WRITE || ret == MBEDTLS_ERR_SSL_WANT_READ)
continue;
if (ret < 0)
{
ESP_LOGE(TAG, "esp_tls_conn_read returned -0x%x", -ret);
error = true;
}
else if (ret == 0)
{
ESP_LOGI(TAG, "connection closed, bytes read: %d", bytes_read);
done = true;
}
else
{
bytes_read += ret;
}
}
}
esp_tls_conn_delete(tls);
if (ret < 0)
return ret;
else
return bytes_read;
}