Skip to content

Commit 7ebe38e

Browse files
committed
Merge branch 'issue37'
2 parents 7bbcbf7 + 40ffb03 commit 7ebe38e

21 files changed

+48
-47
lines changed

examples/REST-API/REST-API.ino

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ char contentTypes[][2][32] = {
7676
#include <SSLCert.hpp>
7777
#include <HTTPRequest.hpp>
7878
#include <HTTPResponse.hpp>
79+
#include <util.hpp>
7980

8081
// We use the following struct to store GPIO events:
8182
#define MAX_EVENTS 20
@@ -316,7 +317,7 @@ void handleSPIFFS(HTTPRequest * req, HTTPResponse * res) {
316317
File file = SPIFFS.open(filename.c_str());
317318

318319
// Set length
319-
res->setHeader("Content-Length", "" + file.size());
320+
res->setHeader("Content-Length", httpsserver::intToString(file.size()));
320321

321322
// Content-Type is guessed using the definition of the contentTypes-table defined above
322323
int cTypeIdx = 0;

src/HTTPConnection.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ void handleWebsocketHandshake(HTTPRequest * req, HTTPResponse * res) {
643643
/**
644644
* Function used to compute the value of the Sec-WebSocket-Accept during Websocket handshake
645645
*/
646-
std::string websocketKeyResponseHash(std::string key) {
646+
std::string websocketKeyResponseHash(std::string const &key) {
647647
std::string newKey = key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
648648
uint8_t shaData[HTTPS_SHA1_LENGTH];
649649
esp_sha(SHA1, (uint8_t*)newKey.data(), newKey.length(), shaData);

src/HTTPConnection.hpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ class HTTPConnection : private ConnectionContext {
119119
size_t readBuffer(byte* buffer, size_t length);
120120
size_t getCacheSize();
121121
bool checkWebsocket();
122-
std::string websocketKeyResponseHash(std::string key);
123122

124123
// The receive buffer
125124
char _receiveBuffer[HTTPS_CONNECTION_DATA_CHUNK_SIZE];
@@ -161,7 +160,7 @@ class HTTPConnection : private ConnectionContext {
161160

162161
void handleWebsocketHandshake(HTTPRequest * req, HTTPResponse * res);
163162

164-
std::string websocketKeyResponseHash(std::string key);
163+
std::string websocketKeyResponseHash(std::string const &key);
165164

166165
void validationMiddleware(HTTPRequest * req, HTTPResponse * res, std::function<void()> next);
167166

src/HTTPHeader.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace httpsserver {
44

5-
HTTPHeader::HTTPHeader(const std::string name, const std::string value):
6-
_name(std::move(name)),
7-
_value(std::move(value)) {
8-
5+
HTTPHeader::HTTPHeader(const std::string &name, const std::string &value):
6+
_name(name),
7+
_value(value) {
8+
Serial.printf("Header Constructor: %s=%s\n4", name.c_str(), value.c_str());
99
}
1010

1111
HTTPHeader::~HTTPHeader() {

src/HTTPHeader.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#ifndef SRC_HTTPHEADER_HPP_
22
#define SRC_HTTPHEADER_HPP_
3+
#include <Arduino.h>
34

45
#include <string>
56

67
namespace httpsserver {
78

89
class HTTPHeader {
910
public:
10-
HTTPHeader(const std::string name, const std::string value);
11+
HTTPHeader(const std::string &name, const std::string &value);
1112
virtual ~HTTPHeader();
1213
const std::string _name;
1314
const std::string _value;

src/HTTPHeaders.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ HTTPHeaders::~HTTPHeaders() {
1212
delete _headers;
1313
}
1414

15-
HTTPHeader * HTTPHeaders::get(const std::string name) {
15+
HTTPHeader * HTTPHeaders::get(std::string const &name) {
1616
for(std::vector<HTTPHeader*>::iterator header = _headers->begin(); header != _headers->end(); ++header) {
1717
if ((*header)->_name.compare(name)==0) {
1818
return (*header);
@@ -21,7 +21,7 @@ HTTPHeader * HTTPHeaders::get(const std::string name) {
2121
return NULL;
2222
}
2323

24-
std::string HTTPHeaders::getValue(std::string name) {
24+
std::string HTTPHeaders::getValue(std::string const &name) {
2525
for(std::vector<HTTPHeader*>::iterator header = _headers->begin(); header != _headers->end(); ++header) {
2626
if ((*header)->_name.compare(name)==0) {
2727
return ((*header)->_value);

src/HTTPHeaders.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class HTTPHeaders {
1717
HTTPHeaders();
1818
virtual ~HTTPHeaders();
1919

20-
HTTPHeader * get(std::string name);
21-
std::string getValue(std::string name);
20+
HTTPHeader * get(std::string const &name);
21+
std::string getValue(std::string const &name);
2222
void set(HTTPHeader * header);
2323

2424
std::vector<HTTPHeader *> * getAll();

src/HTTPNode.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace httpsserver {
44

5-
HTTPNode::HTTPNode(const std::string path, const HTTPNodeType nodeType, const std::string tag):
6-
_path(std::move(path)),
7-
_tag(std::move(tag)),
5+
HTTPNode::HTTPNode(std::string const &path, const HTTPNodeType nodeType, std::string const &tag):
6+
_path(path),
7+
_tag(tag),
88
_nodeType(nodeType) {
99

1010
// Create vector for valdiators

src/HTTPNode.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ enum HTTPNodeType {
1919

2020
class HTTPNode {
2121
public:
22-
HTTPNode(const std::string path, const HTTPNodeType nodeType, const std::string tag = "");
22+
HTTPNode(const std::string &path, const HTTPNodeType nodeType, const std::string &tag = "");
2323
virtual ~HTTPNode();
2424

2525
/**

src/HTTPRequest.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ ResourceParameters * HTTPRequest::getParams() {
3636
return _params;
3737
}
3838

39-
std::string HTTPRequest::getHeader(std::string name) {
39+
std::string HTTPRequest::getHeader(std::string const &name) {
4040
HTTPHeader * h = _headers->get(name);
4141
if (h != NULL) {
4242
return h->_value;
@@ -45,7 +45,7 @@ std::string HTTPRequest::getHeader(std::string name) {
4545
}
4646
}
4747

48-
void HTTPRequest::setHeader(std::string name, std::string value) {
48+
void HTTPRequest::setHeader(std::string const &name, std::string const &value) {
4949
_headers->set(new HTTPHeader(name, value));
5050
}
5151

src/HTTPRequest.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ class HTTPRequest {
2020
HTTPRequest(ConnectionContext * con, HTTPHeaders * headers, HTTPNode * resolvedNode, std::string method, ResourceParameters * params, std::string requestString);
2121
virtual ~HTTPRequest();
2222

23-
std::string getHeader(std::string name);
24-
void setHeader(std::string name, std::string value);
23+
std::string getHeader(std::string const &name);
24+
void setHeader(std::string const &name, std::string const &value);
2525
HTTPNode * getResolvedNode();
2626
std::string getRequestString();
2727
std::string getMethod();

src/HTTPResponse.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void HTTPResponse::setStatusCode(uint16_t statusCode) {
3737
_statusCode = statusCode;
3838
}
3939

40-
void HTTPResponse::setStatusText(std::string statusText) {
40+
void HTTPResponse::setStatusText(std::string const &statusText) {
4141
_statusText = statusText;
4242
}
4343

@@ -49,7 +49,7 @@ std::string HTTPResponse::getStatusText() {
4949
return _statusText;
5050
}
5151

52-
void HTTPResponse::setHeader(std::string name, std::string value) {
52+
void HTTPResponse::setHeader(std::string const &name, std::string const &value) {
5353
_headers.set(new HTTPHeader(name, value));
5454
}
5555

src/HTTPResponse.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ class HTTPResponse : public Print {
2525
virtual ~HTTPResponse();
2626

2727
void setStatusCode(uint16_t statusCode);
28-
void setStatusText(std::string statusText);
28+
void setStatusText(std::string const &statusText);
2929
uint16_t getStatusCode();
3030
std::string getStatusText();
31-
void setHeader(std::string name, std::string value);
31+
void setHeader(std::string const &name, std::string const &value);
3232
bool isHeaderWritten();
3333

34-
void printStd(const std::string &str);
34+
void printStd(std::string const &str);
3535

3636
// From Print:
3737
size_t write(const uint8_t *buffer, size_t size);

src/ResourceNode.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace httpsserver {
44

5-
ResourceNode::ResourceNode(const std::string path, const std::string method, const HTTPSCallbackFunction * callback, const std::string tag):
5+
ResourceNode::ResourceNode(const std::string &path, const std::string &method, const HTTPSCallbackFunction * callback, const std::string &tag):
66
HTTPNode(path, HANDLER_CALLBACK, tag),
7-
_method(std::move(method)),
7+
_method(method),
88
_callback(callback) {
99

1010
}

src/ResourceNode.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace httpsserver {
1010

1111
class ResourceNode : public HTTPNode {
1212
public:
13-
ResourceNode(const std::string path, const std::string method, const HTTPSCallbackFunction * callback, const std::string tag = "");
13+
ResourceNode(const std::string &path, const std::string &method, const HTTPSCallbackFunction * callback, const std::string &tag = "");
1414
virtual ~ResourceNode();
1515

1616
const std::string _method;

src/ResourceParameters.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ResourceParameters::~ResourceParameters() {
1010

1111
}
1212

13-
bool ResourceParameters::isRequestParameterSet(std::string &name) {
13+
bool ResourceParameters::isRequestParameterSet(std::string const &name) {
1414
for(auto reqParam = _reqParams.begin(); reqParam != _reqParams.end(); ++reqParam) {
1515
if ((*reqParam).first.compare(name)==0) {
1616
return true;
@@ -19,7 +19,7 @@ bool ResourceParameters::isRequestParameterSet(std::string &name) {
1919
return false;
2020
}
2121

22-
std::string ResourceParameters::getRequestParameter(std::string &name) {
22+
std::string ResourceParameters::getRequestParameter(std::string const &name) {
2323
for(auto reqParam = _reqParams.begin(); reqParam != _reqParams.end(); ++reqParam) {
2424
if ((*reqParam).first.compare(name)==0) {
2525
return (*reqParam).second;
@@ -28,14 +28,14 @@ std::string ResourceParameters::getRequestParameter(std::string &name) {
2828
return "";
2929
}
3030

31-
uint16_t ResourceParameters::getRequestParameterInt(std::string &name) {
31+
uint16_t ResourceParameters::getRequestParameterInt(std::string const &name) {
3232
return parseInt(getRequestParameter(name));
3333
}
3434

35-
void ResourceParameters::setRequestParameter(std::string name, std::string value) {
35+
void ResourceParameters::setRequestParameter(std::string const &name, std::string const &value) {
3636
std::pair<std::string, std::string> param;
37-
param.first = std::move(name);
38-
param.second = std::move(value);
37+
param.first = name;
38+
param.second = value;
3939
_reqParams.push_back(param);
4040
}
4141

@@ -65,7 +65,7 @@ void ResourceParameters::resetUrlParameters() {
6565
_urlParams.clear();
6666
}
6767

68-
void ResourceParameters::setUrlParameter(uint8_t idx, std::string val) {
68+
void ResourceParameters::setUrlParameter(uint8_t idx, std::string const &val) {
6969
if(idx>=_urlParams.capacity()) {
7070
_urlParams.resize(idx + 1);
7171
}

src/ResourceParameters.hpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ class ResourceParameters {
2121
ResourceParameters();
2222
virtual ~ResourceParameters();
2323

24-
bool isRequestParameterSet(std::string &name);
25-
std::string getRequestParameter(std::string &name);
26-
uint16_t getRequestParameterInt(std::string &name);
27-
void setRequestParameter(std::string name, std::string value);
24+
bool isRequestParameterSet(std::string const &name);
25+
std::string getRequestParameter(std::string const &name);
26+
uint16_t getRequestParameterInt(std::string const &name);
27+
void setRequestParameter(std::string const &name, std::string const &value);
2828

2929
std::string getUrlParameter(uint8_t idx);
3030
uint16_t getUrlParameterInt(uint8_t idx);
3131
void resetUrlParameters();
3232
void setUrlParameterCount(uint8_t idx);
33-
void setUrlParameter(uint8_t idx, std::string val);
33+
void setUrlParameter(uint8_t idx, std::string const &val);
3434

3535
private:
3636
std::vector<std::string> _urlParams;

src/WebsocketNode.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace httpsserver {
44

5-
WebsocketNode::WebsocketNode(const std::string path, const WebsocketHandlerCreator * creatorFunction, const std::string tag):
5+
WebsocketNode::WebsocketNode(const std::string &path, const WebsocketHandlerCreator * creatorFunction, const std::string &tag):
66
HTTPNode(path, WEBSOCKET, tag),
77
_creatorFunction(creatorFunction) {
88

src/WebsocketNode.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ typedef WebsocketHandler* (WebsocketHandlerCreator)();
1212

1313
class WebsocketNode : public HTTPNode {
1414
public:
15-
WebsocketNode(const std::string path, const WebsocketHandlerCreator creatorFunction, const std::string tag = "");
15+
WebsocketNode(const std::string &path, const WebsocketHandlerCreator creatorFunction, const std::string &tag = "");
1616
virtual ~WebsocketNode();
1717
WebsocketHandler* newHandler();
1818
std::string getMethod() { return std::string("GET"); }

src/util.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace httpsserver {
44

5-
uint32_t parseUInt(std::string s, uint32_t max) {
5+
uint32_t parseUInt(std::string const &s, uint32_t max) {
66
uint32_t i = 0; // value
77

88
// Check sign
@@ -31,7 +31,7 @@ uint32_t parseUInt(std::string s, uint32_t max) {
3131
return i;
3232
}
3333

34-
int32_t parseInt(std::string s) {
34+
int32_t parseInt(std::string const &s) {
3535
uint32_t max = 0x7fffffff;
3636
if (s[0]=='-') {
3737
return -1 * parseUInt(s.substr(1,max));

src/util.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
namespace httpsserver {
1010

11-
uint32_t parseUInt(std::string s, uint32_t max = 0xffffffff);
11+
uint32_t parseUInt(std::string const &s, uint32_t max = 0xffffffff);
1212

13-
int32_t parseInt(std::string s);
13+
int32_t parseInt(std::string const &s);
1414

1515
std::string intToString(int i);
1616

0 commit comments

Comments
 (0)