* Added (optional) fcl-web support

* Made fcl-web the default
* Fixed connection handling with payload
This commit is contained in:
Andreas Schneider 2015-09-17 15:10:46 +02:00
parent 19c9ea042f
commit 974c5ef298
1 changed files with 92 additions and 21 deletions

View File

@ -19,16 +19,22 @@
program restemplate;
{.$define use_synapse}
{$define use_fclweb}
uses
SysUtils, Classes, strutils, JTemplate, httpsend, IniFiles,
ssl_openssl;
SysUtils, Classes, strutils, IniFiles,
{$ifdef use_synapse}httpsend, ssl_openssl,{$endif}
{$ifdef use_fclweb}fphttpclient,{$endif}
JTemplate;
var
data: TextFile;
line: String;
parser: TJTemplateParser;
http: THTTPSend;
method: String;
{$ifdef use_synapse}http: THTTPSend;{$endif}
{$ifdef use_fclweb}http: TFPHTTPClient;{$endif}
method, url: String;
content: TStringList;
commandMode: Boolean;
configDir, templateDir: String;
@ -57,20 +63,41 @@ begin
end;
procedure CmdHeader(AHeader: String);
var
i: Integer;
name, value: String;
begin
parser.Content := AHeader;
parser.Replace;
{$ifdef use_synapse}
http.Headers.Add(parser.Content);
{$endif}
{$ifdef use_fclweb}
i := 1;
while (i < Length(AHeader)) and (AHeader[i] <> ':') do
Inc(i);
name := Trim(Copy(AHeader, 1, i - 1));
value := Trim(Copy(AHeader, i + 1, Length(AHeader)));
http.AddHeader(name, value);
{$endif}
end;
procedure CmdCall(AURL: String);
procedure ProcessCall(AURL: String);
var
s: String;
{$ifdef use_fclweb}
stream: TStream;
{$endif}
begin
parser.Content := AURL;
parser.Replace;
writeln('Calling ', parser.Content);
{$ifdef use_synapse}
if content.Count > 0 then
content.SaveToStream(http.Document);
@ -91,6 +118,37 @@ begin
writeln;
writeln('FAILED! Last Socket Error: ', http.Sock.SocksLastError);
end;
{$endif}
{$ifdef use_fclweb}
stream := TMemoryStream.Create;
if content.Count > 0 then
content.SaveToStream(stream);
try
http.HTTPMethod(method, parser.Content, stream, []);
except
on E: Exception do
begin
writeln;
writeln('Failed! ', E.Message);
Halt(1);
end;
end;
writeln;
writeln('Status: ', http.ResponseStatusCode);
writeln;
writeln('Headers:');
for s in http.ResponseHeaders do
writeln(' ', s);
writeln;
stream.Position := 0;
content.LoadFromStream(stream);
writeln(content.Text);
stream.Free;
{$endif}
end;
function ProcessCommand(ALine: String): Boolean;
@ -114,7 +172,7 @@ begin
if AnsiStartsStr('Call ', ALine) then
begin
Result := True;
CmdCall(Copy(ALine, 6, Length(ALine)));
url := Copy(ALine, 6, Length(ALine));
end;
end;
@ -166,11 +224,19 @@ begin
sessionIni := TIniFile.Create(configDir + 'session.ini');
parser := TJTemplateParser.Create;
http := THTTPSend.Create;
content := TStringList.Create;
{$ifdef use_synapse}
http := THTTPSend.Create;
{$endif}
{$ifdef use_fclweb}
http := TFPHttpClient.Create(nil);
{$endif}
commandMode := True;
try
while not EOF(data) do
begin
ReadLn(data, line);
@ -181,11 +247,16 @@ begin
content.Add(line);
end;
if url <> '' then
ProcessCall(url);
finally
sessionIni.Free;
parser.Free;
http.Free;
content.Free;
CloseFile(data);
end;
end.