LUrlParser.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #pragma once
  28. #include <string>
  29. namespace LUrlParser
  30. {
  31. enum LUrlParserError
  32. {
  33. LUrlParserError_Ok = 0,
  34. LUrlParserError_Uninitialized = 1,
  35. LUrlParserError_NoUrlCharacter = 2,
  36. LUrlParserError_InvalidSchemeName = 3,
  37. LUrlParserError_NoDoubleSlash = 4,
  38. LUrlParserError_NoAtSign = 5,
  39. LUrlParserError_UnexpectedEndOfLine = 6,
  40. LUrlParserError_NoSlash = 7,
  41. };
  42. class clParseURL
  43. {
  44. public:
  45. LUrlParserError m_ErrorCode;
  46. std::string m_Scheme;
  47. std::string m_Host;
  48. std::string m_Port;
  49. std::string m_Path;
  50. std::string m_Query;
  51. std::string m_Fragment;
  52. std::string m_UserName;
  53. std::string m_Password;
  54. clParseURL()
  55. : m_ErrorCode( LUrlParserError_Uninitialized )
  56. {}
  57. /// return 'true' if the parsing was successful
  58. bool IsValid() const { return m_ErrorCode == LUrlParserError_Ok; }
  59. /// helper to convert the port number to int, return 'true' if the port is valid (within the 0..65535 range)
  60. bool GetPort( int* OutPort ) const;
  61. /// parse the URL
  62. static clParseURL ParseURL( const std::string& URL );
  63. private:
  64. explicit clParseURL( LUrlParserError ErrorCode )
  65. : m_ErrorCode( ErrorCode )
  66. {}
  67. };
  68. } // namespace LUrlParser