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

Last change on this file was 151, checked in by chronos, 9 months ago
File size: 4.2 KB
Line 
1unit HTTPServerCGI;
2
3interface
4
5uses
6 Classes, SysUtils, HTTPServer, IOStream, Generics.Collections;
7
8type
9
10 { THTTPServerCGI }
11
12 THTTPServerCGI = class(THTTPServer)
13 public
14 EnvVars: TStringList;
15 procedure Run; override;
16 constructor Create(AOwner: TComponent); override;
17 destructor Destroy; override;
18 procedure ServerInfo(HandlerData: THTTPHandlerData); override;
19 end;
20
21procedure Register;
22
23
24implementation
25
26resourcestring
27 SEnvironmentVariables = 'Environment variables:';
28
29procedure Register;
30begin
31 RegisterComponents('CoolWeb', [THTTPServerCGI]);
32end;
33
34{ THTTPServerCGI }
35
36constructor THTTPServerCGI.Create(AOwner: TComponent);
37begin
38 inherited;
39 EnvVars := TStringList.Create;
40end;
41
42destructor THTTPServerCGI.Destroy;
43begin
44 EnvVars.Free;
45 inherited;
46end;
47
48procedure THTTPServerCGI.Run;
49var
50 I: Integer;
51 HandlerData: THTTPHandlerData;
52 InputStream: TIOStream;
53 Line: string;
54 Buffer: string;
55 Count: Integer;
56 Item: TPair<string, string>;
57begin
58 HandlerData := THTTPHandlerData.Create;
59 with HandlerData do try
60 // Load headers
61 try
62 InputStream := TIOStream.Create(iosInput);
63 SetLength(Buffer, 1000);
64 repeat
65 Count := InputStream.Read(Buffer[1], Length(Buffer));
66 if Count > 0 then Request.Content.Write(Buffer[1], Count);
67 until Count = 0;
68 finally
69 InputStream.Free;
70 end;
71
72 //repeat
73 // ReadLn(Line);
74 //until Line = '';
75
76 // Load data
77 (*if Request.Headers.IndexOfName('Content-length') <> -1 then
78 try
79 InputStream := TIOStream.Create(iosInput);
80 Request.Content.CopyFrom(InputStream, StrToInt(Request.Headers.Values['Content-length']));
81 finally
82 InputStream.Free;
83 end; *)
84
85 // Load environment variables
86 for I := 0 to GetEnvironmentVariableCount - 1 do begin
87 EnvVars.Add(GetEnvironmentString(I));
88 end;
89
90 // Process cookies
91 if EnvVars.IndexOfName('HTTP_COOKIE') <> -1 then
92 Request.Cookies.Parse(EnvVars.Values['HTTP_COOKIE']);
93
94 // Parse query string
95 if Length(EnvVars.Values['QUERY_STRING']) > 0 then
96 if EnvVars.Values['QUERY_STRING'][Length(EnvVars.Values['QUERY_STRING'])] = '/' then
97 EnvVars.Values['QUERY_STRING'] := Copy(EnvVars.Values['QUERY_STRING'], 1,
98 Length(EnvVars.Values['QUERY_STRING']) - 1);
99 Request.Path.Explode('/', EnvVars.Values['QUERY_STRING']);
100 if Pos('?', EnvVars.Values['REQUEST_URI']) > 0 then begin
101 Request.Query.Parse(Copy(EnvVars.Values['REQUEST_URI'],
102 Pos('?', EnvVars.Values['REQUEST_URI']) + 1,
103 Length(EnvVars.Values['REQUEST_URI'])));
104 end;
105
106 // Load session variables
107 if Assigned(SessionStorage) then
108 SessionStorage.Load(HandlerData);
109
110 // Load post data
111 if EnvVars.IndexOfName('REQUEST_METHOD') <> -1 then begin
112 if EnvVars.Values['REQUEST_METHOD'] = 'POST' then begin
113 Request.Content.Position := 0;
114 Buffer := Request.Content.ReadString;
115 Request.Post.Parse(Buffer);
116 end;
117 end;
118
119 Response.Content.Clear;
120 Response.Headers.Add('Content-type', 'text/html');
121
122 // Execute content handler
123 if Assigned(OnRequest) then OnRequest(HandlerData)
124 else raise EEmptyHTTPHandler.Create(SEmptyHTTPHandler);
125
126 // Store session variables
127 if Assigned(SessionStorage) then
128 SessionStorage.Save(HandlerData);
129
130 with Response do begin
131 // Generate cookies
132 for Item in Cookies do
133 Headers.Add('Set-Cookie', Item.Key + '=' + Item.Value);
134 // + ';path=/;expires=' + RFC822DateTime(Now);
135
136 // Generate headers
137 for Item in Headers do begin
138 WriteLn(Item.Key + ': ' + Item.Value);
139 end;
140
141 WriteLn; // Empty line header separator
142
143 // Emit page content
144 Content.Position := 0;
145 WriteLn(Content.ReadString);
146 end;
147 finally
148 HandlerData.Free;
149 end;
150end;
151
152procedure THTTPServerCGI.ServerInfo(HandlerData: THTTPHandlerData);
153var
154 I: Integer;
155begin
156 inherited;
157 with HandlerData, Response.Content do begin
158 WriteString('<h5>' + SEnvironmentVariables + '</h5>');
159 WriteString('<table border="1">');
160 for I := 0 to EnvVars.Count - 1 do begin
161 WriteString('<tr><td>' + EnvVars.Names[I] + '</td><td>' +
162 EnvVars.ValueFromIndex[I] + '</td></tr>');
163 end;
164 WriteString('</table>');
165 end;
166end;
167
168end.
169
Note: See TracBrowser for help on using the repository browser.