60

I need to test some HTTP interaction with a client I'd rather not modify. What I need to test is the behavior of the server when the client's requests include a certain, static header.

I'm thinking the easiest way to run this test is to set up an HTTP proxy that inserts the header on every request. What would be the simplest way to set this up?

dimas
  • 5
  • 2
Logan
  • 1,884
  • 1
  • 12
  • 11

7 Answers7

89

I do something like this in my development environment by configuring Apache on port 80 as a proxy for my application server on port 8080, with the following Apache config:

NameVirtualHost *
<VirtualHost *>
   <Proxy http://127.0.0.1:8080/*>
      Allow from all
   </Proxy>
   <LocationMatch "/myapp">
      ProxyPass http://127.0.0.1:8080/myapp
      ProxyPassReverse http://127.0.0.1:8080/myapp
      Header add myheader "myvalue"
      RequestHeader set myheader "myvalue"   
   </LocationMatch>
</VirtualHost>

See LocationMatch and RequestHeader documentation.

This adds the header myheader: myvalue to requests going to the application server.

Peter
  • 3,067
  • 2
  • 17
  • 18
Peter Hilton
  • 17,211
  • 6
  • 50
  • 75
  • 13
    It will also add myheader: myvalue to the response headers. The full doc on Apache mod_headers: http://httpd.apache.org/docs/current/mod/mod_headers.html – Fred Simon Mar 05 '12 at 10:18
  • 1
    @Fred: So how do we restrict the headers to appear in the proxied request only? – Serge Wautier Mar 29 '13 at 08:42
  • 5
    Well Header is adding to the response, and RequestHeader to the server request behind the proxy. What I did from this is to make sure a settings.xml query never get cached by other proxy, and I did: SetEnvIf Request_URI "\settings.xml$" object_is_settings_xml Header set Cache-Control "no-cache, no-store" env=object_is_settings_xml Header set Expires "Thu, 01 Jan 1970 00:00:00 GMT" env=object_is_settings_xml Header set Pragma "no-cache" env=object_is_settings_xml – Fred Simon Apr 07 '13 at 10:33
  • 1
    If you get an error about `RequestHeader` not being recognized, be sure `mod_headers` is enabled with e.g. `a2enmod headers` (and reload Apache after). – Scott Weldon Apr 06 '20 at 04:11
15

You can also install Fiddler (http://www.fiddler2.com/fiddler2/) which is very easy to install (easier than Apache for example).

After launching it, it will register itself as system proxy. Then open the "Rules" menu, and choose "Customize Rules..." to open a JScript file which allow you to customize requests.

To add a custom header, just add a line in the OnBeforeRequest function:

oSession.oRequest.headers.Add("MyHeader", "MyValue");
Hash
  • 4,647
  • 5
  • 21
  • 39
Nico
  • 13,320
  • 6
  • 32
  • 33
3

i'd try tinyproxy. in fact, the vey best would be to embedd a scripting language there... sounds like a perfect job for Lua, especially after seeing how well it worked for mysqlproxy

Javier
  • 60,510
  • 8
  • 78
  • 126
2

I have had co-workers that have used Burp ("an interactive HTTP/S proxy server for attacking and testing web applications") for this. You also may be able to use Fiddler ("a HTTP Debugging Proxy").

RickyA
  • 15,465
  • 5
  • 71
  • 95
Kevin Hakanson
  • 41,386
  • 23
  • 126
  • 155
1

Use http://www.proxomitron.info and set up the header you want, etc.

Eduardo
  • 7,631
  • 2
  • 30
  • 31
1

Rather than using a proxy, I'm using the Firefox plugin "Modify Headers" to insert headers (in my case, to fake a login using the Single Sign On so I can test as different people).

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
0

If you have ruby on your system, how about a small Ruby Proxy using Sinatra (make sure to install the Sinatra Gem). This should be easier than setting up apache. The code can be found here.

John Bowers
  • 1,695
  • 1
  • 16
  • 26