Skip to content

Commit

Permalink
platform: introduce http user-agent header
Browse files Browse the repository at this point in the history
  • Loading branch information
joelguittet committed Sep 26, 2023
1 parent 4f61fcb commit 0411d14
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 5 deletions.
14 changes: 14 additions & 0 deletions platform/net/curl/src/mender-http.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@
#include "mender-log.h"
#include "mender-utils.h"

/**
* @brief HTTP User-Agent
*/
#ifdef MENDER_CLIENT_VERSION
#define MENDER_HTTP_USER_AGENT "mender-mcu-client/" MENDER_CLIENT_VERSION " (mender-http) curl/" LIBCURL_VERSION
#else
#define MENDER_HTTP_USER_AGENT "mender-mcu-client (mender-http) curl/" LIBCURL_VERSION
#endif /* MENDER_CLIENT_VERSION */

/**
* @brief User data
*/
Expand Down Expand Up @@ -123,6 +132,11 @@ mender_http_perform(char * jwt,
ret = MENDER_FAIL;
goto END;
}
if (CURLE_OK != (err = curl_easy_setopt(curl, CURLOPT_USERAGENT, MENDER_HTTP_USER_AGENT))) {
mender_log_error("Unable to set HTTP User-Agent: %s", curl_easy_strerror(err));
ret = MENDER_FAIL;
goto END;
}
if (CURLE_OK != (err = curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2))) {
mender_log_error("Unable to set TLSv1.2: %s", curl_easy_strerror(err));
ret = MENDER_FAIL;
Expand Down
14 changes: 14 additions & 0 deletions platform/net/curl/src/mender-websocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@
#define CONFIG_MENDER_WEBSOCKET_THREAD_PRIORITY (0)
#endif /* CONFIG_MENDER_WEBSOCKET_THREAD_PRIORITY */

/**
* @brief WebSocket User-Agent
*/
#ifdef MENDER_CLIENT_VERSION
#define MENDER_WEBSOCKET_USER_AGENT "mender-mcu-client/" MENDER_CLIENT_VERSION " (mender-websocket) curl/" LIBCURL_VERSION
#else
#define MENDER_WEBSOCKET_USER_AGENT "mender-mcu-client (mender-websocket) curl/" LIBCURL_VERSION
#endif /* MENDER_CLIENT_VERSION */

/**
* @brief Websocket handle
*/
Expand Down Expand Up @@ -165,6 +174,11 @@ mender_websocket_connect(
ret = MENDER_FAIL;
goto FAIL;
}
if (CURLE_OK != (err_curl = curl_easy_setopt(((mender_websocket_handle_t *)*handle)->client, CURLOPT_USERAGENT, MENDER_WEBSOCKET_USER_AGENT))) {
mender_log_error("Unable to set HTTP User-Agent: %s", curl_easy_strerror(err_curl));
ret = MENDER_FAIL;
goto END;
}
if (CURLE_OK != (err_curl = curl_easy_setopt(((mender_websocket_handle_t *)*handle)->client, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2))) {
mender_log_error("Unable to set TLSv1.2: %s", curl_easy_strerror(err_curl));
ret = MENDER_FAIL;
Expand Down
8 changes: 7 additions & 1 deletion platform/net/esp-idf/src/mender-http.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
#include "mender-log.h"
#include "mender-utils.h"

/**
* @brief HTTP User-Agent
*/
#define MENDER_HTTP_USER_AGENT "mender-mcu-client/" MENDER_CLIENT_VERSION " (mender-http) esp-idf/" IDF_VER

/**
* @brief Mender HTTP configuration
*/
Expand Down Expand Up @@ -86,7 +91,8 @@ mender_http_perform(char * jwt,
}

/* Configuration of the client */
esp_http_client_config_t config = { .url = (NULL != url) ? url : path, .crt_bundle_attach = esp_crt_bundle_attach, .buffer_size_tx = 2048 };
esp_http_client_config_t config
= { .url = (NULL != url) ? url : path, .user_agent = MENDER_HTTP_USER_AGENT, .crt_bundle_attach = esp_crt_bundle_attach, .buffer_size_tx = 2048 };

/* Initialization of the client */
if (NULL == (client = esp_http_client_init(&config))) {
Expand Down
8 changes: 7 additions & 1 deletion platform/net/esp-idf/src/mender-websocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
#define CONFIG_MENDER_WEBSOCKET_PING_INTERVAL (60)
#endif /* CONFIG_MENDER_WEBSOCKET_PING_INTERVAL */

/**
* @brief WebSocket User-Agent
*/
#define MENDER_WEBSOCKET_USER_AGENT "mender-mcu-client/" MENDER_CLIENT_VERSION " (mender-websocket) esp-idf/" IDF_VER

/**
* @brief Websocket handle
*/
Expand Down Expand Up @@ -143,6 +148,7 @@ mender_websocket_connect(

/* Configuration of the client */
esp_websocket_client_config_t config = { .uri = (NULL != url) ? url : path,
.user_agent = MENDER_WEBSOCKET_USER_AGENT,
.crt_bundle_attach = esp_crt_bundle_attach,
.reconnect_timeout_ms = CONFIG_MENDER_WEBSOCKET_RECONNECT_TIMEOUT,
.network_timeout_ms = CONFIG_MENDER_WEBSOCKET_NETWORK_TIMEOUT,
Expand Down Expand Up @@ -309,4 +315,4 @@ mender_websocket_event_handler(void *handler_args, esp_event_base_t base, int32_
mender_log_error("Unknown event occurred (event_id=%d)", event_id);
break;
}
}
}
15 changes: 14 additions & 1 deletion platform/net/zephyr/src/mender-http.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@
* SOFTWARE.
*/

#include <version.h>
#include <zephyr/net/http/client.h>
#include "mender-http.h"
#include "mender-log.h"
#include "mender-net.h"

/**
* @brief HTTP User-Agent
*/
#define MENDER_HTTP_USER_AGENT "mender-mcu-client/" MENDER_CLIENT_VERSION " (mender-http) zephyr/" KERNEL_VERSION_STRING

/**
* @brief Receive buffer length
*/
Expand Down Expand Up @@ -97,7 +103,7 @@ mender_http_perform(char * jwt,
mender_err_t ret;
struct http_request request;
mender_http_request_context request_context;
char * header_fields[4] = { NULL, NULL, NULL, NULL };
char * header_fields[5] = { NULL, NULL, NULL, NULL, NULL };
size_t header_index = 0;
char * host = NULL;
char * port = NULL;
Expand Down Expand Up @@ -132,6 +138,13 @@ mender_http_perform(char * jwt,
goto END;
}
request.recv_buf_len = MENDER_HTTP_RECV_BUF_LENGTH;
if (NULL == (header_fields[header_index] = malloc(strlen("User-Agent: ") + strlen(MENDER_HTTP_USER_AGENT) + strlen("\r\n") + 1))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto END;
}
sprintf(header_fields[header_index], "User-Agent: %s\r\n", MENDER_HTTP_USER_AGENT);
header_index++;
if (NULL != jwt) {
if (NULL == (header_fields[header_index] = (char *)malloc(strlen("Authorization: Bearer ") + strlen(jwt) + strlen("\r\n") + 1))) {
mender_log_error("Unable to allocate memory");
Expand Down
17 changes: 15 additions & 2 deletions platform/net/zephyr/src/mender-websocket.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
* SOFTWARE.
*/

#include <errno.h>
#include <version.h>
#include <zephyr/kernel.h>
#include <zephyr/net/websocket.h>
#include <errno.h>
#include "mender-log.h"
#include "mender-net.h"
#include "mender-websocket.h"
Expand Down Expand Up @@ -67,6 +68,11 @@
#define CONFIG_MENDER_WEBSOCKET_REQUEST_TIMEOUT (3000)
#endif /* CONFIG_MENDER_WEBSOCKET_REQUEST_TIMEOUT */

/**
* @brief WebSocket User-Agent
*/
#define MENDER_WEBSOCKET_USER_AGENT "mender-mcu-client/" MENDER_CLIENT_VERSION " (mender-websocket) zephyr/" KERNEL_VERSION_STRING

/**
* @brief Websocket receive buffer length
*/
Expand Down Expand Up @@ -125,7 +131,7 @@ mender_websocket_connect(
assert(NULL != handle);
mender_err_t ret;
struct websocket_request request;
char * header_fields[2] = { NULL, NULL };
char * header_fields[3] = { NULL, NULL, NULL };
size_t header_index = 0;
char * host = NULL;
char * port = NULL;
Expand Down Expand Up @@ -162,6 +168,13 @@ mender_websocket_connect(
goto FAIL;
}
request.tmp_buf_len = MENDER_WEBSOCKET_RECV_BUF_LENGTH;
if (NULL == (header_fields[header_index] = malloc(strlen("User-Agent: ") + strlen(MENDER_WEBSOCKET_USER_AGENT) + strlen("\r\n") + 1))) {
mender_log_error("Unable to allocate memory");
ret = MENDER_FAIL;
goto FAIL;
}
sprintf(header_fields[header_index], "User-Agent: %s\r\n", MENDER_WEBSOCKET_USER_AGENT);
header_index++;
if (NULL != jwt) {
if (NULL == (header_fields[header_index] = malloc(strlen("Authorization: Bearer ") + strlen(jwt) + strlen("\r\n") + 1))) {
mender_log_error("Unable to allocate memory");
Expand Down

0 comments on commit 0411d14

Please sign in to comment.