Skip to content

Commit dd81336

Browse files
committed
ESP8266HTTPClient: fix duplicate Content-Length headers (esp8266#1902)
1 parent d1fc700 commit dd81336

File tree

5 files changed

+31
-59
lines changed

5 files changed

+31
-59
lines changed

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -706,19 +706,27 @@ String HTTPClient::errorToString(int error)
706706
* @param value
707707
* @param first
708708
*/
709-
void HTTPClient::addHeader(const String& name, const String& value, bool first)
709+
void HTTPClient::addHeader(const String& name, const String& value, bool first, bool replace)
710710
{
711-
712711
// not allow set of Header handled by code
713712
if(!name.equalsIgnoreCase(F("Connection")) &&
714713
!name.equalsIgnoreCase(F("User-Agent")) &&
715714
!name.equalsIgnoreCase(F("Host")) &&
716715
!(name.equalsIgnoreCase(F("Authorization")) && _base64Authorization.length())){
716+
717717
String headerLine = name;
718718
headerLine += ": ";
719+
720+
if (replace) {
721+
int headerStart = _headers.indexOf(headerLine);
722+
if (headerStart != -1) {
723+
int headerEnd = _headers.indexOf('\n', headerStart);
724+
_headers = _headers.substring(0, headerStart) + _headers.substring(headerEnd + 1);
725+
}
726+
}
727+
719728
headerLine += value;
720729
headerLine += "\r\n";
721-
722730
if(first) {
723731
_headers = headerLine + _headers;
724732
} else {

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ class HTTPClient
160160
int sendRequest(const char * type, uint8_t * payload = NULL, size_t size = 0);
161161
int sendRequest(const char * type, Stream * stream, size_t size = 0);
162162

163-
void addHeader(const String& name, const String& value, bool first = false);
163+
void addHeader(const String& name, const String& value, bool first = false, bool replace = true);
164164

165165
/// Response handling
166166
void collectHeaders(const char* headerKeys[], const size_t headerKeysCount);

tests/device/libraries/test_config/test_config.h.template

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
#define AP_SSID "test_wifi_ap"
77
#define AP_PASS "test_wifi_ap_pass"
88

9+
#define SERVER_IP "192.168.10.1"

tests/device/test_http_client/test_http_client.ino

+14-52
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void setup()
2121

2222
const char* fp = "40 A3 6C E3 8A DF A2 D4 13 B0 32 5C 87 44 54 28 0B CE C5 A4";
2323

24-
TEST_CASE("HTTP GET request", "[HTTPClient]")
24+
TEST_CASE("HTTP GET & POST requests", "[HTTPClient]")
2525
{
2626
{
2727
// small request
@@ -47,7 +47,20 @@ TEST_CASE("HTTP GET request", "[HTTPClient]")
4747
}
4848
}
4949
}
50+
{
51+
// can do two POST requests with one HTTPClient object (#1902)
52+
HTTPClient http;
53+
http.begin(SERVER_IP, 8088, "/");
54+
http.addHeader("Content-Type", "text/plain");
55+
auto httpCode = http.POST("foo");
56+
Serial.println(httpCode);
57+
REQUIRE(httpCode == HTTP_CODE_OK);
58+
http.end();
5059

60+
httpCode = http.POST("bar");
61+
REQUIRE(httpCode == HTTP_CODE_OK);
62+
http.end();
63+
}
5164
}
5265

5366

@@ -79,57 +92,6 @@ TEST_CASE("HTTPS GET request", "[HTTPClient]")
7992
}
8093

8194
}
82-
// TEST_CASE("HTTP GET request", "[HTTPClient]")
83-
// {
84-
// const int repeatCount = 10;
85-
86-
// String url = createBin(false);
87-
// int heapBefore = ESP.getFreeHeap();
88-
// for (int i = 0; i < repeatCount; ++i) {
89-
// HTTPClient http;
90-
// http.begin(url);
91-
// auto httpCode = http.GET();
92-
// REQUIRE(httpCode == HTTP_CODE_OK);
93-
// String payload = http.getString();
94-
// payload.replace("\n", "\\n");
95-
// String quotedPayload;
96-
// quotedPayload.reserve(payload.length() + 3);
97-
// quotedPayload += "\"";
98-
// quotedPayload += payload;
99-
// quotedPayload += "\"";
100-
// Serial.println("----payload:");
101-
// Serial.println(quotedPayload);
102-
// Serial.println("----");
103-
// Serial.println("----test_payload:");
104-
// Serial.println(test_payload);
105-
// Serial.println("----");
106-
// CHECK(quotedPayload == test_payload);
107-
// http.end();
108-
// delay(100);
109-
// }
110-
// int heapAfter = ESP.getFreeHeap();
111-
// CHECK(heapBefore - heapAfter <= 8);
112-
// }
113-
114-
// TEST_CASE("HTTPS GET request", "[HTTPClient]")
115-
// {
116-
// const int repeatCount = 10;
117-
118-
// String url = createBin(true);
119-
// int heapBefore = ESP.getFreeHeap();
120-
// for (int i = 0; i < repeatCount; ++i) {
121-
// HTTPClient http;
122-
// http.begin(url, mockbin_fingerprint);
123-
// auto httpCode = http.GET();
124-
// REQUIRE(httpCode == HTTP_CODE_OK);
125-
// String payload = http.getString();
126-
// CHECK(payload == test_payload);
127-
// http.end();
128-
// delay(100);
129-
// }
130-
// int heapAfter = ESP.getFreeHeap();
131-
// CHECK(heapBefore - heapAfter <= 8);
132-
// }
13395

13496
void loop()
13597
{

tests/device/test_http_client/test_http_client.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import os
66
import ssl
77

8-
@setup('HTTP GET request')
8+
@setup('HTTP GET & POST requests')
99
def setup_http_get(e):
1010
app = Flask(__name__)
1111
def shutdown_server():
@@ -17,8 +17,9 @@ def shutdown_server():
1717
def shutdown():
1818
shutdown_server()
1919
return 'Server shutting down...'
20-
@app.route("/")
20+
@app.route("/", methods = ['GET', 'POST'])
2121
def root():
22+
print('Got data: ' + request.data);
2223
return 'hello!!!'
2324
@app.route("/data")
2425
def get_data():
@@ -29,7 +30,7 @@ def flaskThread():
2930
th = Thread(target=flaskThread)
3031
th.start()
3132

32-
@teardown('HTTP GET request')
33+
@teardown('HTTP GET & POST requests')
3334
def teardown_http_get(e):
3435
response = urllib2.urlopen('http://localhost:8088/shutdown')
3536
html = response.read()

0 commit comments

Comments
 (0)