From 7c3f76009a17a0dcf2fe8045b3a0b895ba4c34f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20St=C3=B6r?= Date: Sat, 29 Oct 2016 15:04:23 +0300 Subject: [PATCH] Change HTTP failures from debug to error messages (#1568) * Change HTTP failures from debug to error messages * Add tag to HTTP error messages * Create macro for error msg and improve dbg msg --- app/http/httpclient.c | 55 ++++++++++++++++++++++--------------------- app/http/httpclient.h | 15 ++++++++---- 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/app/http/httpclient.c b/app/http/httpclient.c index 9c8b9fc7..42e5b8f1 100644 --- a/app/http/httpclient.c +++ b/app/http/httpclient.c @@ -95,7 +95,7 @@ static int ICACHE_FLASH_ATTR http_chunked_decode( const char * chunked, char * d char * endstr; /* [chunk-size] */ i = strtoul( str + j, NULL, 16 ); - HTTPCLIENT_DEBUG( "Chunk Size:%d\r\n", i ); + HTTPCLIENT_DEBUG( "Chunk Size:%d", i ); if ( i <= 0 ) break; /* [chunk-size-end-ptr] */ @@ -138,7 +138,7 @@ static void ICACHE_FLASH_ATTR http_receive_callback( void * arg, char * buf, uns char * new_buffer; if ( new_size > BUFFER_SIZE_MAX || NULL == (new_buffer = (char *) os_malloc( new_size ) ) ) { - HTTPCLIENT_DEBUG( "Response too long (%d)\n", new_size ); + HTTPCLIENT_ERR( "Response too long (%d)", new_size ); req->buffer[0] = '\0'; /* Discard the buffer to avoid using an incomplete response. */ if ( req->secure ) espconn_secure_disconnect( conn ); @@ -164,12 +164,12 @@ static void ICACHE_FLASH_ATTR http_send_callback( void * arg ) if ( req->post_data == NULL ) { - HTTPCLIENT_DEBUG( "All sent\n" ); + HTTPCLIENT_DEBUG( "All sent" ); } else { /* The headers were sent, now send the contents. */ - HTTPCLIENT_DEBUG( "Sending request body\n" ); + HTTPCLIENT_DEBUG( "Sending request body" ); if ( req->secure ) espconn_secure_send( conn, (uint8_t *) req->post_data, strlen( req->post_data ) ); else @@ -182,7 +182,7 @@ static void ICACHE_FLASH_ATTR http_send_callback( void * arg ) static void ICACHE_FLASH_ATTR http_connect_callback( void * arg ) { - HTTPCLIENT_DEBUG( "Connected\n" ); + HTTPCLIENT_DEBUG( "Connected" ); struct espconn * conn = (struct espconn *) arg; request_args_t * req = (request_args_t *) conn->reverse; espconn_regist_recvcb( conn, http_receive_callback ); @@ -251,7 +251,7 @@ static void ICACHE_FLASH_ATTR http_connect_callback( void * arg ) } req->headers = NULL; - HTTPCLIENT_DEBUG( "Sending request header\n" ); + HTTPCLIENT_DEBUG( "Sending request header" ); } static void http_free_req( request_args_t * req) @@ -273,7 +273,7 @@ static void http_free_req( request_args_t * req) static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg ) { - HTTPCLIENT_DEBUG( "Disconnected\n" ); + HTTPCLIENT_DEBUG( "Disconnected" ); struct espconn *conn = (struct espconn *) arg; if ( conn == NULL ) @@ -296,7 +296,7 @@ static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg ) if ( req->buffer == NULL ) { - HTTPCLIENT_DEBUG( "Buffer probably shouldn't be NULL\n" ); + HTTPCLIENT_DEBUG( "Buffer probably shouldn't be NULL" ); } else if ( req->buffer[0] != '\0' ) { @@ -306,7 +306,7 @@ static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg ) if (( os_strncmp( req->buffer, version_1_0, strlen( version_1_0 ) ) != 0 ) && ( os_strncmp( req->buffer, version_1_1, strlen( version_1_1 ) ) != 0 )) { - HTTPCLIENT_DEBUG( "Invalid version in %s\n", req->buffer ); + HTTPCLIENT_ERR( "Invalid version in %s", req->buffer ); } else { @@ -327,7 +327,7 @@ static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg ) char *locationOffsetEnd = (char *) os_strstr(locationOffset, "\r\n"); if ( locationOffsetEnd == NULL ) { - HTTPCLIENT_DEBUG( "Found Location header but was incomplete\n" ); + HTTPCLIENT_ERR( "Found Location header but was incomplete" ); http_status = -1; } else { *locationOffsetEnd = '\0'; @@ -372,7 +372,7 @@ static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg ) return; } } else { - HTTPCLIENT_DEBUG("Too many redirections\n"); + HTTPCLIENT_ERR("Too many redirections"); http_status = -1; } } else { @@ -380,7 +380,7 @@ static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg ) if (NULL == body) { /* Find missing body */ - HTTPCLIENT_DEBUG("Body shouldn't be NULL\n"); + HTTPCLIENT_ERR("Body shouldn't be NULL"); /* To avoid NULL body */ body = ""; } else { @@ -414,7 +414,7 @@ static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg ) static void ICACHE_FLASH_ATTR http_timeout_callback( void *arg ) { - HTTPCLIENT_DEBUG( "Connection timeout\n" ); + HTTPCLIENT_ERR( "Connection timeout" ); struct espconn * conn = (struct espconn *) arg; if ( conn == NULL ) { @@ -435,7 +435,7 @@ static void ICACHE_FLASH_ATTR http_timeout_callback( void *arg ) static void ICACHE_FLASH_ATTR http_error_callback( void *arg, sint8 errType ) { - HTTPCLIENT_DEBUG( "Disconnected with error: %d\n", errType ); + HTTPCLIENT_ERR( "Disconnected with error: %d", errType ); http_timeout_callback( arg ); } @@ -446,7 +446,7 @@ static void ICACHE_FLASH_ATTR http_dns_callback( const char * hostname, ip_addr_ if ( addr == NULL ) { - HTTPCLIENT_DEBUG( "DNS failed for %s\n", hostname ); + HTTPCLIENT_ERR( "DNS failed for %s", hostname ); if ( req->callback_handle != NULL ) { req->callback_handle( "", -1, "" ); @@ -455,7 +455,7 @@ static void ICACHE_FLASH_ATTR http_dns_callback( const char * hostname, ip_addr_ } else { - HTTPCLIENT_DEBUG( "DNS found %s " IPSTR "\n", hostname, IP2STR( addr ) ); + HTTPCLIENT_DEBUG( "DNS found %s " IPSTR, hostname, IP2STR( addr ) ); struct espconn * conn = (struct espconn *) os_zalloc( sizeof(struct espconn) ); conn->type = ESPCONN_TCP; @@ -490,7 +490,7 @@ static void ICACHE_FLASH_ATTR http_dns_callback( const char * hostname, ip_addr_ void ICACHE_FLASH_ATTR http_raw_request( const char * hostname, int port, bool secure, const char * method, const char * path, const char * headers, const char * post_data, http_callback_t callback_handle, int redirect_follow_count ) { - HTTPCLIENT_DEBUG( "DNS request\n" ); + HTTPCLIENT_DEBUG( "DNS request" ); request_args_t * req = (request_args_t *) os_zalloc( sizeof(request_args_t) ); req->hostname = esp_strdup( hostname ); @@ -513,7 +513,7 @@ void ICACHE_FLASH_ATTR http_raw_request( const char * hostname, int port, bool s if ( error == ESPCONN_INPROGRESS ) { - HTTPCLIENT_DEBUG( "DNS pending\n" ); + HTTPCLIENT_DEBUG( "DNS pending" ); } else if ( error == ESPCONN_OK ) { @@ -524,9 +524,9 @@ void ICACHE_FLASH_ATTR http_raw_request( const char * hostname, int port, bool s { if ( error == ESPCONN_ARG ) { - HTTPCLIENT_DEBUG( "DNS arg error %s\n", hostname ); + HTTPCLIENT_ERR( "DNS arg error %s", hostname ); }else { - HTTPCLIENT_DEBUG( "DNS error code %d\n", error ); + HTTPCLIENT_ERR( "DNS error code %d", error ); } http_dns_callback( hostname, NULL, req ); /* Handle all DNS errors the same way. */ } @@ -562,7 +562,7 @@ void ICACHE_FLASH_ATTR http_request( const char * url, const char * method, cons } else { - HTTPCLIENT_DEBUG( "URL is not HTTP or HTTPS %s\n", url ); + HTTPCLIENT_ERR( "URL is not HTTP or HTTPS %s", url ); return; } @@ -579,7 +579,7 @@ void ICACHE_FLASH_ATTR http_request( const char * url, const char * method, cons } if (path - url >= sizeof(hostname)) { - HTTPCLIENT_DEBUG( "hostname is too long %s\n", url ); + HTTPCLIENT_ERR( "hostname is too long %s", url ); return; } @@ -593,7 +593,7 @@ void ICACHE_FLASH_ATTR http_request( const char * url, const char * method, cons port = atoi( colon + 1 ); if ( port == 0 ) { - HTTPCLIENT_DEBUG( "Port error %s\n", url ); + HTTPCLIENT_ERR( "Port error %s", url ); return; } @@ -607,10 +607,10 @@ void ICACHE_FLASH_ATTR http_request( const char * url, const char * method, cons path = "/"; } - HTTPCLIENT_DEBUG( "hostname=%s\n", hostname ); - HTTPCLIENT_DEBUG( "port=%d\n", port ); - HTTPCLIENT_DEBUG( "method=%s\n", method ); - HTTPCLIENT_DEBUG( "path=%s\n", path ); + HTTPCLIENT_DEBUG( "hostname=%s", hostname ); + HTTPCLIENT_DEBUG( "port=%d", port ); + HTTPCLIENT_DEBUG( "method=%s", method ); + HTTPCLIENT_DEBUG( "path=%s", path ); http_raw_request( hostname, port, secure, method, path, headers, post_data, callback_handle, redirect_follow_count); } @@ -653,3 +653,4 @@ void ICACHE_FLASH_ATTR http_callback_example( char * response, int http_status, dbg_printf( "response=%s\n", response ); } } + diff --git a/app/http/httpclient.h b/app/http/httpclient.h index be49c451..d179ae85 100644 --- a/app/http/httpclient.h +++ b/app/http/httpclient.h @@ -11,17 +11,22 @@ #define __HTTPCLIENT_H__ #if defined(GLOBAL_DEBUG_ON) -#define HTTPCLIENT_DEBUG_ON + #define HTTPCLIENT_DEBUG_ON #endif #if defined(HTTPCLIENT_DEBUG_ON) -#define HTTPCLIENT_DEBUG(format, ...) dbg_printf(format, ##__VA_ARGS__) + #define HTTPCLIENT_DEBUG(format, ...) dbg_printf("HTTP client: "format"\n", ##__VA_ARGS__) #else -#define HTTPCLIENT_DEBUG(format, ...) + #define HTTPCLIENT_DEBUG(format, ...) +#endif +#if defined(NODE_ERROR) + #define HTTPCLIENT_ERR(format, ...) NODE_ERR("HTTP client: "format"\n", ##__VA_ARGS__) +#else + #define HTTPCLIENT_ERR(...) #endif #if defined(USES_SDK_BEFORE_V140) -#define espconn_send espconn_sent -#define espconn_secure_send espconn_secure_sent + #define espconn_send espconn_sent + #define espconn_secure_send espconn_secure_sent #endif /*