mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-12 01:53:07 +03:00
optionally allow redirects on HTTPClient & OTA updates (#5009)
* optionally allow redirects on http OTA updates * Refactored HTTPClient::begin(url...) & setURL functions, now only beginInternal parses URL, sets ports Added HTTPRedirect example. * fix indentation for style check * add space after while for style check * don't use deprecated begin method in redirect example * moved redirect handling code to HTTPClient. only GET and HEAD requests are currently handled automatically Redirects that fail to be automatically handled return the redirect code as before * added support for POST/303 redirect added device redirect tests * add missing getLocation() implementation * if the new location is only a path then only update the URI
This commit is contained in:
@ -65,9 +65,110 @@ TEST_CASE("HTTP GET & POST requests", "[HTTPClient]")
|
||||
REQUIRE(httpCode == HTTPC_ERROR_CONNECTION_REFUSED);
|
||||
http.end();
|
||||
}
|
||||
{
|
||||
// 301 redirect with follow enabled
|
||||
WiFiClient client;
|
||||
HTTPClient http;
|
||||
http.setFollowRedirects(true);
|
||||
String uri = String("/redirect301?host=")+getenv("SERVER_IP");
|
||||
http.begin(client, getenv("SERVER_IP"), 8088, uri.c_str());
|
||||
auto httpCode = http.GET();
|
||||
REQUIRE(httpCode == HTTP_CODE_OK);
|
||||
String payload = http.getString();
|
||||
REQUIRE(payload == "redirect success");
|
||||
}
|
||||
{
|
||||
// 301 redirect with follow disabled
|
||||
WiFiClient client;
|
||||
HTTPClient http;
|
||||
String uri = String("/redirect301?host=")+getenv("SERVER_IP");
|
||||
http.begin(client, getenv("SERVER_IP"), 8088, uri.c_str());
|
||||
auto httpCode = http.GET();
|
||||
REQUIRE(httpCode == 301);
|
||||
}
|
||||
{
|
||||
// 302 redirect with follow enabled
|
||||
WiFiClient client;
|
||||
HTTPClient http;
|
||||
http.setFollowRedirects(true);
|
||||
String uri = String("/redirect302?host=")+getenv("SERVER_IP");
|
||||
http.begin(client, getenv("SERVER_IP"), 8088, uri.c_str());
|
||||
auto httpCode = http.GET();
|
||||
REQUIRE(httpCode == HTTP_CODE_OK);
|
||||
String payload = http.getString();
|
||||
REQUIRE(payload == "redirect success");
|
||||
}
|
||||
{
|
||||
// 302 redirect with follow disabled
|
||||
WiFiClient client;
|
||||
HTTPClient http;
|
||||
String uri = String("/redirect302?host=")+getenv("SERVER_IP");
|
||||
http.begin(client, getenv("SERVER_IP"), 8088, uri.c_str());
|
||||
auto httpCode = http.GET();
|
||||
REQUIRE(httpCode == 302);
|
||||
}
|
||||
{
|
||||
// 307 redirect with follow enabled
|
||||
WiFiClient client;
|
||||
HTTPClient http;
|
||||
http.setFollowRedirects(true);
|
||||
String uri = String("/redirect307?host=")+getenv("SERVER_IP");
|
||||
http.begin(client, getenv("SERVER_IP"), 8088, uri.c_str());
|
||||
auto httpCode = http.GET();
|
||||
REQUIRE(httpCode == HTTP_CODE_OK);
|
||||
String payload = http.getString();
|
||||
REQUIRE(payload == "redirect success");
|
||||
}
|
||||
{
|
||||
// 307 redirect with follow disabled
|
||||
WiFiClient client;
|
||||
HTTPClient http;
|
||||
String uri = String("/redirect307?host=")+getenv("SERVER_IP");
|
||||
http.begin(client, getenv("SERVER_IP"), 8088, uri.c_str());
|
||||
auto httpCode = http.GET();
|
||||
REQUIRE(httpCode == 307);
|
||||
}
|
||||
{
|
||||
// 301 exceeding redirect limit
|
||||
WiFiClient client;
|
||||
HTTPClient http;
|
||||
http.setFollowRedirects(true);
|
||||
http.setRedirectLimit(0);
|
||||
String uri = String("/redirect301?host=")+getenv("SERVER_IP");
|
||||
http.begin(client, getenv("SERVER_IP"), 8088, uri.c_str());
|
||||
auto httpCode = http.GET();
|
||||
REQUIRE(httpCode == 301);
|
||||
}
|
||||
{
|
||||
// POST 303 redirect with follow enabled
|
||||
WiFiClient client;
|
||||
HTTPClient http;
|
||||
http.setFollowRedirects(true);
|
||||
http.begin(client, getenv("SERVER_IP"), 8088, "/redirect303");
|
||||
auto httpCode = http.POST(getenv("SERVER_IP"));
|
||||
REQUIRE(httpCode == HTTP_CODE_OK);
|
||||
String payload = http.getString();
|
||||
REQUIRE(payload == "redirect success");
|
||||
}
|
||||
{
|
||||
// POST 303 redirect with follow disabled
|
||||
WiFiClient client;
|
||||
HTTPClient http;
|
||||
http.begin(client, getenv("SERVER_IP"), 8088, "/redirect303");
|
||||
auto httpCode = http.POST(getenv("SERVER_IP"));
|
||||
REQUIRE(httpCode == 303);
|
||||
}
|
||||
{
|
||||
// 302 redirect with follow disabled
|
||||
WiFiClient client;
|
||||
HTTPClient http;
|
||||
String uri = String("/redirect302?host=")+getenv("SERVER_IP");
|
||||
http.begin(client, getenv("SERVER_IP"), 8088, uri.c_str());
|
||||
auto httpCode = http.GET();
|
||||
REQUIRE(httpCode == 302);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("HTTPS GET request", "[HTTPClient]")
|
||||
{
|
||||
//
|
||||
|
@ -1,5 +1,5 @@
|
||||
from mock_decorators import setup, teardown
|
||||
from flask import Flask, request
|
||||
from flask import Flask, request, redirect
|
||||
from threading import Thread
|
||||
import urllib2
|
||||
import os
|
||||
@ -26,6 +26,21 @@ def setup_http_get(e):
|
||||
def get_data():
|
||||
size = int(request.args['size'])
|
||||
return 'a'*size
|
||||
@app.route("/target")
|
||||
def target():
|
||||
return "redirect success"
|
||||
@app.route("/redirect301")
|
||||
def redirect301():
|
||||
return redirect("http://{}:8088/target".format(request.args['host']), code=301)
|
||||
@app.route("/redirect302")
|
||||
def redirect302():
|
||||
return redirect("http://{}:8088/target".format(request.args['host']), code=302)
|
||||
@app.route("/redirect303", methods = ['POST'])
|
||||
def redirect303():
|
||||
return redirect("http://{}:8088/target".format(request.data), code=303)
|
||||
@app.route("/redirect307")
|
||||
def redirect307():
|
||||
return redirect("http://{}:8088/target".format(request.args['host']), code=307)
|
||||
def flaskThread():
|
||||
app.run(host='0.0.0.0', port=8088)
|
||||
th = Thread(target=flaskThread)
|
||||
@ -35,7 +50,7 @@ def setup_http_get(e):
|
||||
def teardown_http_get(e):
|
||||
response = urllib2.urlopen('http://localhost:8088/shutdown')
|
||||
html = response.read()
|
||||
time.sleep(30)
|
||||
time.sleep(1) # avoid address in use error on macOS
|
||||
|
||||
|
||||
@setup('HTTPS GET request')
|
||||
|
Reference in New Issue
Block a user