Recent

Author Topic: HTTP redirector app  (Read 2375 times)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
HTTP redirector app
« on: November 24, 2017, 04:17:25 pm »
hi All,
i am trying to troubleshoot a HTTP POST i am receiving on a socket from an application i did now write.
is there an application i could use to route this HTTP POST from http://127.0.0.1:xxxx to http://y.y.y.y:zzzz?
i would like to send the request to something like https://hookbin.com/bin/vyxDpGP4 to double check my work.
thank you
Lazarus 2.0.2 64b on Debian LXDE 10

Leledumbo

  • Hero Member
  • *****
  • Posts: 8744
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: HTTP redirector app
« Reply #1 on: November 25, 2017, 06:25:19 pm »
Make a proxy app (it's no different from normal HTTP server from client POV), check the HTTP_HOST and REQUEST_URI headers, if they match the URL you want to redirect, then modify them and forward the request to the address you want. Point the browser's proxy to your proxy app. Something like:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2.  
  3. uses
  4.   fphttpapp,httproute,httpdefs,fphttpclient;
  5.  
  6. procedure RedirectIfMatches(ARequest : TRequest; AResponse : TResponse);
  7. var
  8.   Client: TFPHTTPClient;
  9. begin
  10.   Client := TFPHTTPClient.Create(nil);
  11.   try
  12.     if (ARequest.Method = 'POST') and (ARequest.URL = 'http://127.0.0.1/xxxx') then begin
  13.       // redirect request
  14.       AResponse.Content := Client.Post('http://y.y.y.y:zzzz');
  15.     end else begin
  16.       // forward request, note that this discards headers and body that might present in the request
  17.       case ARequest.Method of
  18.         'GET': AResponse.Content := Client.Get(ARequest.URL);
  19.         'POST': AResponse.Content := Client.Post(ARequest.URL);
  20.         // complete the other methods
  21.       end;
  22.     end;
  23.   finally
  24.     Client.Free;
  25.   end;
  26. end;
  27.  
  28. begin
  29.   HTTPRouter.RegisterRoute('*path',@RedirectIfMatches);
  30.   Application.Port := 8888;
  31.   Application.Initialize;
  32.   Application.Run;
  33. end.
  34.  

 

TinyPortal © 2005-2018