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
This commit is contained in:
Marcel Stör 2016-08-11 05:22:31 +02:00 committed by Johny Mattsson
parent 5f340f5f48
commit 567b0a5553
2 changed files with 51 additions and 40 deletions

View File

@ -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 )
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)
{
os_sprintf( ua_header, "User-Agent: %s\r\n", "ESP8266" );
ua_len = strlen(ua_header);
}
if ((req->port == 80) || ((req->port == 443) && ( req->secure ))) char host_header[32] = "";
{ int host_len = 0;
len = os_sprintf( buf, if ( os_strstr( req->headers, "Host:" ) == NULL && os_strstr( req->headers, "host:" ) == NULL)
"%s %s HTTP/1.1\r\n" {
"Host: %s\r\n" if ((req->port == 80) || ((req->port == 443) && ( req->secure )))
"Connection: close\r\n" {
"User-Agent: ESP8266\r\n" os_sprintf( host_header, "Host: %s\r\n", req->hostname );
"%s" }
"%s" else
"\r\n", {
req->method, req->path, req->hostname, req->headers, post_headers ); os_sprintf( host_header, "Host: %s:%d\r\n", req->hostname, req->port );
} else { }
len = os_sprintf( buf, host_len = strlen(host_header);
"%s %s HTTP/1.1\r\n" }
"Host: %s:%d\r\n"
"Connection: close\r\n" char buf[69 + strlen( req->method ) + strlen( req->path ) + host_len +
"User-Agent: ESP8266\r\n" strlen( req->headers ) + ua_len + strlen( post_headers )];
"%s" int len = os_sprintf( buf,
"%s" "%s %s HTTP/1.1\r\n"
"\r\n", "%s" // Host (if not provided in the headers from Lua)
req->method, req->path, req->hostname, req->port, req->headers, post_headers ); "Connection: close\r\n"
} "%s" // Headers from Lua (optional)
if ( req->secure ) "%s" // User-Agent (if not provided in the headers from Lua)
espconn_secure_send( conn, (uint8_t *) buf, len ); "%s" // Content-Length
else "\r\n",
espconn_send( conn, (uint8_t *) buf, len ); req->method, req->path, host_header, req->headers, ua_header, post_headers );
if(req->headers != NULL)
os_free( req->headers ); if (req->secure)
req->headers = NULL; {
HTTPCLIENT_DEBUG( "Sending request header\n" ); 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)

View File

@ -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**