From 76cd3854625c3c7dca4e041a51902228cc84a2b9 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Sun, 4 Oct 2015 14:31:32 +0200 Subject: [PATCH] Added proxy support --- README.md | 7 +++++++ URestemplateApp.pas | 44 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5dec0fc..872f829 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,13 @@ Once the first none-empty line is unrecognized (no command found), the parser wi `Compress[ed]` : Adds support for gzip,deflate compression to the connection. +`Proxy ` +: Enables HTTP proxy support. + +`ProxyAuth ` +: Sets authentication for the HTTP proxy. + Same syntax as `BasicAuth`. + `Call ` : This prepares the actual call by providing the URL to be called. Variables in the form of `@` are replaced accordingly. diff --git a/URestemplateApp.pas b/URestemplateApp.pas index 0767d88..204b8e2 100644 --- a/URestemplateApp.pas +++ b/URestemplateApp.pas @@ -69,6 +69,8 @@ type procedure CmdBasicAuth(AData: String); procedure CmdHighlight(AData: String); procedure CmdFormField(AData: String); + procedure CmdProxy(AData: String); + procedure CmdProxyAuth(AData: String); procedure ProcessCall(AURL: String); //Helper procedure WriteHelp; @@ -236,6 +238,16 @@ begin Result := True; FHttp.Compressor := TIdCompressorZLib.Create(FHttp); end else + if AnsiStartsStr('Proxy ', ALine) then + begin + Result := True; + CmdProxy(Copy(ALine, 7, Length(ALine))); + end else + if AnsiStartsStr('ProxyAuth', ALine) then + begin + Result := True; + CmdProxy(Copy(ALine, 10, Length(ALine))); + end else if AnsiStartsStr('Call ', ALine) then begin Result := True; @@ -292,11 +304,7 @@ begin varName := Trim(Copy(AHeader, 1, i - 1)); varValue := Trim(Copy(AHeader, i + 1, Length(AHeader))); - //Special handling is required for Indy - {if SameText(varName, 'Accept-Encoding') then - FHttp.Request.AcceptEncoding := varValue - else} - FRequest.RawHeaders.AddValue(varName, varValue); + FRequest.RawHeaders.AddValue(varName, varValue); end; procedure TRestemplateApplication.CmdBasicAuth(AData: String); @@ -334,6 +342,32 @@ begin FFormFields.AddFormField(Copy(AData, 1, i - 1), Copy(AData, i + 1, Length(AData))); end; +procedure TRestemplateApplication.CmdProxy(AData: String); +var + i: Integer; +begin + i := 1; + while (i < Length(AData)) and (AData[i] <> ' ') do + Inc(i); + + FHttp.ProxyParams.ProxyServer := Copy(AData, 1, i - 1); + FHttp.ProxyParams.ProxyPort := StrToInt(Copy(AData, i + 1, Length(AData))); +end; + +procedure TRestemplateApplication.CmdProxyAuth(AData: String); +var + separator: Char; + i: Integer; +begin + separator := AData[1]; + i := 2; + while (i < Length(AData)) and (AData[i] <> separator) do + Inc(i); + + FHttp.ProxyParams.ProxyUsername := Copy(AData, 2, i - 2); + FHttp.ProxyParams.ProxyPassword := Copy(AData, i + 1, Length(AData)); +end; + procedure TRestemplateApplication.ProcessCall(AURL: String); var s: String;