* * *

Author Topic: HTTP redirector app  (Read 1098 times)

tudi_x

  • Hero Member
  • *****
  • Posts: 524
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 1.8.4 64b on MX Linux "Horizon"

Leledumbo

  • Hero Member
  • *****
  • Posts: 7958
  • 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.  

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus