RTC Forums
May 15, 2024, 02:00:56 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Did i do this redirect correctly?  (Read 3472 times)
Dany
RTC License++
*****
Posts: 69


« 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
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 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
Logged
Dany
RTC License++
*****
Posts: 69


« Reply #2 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
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.023 seconds with 17 queries.