From 444e8966e83c129b08e3b759534da1bc0049d4b5 Mon Sep 17 00:00:00 2001 From: pschatzmann Date: Wed, 20 Nov 2024 20:09:44 +0100 Subject: [PATCH] HttpRequest chunked corrections --- .../http-client/streams-http_post/streams-http_post.ino | 5 +++-- src/AudioTools/CoreAudio/AudioHttp/HttpRequest.h | 9 +++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/examples/examples-communication/http-client/streams-http_post/streams-http_post.ino b/examples/examples-communication/http-client/streams-http_post/streams-http_post.ino index 6b86916bc..b6eedcc2b 100644 --- a/examples/examples-communication/http-client/streams-http_post/streams-http_post.ino +++ b/examples/examples-communication/http-client/streams-http_post/streams-http_post.ino @@ -11,6 +11,7 @@ const char *ssid = "your SSID"; const char *password = "your PASSWORD"; +const char *url_str = "http://192.168.1.44:9999"; AudioInfo info(44100, 2, 16); SineWaveGenerator sineWave(32000); GeneratedSoundStream sound(sineWave); @@ -47,8 +48,8 @@ void setup(void) { timed.begin(); // start post - Url url("http://192.168.1.35:8000"); - // http.header().put(TRANSFER_ENCODING, CHUNKED); // uncomment if chunked + Url url(url_str); + http.header().put(TRANSFER_ENCODING, CHUNKED); // uncomment if chunked if (!http.processBegin(POST, url, "audio/pcm")){ Serial.println("post failed"); stop(); diff --git a/src/AudioTools/CoreAudio/AudioHttp/HttpRequest.h b/src/AudioTools/CoreAudio/AudioHttp/HttpRequest.h index 2be440838..fb8ab0a4b 100644 --- a/src/AudioTools/CoreAudio/AudioHttp/HttpRequest.h +++ b/src/AudioTools/CoreAudio/AudioHttp/HttpRequest.h @@ -132,12 +132,14 @@ class HttpRequest : public BaseStream { } size_t readBytesUntil(char terminator, char *buffer, size_t length) { + TRACED(); return client_ptr->readBytesUntil(terminator, buffer, length); } // read the reply data up to the next new line. For Chunked data we provide // the full chunk! virtual int readln(uint8_t *str, int len, bool incl_nl = true) { + TRACED(); if (reply_header.isChunked()) { return chunk_reader.readln(*client_ptr, str, len); } else { @@ -278,13 +280,14 @@ class HttpRequest : public BaseStream { /// Write data to the client: can be used to post data after calling /// processBegin size_t write(const uint8_t *data, size_t len) override { + TRACED(); size_t result = 0; if (isChunked()) { - client_ptr->println(len); if (len > 0) { + client_ptr->println(len, HEX); result = client_ptr->write(data, len); + client_ptr->println(); } - client_ptr->println(); } else { result = client_ptr->write(data, len); } @@ -293,6 +296,7 @@ class HttpRequest : public BaseStream { /// Ends the http request processing and returns the status code virtual int processEnd() { + TRACED(); // if sending is chunked we terminate with an empty chunk if (isChunked()) { write(nullptr, 0); @@ -342,6 +346,7 @@ class HttpRequest : public BaseStream { // opens a connection to the indicated host virtual int connect(const char *ip, uint16_t port, int32_t timeout) { + TRACED(); client_ptr->setTimeout(timeout / 1000); // client timeout is in seconds! request_header.setTimeout(timeout); reply_header.setTimeout(timeout);