Skip to content

Commit 5d49085

Browse files
author
Me No Dev
committed
Merge remote-tracking branch 'esp8266/master'
2 parents 370cb75 + 14949f5 commit 5d49085

File tree

7 files changed

+280
-102
lines changed

7 files changed

+280
-102
lines changed

doc/ota_updates/ota_updates.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ function sendFile($path) {
340340
header('Content-Type: application/octet-stream', true);
341341
header('Content-Disposition: attachment; filename='.basename($path));
342342
header('Content-Length: '.filesize($path), true);
343+
header('x-MD5: '.md5_file($path), true);
343344
readfile($path);
344345
}
345346

libraries/ESP8266WebServer/examples/SDWebServer/SDWebServer.ino

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,10 @@ File uploadFile;
4747

4848

4949
void returnOK() {
50-
server.sendHeader("Connection", "close");
51-
server.sendHeader("Access-Control-Allow-Origin", "*");
5250
server.send(200, "text/plain", "");
5351
}
5452

5553
void returnFail(String msg) {
56-
server.sendHeader("Connection", "close");
57-
server.sendHeader("Access-Control-Allow-Origin", "*");
5854
server.send(500, "text/plain", msg + "\r\n");
5955
}
6056

libraries/ESP8266httpClient/src/ESP8266httpClient.cpp

Lines changed: 104 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ httpClient::httpClient() {
3636
_tcp = NULL;
3737
_tcps = NULL;
3838

39+
_port = 0;
40+
3941
_reuse = false;
42+
_https = false;
43+
44+
_userAgent = "ESP8266httpClient";
4045

4146
_headerKeysCount = 0;
4247
_currentHeaders = NULL;
@@ -74,7 +79,7 @@ httpClient::~httpClient() {
7479
* @param httpsFingerprint const char *
7580
*/
7681
void httpClient::begin(const char *url, const char * httpsFingerprint) {
77-
begin(String(url), String(httpsFingerprint));
82+
begin(String(url), String(httpsFingerprint));
7883
}
7984

8085
/**
@@ -108,7 +113,7 @@ void httpClient::begin(String url, String httpsFingerprint) {
108113
_host = url.substring(0, index); // hostname
109114
url.remove(0, (index + 1)); // remove hostname + :
110115

111-
index = url.indexOf('/');
116+
index = url.indexOf('/');
112117
_port = url.substring(0, index).toInt(); // get port
113118
url.remove(0, index); // remove port
114119
hasPort = true;
@@ -197,7 +202,6 @@ bool httpClient::connected() {
197202
return false;
198203
}
199204

200-
201205
/**
202206
* try to reuse the connection to the server
203207
* keep-alive
@@ -207,6 +211,14 @@ void httpClient::setReuse(bool reuse) {
207211
_reuse = reuse;
208212
}
209213

214+
/**
215+
* set User Agent
216+
* @param userAgent const char *
217+
*/
218+
void httpClient::setUserAgent(const char * userAgent) {
219+
_userAgent = userAgent;
220+
}
221+
210222
/**
211223
* send a GET request
212224
* @return http code
@@ -237,7 +249,7 @@ int httpClient::POST(String payload) {
237249
* @return -1 if no info or > 0 when Content-Length is set by server
238250
*/
239251
int httpClient::sendRequest(const char * type, uint8_t * payload, size_t size) {
240-
// connect ro server
252+
// connect to server
241253
if(!connect()) {
242254
return HTTPC_ERROR_CONNECTION_REFUSED;
243255
}
@@ -262,6 +274,77 @@ int httpClient::sendRequest(const char * type, uint8_t * payload, size_t size) {
262274
return handleHeaderResponse();
263275
}
264276

277+
/**
278+
* sendRequest
279+
* @param type const char * "GET", "POST", ....
280+
* @param stream Stream * data stream for the message body
281+
* @param size size_t size for the message body if 0 not Content-Length is send
282+
* @return -1 if no info or > 0 when Content-Length is set by server
283+
*/
284+
int httpClient::sendRequest(const char * type, Stream * stream, size_t size) {
285+
286+
if(!stream) {
287+
return HTTPC_ERROR_NO_STREAM;
288+
}
289+
290+
// connect to server
291+
if(!connect()) {
292+
return HTTPC_ERROR_CONNECTION_REFUSED;
293+
}
294+
295+
if(size > 0) {
296+
addHeader("Content-Length", String(size));
297+
}
298+
299+
// send Header
300+
if(!sendHeader(type)) {
301+
return HTTPC_ERROR_SEND_HEADER_FAILED;
302+
}
303+
304+
// create buffer for read
305+
uint8_t buff[1460] = { 0 };
306+
307+
int len = size;
308+
int bytesWritten = 0;
309+
310+
if(len == 0) {
311+
len = -1;
312+
}
313+
314+
// read all data from stream and send it to server
315+
while(connected() && stream->available() && (len > 0 || len == -1)) {
316+
317+
// get available data size
318+
size_t s = stream->available();
319+
320+
if(s) {
321+
int c = stream->readBytes(buff, ((s > sizeof(buff)) ? sizeof(buff) : s));
322+
323+
// write it to Stream
324+
bytesWritten += _tcp->write((const uint8_t *)buff, c);
325+
326+
if(len > 0) {
327+
len -= c;
328+
}
329+
330+
delay(0);
331+
} else {
332+
delay(1);
333+
}
334+
}
335+
336+
if(size && (int)size != bytesWritten) {
337+
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n", bytesWritten, _size);
338+
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!");
339+
return HTTPC_ERROR_SEND_PAYLOAD_FAILED;
340+
} else {
341+
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten);
342+
}
343+
344+
// handle Server Response (Header)
345+
return handleHeaderResponse();
346+
}
347+
265348
/**
266349
* size of message body / payload
267350
* @return -1 if no info or > 0 when Content-Length is set by server
@@ -345,7 +428,7 @@ int httpClient::writeToStream(Stream * stream) {
345428
DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] connection closed or file end (written: %d).\n", bytesWritten);
346429

347430
if(_size && _size != bytesWritten) {
348-
DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] bytesWritten %d and size %d missmatch!.\n", bytesWritten, _size);
431+
DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] bytesWritten %d and size %d mismatch!.\n", bytesWritten, _size);
349432
}
350433

351434
end();
@@ -362,7 +445,7 @@ String httpClient::getString(void) {
362445
if(_size) {
363446
// try to reserve needed memmory
364447
if(!sstring.reserve((_size + 1))) {
365-
DEBUG_HTTPCLIENT("[HTTP-Client][getString] too less memory to resive as string! need: %d\n", (_size + 1));
448+
DEBUG_HTTPCLIENT("[HTTP-Client][getString] too less memory to reserve as string! need: %d\n", (_size + 1));
366449
return String("--too less memory--");
367450
}
368451
}
@@ -371,7 +454,6 @@ String httpClient::getString(void) {
371454
return sstring;
372455
}
373456

374-
375457
/**
376458
* adds Header to the request
377459
* @param name
@@ -380,16 +462,20 @@ String httpClient::getString(void) {
380462
*/
381463
void httpClient::addHeader(const String& name, const String& value, bool first) {
382464

383-
String headerLine = name;
384-
headerLine += ": ";
385-
headerLine += value;
386-
headerLine += "\r\n";
465+
// not allow set of Header handled by code
466+
if(!name.equalsIgnoreCase("Connection") && !name.equalsIgnoreCase("User-Agent") && !name.equalsIgnoreCase("Host")) {
467+
String headerLine = name;
468+
headerLine += ": ";
469+
headerLine += value;
470+
headerLine += "\r\n";
387471

388-
if(first) {
389-
_Headers = headerLine + _Headers;
390-
} else {
391-
_Headers += headerLine;
472+
if(first) {
473+
_Headers = headerLine + _Headers;
474+
} else {
475+
_Headers += headerLine;
476+
}
392477
}
478+
393479
}
394480

395481
void httpClient::collectHeaders(const char* headerKeys[], const size_t headerKeysCount) {
@@ -445,7 +531,6 @@ bool httpClient::connect(void) {
445531
return true;
446532
}
447533

448-
449534
if(_https) {
450535
DEBUG_HTTPCLIENT("[HTTP-Client] connect https...\n");
451536
if(_tcps) {
@@ -499,9 +584,10 @@ bool httpClient::sendHeader(const char * type) {
499584
if(!connected()) {
500585
return false;
501586
}
587+
502588
String header = String(type) + " " + _url + " HTTP/1.1\r\n"
503589
"Host: " + _host + "\r\n"
504-
"User-Agent: ESP8266httpClient\r\n"
590+
"User-Agent: " + _userAgent + "\r\n"
505591
"Connection: ";
506592

507593
if(_reuse) {
@@ -511,7 +597,7 @@ bool httpClient::sendHeader(const char * type) {
511597
}
512598
header += "\r\n" + _Headers + "\r\n";
513599

514-
return _tcp->write(header.c_str(), header.length());
600+
return (_tcp->write(header.c_str(), header.length()) == header.length());
515601
}
516602

517603
/**

libraries/ESP8266httpClient/src/ESP8266httpClient.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@ class httpClient {
5959
bool connected(void);
6060

6161
void setReuse(bool reuse); /// keep-alive
62+
void setUserAgent(const char * userAgent);
6263

6364
/// request handling
6465
int GET();
6566
int POST(uint8_t * payload, size_t size);
6667
int POST(String payload);
6768
int sendRequest(const char * type, uint8_t * payload = NULL, size_t size = 0);
69+
int sendRequest(const char * type, Stream * stream, size_t size = 0);
6870

6971
void addHeader(const String& name, const String& value, bool first = false);
7072

@@ -105,7 +107,8 @@ class httpClient {
105107
bool _https;
106108
String _httpsFingerprint;
107109

108-
String _Headers;
110+
String _Headers;
111+
String _userAgent;
109112

110113
/// Response handling
111114
RequestArgument* _currentHeaders;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* httpUpdate.ino
3+
*
4+
* Created on: 27.11.2015
5+
*
6+
*/
7+
8+
#include <Arduino.h>
9+
10+
#include <ESP8266WiFi.h>
11+
#include <ESP8266WiFiMulti.h>
12+
13+
#include <ESP8266httpClient.h>
14+
#include <ESP8266httpUpdate.h>
15+
16+
#define USE_SERIAL Serial
17+
18+
ESP8266WiFiMulti WiFiMulti;
19+
20+
void setup() {
21+
22+
USE_SERIAL.begin(115200);
23+
// USE_SERIAL.setDebugOutput(true);
24+
25+
USE_SERIAL.println();
26+
USE_SERIAL.println();
27+
USE_SERIAL.println();
28+
29+
for(uint8_t t = 4; t > 0; t--) {
30+
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
31+
USE_SERIAL.flush();
32+
delay(1000);
33+
}
34+
35+
WiFiMulti.addAP("SSID", "PASSWORD");
36+
37+
}
38+
39+
void loop() {
40+
// wait for WiFi connection
41+
if((WiFiMulti.run() == WL_CONNECTED)) {
42+
43+
t_httpUpdate_return ret = ESPhttpUpdate.update("http://server/file.bin");
44+
//t_httpUpdate_return ret = ESPhttpUpdate.update("https://server/file.bin");
45+
46+
switch(ret) {
47+
case HTTP_UPDATE_FAILED:
48+
USE_SERIAL.println("HTTP_UPDATE_FAILD");
49+
break;
50+
51+
case HTTP_UPDATE_NO_UPDATES:
52+
USE_SERIAL.println("HTTP_UPDATE_NO_UPDATES");
53+
break;
54+
55+
case HTTP_UPDATE_OK:
56+
USE_SERIAL.println("HTTP_UPDATE_OK");
57+
break;
58+
}
59+
}
60+
}
61+

0 commit comments

Comments
 (0)