-
Notifications
You must be signed in to change notification settings - Fork 78
/
unPHPModule.pas
161 lines (122 loc) · 3.76 KB
/
unPHPModule.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
unit unPHPModule;
interface
uses
SysUtils, Classes, diocp_ex_httpServer, Contnrs, CnObjectPool
, superobject, PHPCommon, php4delphi, zendAPI,
phpAPI, PHPCustomLibrary, utils_strings, diocp_ex_httpClient;
const
PHP_SESSIONID = 'PHPSESSID';
type
TPHPModule = class(TDataModule)
FPHPEngine: TPHPEngine;
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
procedure FPHPEngineScriptError(Sender: TObject; AText: AnsiString;
AType: TPHPErrorType; AFileName: AnsiString; ALineNo: Integer);
private
{ Private declarations }
FHttpServer: TDiocpHttpServer;
FDbModuleList: TObjectList;
FCrs: TCriticalSection;
//请求响应
procedure OnHttpSvrRequest(pvRequest: TDiocpHttpRequest);
public
{ Public declarations }
procedure start(iPort: Integer);
procedure stop();
end;
var
PHPModule: TPHPModule;
implementation
{$R *.dfm}
{ TDataModule1 }
procedure TPHPModule.DataModuleCreate(Sender: TObject);
begin
FPHPEngine.StartupEngine;
FHttpServer := TDiocpHttpServer.Create(nil);
FHttpServer.Name := 'Ys_AppServer';
FHttpServer.OnDiocpHttpRequest := OnHttpSvrRequest;
end;
procedure TPHPModule.DataModuleDestroy(Sender: TObject);
begin
FHttpServer.SafeStop();
FHttpServer.Free;
FPHPEngine.ShutdownAndWaitFor;
end;
procedure TPHPModule.FPHPEngineScriptError(Sender: TObject; AText: AnsiString;
AType: TPHPErrorType; AFileName: AnsiString; ALineNo: Integer);
begin
//
end;
procedure TPHPModule.OnHttpSvrRequest(pvRequest: TDiocpHttpRequest);
var
S, tmp, sessionID: string;
L, iCount, iStart, iStop: integer;
FileName: string;
FilePath: string;
ws: UTF8String;
fs: TStringList;
psvPhpOne: TpsvPHP;
i: Integer;
lvSession: TDiocpHttpDValueSession;
list: TStrings;
strUrl: string;
httpClient: TDiocpHttpClient;
begin
if pvRequest.RequestURI = '/favicon.ico' then
Exit;
// pvRequest.Response.Header.ForceByName('Access-Control-Allow-Origin').AsString := '*';
// pvRequest.Response.Header.ForceByName('Access-Control-Allow-Methods').AsString := 'POST, GET, OPTIONS, DELETE';
// pvRequest.Response.Header.ForceByName('Access-Control-Allow-Headers').AsString := 'x-requested-with,content-type';
FilePath := ExtractFilePath(ParamStr(0));
if pvRequest.RequestURI = '/' then
strUrl := '/test.php'
else
strUrl := pvRequest.RequestURI;
FileName := FilePath + 'app'+strUrl;
psvPhpOne := TpsvPHP.Create(nil);
iCount := pvRequest.Header.Count;
//1. 获取delphi的PHPSESSID到PHP
with psvPhpOne.Variables.Add do
begin
Name := PHP_SESSIONID;
Value := pvRequest.GetCookie(PHP_SESSIONID);
end;
with psvPhpOne.Variables.Add do
begin
Name := 'f';
Value := 'test_test';
end;
try
S := psvPhpOne.Execute(FileName);
for i := 0 to psvPhpOne.Headers.Count - 1 do //psvPHP1.Headers.GetHeaders
tmp := tmp + psvPhpOne.Headers[i].Header + '<br>';
//2. 获取到PHP中的sessionID,并通过delphi到浏览器
sessionID := psvPhpOne.Headers[2].Header;
sessionID := GetStrValueOfName(sessionID, 'Set-Cookie', [':', ' '], [' ']); //截取变量值
sessionID := GetStrValueOfName(sessionID, 'PHPSESSID', ['=', ' '], [';', ' ']);
//3. 把sessionID值设置到浏览器
with pvRequest.Response.AddCookie do
begin
Name := PHP_SESSIONID;
Value := sessionID;
end;
tmp := tmp + pvRequest.Header.AsString + '<br>';
tmp := tmp + sessionID + '<br>';
finally
psvPhpOne.Free;
end;
pvRequest.Response.ResponseCode := 200;
pvRequest.Response.WriteString(s + '<hr>' + AnsiToUtf8(tmp), False); //
pvRequest.ResponseEnd();
end;
procedure TPHPModule.start(iPort: Integer);
begin
FHttpServer.Port := iPort;
Self.FHttpServer.Active := True;
end;
procedure TPHPModule.stop;
begin
Self.FHttpServer.SafeStop;
end;
end.