Skip to content

Commit 33e06dc

Browse files
committed
Http - Multiple Redirects
1 parent 1a41341 commit 33e06dc

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

src/AudioHttp/HttpHeader.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace audio_tools {
1111

1212
// Class Configuration
13-
const int MaxHeaderLineLength = 200;
13+
const int MaxHeaderLineLength = 240;
1414

1515
// Define relevant header content
1616
const char* CONTENT_TYPE = "Content-Type";
@@ -87,6 +87,7 @@ class HttpHeader {
8787

8888
HttpHeader& put(const char* key, const char* value){
8989
if (value!=nullptr && strlen(value)>0){
90+
LOGD("HttpHeader::put %s %s", key, value);
9091
HttpHeaderLine *hl = headerLine(key);
9192
if (hl==nullptr){
9293
LOGE("HttpHeader::put - did not add HttpHeaderLine for %s", key);
@@ -138,15 +139,15 @@ class HttpHeader {
138139
if (value[0]==' '){
139140
value = line+pos+2;
140141
}
141-
return put((const char*)key,value);
142+
return put((const char*)key, value);
142143
}
143144

144145
// determines a header value with the key
145146
const char* get(const char* key){
146147
for (auto it = lines.begin() ; it != lines.end(); ++it){
147148
HttpHeaderLine *line = *it;
148149
line->key.trim();
149-
if (Str(line->key).equalsIgnoreCase(key)){
150+
if (line->key.equalsIgnoreCase(key)){
150151
const char* result = line->value.c_str();
151152
return line->active ? result : nullptr;
152153
}
@@ -236,7 +237,7 @@ class HttpHeader {
236237
parse1stLine(line);
237238
while (in.available()){
238239
readLine(in, line, MaxHeaderLineLength);
239-
if (isValidStatus()){
240+
if (isValidStatus() || isRedirectStatus()){
240241
Str lineStr(line);
241242
lineStr.ltrim();
242243
if (lineStr.isEmpty()){
@@ -271,6 +272,9 @@ class HttpHeader {
271272
return status_code >= 200 && status_code < 300;
272273
}
273274

275+
bool isRedirectStatus() {
276+
return status_code >= 300 && status_code < 400;
277+
}
274278

275279
protected:
276280
int status_code = UNDEFINED;
@@ -299,7 +303,7 @@ class HttpHeader {
299303
for (auto it = lines.begin() ; it != lines.end(); ++it){
300304
HttpHeaderLine *pt = (*it);
301305
if (pt!=nullptr && pt->key.c_str()!=nullptr){
302-
if (strcmp(pt->key.c_str(),key)==0){
306+
if (pt->key.equalsIgnoreCase(key)){
303307
pt->active = true;
304308
return pt;
305309
}

src/AudioHttp/URLStream.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,19 @@ class URLStreamDefault : public AbstractURLStream {
177177
getClient(url.isSecure()).setTimeout(clientTimeout);
178178
int status_code = request.process(action, url, reqMime, reqData, len);
179179
// redirect
180-
if (status_code>=300 && status_code<400){
180+
while (request.reply().isRedirectStatus()){
181181
const char *redirect_url = request.reply().get(LOCATION);
182-
LOGW("Rederected to: %s", redirect_url);
183-
url.setUrl(redirect_url);
184-
request.setClient(getClient(url.isSecure()));
185-
status_code = request.process(action, url, reqMime, reqData, len);
182+
if (redirect_url!=nullptr) {
183+
LOGW("Redirected to: %s", redirect_url);
184+
url.setUrl(redirect_url);
185+
Client* p_client = &getClient(url.isSecure());
186+
p_client->stop();
187+
request.setClient(*p_client);
188+
status_code = request.process(action, url, reqMime, reqData, len);
189+
} else {
190+
LOGE("Location is null");
191+
break;
192+
}
186193
}
187194
return status_code;
188195
}

0 commit comments

Comments
 (0)