| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -49,6 +49,8 @@ class HTTPNode { | |||
| 49 | 49 | * Adds a validation function that checks if the actual value of a parameter matches the expectation | |
| 50 | 50 | * @param paramIdx defines the ID of the parameter that should be checked (starts by 0) | |
| 51 | 51 | * @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 | ||
| 52 | 54 | */ | |
| 53 | 55 | void addURLParamValidator(uint8_t paramIdx, const HTTPValidationFunction * validator); | |
| 54 | 56 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -58,7 +58,10 @@ size_t HTTPRequest::readBytes(byte * buffer, size_t length) { | |||
| 58 | 58 | length = _remainingContent; | |
| 59 | 59 | } | |
| 60 | 60 | ||
| 61 | - size_t bytesRead = _con->readBuffer(buffer, length); | ||
| 61 | + size_t bytesRead = 0; | ||
| 62 | + if (length > 0) { | ||
| 63 | + _con->readBuffer(buffer, length); | ||
| 64 | + } | ||
| 62 | 65 | ||
| 63 | 66 | if (_contentLengthSet) { | |
| 64 | 67 | _remainingContent -= bytesRead; | |
| Original file line number | Diff line number | Diff 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 | + } | ||
| Original file line number | Diff line number | Diff 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 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,31 +2,41 @@ | |||
| 2 | 2 | ||
| 3 | 3 | namespace httpsserver { | |
| 4 | 4 | ||
| 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 | ||
| 8 | 7 | ||
| 9 | 8 | // Check sign | |
| 10 | 9 | size_t x = 0; | |
| 11 | - if (s[0]=='-') { | ||
| 12 | - | ||
| 13 | - x = 1; | ||
| 14 | - } else if (s[0]=='+') { | ||
| 10 | + if (s[0]=='+') { | ||
| 15 | 11 | x = 1; | |
| 16 | 12 | } | |
| 17 | 13 | ||
| 14 | + // We device max by 10, so we can check if we would exceed it by the next *10 multiplication | ||
| 15 | + max/=10; | ||
| 16 | + | ||
| 18 | 17 | // Convert by base 10 | |
| 19 | 18 | for(; x < s.size(); x++) { | |
| 20 | 19 | 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 | + } | ||
| 23 | 26 | } else { | |
| 24 | - break; | ||
| 27 | + return max; | ||
| 25 | 28 | } | |
| 26 | 29 | } | |
| 27 | 30 | ||
| 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); | ||
| 30 | 40 | } | |
| 31 | 41 | ||
| 32 | 42 | std::string intToString(int i) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,7 +8,9 @@ | |||
| 8 | 8 | ||
| 9 | 9 | namespace httpsserver { | |
| 10 | 10 | ||
| 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); | ||
| 12 | 14 | ||
| 13 | 15 | std::string intToString(int i); | |
| 14 | 16 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments