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