RTC Forums

Subscription => Support => Topic started by: Dany on March 03, 2014, 04:44:45 PM



Title: Did i do this redirect correctly?
Post by: Dany on March 03, 2014, 04:44:45 PM
Hello!

I have a TRtcDualDataServerLink in my server module. One TRtcHTTPServer for port 80 and one for port 443. I need to redirect all http requests to the secure server. This seems to work but since i have only worked with TRtcDataProviders before i'd like to ask if i did it in a correct and optimal way. I have connected the OnHTTPRequestAccepted event of the TRtcHTTPServer listening on port 80 to the following code:

Code:
procedure TData_Server_Logon.ServerHTTPRequestAccepted(Sender: TRtcConnection);
var
  Server: TRtcDataServer absolute Sender;
  lAltPortNum: string;
begin
  inherited;
  if ServerHTTPS.ServerPort <> '443' then
    lAltPortNum := ':' + ServerHTTPS.ServerPort;

  Server.Response.Status(301, 'Moved Permanently');
  Server.Response.Value['Location'] := 'https://' + Server.Request.Host + lAltPortNum + '/';
  Server.Disconnect;
end;

Thank you in advance,

/Dany


Title: Re: Did i do this redirect correctly?
Post by: D.Tkalcec (RTC) on March 03, 2014, 05:48:53 PM
You should not use any events directly on the TRtcHttpServer component for reading or sending any data out. These events can ONLY be used for logging and monitoring purposes.

If you want to redirect traffic from your plain HTTP Server to the SSL Server, use a TRtcDataProvider component attached to the plain HTTP Server and implement the OnCheckRequest event on that TRtcDataProvider component like this:

const
  // URL of your HTTPS Server, without "/"
  MySecureServerLocation = 'https://www.myhostaddr.com';
var
  Server: TRtcDataServer absolute Sender;
begin
  // Accept the request
  Server.Accept;
  // Set Response code
  Server.Response.Status(301, 'Moved Permanently');
  // Set the Server Address and Port part of the "Location" Response header by using a fixed parameter,
  // instead of using the Request.Host parameter received from your Clients, but
  // use the full URI received from the Client instead of forwarding all requests to the room URI "/"
  Server.Response['Location'] := MySecureServerLocation + Server.Request.URI;
  // Call "Write" and not "Disconnect"
  // If you do NOT call "Write", nothing will be sent to the Client (no redirect happening).
  Server.Write;
end;

Please note that by doing this, ALL requests sent from Web Browsers to the plain HTTP Server will be redirected to your HTTPS (SSL) Server, without exception. If you only want specific requests to be redirected, use the Server.Request property inside the event (above) to decide which requests you want to accept and redirect, and which requests to ignore.

Best Regards,
Danijel Tkalcec


Title: Re: Did i do this redirect correctly?
Post by: Dany on March 04, 2014, 08:56:58 AM
You should not use any events directly on the TRtcHttpServer component for reading or sending any data out. These events can ONLY be used for logging and monitoring purposes.

I had that feeling... Thank you for your excellent support.

/Dany