Skip to content

Commit 425c208

Browse files
committed
Fixed #3 Implemented validator URL
1 parent dc1cf21 commit 425c208

File tree

8 files changed

+99
-40
lines changed

8 files changed

+99
-40
lines changed

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
wsjcpp-validators
22
tmp/*
33

4+
# Vim temp files
5+
*.swp
6+
47
# Prerequisites
58
*.d
69

@@ -34,5 +37,4 @@ tmp/*
3437
*.out
3538
*.app
3639

37-
# Vim temp files
38-
*.swp
40+

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Classes for data validation
1515
- `new WSJCppValidatorDate()` - validate format date like 'YYYY-MM-DD'
1616
- `new WSJCppValidatorTimeH24()` - validate format date like 'HH:mm:ss' (24 hours)
1717
- `new WSJCppValidatorDateTime()` - validate format date like 'YYYY-MM-DD\THH:mm:ss'
18+
- `new WSJCppValidatorURL()` - validate format of url
1819
- `new WSJCppValidatorBase64()` - validate format of base64
1920
- `new WSJCppValidatorNumber()` - validate format of number
2021
- `new WSJCppValidatorHex()` - validate hex value

src.wsjcpp/wsjcpp_core/wsjcpp_core.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,18 @@ std::string WSJCppCore::getPointerAsHex(void *p) {
435435
return "0x" + std::string(stream.str());
436436
}
437437

438+
// ---------------------------------------------------------------------
439+
440+
std::string WSJCppCore::extractURLProtocol(const std::string& sValue) {
441+
std::string sRet = "";
442+
int nPosProtocol = sValue.find("://");
443+
if (nPosProtocol == std::string::npos) {
444+
return sRet;
445+
}
446+
sRet = sValue.substr(0, nPosProtocol);
447+
return sRet;
448+
}
449+
438450
// ---------------------------------------------------------------------
439451
// WSJCppLog
440452

src.wsjcpp/wsjcpp_core/wsjcpp_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class WSJCppCore {
5252

5353
static unsigned long convertVoidToULong(void *p);
5454
static std::string getPointerAsHex(void *p);
55+
static std::string extractURLProtocol(const std::string& sValue);
5556
};
5657

5758

src/wsjcpp_validators.cpp

Lines changed: 72 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "wsjcpp_validators.h"
22
#include <arpa/inet.h>
3+
#include <wsjcpp_core.h>
34

45
bool WSJCppValidators::isValidDate(const std::string &sValue, std::string &sError) {
56
int nSize = sValue.size();
@@ -214,6 +215,40 @@ bool WSJCppValidators::isValidDomainName(const std::string &sValue, std::string
214215

215216
// ----------------------------------------------------------------------
216217

218+
bool WSJCppValidators::isValidPort(const std::string &sValue, std::string &sError) {
219+
int nPort = std::atoi(sValue.c_str());
220+
return WSJCppValidators::isValidPort(nPort, sError);
221+
}
222+
223+
// ----------------------------------------------------------------------
224+
225+
bool WSJCppValidators::isValidPort(int nValue, std::string &sError) {
226+
if (nValue < 1 || nValue > 65535) {
227+
sError = "Port '" + std::to_string(nValue) + "' must be more then 0 and less then 65536";
228+
return false;
229+
}
230+
return true;
231+
}
232+
233+
// ----------------------------------------------------------------------
234+
235+
bool WSJCppValidators::isValidURLProtocol(const std::string &sValue, std::string &sError) {
236+
if (
237+
sValue != "http"
238+
&& sValue != "https"
239+
&& sValue != "ws"
240+
&& sValue != "wss"
241+
&& sValue != "ftp"
242+
&& sValue != "ssl"
243+
) {
244+
sError = "Unexpected protocol '" + sValue + "'";
245+
return false;
246+
}
247+
return true;
248+
}
249+
250+
// ----------------------------------------------------------------------
251+
217252
bool WSJCppValidators::isValidBase64(const std::string &sValue, std::string &sError) {
218253
int nSize = sValue.size();
219254
if (nSize % 4 != 0) {
@@ -490,11 +525,10 @@ bool WSJCppValidatorDateTime::isValid(const std::string &sValue, std::string &sE
490525
// ----------------------------------------------------------------------
491526
// WSJCppValidatorURL
492527

493-
/*
494528
WSJCppValidatorURL::WSJCppValidatorURL()
495529
: WSJCppValidatorStringBase("url") {
496530
TAG = "WSJCppValidatorURL";
497-
m_rxLikeIPv4Format = std::regex("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}(:\\d{1,5}){0,1}$");
531+
m_rxLikeIPv4Format = std::regex("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$");
498532
}
499533

500534
// ----------------------------------------------------------------------
@@ -507,26 +541,13 @@ bool WSJCppValidatorURL::isValid(const std::string &sValue, std::string &sError)
507541
if (sValue.find(".") == std::string::npos) {
508542
sError = "Must contain at least one dot";
509543
return false;
510-
}
511-
512-
int nPosProtocol = sValue.find("://");
513-
if (nPosProtocol == std::string::npos) {
514-
sError = "Value must contains '://'";
515-
return false;
516-
}
517-
std::string sProtocol = sValue.substr(0, nPosProtocol);
518-
if (
519-
sProtocol != "http"
520-
&& sProtocol != "https"
521-
&& sProtocol != "ws"
522-
&& sProtocol != "wss"
523-
&& sProtocol != "ftp"
524-
) {
525-
sError = "Unexpected protocol '" + sProtocol + "'";
544+
}
545+
std::string sProtocol = WSJCppCore::extractURLProtocol(sValue);
546+
if (!WSJCppValidators::isValidURLProtocol(sProtocol, sError)) {
526547
return false;
527548
}
528549

529-
int nStartPos = nPosProtocol + 3;
550+
int nStartPos = sProtocol.length() + 3;
530551
std::string sAuthorityAddressPath = "";
531552
for (int i = nStartPos; i < sValue.size(); i++) {
532553
char c = sValue[i];
@@ -535,7 +556,7 @@ bool WSJCppValidatorURL::isValid(const std::string &sValue, std::string &sError)
535556
}
536557
sAuthorityAddressPath += c;
537558
}
538-
std::string sQuery = sValue.substr(nPosProtocol + 3 + sAuthorityAddressPath.size());
559+
std::string sQuery = sValue.substr(sProtocol.length() + 3 + sAuthorityAddressPath.size());
539560
std::string sAddressAndPath = sAuthorityAddressPath;
540561

541562
int nPosAuthority = sAuthorityAddressPath.find("@");
@@ -545,9 +566,10 @@ bool WSJCppValidatorURL::isValid(const std::string &sValue, std::string &sError)
545566
sAddressAndPath = sAuthorityAddressPath.substr(nPosAuthority + 1);
546567
}
547568
if (sAuthority != "") {
548-
sError = "TODO check username and password sAuthority= [" + sAuthority + "]";
569+
// sError = "TODO check username and password sAuthority= [" + sAuthority + "]";
549570
// -.~_!$&'()*+,;=:%40:80%2f::::::
550-
return false;
571+
// WSJCppLog::warn(TAG, sError);
572+
// return false;
551573
}
552574
std::string sAddress = sAddressAndPath;
553575
std::string sPath = "";
@@ -562,13 +584,20 @@ bool WSJCppValidatorURL::isValid(const std::string &sValue, std::string &sError)
562584
return false;
563585
}
564586

565-
if (std::regex_match(sAddress, m_rxLikeIPv4Format)) {
566-
int nPosPort = sAddress.find(":");
567-
std::string sPort = "";
568-
if (sAddress.find(":") != std::string::npos) {
569-
sPort = sAddress.substr(nPosPort + 1);
570-
sAddress = sAddress.substr(0, nPosPort);
587+
int nPosPort = sAddress.find(":");
588+
std::string sPort = "";
589+
if (sAddress.find(":") != std::string::npos) {
590+
sPort = sAddress.substr(nPosPort + 1);
591+
sAddress = sAddress.substr(0, nPosPort);
592+
}
593+
594+
if (sPort != "") {
595+
if (!WSJCppValidators::isValidPort(sPort, sError)) {
596+
return false;
571597
}
598+
}
599+
600+
if (std::regex_match(sAddress, m_rxLikeIPv4Format)) {
572601
if (!WSJCppValidators::isValidIPv4(sAddress, sError)) {
573602
return false;
574603
}
@@ -590,19 +619,28 @@ bool WSJCppValidatorURL::isValid(const std::string &sValue, std::string &sError)
590619
}
591620

592621
if (sPath != "") {
593-
sError = "TODO check path sPath=" + sPath;
594-
return false;
622+
for (int i = 0; i < sPath.length(); i++) {
623+
char c = sPath[i];
624+
if (c == ' ') {
625+
sError = "Path could not contains whitespace ' '";
626+
return false;
627+
}
628+
}
595629
}
596630

597631
if (sQuery != "") {
598-
sError = "TODO check query sQuery=[" + sQuery + "]";
599-
return false;
632+
for (int i = 0; i < sQuery.length(); i++) {
633+
char c = sQuery[i];
634+
if (c == ' ') {
635+
sError = "Query could not contains whitespace ' ' (must be encoded)";
636+
return false;
637+
}
638+
}
600639
}
601640

602-
sError = "sAddressAndPath=[" + sAddressAndPath + "], , sAddress=[" + sAddress + "]";
641+
// sError = "sAddressAndPath=[" + sAddressAndPath + "], , sAddress=[" + sAddress + "]";
603642
return true;
604643
}
605-
*/
606644

