source: trunk/Demo/Packages/CoolWeb/WebServer/UHTTPServerCGI.pas

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