Skip to content

Commit df56c20

Browse files
committed
Some small bug fixes, validator functions
1 parent 3640f6c commit df56c20

File tree

6 files changed

+78
-14
lines changed

6 files changed

+78
-14
lines changed

src/HTTPNode.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class HTTPNode {
4949
* Adds a validation function that checks if the actual value of a parameter matches the expectation
5050
* @param paramIdx defines the ID of the parameter that should be checked (starts by 0)
5151
* @param validator the function (string -> bool) that checks if the parameter matches the expecatation
52+
*
53+
* @see ValidatorFunctions.hpp if you need some predefined templates for functions
5254
*/
5355
void addURLParamValidator(uint8_t paramIdx, const HTTPValidationFunction * validator);
5456

src/HTTPRequest.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ size_t HTTPRequest::readBytes(byte * buffer, size_t length) {
5858
length = _remainingContent;
5959
}
6060

61-
size_t bytesRead = _con->readBuffer(buffer, length);
61+
size_t bytesRead = 0;
62+
if (length > 0) {
63+
_con->readBuffer(buffer, length);
64+
}
6265

6366
if (_contentLengthSet) {
6467
_remainingContent -= bytesRead;

src/ValidatorFunctions.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "ValidatorFunctions.hpp"
2+
3+
namespace httpsserver {
4+
bool validateNotEmpty(std::string s) {
5+
return s!="";
6+
}
7+
8+
bool validateUnsignedInteger(std::string s) {
9+
for(size_t x = 0; x < s.size(); x++) {
10+
if (s[x]<'0' || s[x]>'9') return false;
11+
}
12+
return true;
13+
}
14+
}

src/ValidatorFunctions.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#ifndef SRC_VALIDATORFUNCTIONS_HPP_
2+
#define SRC_VALIDATORFUNCTIONS_HPP_
3+
4+
#include <Arduino.h>
5+
#include <string>
6+
#undef max
7+
#undef min
8+
#include <functional>
9+
#include <memory>
10+
#include "HTTPValidator.hpp"
11+
#include "util.hpp"
12+
13+
/**
14+
* This file contains some validator functions that can be used to validate URL parameters.
15+
*
16+
* They covor common cases like checking for integer, non-empty, ..., so the user of this library
17+
* does not need to write them on his own.
18+
*/
19+
20+
namespace httpsserver {
21+
22+
/** Checks that a string is not empty. */
23+
bool validateNotEmpty(std::string s);
24+
25+
/**
26+
* Checks that the value is a positive integer (combine it with newValidateUnsignedIntegerMax if
27+
* you have constraints regarding the size of that number
28+
*/
29+
bool validateUnsignedInteger(std::string s);
30+
31+
}
32+
33+
#endif

src/util.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,41 @@
22

33
namespace httpsserver {
44

5-
int parseInt(std::string s) {
6-
int i = 0; // value
7-
int m = 1; // multiplier
5+
uint32_t parseUInt(std::string s, uint32_t max) {
6+
uint32_t i = 0; // value
87

98
// Check sign
109
size_t x = 0;
11-
if (s[0]=='-') {
12-
13-
x = 1;
14-
} else if (s[0]=='+') {
10+
if (s[0]=='+') {
1511
x = 1;
1612
}
1713

14+
// We device max by 10, so we can check if we would exceed it by the next *10 multiplication
15+
max/=10;
16+
1817
// Convert by base 10
1918
for(; x < s.size(); x++) {
2019
char c = s[x];
21-
if (c >= '0' && c<='9') {
22-
i = i*10 + (c-'0');
20+
if (i < max) {
21+
if (c >= '0' && c<='9') {
22+
i = i*10 + (c-'0');
23+
} else {
24+
break;
25+
}
2326
} else {
24-
break;
27+
return max;
2528
}
2629
}
2730

28-
// Combine both.
29-
return m*i;
31+
return i;
32+
}
33+
34+
int32_t parseInt(std::string s) {
35+
uint32_t max = 0x7fffffff;
36+
if (s[0]=='-') {
37+
return -1 * parseUInt(s.substr(1,max));
38+
}
39+
return parseUInt(s,max);
3040
}
3141

3242
std::string intToString(int i) {

src/util.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
namespace httpsserver {
1010

11-
int parseInt(std::string s);
11+
uint32_t parseUInt(std::string s, uint32_t max = 0xffffffff);
12+
13+
int32_t parseInt(std::string s);
1214

1315
std::string intToString(int i);
1416

0 commit comments

Comments
 (0)