607645
// ----------------------------------------------------------------------
608646
// WSJCppValidatorBase64

src/wsjcpp_validators.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ class WSJCppValidators {
1616
public:
1717
static bool isValidDate(const std::string &sValue, std::string &sError);
1818
static bool isValidTimeH24(const std::string &sValue, std::string &sError);
19+
static bool isValidURLProtocol(const std::string &sValue, std::string &sError);
1920
static bool isValidDomainName(const std::string &sValue, std::string &sError);
21+
static bool isValidPort(const std::string &sValue, std::string &sError);
22+
static bool isValidPort(int nValue, std::string &sError);
2023
static bool isValidBase64(const std::string &sValue, std::string &sError);
2124
static bool isValidIPv4(const std::string &sValue, std::string &sError);
2225
static bool isValidIPv6(const std::string &sValue, std::string &sError);
@@ -139,7 +142,7 @@ class WSJCppValidatorDateTime : public WSJCppValidatorStringBase {
139142
};
140143

141144
// ----------------------------------------------------------------------
142-
/*
145+
143146
class WSJCppValidatorURL : public WSJCppValidatorStringBase {
144147
public:
145148
WSJCppValidatorURL();
@@ -149,7 +152,7 @@ class WSJCppValidatorURL : public WSJCppValidatorStringBase {
149152
std::string TAG;
150153
std::regex m_rxLikeIPv4Format;
151154
};
152-
*/
155+
153156
// ----------------------------------------------------------------------
154157

155158
class WSJCppValidatorBase64 : public WSJCppValidatorStringBase {

unit-tests.wsjcpp/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_validator_reg_ex
4949
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_validator_reg_exp.cpp")
5050
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_validator_base64.h")
5151
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_validator_base64.cpp")
52+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_validator_url.h")
53+
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_validator_url.cpp")
5254
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_validator_number.h")
5355
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_validator_number.cpp")
5456
list (APPEND WSJCPP_SOURCES "../unit-tests.wsjcpp/src/unit_test_validator_hex.h")

wsjcpp.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ unit-tests:
6060
description: "Regular expression"
6161
- name: "ValidatorBase64"
6262
description: "Validator Base64 format"
63-
# - name: "ValidatorUrl"
64-
# description: "Validator URL-format"
63+
- name: "ValidatorUrl"
64+
description: "Validator URL-format"
6565
- name: "ValidatorNumber"
6666
description: "Number validator"
6767
- name: "ValidatorHex"

0 commit comments

Comments
 (0)