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