Mod http cache

From FreeSWITCH Wiki
Jump to: navigation, search


mod_http_cache (HTTP GET files with caching)

mod_http_cache allows one to make a HTTP GET request to cache a document and HTTP PUT to store a document. The primary use case is to download and cache audio files from a web server.

mod_http_cache will only respect the max-age cache-control directive to determine when a cached file becomes stale.

mod_http_cache offers the http_cache:// read and write file interfaces. See these examples for usage.

mod_http_cache offers the http:// and https:// read and write file interfaces if the "enable-file-formats" param is set to true. DO NOT LOAD mod_httapi IF YOU USE THIS PARAM.

mod_http_cache supports GET/PUT to Amazon S3 private buckets. See configuration below.

mod_httapi is also available which offers an HTTP read/write file interface.


To use mod_http_cache:

Tell FreeSWITCH to compile in this module by editing modules.conf in /usr/src/freeswitch/trunk and adding:


Now go recompile FreeSWITCH.

make install

Tell FreeSWITCH to actually use the module when running by adding the module to modules.conf.xml in /usr/local/freeswitch/conf/autoload_configs:

<load module="mod_http_cache"/>

For HTTPS support, grab the latest CA certs from and install in /usr/local/freeswitch/conf. An older copy is also available in freeswitch/src/mod/applications/mod_http_cache/conf/cacert.pem.


The conf/autoload_configs/http_cache.conf.xml file contains the configuration.

<configuration name="http_cache.conf" description="HTTP GET cache">
    <!-- set to true if you want to enable http:// and https:// formats.  Do not use if mod_httapi is also loaded -->
    <param name="enable-file-formats" value="false"/>
    <!-- maximum size of cache -->
    <param name="max-urls" value="10000"/>
    <!-- location of cached files -->
    <param name="location" value="$${base_dir}/http_cache"/>
    <!-- if not specified by cache-control max-age directive, this value will be used
         to expire cached files -->
    <param name="default-max-age" value="86400"/>
    <!-- size of the prefetch thread pool -->
    <param name="prefetch-thread-count" value="8"/>
    <!-- size of the prefetch request queue -->
    <param name="prefetch-queue-size" value="100"/>
    <!-- absolute path to CA bundle file -->
    <param name="ssl-cacert" value="$${base_dir}/conf/cacert.pem"/>
    <!-- verify certificates -->
    <param name="ssl-verifypeer" value="true"/>
    <!-- verify host name matches certificate -->
    <param name="ssl-verifyhost" value="true"/>

    <!-- amazon s3 security credentials -->
    <profile name="s3">
        <!-- 20 character key identifier -->
        <!-- 40 character secret -->
      <!-- optional list of domains that this profile will automatically be applied to -->
      <!-- if you wish to apply the s3 credentials to a domain not listed here, then use
           {profile=s3}http://foo.s3... -->
        <domain name=""/>




Download a URL to the cache in a background thread. Does not wait for the download to finish.



Download a document to the cache. Returns the name of the cached file suitable for use by FreeSWITCH APPs or "-ERR" on error.



Same as http_get except if the document is missing from the cache or stale, "-ERR download" is returned. i.e. this API call checks the cache only. "-ERR" is returned on all other errors.



PUT a file. This can be used to PUT a file like a session recording to a webserver.

  http_put /tmp/1df4a12708bcea.wav


Empty the cache.



Play a .wav file hosted by a webserver

While this example below will work, it is probably best to download all the .wav files needed by a call in a background thread early in the call using http_prefetch. Subsequent calls to http_get will then just grab the cached file. That way, delays in downloading will be less likely to cause pauses in playback.

playback (enable-file-formats=false)

<action application="playback" data="http_cache://"/>

playback (enable-file-formats=true)

<action application="playback" data=""/>

Record a .wav file to a webserver

mod_http_cache can record using the http/https/http_cache file formats. The file is PUT when the recording is completed and the file is closed.

session record (enable-file-formats=false)

<action application="record" data="http_cache://${uuid}.wav"/>

session record (enable-file-formats=true)

<action application="record" data="${uuid}.wav"/>