Mobile redirect question

Geoff Simmons geoff at uplex.de
Tue Mar 29 16:16:17 CEST 2011


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

On 03/29/11 02:56 PM, Maikel - Songteksten.nl wrote:
>
> if ( req.http.user-agent ~
>
"(.*Android.*|.*Blackberry.*|.*BlackBerry.*|.*Blazer.*|.*Ericsson.*|.*htc.*|.*Huawei.*|.*iPhone.*|.*iPod.*|.*MobilePhone.*|.*Motorola.*|.*nokia.*|.*Novarra.*|.*O2.*|.*Palm.*|.*Samsung.*|.*Sanyo.*|.*Smartphone.*|.*SonyEricsson.*|.*Symbian.*|.*Toshiba.*|.*Treo.*|.*vodafone.*|.*Xda.*|^Alcatel.*|^Amoi.*|^ASUS.*|^Audiovox.*|^AU-MIC.*|^BenQ.*|^Bird.*|^CDM.*|^DoCoMo.*|^dopod.*|^Fly.*|^Haier.*|^HP.*iPAQ.*|^i-mobile.*|^KDDI.*|^KONKA.*|^KWC.*|^Lenovo.*|^LG.*|^NEWGEN.*|^Panasonic.*|^PANTECH.*|^PG.*|^Philips.*|^portalmmm.*|^PPC.*|^PT.*|^Qtek.*|^Sagem.*|^SCH.*|^SEC.*|^Sendo.*|^SGH.*|^Sharp.*|^SIE.*|^SoftBank.*|^SPH.*|^UTS.*|^Vertu.*|.*Opera.Mobi.*|.*Windows.CE.*|^ZTE.*)"
>
>         && req.http.host ~ "(www.example.nl|www.example.be)"
>
>         ) {
>
>         set req.http.newhost = regsub(req.http.host, "(www)?\.(.*)",
> "http://m.\2");
>         error 750 req.http.newhost;

This is not what you asked about, but you're almost certainly losing a
lot of performance with that regex.

I would suggest that you put the check against req.http.host first (so
that it doesn't bother with the pattern match when it doesn't have to),
and above all, get rid of the leading and trailing .*'s in the regex.

When you match a string against a regex like ".*foobar.*", it first
matches the leading .* all the way until the end of the input string,
overlooking any instance of "foobar" it sees along the way. Then it
starts backtracking until it can match ".*f", then ".*fo", and so on. If
it can match ".*foobar", it then takes the trouble to match the trailing
.* to the end of the string. This is happening for all of the alternates
in your regex until a match is found.

phk's advice at VUG was: write your regex so that you can prove as
quickly as possible that an input string *doesn't* match it.


Best,
Geoff
- -- 
** * * UPLEX - Nils Goroll Systemoptimierung

Schwanenwik 24
22087 Hamburg

Tel +49 40 2880 5731
Mob +49 176 636 90917
Fax +49 40 42949753

http://uplex.de
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (SunOS)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBCAAGBQJNkemwAAoJEOUwvh9pJNURLpEP/3t9+FTbTpzXmx9cts+2NOcr
VJl4L+bi4+b+Zkn46yMgZjwLOyRWhqYQBfFozqKVOIX204jH0kzuHFqWwkF3luNO
B6izenicK6jhQurdUsS4CTJ6j74yCgX1Jks9DC4Z3pLcwwY/swzJsV2ldKx9rqWJ
sr6NJv8WxSz1Pb/i5BP6C7veplmO/rdKLZxzll5b7Qic6LicrRGG5ny0exUdysce
q2ZlcAXCe7//7Ha7+1wlw5xXb3APcx96SB4bh+ASS63KgHevKkSwPOFFUdv//FzG
xLEc/U5MqKjiFErx0IPzPZrD+E2Yf0PIVqRc9L7eL9g5SSJEfqwmFCrHucLYpmpW
tpdDepflnUv1p7IkY0boNabds8AhRPAIAtYi6o8+mjGQBtGVdOuQ4SbH2+2OOMLz
x3YtAcjUjhArg8gUSjZRPIXfbHHy6vSiYKBPBqJUPmUBRw009VsCNO1F58b1sXJb
YVmX6cKwfcq97GFqBBp+CsKEyJsJaubIReXQOoJTRrPVHqqn4aWmYOk1UHQiN5Pw
iXNFJQbV/bh0jrgk5W5bcOS+WyvwSQm0aK8SMsHnVY4gh73md6kcD1rybc3S5doC
+WEBLMdJWteDOZMQDBVgXXUmwmzHk8eX+6cRQKe4IaXXgRSoGOAZiwy+6G7a3YYk
klz7Nm1RM3vs6EmQfvoY
=kwSJ
-----END PGP SIGNATURE-----




More information about the varnish-misc mailing list