/varnish-device-detect

varnish sub-routine to detect device (mobile, tablet, desktop)

Primary LanguagePythonMIT LicenseMIT

Varnish Device Detect

Varnish subroutine to detect device (mobile, tablet and desktop)

Quick Start

Table of contents

  1. Installation
  2. Getting started
  3. Usage
  4. Run the test
  5. Contributing
  6. Requirements
  7. Authors
  8. License

Installation

git clone http://git.thebigbrainscompany.com/borisguery/varnish-device-detect.git varnish/routines/folder/or/whatever

Getting started

Varnish subroutines are a really simple way to somewhat modularize your VCL, they are actually not functions as they don't take arguments nor they return anything.

To make use of this subroutine, you have to call it from inside the vcl_recv subroutine. It will set two new header in the req object which allow you to conditionally work with it.

Usage

include "routines/devicedetect.vcl";

sub vcl_recv {
    call devicedetect;

    if (req.http.X-UA-Device-Type == "mobile") {
        error 701 regsub(req.http.host, "^www\.(.*)", "http://mobile.\1") + req.url; //Capture everything after the www. and place it after a http://
    }
}

sub vcl_error {
    // Redirect to mobile host
    if (obj.status == 701) {
        set obj.http.Location = obj.response;
        set obj.status = 302;

        return(deliver);
    }
}

Run the test

make test

Contributing

  1. Take a look at the list of issues.
  2. Fork
  3. Write a test (for either new feature or bug)
  4. Make a PR

Requirements

  • Varnish 3.x
  • Python 2.x

Authors

License

Varnish Device Dectect is licensed under the MIT License - see the LICENSE file for details