source: trunk/Packages/CoolWeb/WebServer/UHTTPServerTCP.pas

Last change on this file was 5, checked in by chronos, 12 years ago
  • Added: Required packages.
File size: 4.9 KB
Line 
1unit UHTTPServerTCP;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, UHTTPServer, UTCPServer, SpecializedList, SynaUtil;
9
10type
11
12 { THTTPServerTCP }
13
14 THTTPServerTCP = class(THTTPServer)
15 private
16 FMaxConnection: Integer;
17 procedure HandleClient(Sender: TObject);
18 public
19 Socket: TTCPServer;
20 RequestHandlerList: TRequestHandlerList;
21 constructor Create(AOwner: TComponent); override;
22 destructor Destroy; override;
23 procedure Run; override;
24 published
25 property MaxConnection: Integer read FMaxConnection write FMaxConnection;
26 end;
27
28procedure Register;
29
30implementation
31
32procedure Register;
33begin
34 RegisterComponents('CoolWeb', [THTTPServerTCP]);
35end;
36
37procedure THTTPServerTCP.HandleClient(Sender: TObject);
38var
39 Line: string;
40 LineIndex: Integer;
41 LineParts: TListString;
42 HandlerData: THTTPHandlerData;
43 I: Integer;
44begin
45 with TTCPClientThread(Sender), Socket do begin
46 WriteLn('Used thrads ' + IntToStr(Parent.ThreadPool.UsedCount) + '. Client connected from ' + GetRemoteSinIP);
47
48 HandlerData := THTTPHandlerData.Create;
49 with HandlerData do try
50 Server := Self;
51
52 Response := THTTPResponse.Create;
53 Response.Headers.Add('Server', Name);
54 Request := THTTPRequest.Create;
55 LineIndex := 0;
56 try
57 LineParts := TListString.Create;
58 repeat
59 Line := RecvString(10000);
60 if (LineIndex = 0) then begin
61 LineParts.Explode(Line, ' ', StrToStr);
62 if (LineParts.Count >= 3) then begin
63 Request.Method := LineParts[0];
64 if Pos('?', LineParts[1]) > 0 then begin
65 Request.Query.Parse(Copy(LineParts[1], Pos('?', LineParts[1]) + 1, Length(LineParts[1])));
66 Request.Path.Explode(Copy(LineParts[1], 1, Pos('?', LineParts[1]) - 1), '/', StrToStr);
67 end else begin
68 Request.Path.Explode(LineParts[1], '/', StrToStr);
69 Request.Query.Clear;
70 end;
71 end;
72 end else begin
73 LineParts.Explode(Line, ' ', StrToStr, 2);
74 if (LineParts.Count = 2) and (LineParts[0][Length(LineParts[0])] = ':') then begin
75 LineParts[0] := Copy(LineParts[0], 1, Length(LineParts[0]) - 1);
76 Request.Headers.Add(LineParts[0], LineParts[1]);
77 //WriteLn(Line);
78 end;
79 end;
80 Inc(LineIndex);
81 until Line = '';
82 finally
83 LineParts.Free;
84 end;
85
86 // Process cookies
87 if Request.Headers.SearchKey('Cookie') <> -1 then
88 Request.Cookies.Parse(Request.Headers.Values['Cookie']);
89
90 if (Request.Method = 'POST') and (Request.Headers.SearchKey('Content-Type') <> -1) then begin
91 if (Request.Headers.Values['Content-Type'] = 'application/x-www-form-urlencoded') and
92 (Request.Headers.SearchKey('Content-Length') <> -1) then begin
93 SetLength(Line, StrToInt(Request.Headers.Values['Content-Length']));
94 RecvBuffer(Pointer(Line[1]), Length(Line));
95 Request.Post.Parse(Line);
96 end;
97 end;
98
99 // Load session variables
100 if Assigned(SessionStorage) then
101 SessionStorage.Load(HandlerData);
102
103 Response.Content.Clear;
104 Response.Headers.Add('Content-Type', 'text/html');
105
106 if Assigned(OnRequest) then OnRequest(HandlerData)
107 else raise EEmptyHTTPHandler.Create(SEmptyHTTPHandler);
108
109 // Store session variables
110 if Assigned(SessionStorage) then
111 SessionStorage.Save(HandlerData);
112
113 with Response do begin
114 SendString('HTTP/1.0 200 OK'#13#10);
115 Headers.Add('Content-Length', IntToStr(Content.Size));
116 Headers.Add('Connection', 'close');
117 Headers.Add('Date', RFC822DateTime(Now));
118
119 // Handle cookies
120 for I := 0 to Cookies.Count - 1 do
121 Headers.Add('Set-Cookie', Cookies.Names[I] + '=' + Cookies.ValueFromIndex[I]);
122 // + ';path=/;expires=' + RFC822DateTime(Now);
123
124 // Send headers
125 for I := 0 to Headers.Count - 1 do begin
126 WriteLn(Headers.Keys[I] + ': ' + Headers.Items[I].Value + #13#10);
127 SendString(Headers.Keys[I] + ': ' + Headers.Items[I].Value + #13#10);
128 end;
129 SendString(#13#10);
130 SendBuffer(Content.Memory, Content.Size);
131 SendString(#13#10);
132 end;
133 finally
134 Free;
135 end;
136 end;
137end;
138
139
140{ THTTPServerTCP }
141
142constructor THTTPServerTCP.Create(AOwner: TComponent);
143begin
144 inherited;
145 MaxConnection := 10000;
146 Socket := TTCPServer.Create;
147 Socket.OnClientConnect := HandleClient;
148 RequestHandlerList := TRequestHandlerList.Create;
149end;
150
151destructor THTTPServerTCP.Destroy;
152begin
153 Socket.Free;
154 RequestHandlerList.Free;
155 inherited Destroy;
156end;
157
158procedure THTTPServerTCP.Run;
159begin
160 inherited Run;
161 WriteLn('HTTP Server started in TCP mode.');
162 WriteLn('Listen on ' + Socket.Address + ':' + IntToStr(Socket.Port));
163 WriteLn('Press any key to terminate...');
164 Socket.ThreadPool.TotalCount := MaxConnection;
165 Socket.Active := True;
166 ReadLn;
167 WriteLn('Exiting');
168end;
169
170end.
171
Note: See TracBrowser for help on using the repository browser.