Skip to content

Commit 4ecf0dc

Browse files
committed
Add Host and User-Agent header in HTTP / Webdav responses
Update API doc
1 parent 380f4a6 commit 4ecf0dc

17 files changed

Lines changed: 117 additions & 42 deletions

docs/WebDavService.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ The allow header will be set to GET, HEAD, PUT, DELETE, OPTIONS, PROPFIND, MKCOL
1616
The time format used is the local time with the timezone offset:
1717
YYYY-MM-DDThh:mm:ssTZD (e.g. 1997-07-16T19:20:30+01:00)
1818

19+
## Host / User-Agent
20+
User-Agent: ESP3D-TFT-HttpdavServer/1.0 (ESP32S3; Firmware/1.0.0; Platform/RTOS; Embedded; http://www.esp3d.io)
21+
Host: http://192.168.0.1/webdav
22+
1923
## GET method
2024
The GET method is used to retrieve information about the resource identified by the Request-URI. And the content of the resource is returned as the response body.
2125
The necessary headers in response are:

main/modules/http/esp3d_http_service.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "esp3d_log.h"
2727
#include "esp3d_settings.h"
2828
#include "esp3d_string.h"
29+
#include "esp3d_version.h"
2930
#include "esp_tls_crypto.h"
3031
#include "esp_wifi.h"
3132
#include "filesystem/esp3d_globalfs.h"
@@ -220,10 +221,9 @@ bool ESP3DHttpService::begin() {
220221
return true;
221222
}
222223
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
223-
uint32_t intValue =
224-
esp3dTftsettings.readUint32(ESP3DSettingIndex::esp3d_http_port);
224+
_port = esp3dTftsettings.readUint32(ESP3DSettingIndex::esp3d_http_port);
225225
// HTTP port
226-
config.server_port = intValue;
226+
config.server_port = _port;
227227
// Http server core
228228
config.core_id = 0;
229229
// stack size (default 4096)
@@ -997,6 +997,37 @@ ESP3DAuthenticationLevel ESP3DHttpService::getAuthenticationLevel(
997997
return ESP3DAuthenticationLevel::admin;
998998
#endif // #if ESP3D_AUTHENTICATION_FEATURE
999999
}
1000+
1001+
esp_err_t ESP3DHttpService::httpd_resp_set_http_hdr(httpd_req_t *req) {
1002+
esp_err_t err = ESP_OK;
1003+
1004+
err = httpd_resp_set_hdr(req, "User-Agent",
1005+
"ESP3D-TFT-HttpServer/1.0 (" CONFIG_IDF_TARGET
1006+
"; Firmware/" ESP3D_TFT_VERSION
1007+
"; Platform/RTOS; Embedded; http://www.esp3d.io)");
1008+
if (err != ESP_OK) {
1009+
esp3d_log_e("httpd_resp_set_hdr failed for User-Agent");
1010+
return err;
1011+
}
1012+
1013+
static std::string host = "";
1014+
if (host.empty()) {
1015+
host = "http://";
1016+
host += esp3dNetwork.getLocalIpString();
1017+
if (esp3dHttpService.getPort() != 80) {
1018+
host += ":";
1019+
host += esp3dHttpService.getPort();
1020+
}
1021+
}
1022+
1023+
err = httpd_resp_set_hdr(req, "Host", host.c_str());
1024+
if (err != ESP_OK) {
1025+
esp3d_log_e("httpd_resp_set_hdr failed for Host");
1026+
return err;
1027+
}
1028+
return err;
1029+
}
1030+
10001031
esp_err_t ESP3DHttpService::streamFile(const char *path, httpd_req_t *req) {
10011032
esp_err_t res = ESP_OK;
10021033
if (!_started || !_server) {

main/modules/http/esp3d_http_service.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class ESP3DHttpService final {
110110
static char *generate_http_auth_basic_digest(const char *username,
111111
const char *password);
112112
#endif // #if ESP3D_AUTHENTICATION_FEATURE
113+
static esp_err_t httpd_resp_set_http_hdr(httpd_req_t *req);
113114
static ESP3DAuthenticationLevel getAuthenticationLevel(httpd_req_t *req);
114115
static esp_err_t root_get_handler(httpd_req_t *req);
115116
static esp_err_t command_handler(httpd_req_t *req);
@@ -178,11 +179,13 @@ class ESP3DHttpService final {
178179
void pop(esp3dSocketType socketType, int socketFd);
179180

180181
const char *getArg(httpd_req_t *req, const char *argname);
182+
uint32_t getPort() { return _port; };
181183

182184
private:
183185
static char _chunk[CHUNK_BUFFER_SIZE];
184186
bool _started;
185187
httpd_handle_t _server;
188+
uint32_t _port;
186189
const char *getBoundaryString(httpd_req_t *req);
187190
// it is based on : `only one post is supported at once`
188191
static PostUploadContext _post_files_upload_ctx;

main/modules/http/handlers/esp3d_command.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
esp_err_t ESP3DHttpService::command_handler(httpd_req_t *req) {
3131
// TODO: check authentication level
3232
ESP3DAuthenticationLevel authentication_level = getAuthenticationLevel(req);
33+
// Send httpd header
34+
httpd_resp_set_http_hdr(req);
3335
#if ESP3D_AUTHENTICATION_FEATURE
3436
if (authentication_level == ESP3DAuthenticationLevel::guest) {
3537
// send 401

main/modules/http/handlers/esp3d_config.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
esp_err_t ESP3DHttpService::config_handler(httpd_req_t *req) {
2727
esp3d_log("Uri: %s", req->uri);
2828
ESP3DAuthenticationLevel authentication_level = getAuthenticationLevel(req);
29+
// Send httpd header
30+
httpd_resp_set_http_hdr(req);
2931
#if ESP3D_AUTHENTICATION_FEATURE
3032
if (authentication_level == ESP3DAuthenticationLevel::guest) {
3133
// send 401

main/modules/http/handlers/esp3d_favicon_ico.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
esp_err_t ESP3DHttpService::favicon_ico_handler(httpd_req_t *req) {
2525
// No authentication for this URL
2626
esp3d_log("Uri: %s", req->uri);
27+
// Send httpd header
28+
httpd_resp_set_http_hdr(req);
2729
esp_err_t err = esp3dHttpService.streamFile("/fs/favicon.ico", req);
2830
if (err == ESP_ERR_NOT_FOUND) {
2931
esp3d_log("Use embedded favicon.ico.gz");

main/modules/http/handlers/esp3d_file_not_found.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
esp_err_t ESP3DHttpService::file_not_found_handler(httpd_req_t *req,
3030
httpd_err_code_t err) {
31+
// Send httpd header
32+
httpd_resp_set_http_hdr(req);
3133
#if ESP3D_AUTHENTICATION_FEATURE
3234
ESP3DAuthenticationLevel authentication_level = getAuthenticationLevel(req);
3335
if (authentication_level == ESP3DAuthenticationLevel::guest) {

main/modules/http/handlers/esp3d_files.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "authentication/esp3d_authentication.h"
3131

3232
esp_err_t ESP3DHttpService::files_handler(httpd_req_t *req) {
33+
// Send httpd header
34+
httpd_resp_set_http_hdr(req);
3335
#if ESP3D_AUTHENTICATION_FEATURE
3436
ESP3DAuthenticationLevel authentication_level = getAuthenticationLevel(req);
3537
if (authentication_level == ESP3DAuthenticationLevel::guest) {

main/modules/http/handlers/esp3d_login.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
esp_err_t ESP3DHttpService::login_handler(httpd_req_t *req) {
3232
esp3d_log("Uri: %s", req->uri);
33+
// Send httpd header
34+
httpd_resp_set_http_hdr(req);
3335
#if ESP3D_AUTHENTICATION_FEATURE
3436
std::string tmpstr;
3537
if (esp3dHttpService.hasArg(req, "DISCONNECT")) {

main/modules/http/handlers/esp3d_not_authenticated.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ esp_err_t ESP3DHttpService::not_authenticated_handler(httpd_req_t *req) {
3232
int socketId = httpd_req_to_sockfd(req);
3333
esp3d_log("Uri: %s, socket: %d", req->uri, socketId);
3434
esp3dHttpService.onClose(socketId);
35+
// Send httpd header
36+
httpd_resp_set_http_hdr(req);
3537
httpd_resp_set_hdr(req, "Set-Cookie", "ESPSESSIONID=0");
3638
httpd_resp_set_hdr(req, "Cache-Control", "no-cache");
3739
httpd_resp_set_type(req, "application/json");

0 commit comments

Comments
 (0)