From aa9a5e0d47b29b29ad71785218101b7f6132b684 Mon Sep 17 00:00:00 2001 From: Phil Schatzmann Date: Thu, 16 Nov 2023 09:10:04 +0100 Subject: [PATCH] Cleanup URL timeout --- src/AudioHttp/HttpHeader.h | 15 ++++++++++++++- src/AudioHttp/HttpRequest.h | 17 ++++++----------- src/AudioHttp/URLStream.h | 8 -------- src/AudioTools/AudioStreams.h | 8 -------- 4 files changed, 20 insertions(+), 28 deletions(-) diff --git a/src/AudioHttp/HttpHeader.h b/src/AudioHttp/HttpHeader.h index f9d4721bb9..23197a5c4b 100644 --- a/src/AudioHttp/HttpHeader.h +++ b/src/AudioHttp/HttpHeader.h @@ -228,7 +228,7 @@ class HttpHeader { } /// reads the full header from the request (stream) - void read(Client &in) { + bool read(Client &in) { LOGD("HttpHeader::read"); // remove all existing value clear(); @@ -237,12 +237,19 @@ class HttpHeader { if (in.connected()){ if (in.available()==0) { int count = 0; + uint32_t timeout = millis() + timeout_ms; while(in.available()==0){ delay(50); count++; if (count==2){ LOGI("Waiting for data..."); } + // If we dont get an answer, we abort + if(millis() > timeout ){ + LOGE("Request timed out after %d ms", timeout_ms); + status_code = 401; + return false; + } } LOGI("Data availble"); } @@ -261,6 +268,7 @@ class HttpHeader { } } } + return true; } /// writes the full header to the indicated HttpStreamedMultiOutput stream @@ -301,6 +309,10 @@ class HttpHeader { temp_buffer.resize(0); } + void setTimeout(int timeoutMs){ + timeout_ms = timeoutMs; + } + protected: int status_code = UNDEFINED; bool is_written = false; @@ -315,6 +327,7 @@ class HttpHeader { Vector lines; HttpLineReader reader; const char* CRLF = "\r\n"; + int timeout_ms = URL_CLIENT_TIMEOUT; char* tempBuffer(){ temp_buffer.resize(HTTP_MAX_LEN); diff --git a/src/AudioHttp/HttpRequest.h b/src/AudioHttp/HttpRequest.h index d7ae938ccc..f486b5e666 100644 --- a/src/AudioHttp/HttpRequest.h +++ b/src/AudioHttp/HttpRequest.h @@ -8,14 +8,6 @@ #include "AudioHttp/Url.h" #include "AudioTools/AudioLogger.h" -#ifndef URL_CLIENT_TIMEOUT -#define URL_CLIENT_TIMEOUT 60000 -#endif - -#ifndef URL_HANDSHAKE_TIMEOUT -#define URL_HANDSHAKE_TIMEOUT 120000 -#endif - namespace audio_tools { @@ -308,8 +300,9 @@ class HttpRequest { http_connect_callback = callback; } - void setTimeout(int timeout){ - clientTimeout = timeout; + /// Defines the client timeout in ms + void setTimeout(int timeoutMs){ + clientTimeout = timeoutMs; } protected: @@ -329,7 +322,9 @@ class HttpRequest { // opens a connection to the indicated host virtual int connect(const char *ip, uint16_t port, int32_t timeout) { - client_ptr->setTimeout(timeout); + client_ptr->setTimeout(timeout/1000); // client timeout is in seconds! + request_header.setTimeout(timeout); + reply_header.setTimeout(timeout); int is_connected = this->client_ptr->connect(ip, port); LOGI("connected %d timeout %d", is_connected, (int) timeout); return is_connected; diff --git a/src/AudioHttp/URLStream.h b/src/AudioHttp/URLStream.h index 2fa4e52982..ee3b6bafcf 100644 --- a/src/AudioHttp/URLStream.h +++ b/src/AudioHttp/URLStream.h @@ -12,14 +12,6 @@ #include "AudioHttp/HttpRequest.h" #include "AudioHttp/AbstractURLStream.h" -#ifndef URL_CLIENT_TIMEOUT -#define URL_CLIENT_TIMEOUT 60000 -#endif - -#ifndef URL_HANDSHAKE_TIMEOUT -#define URL_HANDSHAKE_TIMEOUT 120000 -#endif - namespace audio_tools { diff --git a/src/AudioTools/AudioStreams.h b/src/AudioTools/AudioStreams.h index df07b6a305..94439ca6c7 100644 --- a/src/AudioTools/AudioStreams.h +++ b/src/AudioTools/AudioStreams.h @@ -6,14 +6,6 @@ #include "AudioTools/AudioLogger.h" #include "AudioEffects/SoundGenerator.h" -#ifndef URL_CLIENT_TIMEOUT -# define URL_CLIENT_TIMEOUT 60000 -#endif - -#ifndef URL_HANDSHAKE_TIMEOUT -# define URL_HANDSHAKE_TIMEOUT 120000 -#endif - #ifndef IRAM_ATTR # define IRAM_ATTR #endif