1
1
#include " wsjcpp_validators.h"
2
2
#include < arpa/inet.h>
3
+ #include < wsjcpp_core.h>
3
4
4
5
bool WSJCppValidators::isValidDate (const std::string &sValue , std::string &sError ) {
5
6
int nSize = sValue .size ();
@@ -214,6 +215,40 @@ bool WSJCppValidators::isValidDomainName(const std::string &sValue, std::string
214
215
215
216
// ----------------------------------------------------------------------
216
217
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
+
217
252
bool WSJCppValidators::isValidBase64 (const std::string &sValue , std::string &sError ) {
218
253
int nSize = sValue .size ();
219
254
if (nSize % 4 != 0 ) {
@@ -490,11 +525,10 @@ bool WSJCppValidatorDateTime::isValid(const std::string &sValue, std::string &sE
490
525
// ----------------------------------------------------------------------
491
526
// WSJCppValidatorURL
492
527
493
- /*
494
528
WSJCppValidatorURL::WSJCppValidatorURL ()
495
529
: WSJCppValidatorStringBase(" url" ) {
496
530
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}$" );
498
532
}
499
533
500
534
// ----------------------------------------------------------------------
@@ -507,26 +541,13 @@ bool WSJCppValidatorURL::isValid(const std::string &sValue, std::string &sError)
507
541
if (sValue .find (" ." ) == std::string::npos) {
508
542
sError = " Must contain at least one dot" ;
509
543
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 )) {
526
547
return false ;
527
548
}
528
549
529
- int nStartPos = nPosProtocol + 3;
550
+ int nStartPos = sProtocol . length () + 3 ;
530
551
std::string sAuthorityAddressPath = " " ;
531
552
for (int i = nStartPos; i < sValue .size (); i++) {
532
553
char c = sValue [i];
@@ -535,7 +556,7 @@ bool WSJCppValidatorURL::isValid(const std::string &sValue, std::string &sError)
535
556
}
536
557
sAuthorityAddressPath += c;
537
558
}
538
- std::string sQuery = sValue.substr(nPosProtocol + 3 + sAuthorityAddressPath.size());
559
+ std::string sQuery = sValue .substr (sProtocol . length () + 3 + sAuthorityAddressPath .size ());
539
560
std::string sAddressAndPath = sAuthorityAddressPath ;
540
561
541
562
int nPosAuthority = sAuthorityAddressPath .find (" @" );
@@ -545,9 +566,10 @@ bool WSJCppValidatorURL::isValid(const std::string &sValue, std::string &sError)
545
566
sAddressAndPath = sAuthorityAddressPath .substr (nPosAuthority + 1 );
546
567
}
547
568
if (sAuthority != " " ) {
548
- sError = "TODO check username and password sAuthority= [" + sAuthority + "]";
569
+ // sError = "TODO check username and password sAuthority= [" + sAuthority + "]";
549
570
// -.~_!$&'()*+,;=:%40:80%2f::::::
550
- return false;
571
+ // WSJCppLog::warn(TAG, sError);
572
+ // return false;
551
573
}
552
574
std::string sAddress = sAddressAndPath ;
553
575
std::string sPath = " " ;
@@ -562,13 +584,20 @@ bool WSJCppValidatorURL::isValid(const std::string &sValue, std::string &sError)
562
584
return false ;
563
585
}
564
586
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 ;
571
597
}
598
+ }
599
+
600
+ if (std::regex_match (sAddress , m_rxLikeIPv4Format)) {
572
601
if (!WSJCppValidators::isValidIPv4 (sAddress , sError )) {
573
602
return false ;
574
603
}
@@ -590,19 +619,28 @@ bool WSJCppValidatorURL::isValid(const std::string &sValue, std::string &sError)
590
619
}
591
620
592
621
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
+ }
595
629
}
596
630
597
631
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
+ }
600
639
}
601
640
602
- sError = "sAddressAndPath=[" + sAddressAndPath + "], , sAddress=[" + sAddress + "]";
641
+ // sError = "sAddressAndPath=[" + sAddressAndPath + "], , sAddress=[" + sAddress + "]";
603
642
return true ;
604
643
}
605
- */
606
644
607
645
// ----------------------------------------------------------------------
608
646
// WSJCppValidatorBase64
0 commit comments