Commit 567b0a55 authored by Marcel Stör's avatar Marcel Stör Committed by Johny Mattsson
Browse files

Allow to override User-Agent and Host HTTP headers (#1426)

* Allow to override User-Agent and Host HTTP headers

Fixes #1410

Idea borrowed from (unmerged) #1157

* Do not send port for host header for default ports
parent 5f340f5f
...@@ -181,7 +181,6 @@ static void ICACHE_FLASH_ATTR http_connect_callback( void * arg ) ...@@ -181,7 +181,6 @@ static void ICACHE_FLASH_ATTR http_connect_callback( void * arg )
HTTPCLIENT_DEBUG( "Connected\n" ); HTTPCLIENT_DEBUG( "Connected\n" );
struct espconn * conn = (struct espconn *) arg; struct espconn * conn = (struct espconn *) arg;
request_args_t * req = (request_args_t *) conn->reverse; request_args_t * req = (request_args_t *) conn->reverse;
int len;
espconn_regist_recvcb( conn, http_receive_callback ); espconn_regist_recvcb( conn, http_receive_callback );
espconn_regist_sentcb( conn, http_send_callback ); espconn_regist_sentcb( conn, http_send_callback );
...@@ -198,39 +197,57 @@ static void ICACHE_FLASH_ATTR http_connect_callback( void * arg ) ...@@ -198,39 +197,57 @@ static void ICACHE_FLASH_ATTR http_connect_callback( void * arg )
req->headers[0] = '\0'; req->headers[0] = '\0';
} }
char buf[69 + strlen( req->method ) + strlen( req->path ) + strlen( req->hostname ) + char ua_header[32] = "";
strlen( req->headers ) + strlen( post_headers )]; int ua_len = 0;
if (os_strstr( req->headers, "User-Agent:" ) == NULL && os_strstr( req->headers, "user-agent:" ) == NULL)
if ((req->port == 80) || ((req->port == 443) && ( req->secure ))) {
{ os_sprintf( ua_header, "User-Agent: %s\r\n", "ESP8266" );
len = os_sprintf( buf, ua_len = strlen(ua_header);
"%s %s HTTP/1.1\r\n" }
"Host: %s\r\n"
"Connection: close\r\n" char host_header[32] = "";
"User-Agent: ESP8266\r\n" int host_len = 0;
"%s" if ( os_strstr( req->headers, "Host:" ) == NULL && os_strstr( req->headers, "host:" ) == NULL)
"%s" {
"\r\n", if ((req->port == 80) || ((req->port == 443) && ( req->secure )))
req->method, req->path, req->hostname, req->headers, post_headers ); {
} else { os_sprintf( host_header, "Host: %s\r\n", req->hostname );
len = os_sprintf( buf, }
"%s %s HTTP/1.1\r\n" else
"Host: %s:%d\r\n" {
"Connection: close\r\n" os_sprintf( host_header, "Host: %s:%d\r\n", req->hostname, req->port );
"User-Agent: ESP8266\r\n" }
"%s" host_len = strlen(host_header);
"%s" }
"\r\n",
req->method, req->path, req->hostname, req->port, req->headers, post_headers ); char buf[69 + strlen( req->method ) + strlen( req->path ) + host_len +
} strlen( req->headers ) + ua_len + strlen( post_headers )];
if ( req->secure ) int len = os_sprintf( buf,
espconn_secure_send( conn, (uint8_t *) buf, len ); "%s %s HTTP/1.1\r\n"
else "%s" // Host (if not provided in the headers from Lua)
espconn_send( conn, (uint8_t *) buf, len ); "Connection: close\r\n"
if(req->headers != NULL) "%s" // Headers from Lua (optional)
os_free( req->headers ); "%s" // User-Agent (if not provided in the headers from Lua)
req->headers = NULL; "%s" // Content-Length
HTTPCLIENT_DEBUG( "Sending request header\n" ); "\r\n",
req->method, req->path, host_header, req->headers, ua_header, post_headers );
if (req->secure)
{
espconn_secure_send( conn, (uint8_t *) buf, len );
}
else
{
espconn_send( conn, (uint8_t *) buf, len );
}
if (req->headers != NULL)
{
os_free( req->headers );
}
req->headers = NULL;
HTTPCLIENT_DEBUG( "Sending request header\n" );
} }
static void http_free_req( request_args_t * req) static void http_free_req( request_args_t * req)
......
...@@ -11,13 +11,7 @@ Basic HTTP *client* module that provides an interface to do GET/POST/PUT/DELETE ...@@ -11,13 +11,7 @@ Basic HTTP *client* module that provides an interface to do GET/POST/PUT/DELETE
Each request method takes a callback which is invoked when the response has been received from the server. The first argument is the status code, which is either a regular HTTP status code, or -1 to denote a DNS, connection or out-of-memory failure, or a timeout (currently at 10 seconds). Each request method takes a callback which is invoked when the response has been received from the server. The first argument is the status code, which is either a regular HTTP status code, or -1 to denote a DNS, connection or out-of-memory failure, or a timeout (currently at 10 seconds).
For each operation it is also possible to include custom headers. Note that following headers *can not* be overridden however: For each operation it is possible to provide custom HTTP headers or override standard headers. By default the `Host` header is deduced from the URL and `User-Agent` is `ESP8266`. Note, however, that the `Connection` header *can not* be overridden! It is always set to `close`.
- Host
- Connection
- User-Agent
The `Host` header is taken from the URL itself, the `Connection` is always set to `close`, and the `User-Agent` is `ESP8266`.
**SSL/TLS support** **SSL/TLS support**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment