I just have a problem for now is that i can't set the Headers from an THTTPClient object to TIdHTTPResponseInfo RawHeaders
This is my code for the On GET event
Code: Select all
void __fastcall TMain::IdHTTPServer1CommandGet(TIdContext *AContext, TIdHTTPRequestInfo *ARequestInfo,
TIdHTTPResponseInfo *AResponseInfo)
{
unique_ptr<THTTPClient> BackendRequest(THTTPClient::Create());
BackendRequest->ConnectionTimeout = 1000;
BackendRequest->ResponseTimeout = 1000;
TNameValueArray Headers;
unique_ptr<TStringList> str(new TStringList);
if (ARequestInfo->RawHeaders->Count > 0)
{
ARequestInfo->RawHeaders->ConvertToStdValues(str.get());
Headers.Length = ARequestInfo->RawHeaders->Count;
TNameValuePair *H = &Headers[0];
for (int i = 0; i < ARequestInfo->RawHeaders->Count; ++i, *H++)
{
H->Name = str->Names[i];
H->Value = str->ValueFromIndex[i];
}
}
AResponseInfo->ContentStream = new TMemoryStream;
BackendRequest->CustomHeaders["X-Forwarded-For"] = AContext->Connection->Socket->Binding->PeerIP;
TStringDynArray PortsArr = SplitString(Ports->Text, ";");
String *Port = &PortsArr[0];
int Count = PortsArr.High;
ProxyRequest :
{
try
{
_di_IHTTPResponse Response = BackendRequest->Execute(ARequestInfo->Command,
"http://localhost:" + *Port + ARequestInfo->URI,
ARequestInfo->PostStream,
AResponseInfo->ContentStream,
Headers);
AResponseInfo->CustomHeaders->Clear();
forDynArray(Head, Response->Headers)
{
AResponseInfo->CustomHeaders->Values[(*Head).Name] = (*Head).Value; //this causes the client to the error
}
AResponseInfo->ResponseNo = Response->StatusCode;
AResponseInfo->ResponseText = Response->StatusText;
}
catch (ENetHTTPClientException &NetHttpEx)
{
*Port++;
if (Port <= &PortsArr[Count])
goto ProxyRequest;
else
throw Exception("All backend servers are down");
}
}
}
//---------------------------------------------------------------------------
Code: Select all
unit ProxyResponse;
interface
uses
System.Classes, System.SysUtils, IdCustomHTTPServer, System.Net.HttpClient, System.Net.URLClient;
type
TIdHTTPResponseInfo = class(IdCustomHTTPServer.TIdHTTPResponseInfo)
private
FBackEndResponse: IHTTPResponse;
protected
public
procedure SetHeaders; override;
property BackEndResponse: IHTTPResponse read FBackEndResponse write FBackEndResponse;
end;
//---------------------------------------------------------------------------
implementation
//---------------------------------------------------------------------------
procedure TIdHTTPResponseInfo.SetHeaders;
var
Header: TNetHeader;
begin
inherited SetHeaders;
for Header in FBackEndResponse.Headers do
begin
FRawHeaders.Values[Header.Name] := Header.Value;
end;
FRawHeaders.AddValue('MyName','MIDO');
end;
//---------------------------------------------------------------------------
end.
If i used the interposer class instead nothing happens and i made sure to add this to the code:
Code: Select all
#include "ProxyResponse.hpp"
#define TIdHTTPResponseInfo Proxyresponse::TIdHTTPResponseInfo
//inside OnGet event
AResponseInfo->BackEndResponse = Response;
Thanks in advance