So you can do this with a combination of a web proxy and an ICAP server. I'm most familiar with Squid Proxy & GreasySpoon for the ICAP. I am using Squid v3.2.1 & GreasySpoon 1.0.8.
- Squid: http://www.squid-cache.org/
- GreasySpoon: Defunct! :( The idea will be the same on other ICAP servers just with different impl's. It used to be hosted on sourceforge so maybe the Wayback Machine will have it. Not sure.
Squid Configuration
Anyways, configure Squid to act as a standard cache. Here's a sample configuration. For more details on proper squid configuration, check out the extensive docs out there. The section you care about for this question is the # ICAP Configurations
at the bottom.
cache_effective_user squid
cache_effective_group squid
acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7 # RFC 4193 local private network range
acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines
acl SSL_ports port 443
acl Safe_ports port 80 # http
acl Safe_ports port 81 # http - public
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
acl Safe_ports port 70 # gopher
acl Safe_ports port 210 # wais
acl Safe_ports port 1025-65535 # unregistered ports
acl Safe_ports port 280 # http-mgmt
acl Safe_ports port 488 # gss-http
acl Safe_ports port 591 # filemaker
acl Safe_ports port 777 # multiling http
acl CONNECT method CONNECT
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access deny blocksites
http_access allow localnet
http_access allow localhost
http_access deny all
http_port 3128 transparent
# Leave coredumps in the first cache dir
core dump_dir /var/cache/squid
# ICAP Configurations
icap_enable on
icap_preview_enable on
icap_service service_req reqmod_precache bypass=0 icap://127.0.0.1:1344/reqmod
adaptation_access service_req allow all
icap_service service_resp respmod_precache bypass=0 icap://127.0.0.1:1344/respmod
adaptation_access service_resp allow all
Note that the ICAP server I used was on the same host as the squid proxy so I used 127.0.0.1. If your proxy & ICAP are on different hosts then be sure to swap out the loopback for the other server's IP or server name.
ICAP Configuration
This is the easy part.
Again, I'm using the now defunct "Greasy Spoon" ICAP server. I found it to be very straight forward and did what I wanted with minimal headache. Also, while other options are available, I use the Java plugin capabilities.
For the case of GreasySpoon, I just created a small Java script (not javascript, although that is possible with many ICAP servers) that targets the HTTP request and added the needed header (Note the leading comments provide meta-data to the GS server. Probably not needed for others):
//-------------------------------------------------------------------
// ==ServerScript==
// @name Add_Header
// @status on
// @description
// @include .*
// @exclude
// @order 0
// ==/ServerScript==
// --------------------------------------------------------------------
public void main(HttpMessage httpMessage){
// Add the "my-header" header with a value of "test.server.com"
httpMessage.addHeader("my-header", "test.server.com");
}
This adds the my-header header element into every request.