11#include " wsjcpp_validators.h"
22#include < arpa/inet.h>
3+ #include < wsjcpp_core.h>
34
45bool 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+
217252bool 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- /*
494528WSJCppValidatorURL::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
0 commit comments