-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTP11.pas
More file actions
288 lines (270 loc) · 6.82 KB
/
Copy pathHTTP11.pas
File metadata and controls
288 lines (270 loc) · 6.82 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//VPSERVER 3.0 - HTTP Server
//Copyright (C) 2009 Ivanov Viktor
//
//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
unit HTTP11;
interface
uses STypes;
{$DEFINE DYNMIME}
procedure ProccessRequest(const Socket:TSocket;const Params:PParams);
{$IFDEF DYNMIME}
procedure MimeInit;
{$ENDIF}
{$IFDEF DYNMIME}
procedure MimeDestroy;
{$ENDIF}
implementation
uses SUtils, SLog, WSUtils;
{$I HTTP11T.inc}
{$I HTTP11F.inc}
{$I HTTP11T2.inc}
function ReadRequest(var Sock:TextFile;out Data:THTTPRequest):Boolean;
var
CLine:LongWord;
function ParseLine(const Line:String):Boolean;
var
t1, t2:String;
t3:LongWord;
Header:THeader;
begin
ParseLine:=false;
if Line='' then
Exit;
if CLine=1 then
begin
t1:=Line;
t3:=1;
while Pos(' ', t1)<>0 do
begin
inc(t3);
t1[Pos(' ', t1)]:='?';
end;
if t3<>3 then
begin
SetStatusCode(Data.PreError, 400, 'Bad Request');
Exit;
end;
t1:=Line;
t2:=copy(t1, 1, Pos(' ', t1)-1);
Delete(t1, 1, Pos(' ', t1));
Data.Method:=IndexStr(t2, Methods);
t2:=copy(t1, 1, Pos(' ', t1)-1);
Delete(t1, 1, Pos(' ', t1));
Data.URI:=t2;
Data.HTTPVer:=IndexStr(t1, Versions);
if Data.HTTPVer<0 then
begin
SetStatusCode(Data.PreError, 505, 'HTTP Version Not Supported');
Exit;
end;
if Data.Method<0 then
begin
SetStatusCode(Data.PreError, 501, 'Not Implemented');
Exit;
end;
end
else
for t3:=1 to AHEAD do
if HeaderType[t3-1] in [htGeneral, htRequest, htEntity] then
if HeaderMatch[t3-1](Line, Header.Data) then
begin
Header.Header:=t3-1;
AddHeader(Data.Headers, Header);
break;
end;
ParseLine:=true;
end;
var
s:String;
begin
CLine:=0;
FillChar(Data, sizeof(Data), 0);
repeat
{$I-}
readln(Sock, s);
{$I+}
inc(CLine);
if (IOResult<>0) or ((s<>'') and (not ParseLine(s))) then
begin
ReadRequest:=false;
Exit;
end;
until s='';
ReadRequest:=true;
end;
function FormErrorBody(var Response:THTTPResponse):Boolean;
var
Code:TStatusCode;
F:^File;
begin
FormErrorBody:=false;
if (not (Byte(Response.StatusCode.StatusCode div 100) in [4, 5])) or (EntityBodyLength(Response.Body)<>0) then
Exit;
Code:=Response.StatusCode;
FreeResponse(Response);
FillChar(Response, sizeof(Response), 0);
Response.StatusCode:=Code;
New(F);
GetDir(0, Response.URI);
Response.URI:=Response.URI+{$IFDEF MSWINDWOS}'\'{$ELSE}'/'{$ENDIF}+'error'+IntToStr(Code.StatusCode)+'.html';
Assign(F^, Response.URI);
{$I-}
FileMode:=0;
Reset(F^, 1);
{$I+}
if IOResult<>0 then
with Response.Body.Stat do
begin
Dispose(F);
Response.URI:='';
Response.Body.IsDyn:=false;
Size:=Length(Code.Description);
GetMem(Buf, Size);
move(PChar(Code.Description)^, Buf^, Size);
end
else
begin
SetFileBody(Pointer(F), Response.Body);
Response.Body.Dyn.DynSeek:=nil;
end;
FormErrorBody:=true;
end;
procedure FillResponse(var Request:THTTPRequest;out Response:THTTPResponse;out KAlive:Boolean);
var
Header:PHeader;
NewH:THeader;
I, C:LongWord;
begin
FillChar(Response, sizeof(Response), 0);
Response.StatusCode:=Request.PreError;
for I:=1 to Request.Headers.Count do
begin
if Response.StatusCode.StatusCode<>0 then
break;
Header:=GetHeader(Request.Headers, I);
if Header=nil then
continue;
HeaderProcess[Header^.Header](Header^.Data, Response);
end;
if Response.StatusCode.StatusCode=0 then
MethodProcess[Request.Method](Request, Response);
if Response.StatusCode.StatusCode=0 then
SetResponseError(Response, 400, 'Bad Request');
I:=0;
while true do
begin
if FormErrorBody(Response) then
I:=0;
C:=Response.StatusCode.StatusCode;
while I<>AHEAD do
begin
if HeaderType[I] in [htGeneral, htResponse, htEntity] then
begin
if HeaderAdd[I](NewH.Data, Response) then
begin
NewH.Header:=I;
AddHeader(Response.Headers, NewH);
end;
if Response.StatusCode.StatusCode<>C then
begin
C:=0;
inc(I);
break;
end;
end;
inc(I);
end;
if C<>0 then
break;
end;
KAlive:=Response.KAlive;
end;
procedure SendResponse(var Sock:TextFile;var Data:THTTPResponse);
var
I, RealSent:LongWord;
Header:PHeader;
Buf:TEntityBodyStat;
TimeOut:Boolean;
begin
{$I-}
writeln(Sock, 'HTTP/1.1 ', Data.StatusCode.StatusCode, ' ', Data.StatusCode.Description);
for I:=1 to Data.Headers.Count do
begin
Header:=GetHeader(Data.Headers, I);
if Header=nil then
continue;
HeaderPrint[Header^.Header](Sock, Header^.Data);
end;
writeln(Sock);
Flush(Sock);
if not Data.NoBody then
begin
if not Data.Body.IsDyn then
begin
Buf:=Data.Body.Stat;
FillChar(Data.Body.Stat, sizeof(Data.Body.Stat), 0);
end;
while true do
begin
if Data.Body.IsDyn then
Buf:=Data.Body.Dyn.DynFunct(Data.Body.Dyn.Data);
if Buf.Size=0 then
break;
I:=SendBuf(GetFileSockRecord(Sock)^, Buf.Buf^, Buf.Size, TimeOut, RealSent);
FreeMem(Buf.Buf);
if (I<>0) or (RealSent<>Buf.Size) or TimeOut then
break;
Buf.Size:=0;
end;
end;
{$I+}
IOResult;
end;
procedure ProccessRequest(const Socket:TSocket;const Params:PParams);
var
KAlive, TimeOut:Boolean;
SockIn, SockOut:TextFile;
Request:THTTPRequest;
Response:THTTPResponse;
p:Pointer;
begin
AssignSocket(SockIn, Socket, Params^.RWait);
AssignSocket(SockOut, Socket, Params^.RWait);
FileMode:=0;
Reset(SockIn);
Rewrite(SockOut);
repeat
KAlive:=false;
FillChar(Request, sizeof(Request), 0);
FillChar(Response, sizeof(Response), 0);
try
if ReadRequest(SockIn, Request) or (Request.PreError.StatusCode<>0) then
begin
FillResponse(Request, Response, KAlive);
SendResponse(SockOut, Response);
end;
if Response.ToRecv<>0 then
begin
GetMem(p, Response.ToRecv);
RecvBuf(GetFileSockRecord(SockIn)^, p^, Response.ToRecv, TimeOut, Response.ToRecv);
FreeMem(p);
if TimeOut then
KAlive:=false;
end;
finally
FreeRequest(Request);
FreeResponse(Response);
end;
until not KAlive;
Close(SockIn);
Close(SockOut);
end;
end.