William Shallum

Apache as proxy (or: how to download podcasts with wget and get it into iTunes)

Posted May 10 2011, 22:58 by William Shallum [updated May 11 2011, 00:35]

Problem: iTunes can’t resume downloads of a certain podcast.
Solution: Download it with wget.

Problem: Podcasts downloaded with wget can’t be imported into iTunes “properly”.
Solution: Use a proxy to fool iTunes into thinking it’s downloading the podcast from its original source.

Apache has a proxy feature (mod_proxy + mod_proxy_http). Using the fact that proxy requests are subject to virtual-host matching, we can handle some hosts using Apache itself instead of proxying out. Within the hosts, we can proxy all directories but a few using the ProxyPass directive with ‘!’. Thus, we can be a proxy for most URLs yet handle some requests using our local versions of those resources.

For the Apache config file below, assume we are trying to download http://download.example.com/pod/cast/xyz.mp3, we will download it manually to /Users/me/pkg/http-proxy/htdocs-podcast so it should be /Users/me/pkg/http-proxy/htdocs-podcast/xyz.mp3.

Apache config file:

ServerRoot "/Users/me/pkg/http-proxy"
Listen 127.0.0.1:8000
PidFile logs/httpd.pid
LockFile logs/accept.lock

# use for aliasing
LoadModule alias_module modules/mod_alias.so

# access log
LoadModule log_config_module modules/mod_log_config.so

# proxying
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so


NameVirtualHost 127.0.0.1:8000

<VirtualHost 127.0.0.1:8000>
  # By default, proxy everything
  ServerName localhost
  DocumentRoot /Users/me/pkg/http-proxy/htdocs
  ProxyRequests on
  TransferLog logs/access_log
</VirtualHost>

<VirtualHost 127.0.0.1:8000>
  ServerName download.example.com
  DocumentRoot /Users/me/pkg/http-proxy/htdocs-podcast
  # if it's under /pod/cast handle it ourselves, else proxy to the original server
  ProxyPass /pod/cast !
  ProxyPass / http://download.example.com/
  # This is where to find the files
  Alias /pod/cast /Users/me/pkg/http-proxy/htdocs-podcast
  TransferLog logs/access_log_proxy
</VirtualHost>

Set the proxy in System Preferences, and click download. iTunes will immediately download the podcast from the local copy.