LUrlParser.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Lightweight URL & URI parser (RFC 1738, RFC 3986)
  3. * https://github.com/corporateshark/LUrlParser
  4. *
  5. * The MIT License (MIT)
  6. *
  7. * Copyright (C) 2015 Sergey Kosarevsky (sk@linderdaum.com)
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in all
  17. * copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. */
  27. #include "LUrlParser.h"
  28. #include <algorithm>
  29. #include <cstring>
  30. #include <stdlib.h>
  31. // check if the scheme name is valid
  32. static bool IsSchemeValid( const std::string& SchemeName )
  33. {
  34. for ( auto c : SchemeName )
  35. {
  36. if ( !isalpha( c ) && c != '+' && c != '-' && c != '.' ) return false;
  37. }
  38. return true;
  39. }
  40. bool LUrlParser::clParseURL::GetPort( int* OutPort ) const
  41. {
  42. if ( !IsValid() ) { return false; }
  43. int Port = atoi( m_Port.c_str() );
  44. if ( Port <= 0 || Port > 65535 ) { return false; }
  45. if ( OutPort ) { *OutPort = Port; }
  46. return true;
  47. }
  48. // based on RFC 1738 and RFC 3986
  49. LUrlParser::clParseURL LUrlParser::clParseURL::ParseURL( const std::string& URL )
  50. {
  51. LUrlParser::clParseURL Result;
  52. const char* CurrentString = URL.c_str();
  53. /*
  54. * <scheme>:<scheme-specific-part>
  55. * <scheme> := [a-z\+\-\.]+
  56. * For resiliency, programs interpreting URLs should treat upper case letters as equivalent to lower case in scheme names
  57. */
  58. // try to read scheme
  59. {
  60. const char* LocalString = strchr( CurrentString, ':' );
  61. if ( !LocalString )
  62. {
  63. return clParseURL( LUrlParserError_NoUrlCharacter );
  64. }
  65. // save the scheme name
  66. Result.m_Scheme = std::string( CurrentString, LocalString - CurrentString );
  67. if ( !IsSchemeValid( Result.m_Scheme ) )
  68. {
  69. return clParseURL( LUrlParserError_InvalidSchemeName );
  70. }
  71. // scheme should be lowercase
  72. std::transform( Result.m_Scheme.begin(), Result.m_Scheme.end(), Result.m_Scheme.begin(), ::tolower );
  73. // skip ':'
  74. CurrentString = LocalString+1;
  75. }
  76. /*
  77. * //<user>:<password>@<host>:<port>/<url-path>
  78. * any ":", "@" and "/" must be normalized
  79. */
  80. // skip "//"
  81. if ( *CurrentString++ != '/' ) return clParseURL( LUrlParserError_NoDoubleSlash );
  82. if ( *CurrentString++ != '/' ) return clParseURL( LUrlParserError_NoDoubleSlash );
  83. // check if the user name and password are specified
  84. bool bHasUserName = false;
  85. const char* LocalString = CurrentString;
  86. while ( *LocalString )
  87. {
  88. if ( *LocalString == '@' )
  89. {
  90. // user name and password are specified
  91. bHasUserName = true;
  92. break;
  93. }
  94. else if ( *LocalString == '/' )
  95. {
  96. // end of <host>:<port> specification
  97. bHasUserName = false;
  98. break;
  99. }
  100. LocalString++;
  101. }
  102. // user name and password
  103. LocalString = CurrentString;
  104. if ( bHasUserName )
  105. {
  106. // read user name
  107. while ( *LocalString && *LocalString != ':' && *LocalString != '@' ) LocalString++;
  108. Result.m_UserName = std::string( CurrentString, LocalString - CurrentString );
  109. // proceed with the current pointer
  110. CurrentString = LocalString;
  111. if ( *CurrentString == ':' )
  112. {
  113. // skip ':'
  114. CurrentString++;
  115. // read password
  116. LocalString = CurrentString;
  117. while ( *LocalString && *LocalString != '@' ) LocalString++;
  118. Result.m_Password = std::string( CurrentString, LocalString - CurrentString );
  119. CurrentString = LocalString;
  120. }
  121. // skip '@'
  122. if ( *CurrentString != '@' )
  123. {
  124. return clParseURL( LUrlParserError_NoAtSign );
  125. }
  126. CurrentString++;
  127. }
  128. bool bHasBracket = ( *CurrentString == '[' );
  129. // go ahead, read the host name
  130. LocalString = CurrentString;
  131. while ( *LocalString )
  132. {
  133. if ( bHasBracket && *LocalString == ']' )
  134. {
  135. // end of IPv6 address
  136. LocalString++;
  137. break;
  138. }
  139. else if ( !bHasBracket && ( *LocalString == ':' || *LocalString == '/' ) )
  140. {
  141. // port number is specified
  142. break;
  143. }
  144. LocalString++;
  145. }
  146. Result.m_Host = std::string( CurrentString, LocalString - CurrentString );
  147. CurrentString = LocalString;
  148. // is port number specified?
  149. if ( *CurrentString == ':' )
  150. {
  151. CurrentString++;
  152. // read port number
  153. LocalString = CurrentString;
  154. while ( *LocalString && *LocalString != '/' ) LocalString++;
  155. Result.m_Port = std::string( CurrentString, LocalString - CurrentString );
  156. CurrentString = LocalString;
  157. }
  158. // end of string
  159. if ( !*CurrentString )
  160. {
  161. Result.m_ErrorCode = LUrlParserError_Ok;
  162. return Result;
  163. }
  164. // skip '/'
  165. if ( *CurrentString != '/' )
  166. {
  167. return clParseURL( LUrlParserError_NoSlash );
  168. }
  169. CurrentString++;
  170. // parse the path
  171. LocalString = CurrentString;
  172. while ( *LocalString && *LocalString != '#' && *LocalString != '?' ) LocalString++;
  173. Result.m_Path = std::string( CurrentString, LocalString - CurrentString );
  174. CurrentString = LocalString;
  175. // check for query
  176. if ( *CurrentString == '?' )
  177. {
  178. // skip '?'
  179. CurrentString++;
  180. // read query
  181. LocalString = CurrentString;
  182. while ( *LocalString && *LocalString != '#' ) LocalString++;
  183. Result.m_Query = std::string( CurrentString, LocalString - CurrentString );
  184. CurrentString = LocalString;
  185. }
  186. // check for fragment
  187. if ( *CurrentString == '#' )
  188. {
  189. // skip '#'
  190. CurrentString++;
  191. // read fragment
  192. LocalString = CurrentString;
  193. while ( *LocalString ) LocalString++;
  194. Result.m_Fragment = std::string( CurrentString, LocalString - CurrentString );
  195. CurrentString = LocalString;
  196. }
  197. Result.m_ErrorCode = LUrlParserError_Ok;
  198. return Result;
  199. }