diff --git a/restemplate.lpr b/restemplate.lpr index 3a25665..be4a40e 100644 --- a/restemplate.lpr +++ b/restemplate.lpr @@ -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,26 +224,39 @@ 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; - while not EOF(data) do - begin - ReadLn(data, line); - if commandMode and (line <> '') and not ProcessCommand(line) then - commandMode := False; + try + 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); + if not commandMode then + content.Add(line); + end; + + if url <> '' then + ProcessCall(url); + + finally + sessionIni.Free; + parser.Free; + http.Free; + content.Free; + + CloseFile(data); end; - - sessionIni.Free; - parser.Free; - http.Free; - content.Free; - - CloseFile(data); end.