2015-09-17 11:47:04 +02:00
|
|
|
{
|
|
|
|
This file is part of restemplate.
|
|
|
|
|
|
|
|
Copyright (C) 2015 Andreas Schneider
|
|
|
|
|
|
|
|
restemplate is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
sqlvision is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with restemplate. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
}
|
|
|
|
|
|
|
|
program restemplate;
|
|
|
|
|
|
|
|
uses
|
2015-09-17 14:09:37 +02:00
|
|
|
SysUtils, Classes, strutils, JTemplate, httpsend, IniFiles,
|
|
|
|
ssl_openssl;
|
2015-09-17 11:47:04 +02:00
|
|
|
|
|
|
|
var
|
|
|
|
data: TextFile;
|
|
|
|
line: String;
|
|
|
|
parser: TJTemplateParser;
|
|
|
|
http: THTTPSend;
|
|
|
|
method: String;
|
|
|
|
content: TStringList;
|
|
|
|
commandMode: Boolean;
|
2015-09-17 14:09:37 +02:00
|
|
|
configDir, templateDir: String;
|
|
|
|
templateFile, templateName: String;
|
|
|
|
sessionIni: TIniFile;
|
2015-09-17 11:47:04 +02:00
|
|
|
|
|
|
|
procedure CmdAskUser(AName: String);
|
|
|
|
var
|
2015-09-17 14:09:37 +02:00
|
|
|
value, default: String;
|
2015-09-17 11:47:04 +02:00
|
|
|
begin
|
2015-09-17 14:09:37 +02:00
|
|
|
Write(AName);
|
|
|
|
default := sessionIni.ReadString(templateName, AName, '');
|
|
|
|
if default <> '' then
|
|
|
|
Write(' [', default, ']: ')
|
|
|
|
else
|
|
|
|
Write(': ');
|
|
|
|
|
2015-09-17 11:47:04 +02:00
|
|
|
ReadLn(value);
|
2015-09-17 14:09:37 +02:00
|
|
|
|
|
|
|
if value = '' then
|
|
|
|
value := default;
|
|
|
|
|
2015-09-17 11:47:04 +02:00
|
|
|
parser.Fields.Add(AName, value);
|
2015-09-17 14:09:37 +02:00
|
|
|
|
|
|
|
sessionIni.WriteString(templateName, AName, value);
|
2015-09-17 11:47:04 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure CmdHeader(AHeader: String);
|
|
|
|
begin
|
|
|
|
parser.Content := AHeader;
|
|
|
|
parser.Replace;
|
|
|
|
http.Headers.Add(parser.Content);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure CmdCall(AURL: String);
|
|
|
|
var
|
|
|
|
s: String;
|
|
|
|
begin
|
|
|
|
parser.Content := AURL;
|
|
|
|
parser.Replace;
|
|
|
|
writeln('Calling ', parser.Content);
|
|
|
|
|
|
|
|
if content.Count > 0 then
|
|
|
|
content.SaveToStream(http.Document);
|
|
|
|
|
|
|
|
if http.HTTPMethod(method, parser.Content) then
|
|
|
|
begin
|
|
|
|
writeln;
|
|
|
|
writeln('Status: ', http.ResultCode);
|
|
|
|
writeln;
|
|
|
|
writeln('Headers:');
|
|
|
|
for s in http.Headers do
|
|
|
|
writeln(' ', s);
|
|
|
|
writeln;
|
|
|
|
content.LoadFromStream(http.Document);
|
|
|
|
writeln(content.Text);
|
|
|
|
end else
|
2015-09-17 14:09:37 +02:00
|
|
|
begin
|
|
|
|
ExitCode := 2;
|
|
|
|
writeln;
|
|
|
|
writeln('FAILED! Last Socket Error: ', http.Sock.SocksLastError);
|
|
|
|
end;
|
2015-09-17 11:47:04 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function ProcessCommand(ALine: String): Boolean;
|
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
if AnsiStartsStr('Ask ', ALine) then
|
|
|
|
begin
|
|
|
|
Result := True;
|
|
|
|
CmdAskUser(Copy(ALine, 5, Length(ALine)));
|
|
|
|
end else
|
|
|
|
if AnsiStartsStr('Header ', ALine) then
|
|
|
|
begin
|
|
|
|
Result := True;
|
|
|
|
CmdHeader(Copy(ALine, 8, Length(ALine)));
|
|
|
|
end else
|
|
|
|
if AnsiStartsStr('Method ', ALine) then
|
|
|
|
begin
|
|
|
|
Result := True;
|
|
|
|
method := Copy(ALine, 8, Length(ALine));
|
|
|
|
end else
|
|
|
|
if AnsiStartsStr('Call ', ALine) then
|
|
|
|
begin
|
|
|
|
Result := True;
|
|
|
|
CmdCall(Copy(ALine, 6, Length(ALine)));
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2015-09-17 14:09:37 +02:00
|
|
|
procedure ListProfiles;
|
|
|
|
var
|
|
|
|
sr: TSearchRec;
|
|
|
|
begin
|
|
|
|
Writeln('Known profiles:');
|
|
|
|
if FindFirst(templateDir + '*.rest', faAnyFile, sr) = 0 then
|
|
|
|
begin
|
|
|
|
repeat
|
|
|
|
writeln(' ', Copy(sr.Name, 1, Length(sr.Name) - 5));
|
|
|
|
until FindNext(sr) <> 0;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2015-09-17 11:47:04 +02:00
|
|
|
begin
|
2015-09-17 14:09:37 +02:00
|
|
|
configDir := GetAppConfigDir(False);
|
|
|
|
templateDir := configDir + 'templates' + PathDelim;
|
|
|
|
|
|
|
|
templateFile := templateDir + ParamStr(1) + '.rest';
|
|
|
|
|
|
|
|
if ParamCount <> 1 then
|
|
|
|
begin
|
|
|
|
Writeln('Usage: ', ExtractFileName(ParamStr(0)), ' <profile or file>');
|
|
|
|
Writeln;
|
|
|
|
ListProfiles;
|
|
|
|
Halt(0);
|
|
|
|
end;
|
|
|
|
|
|
|
|
if FileExists(ParamStr(1)) then
|
|
|
|
begin
|
|
|
|
AssignFile(data, ParamStr(1));
|
|
|
|
templateName := ExtractFileName(ParamStr(1));
|
|
|
|
if AnsiEndsStr('.rest', templateName) then
|
|
|
|
templateName := Copy(templateName, 1, Length(templateName) - 5);
|
|
|
|
end else
|
|
|
|
if FileExists(templateFile) then
|
|
|
|
begin
|
|
|
|
AssignFile(data, templateFile);
|
|
|
|
templateName := ParamStr(1);
|
|
|
|
end else
|
|
|
|
begin
|
|
|
|
writeln('Template not found!');
|
|
|
|
Halt(1);
|
|
|
|
end;
|
2015-09-17 11:47:04 +02:00
|
|
|
Reset(data);
|
|
|
|
|
2015-09-17 14:09:37 +02:00
|
|
|
sessionIni := TIniFile.Create(configDir + 'session.ini');
|
|
|
|
|
2015-09-17 11:47:04 +02:00
|
|
|
parser := TJTemplateParser.Create;
|
|
|
|
http := THTTPSend.Create;
|
|
|
|
content := TStringList.Create;
|
|
|
|
|
|
|
|
commandMode := True;
|
|
|
|
|
|
|
|
while not EOF(data) do
|
|
|
|
begin
|
|
|
|
ReadLn(data, line);
|
|
|
|
if commandMode and (line <> '') and not ProcessCommand(line) then
|
|
|
|
commandMode := False;
|
|
|
|
|
|
|
|
if not commandMode then
|
|
|
|
content.Add(line);
|
|
|
|
end;
|
|
|
|
|
2015-09-17 14:09:37 +02:00
|
|
|
sessionIni.Free;
|
2015-09-17 11:47:04 +02:00
|
|
|
parser.Free;
|
|
|
|
http.Free;
|
|
|
|
content.Free;
|
|
|
|
|
|
|
|
CloseFile(data);
|
|
|
|
end.
|
|
|
|
|