2
0

Quick.OAuth.Utils.pas 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. unit Quick.OAuth.Utils;
  2. interface
  3. type
  4. TRequestMethod = (rmGET, rmPOST);
  5. function EncodeURL (const aURL: string): string;
  6. procedure OpenURL (const aURL: string);
  7. function GetDomain (const aURL: string): string;
  8. function GetPort (const aURL: string): integer;
  9. function GetMethodFromRequest (const aRequest: string): TRequestMethod;
  10. function GetCleanRequest (const aRequest: string): string;
  11. implementation
  12. uses
  13. {$IFDEF MSWINDOWS}
  14. WinApi.ShellAPI,
  15. Winapi.Windows,
  16. {$ENDIF}
  17. System.SysUtils,
  18. System.Types, System.Classes, System.StrUtils;
  19. {$I QuickLib.INC}
  20. function EncodeURL (const aURL: string): string;
  21. var
  22. bArray: TBytes;
  23. c: Char;
  24. b: byte;
  25. begin
  26. result:='';
  27. bArray := TEncoding.UTF8.GetBytes(aURL);
  28. for b in bArray do
  29. begin
  30. c := Chr(b);
  31. case c of
  32. 'A'..'Z',
  33. 'a'..'z',
  34. '0'..'9',
  35. '-',
  36. '_',
  37. '.': result := result + c
  38. else
  39. result:= result + '%' + IntToHex(Ord(b),2);
  40. end;
  41. end;
  42. end;
  43. procedure OpenURL (const aURL: string);
  44. begin
  45. {$IFDEF MSWINDOWS}
  46. ShellExecute(0,PChar('open'),PChar(aURL),PChar(''),PChar(''), SW_NORMAL);
  47. {$ELSE}
  48. raise Exception.Create('OpenURL not implemented yet');
  49. {$ENDIF}
  50. end;
  51. function GetDomain (const aURL: string): string;
  52. {$IFDEF DELPHIXE3_UP}
  53. var
  54. parts: TStringDynArray;
  55. begin
  56. result:=aURL;
  57. parts:=aURL.Split([':']);
  58. if Length(parts) > 1 then
  59. result:=parts[1].Replace('/', '');
  60. {$ELSE}
  61. begin
  62. raise Exception.Create('Not implemented yet');
  63. {$ENDIF}
  64. end;
  65. function GetPort (const aURL: string): integer;
  66. {$IFDEF DELPHIXE3_UP}
  67. var
  68. parts: TStringDynArray;
  69. begin
  70. result:=80;
  71. parts:=aURL.Split([':']);
  72. if Length(parts) > 1 then
  73. TryStrToInt(parts[High(parts)].Replace('/', ''), result);
  74. {$ELSE}
  75. begin
  76. raise Exception.Create('Not implemented yet');
  77. {$ENDIF}
  78. end;
  79. function GetMethodFromRequest (const aRequest: string): TRequestMethod;
  80. begin
  81. result:=rmGET;
  82. if aRequest.Trim = '' then
  83. Exit;
  84. case IndexStr(aRequest.Split([' '])[0].ToUpper, ['GET', 'POST']) of
  85. 0: result:=rmGET;
  86. 1: result:=rmPOST;
  87. end;
  88. end;
  89. function GetCleanRequest (const aRequest: string): string;
  90. var
  91. parts: TStringDynArray;
  92. begin
  93. result:=aRequest;
  94. if aRequest.Trim = '' then
  95. Exit;
  96. parts:=aRequest.Split([' ']);
  97. if Length(parts) > 1 then
  98. result:=parts[1];
  99. end;
  100. end.