Created
February 1, 2011 10:59
-
-
Save jaytaph/805710 to your computer and use it in GitHub Desktop.
Sample file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
backend default { | |
.host = "127.0.0.1"; | |
.port = "80"; | |
} | |
// Intialization | |
C{ | |
#include "/usr/src/wurfl/wurfl.h" | |
int is_mobile; | |
}C | |
// simple subroutine that fetches the useragent, checks if its a mobile device and | |
// sets the backend-request header accordingly | |
sub detectmobile { | |
C{ | |
char *ua = VRT_GetHdr(sp, HDR_REQ, "\013User-Agent:"); | |
is_mobile = wurfl_ismobile(ua); | |
VRT_SetHdr(sp, HDR_BEREQ, "\011X-Mobile:", (is_mobile == 1)?"yes":"no", vrt_magic_string_end); | |
}C | |
} | |
// Miss, pass and pipe all make a request to the backend | |
sub vcl_miss { | |
call detectmobile; | |
return(fetch); | |
} | |
sub vcl_pipe { | |
call detectmobile; | |
return(pipe); | |
} | |
sub vcl_pass { | |
call detectmobile; | |
return(pass); | |
} | |
// Deliver sets the header to the response to the client as well | |
sub vcl_deliver { | |
C{ | |
VRT_SetHdr(sp, HDR_RESP, "\011X-Mobile:", (is_mobile == 1)?"yes":"no", vrt_magic_string_end); | |
}C | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ($_SERVER['HTTP_X_MOBILE'] == "yes") { | |
print "Hello mobile!"; | |
} else { | |
print "Hello!"; | |
} | |
print "<pre>"; | |
print_r ($_SERVER); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include <libxml/parser.h> | |
#include <libxml/tree.h> | |
#include <libxml/xpath.h> | |
#include <libxml/xpathInternals.h> | |
#include "wurfl.h" | |
xmlDoc *doc = NULL; | |
// Load document | |
int wurfl_init(char *filename) { | |
doc = xmlReadFile(filename, NULL, 0); | |
return (doc != NULL); | |
} | |
int wurfl_ismobile(char *useragent) { | |
xmlXPathContextPtr xpathCtx; | |
xmlXPathObjectPtr xpathObj; | |
// Lazy loading. Varnish does not support an init() function | |
if (doc == NULL) wurfl_init("/etc/wurfl.xml"); | |
// Create new context | |
xpathCtx = xmlXPathNewContext(doc); | |
if (xpathCtx == NULL) return -1; | |
// Create xpath expression | |
char *expr = malloc(strlen(useragent) + 40); | |
sprintf(expr, "//devices/device[@user_agent='%s']", (unsigned char *)useragent); | |
xpathObj = xmlXPathEvalExpression(expr, xpathCtx); | |
free(expr); | |
// Nothing found? | |
if (xpathObj == NULL) { | |
xmlXPathFreeContext(xpathCtx); | |
return -2; | |
} | |
// Fetch the number of nodes found | |
xmlNodeSetPtr nodes = xpathObj->nodesetval; | |
int size = (nodes) ? nodes->nodeNr : 0; | |
// Something has been found... | |
xmlXPathFreeObject(xpathObj); | |
xmlXPathFreeContext(xpathCtx); | |
return (size > 0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __WURFL_H__ | |
#define __WURFL_H__ | |
int wurfl_ismobile(char *useragent); | |
#endif // __WURFL_H__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment