Skip to content

Commit f1bd565

Browse files
committed
Merge branch 'esp8266' of https://github.com/esp8266/Arduino into doc_modify
2 parents 54bf2d0 + e62d5a9 commit f1bd565

File tree

14 files changed

+291
-45
lines changed

14 files changed

+291
-45
lines changed

cores/esp8266/FS.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,17 @@ File FS::open(const char* path, const char* mode) {
193193
return File(_impl->open(path, om, am));
194194
}
195195

196+
bool FS::exists(const char* path) {
197+
if (!_impl) {
198+
return false;
199+
}
200+
return _impl->exists(path);
201+
}
202+
203+
bool FS::exists(const String& path) {
204+
return exists(path.c_str());
205+
}
206+
196207
Dir FS::openDir(const char* path) {
197208
if (!_impl) {
198209
return Dir();

cores/esp8266/FS.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ class FS
9797
File open(const char* path, const char* mode);
9898
File open(const String& path, const char* mode);
9999

100+
bool exists(const char* path);
101+
bool exists(const String& path);
102+
100103
Dir openDir(const char* path);
101104
Dir openDir(const String& path);
102105

cores/esp8266/FSImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class FSImpl {
6565
virtual bool begin() = 0;
6666
virtual bool format() = 0;
6767
virtual FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) = 0;
68+
virtual bool exists(const char* path) = 0;
6869
virtual DirImplPtr openDir(const char* path) = 0;
6970
virtual bool rename(const char* pathFrom, const char* pathTo) = 0;
7071
virtual bool remove(const char* path) = 0;

cores/esp8266/cbuf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class cbuf {
9595
size_t bytes_available = room();
9696
size_t size_to_write = (size < bytes_available) ? size : bytes_available;
9797
size_t size_written = size_to_write;
98-
if(_end > _begin && size_to_write > (size_t)(_bufend - _end)) {
98+
if(_end >= _begin && size_to_write > (size_t)(_bufend - _end)) {
9999
size_t top_size = _bufend - _end;
100100
memcpy(_end, src, top_size);
101101
_end = _buf;

cores/esp8266/spiffs_api.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class SPIFFSImpl : public FSImpl {
5959
}
6060

6161
FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) override;
62-
62+
bool exists(const char* path) override;
6363
DirImplPtr openDir(const char* path) override;
6464

6565
bool rename(const char* pathFrom, const char* pathTo) override {
@@ -404,6 +404,14 @@ FileImplPtr SPIFFSImpl::open(const char* path, OpenMode openMode, AccessMode acc
404404
return std::make_shared<SPIFFSFileImpl>(this, fd);
405405
}
406406

407+
bool SPIFFSImpl::exists(const char* path) {
408+
char tmpName[SPIFFS_OBJ_NAME_LEN];
409+
strlcpy(tmpName, path, sizeof(tmpName));
410+
spiffs_stat stat;
411+
int rc = SPIFFS_stat(&_fs, tmpName, &stat);
412+
return rc == SPIFFS_OK;
413+
}
414+
407415
DirImplPtr SPIFFSImpl::openDir(const char* path) {
408416
spiffs_DIR dir;
409417
char tmpName[SPIFFS_OBJ_NAME_LEN];

doc/reference.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,14 @@ if (!f) {
231231
}
232232
```
233233

234+
#### exists
235+
236+
```c++
237+
SPIFFS.exists(path)
238+
```
239+
240+
Returns *true* if a file with given path exists, *false* otherwise.
241+
234242
#### openDir
235243

236244
```c++

libraries/ESP8266WebServer/src/ESP8266WebServer.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ ESP8266WebServer::ESP8266WebServer(int port)
4040
{
4141
}
4242

43-
ESP8266WebServer::~ESP8266WebServer()
44-
{
43+
ESP8266WebServer::~ESP8266WebServer() {
4544
if (!_firstHandler)
4645
return;
4746
RequestHandler* handler = _firstHandler;
@@ -56,14 +55,11 @@ void ESP8266WebServer::begin() {
5655
_server.begin();
5756
}
5857

59-
60-
void ESP8266WebServer::on(const char* uri, ESP8266WebServer::THandlerFunction handler)
61-
{
58+
void ESP8266WebServer::on(const char* uri, ESP8266WebServer::THandlerFunction handler) {
6259
on(uri, HTTP_ANY, handler);
6360
}
6461

65-
void ESP8266WebServer::on(const char* uri, HTTPMethod method, ESP8266WebServer::THandlerFunction fn)
66-
{
62+
void ESP8266WebServer::on(const char* uri, HTTPMethod method, ESP8266WebServer::THandlerFunction fn) {
6763
_addRequestHandler(new FunctionRequestHandler(fn, uri, method));
6864
}
6965

@@ -79,11 +75,10 @@ void ESP8266WebServer::_addRequestHandler(RequestHandler* handler) {
7975
}
8076

8177
void ESP8266WebServer::serveStatic(const char* uri, FS& fs, const char* path) {
82-
_addRequestHandler(new StaticRequestHandler(fs, uri));
78+
_addRequestHandler(new StaticRequestHandler(fs, path, uri));
8379
}
8480

85-
void ESP8266WebServer::handleClient()
86-
{
81+
void ESP8266WebServer::handleClient() {
8782
WiFiClient client = _server.available();
8883
if (!client) {
8984
return;

libraries/ESP8266WebServer/src/detail/RequestHandler.h

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
class RequestHandler {
55
public:
66
RequestHandler(const char* uri, HTTPMethod method)
7-
: uri(uri)
8-
, method(method)
7+
: _uri(uri)
8+
, _method(method)
99
, next(NULL)
1010
{
1111
}
@@ -15,8 +15,8 @@ class RequestHandler {
1515
RequestHandler* next;
1616

1717
protected:
18-
String uri;
19-
HTTPMethod method;
18+
String _uri;
19+
HTTPMethod _method;
2020
};
2121

2222

@@ -25,47 +25,59 @@ class FunctionRequestHandler : public RequestHandler {
2525

2626
public:
2727
FunctionRequestHandler(ESP8266WebServer::THandlerFunction fn, const char* uri, HTTPMethod method)
28-
: fn(fn)
28+
: _fn(fn)
2929
, base(uri, method)
3030
{
3131
}
3232

3333
bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override {
34-
if (method != HTTP_ANY && method != requestMethod)
34+
if (_method != HTTP_ANY && _method != requestMethod)
3535
return false;
3636

37-
if (requestUri != uri)
37+
if (requestUri != _uri)
3838
return false;
3939

40-
fn();
40+
_fn();
4141
return true;
4242
}
4343

4444
protected:
45-
ESP8266WebServer::THandlerFunction fn;
45+
ESP8266WebServer::THandlerFunction _fn;
4646
};
4747

4848
class StaticRequestHandler : public RequestHandler {
4949
typedef RequestHandler base;
5050

5151
public:
52-
StaticRequestHandler(FS& fs, const char* uri)
53-
: fs(fs)
52+
StaticRequestHandler(FS& fs, const char* path, const char* uri)
53+
: _fs(fs)
5454
, base(uri, HTTP_GET)
55+
, _path(path)
5556
{
57+
_isFile = fs.exists(path);
58+
DEBUGV("StaticRequestHandler: path=%s uri=%s isFile=%d\r\n", path, uri, _isFile);
59+
_baseUriLength = _uri.length();
5660
}
5761

5862
bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override {
59-
if (requestMethod != method)
63+
if (requestMethod != _method)
6064
return false;
61-
DEBUGV("StaticRequestHandler::handle: %s\r\n", requestUri.c_str());
62-
if (!requestUri.startsWith(uri))
65+
DEBUGV("StaticRequestHandler::handle: request=%s _uri=%s\r\n", requestUri.c_str(), _uri.c_str());
66+
if (!requestUri.startsWith(_uri))
6367
return false;
6468

65-
auto prefixLength = uri.length() - 1;
66-
String path = requestUri.substring(prefixLength);
67-
DEBUGV("StaticRequestHandler::handle: %d %s\r\n", prefixLength, path.c_str());
68-
File f = fs.open(path, "r");
69+
String path(_path);
70+
if (!_isFile) {
71+
// Base URI doesn't point to a file. Append whatever follows this
72+
// URI in request to get the file path.
73+
path += requestUri.substring(_baseUriLength);
74+
}
75+
else if (requestUri != _uri) {
76+
// Base URI points to a file but request doesn't match this URI exactly
77+
return false;
78+
}
79+
DEBUGV("StaticRequestHandler::handle: path=%s, isFile=%d\r\n", path.c_str(), _isFile);
80+
File f = _fs.open(path, "r");
6981
if (!f)
7082
return false;
7183

@@ -90,7 +102,10 @@ class StaticRequestHandler : public RequestHandler {
90102
}
91103

92104
protected:
93-
FS fs;
105+
FS _fs;
106+
String _path;
107+
bool _isFile;
108+
size_t _baseUriLength;
94109
};
95110

96111
#endif //REQUESTHANDLER_H
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* HTTP over TLS (HTTPS) example sketch
3+
*
4+
* This example demonstrates how to use
5+
* WiFiClientSecure class to access HTTPS API.
6+
* We fetch and display the status of
7+
* esp8266/Arduino project continous integration
8+
* build.
9+
*
10+
* Created by Ivan Grokhotkov, 2015.
11+
* This example is in public domain.
12+
*/
13+
14+
#include <ESP8266WiFi.h>
15+
#include <WiFiClientSecure.h>
16+
17+
const char* ssid = "........";
18+
const char* password = "........";
19+
20+
const char* host = "api.github.com";
21+
const int httpsPort = 443;
22+
23+
// Use web browser to view and copy
24+
// SHA1 fingerprint of the certificate
25+
const char* fingerprint = "CF 05 98 89 CA FF 8E D8 5E 5C E0 C2 E4 F7 E6 C3 C7 50 DD 5C";
26+
27+
void setup() {
28+
Serial.begin(115200);
29+
Serial.println();
30+
Serial.print("connecting to ");
31+
Serial.println(ssid);
32+
WiFi.begin(ssid, password);
33+
while (WiFi.status() != WL_CONNECTED) {
34+
delay(500);
35+
Serial.print(".");
36+
}
37+
Serial.println("");
38+
Serial.println("WiFi connected");
39+
Serial.println("IP address: ");
40+
Serial.println(WiFi.localIP());
41+
42+
// Use WiFiClientSecure class to create TLS connection
43+
WiFiClientSecure client;
44+
Serial.print("connecting to ");
45+
Serial.println(host);
46+
if (!client.connect(host, httpsPort)) {
47+
Serial.println("connection failed");
48+
return;
49+
}
50+
51+
if (client.verify(fingerprint, host)) {
52+
Serial.println("certificate matches");
53+
} else {
54+
Serial.println("certificate doesn't match");
55+
}
56+
57+
String url = "/repos/esp8266/Arduino/commits/esp8266/status";
58+
Serial.print("requesting URL: ");
59+
Serial.println(url);
60+
61+
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
62+
"Host: " + host + "\r\n" +
63+
"User-Agent: BuildFailureDetectorESP8266\r\n" +
64+
"Connection: close\r\n\r\n");
65+
66+
Serial.println("request sent");
67+
while (client.connected()) {
68+
String line = client.readStringUntil('\n');
69+
if (line == "\r") {
70+
Serial.println("headers received");
71+
break;
72+
}
73+
}
74+
String line = client.readStringUntil('\n');
75+
if (line.startsWith("{\"state\":\"success\"")) {
76+
Serial.println("esp8266/Arduino CI successfull!");
77+
} else {
78+
Serial.println("esp8266/Arduino CI has failed");
79+
}
80+
Serial.println("reply was:");
81+
Serial.println("==========");
82+
Serial.println(line);
83+
Serial.println("==========");
84+
Serial.println("closing connection");
85+
}
86+
87+
void loop() {
88+
}

libraries/ESP8266WiFi/keywords.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ WiFi KEYWORD1
1616
WiFiClient KEYWORD1
1717
WiFiServer KEYWORD1
1818
WiFiUDP KEYWORD1
19+
WiFiClientSecure KEYWORD1
1920

2021
#######################################
2122
# Methods and Functions (KEYWORD2)
@@ -64,4 +65,3 @@ scanNetworks KEYWORD2
6465
WIFI_AP LITERAL1
6566
WIFI_STA LITERAL1
6667
WIFI_AP_STA LITERAL1
67-

0 commit comments

Comments
 (0)