Skip to content

Commit

Permalink
HttpRequest chunked corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Nov 20, 2024
1 parent ef3a8a0 commit 444e896
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<int16_t> sineWave(32000);
GeneratedSoundStream<int16_t> sound(sineWave);
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 7 additions & 2 deletions src/AudioTools/CoreAudio/AudioHttp/HttpRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 444e896

Please sign in to comment.