Quick.Url.Utils.pas 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. { ***************************************************************************
  2. Copyright (c) 2016-2021 Kike Pérez
  3. Unit : Quick.Url.Utils
  4. Description : Common Url utils
  5. Author : Kike Pérez
  6. Version : 2.0
  7. Created : 17/03/2021
  8. Modified : 17/03/2021
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.Url.Utils;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. System.SysUtils,
  26. {$IFDEF NOTUSEINDY}
  27. System.NetEncoding,
  28. {$ELSE}
  29. IdURI,
  30. {$ENDIF}
  31. Quick.Commons;
  32. type
  33. TUrlUtils = class
  34. class function GetProtocol(const aUrl : string) : string;
  35. class function GetHost(const aUrl : string) : string;
  36. class function GetPath(const aUrl : string) : string;
  37. class function GetQuery(const aUrl : string) : string;
  38. class function RemoveProtocol(const aUrl : string) : string;
  39. class function RemoveQuery(const aUrl : string) : string;
  40. class function EncodeUrl(const aUrl : string) : string;
  41. end;
  42. implementation
  43. { TUrlUtils }
  44. class function TUrlUtils.EncodeUrl(const aUrl: string): string;
  45. {$IFDEF NOTUSEINDY}
  46. const
  47. RestUnsafeChars: TURLEncoding.TUnsafeChars = [Ord('"'), Ord(''''), Ord(':'), Ord(';'), Ord('<'), Ord('='), Ord('>'),
  48. Ord('@'), Ord('['), Ord(']'), Ord('^'), Ord('`'), Ord('{'), Ord('}'), Ord('|'), Ord('\'), Ord('?'), Ord('#'),
  49. Ord('&'), Ord('!'), Ord('$'), Ord('('), Ord(')'), Ord(','), Ord('~'), Ord(' '), Ord('*'), Ord('+')];
  50. {$ENDIF}
  51. var
  52. proto : string;
  53. path : string;
  54. query : string;
  55. begin
  56. {$IFDEF NOTUSEINDY}
  57. Result := TNetEncoding.URL.Encode(aUrl);
  58. //Result := StringReplace(aUrl,' ','%20',[rfReplaceAll]);
  59. proto := UrlGetProtocol(aUrl);
  60. if not proto.IsEmpty then proto := proto + '://';
  61. path := UrlGetPath(aUrl);
  62. path := TNetEncoding.Url.EncodePath(path);
  63. query := UrlGetQuery(aUrl);
  64. query := TNetEncoding.Url.EncodeQuery(query);
  65. if not query.IsEmpty then query := '?' + query;
  66. Result := proto + UrlGetHost(aUrl) + path + query;
  67. {$ELSE}
  68. Result := TIdURI.URLEncode(aUrl);
  69. {$ENDIF}
  70. end;
  71. class function TUrlUtils.GetProtocol(const aUrl: string): string;
  72. begin
  73. Result := UrlGetProtocol(aUrl);
  74. end;
  75. class function TUrlUtils.GetHost(const aUrl: string): string;
  76. begin
  77. Result := UrlGetHost(aUrl);
  78. end;
  79. class function TUrlUtils.GetPath(const aUrl: string): string;
  80. begin
  81. Result := UrlGetPath(aUrl);
  82. end;
  83. class function TUrlUtils.GetQuery(const aUrl: string): string;
  84. begin
  85. Result := UrlGetQuery(aUrl);
  86. end;
  87. class function TUrlUtils.RemoveProtocol(const aUrl: string): string;
  88. begin
  89. Result := UrlRemoveProtocol(aUrl);
  90. end;
  91. class function TUrlUtils.RemoveQuery(const aUrl: string): string;
  92. begin
  93. Result := UrlRemoveQuery(aUrl);
  94. end;
  95. end.