Skip to content

Commit

Permalink
Use std.client to parse user agents, and ensure that the client is at…
Browse files Browse the repository at this point in the history
… least version 5 if it's ios
  • Loading branch information
marcuswestin committed Aug 16, 2011
1 parent 197b6cc commit 3fc4431
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/server/Server.js
Expand Up @@ -7,7 +7,8 @@ var Class = require('std/Class'),
fs = require('fs'),
path = require('path'),
time = require('std/time'),
Response = require('./Response')
Response = require('./Response'),
client = require('std/client')

module.exports = Class(function() {

Expand Down Expand Up @@ -57,11 +58,11 @@ module.exports = Class(function() {
/* Request handlers
******************/
this._handleRootRequest = function(match, req, res) {
var userAgent = req.headers['user-agent']
var reqClient = client.parse(req.headers['user-agent'])
var location =
userAgent.match('iPad') ? 'iphone'
: userAgent.match('iPhone') ? 'iphone'
: userAgent.match('iPod') ? 'iphone'
reqClient.isIPad ? 'iphone'
: reqClient.isIPhone ? 'iphone'
: reqClient.isIPod ? 'iphone'
: 'iphone'

res
Expand All @@ -75,6 +76,11 @@ module.exports = Class(function() {
this._handleClientHTMLRequest = function(match, req, res) {
var clientPathBase = this._clients[match[1]]
if (!clientPathBase) { return this._sendError(res, 404) }
var reqClient = client.parse(req.headers['user-agent'])
if (reqClient.isIOS && reqClient.osVersion < 5.0) {
this._sendJSError(res, 'Aw shucks. You need iOS 5.0')
return
}
this._sendStaticFile(this._currentVersionLink, 'client/' + clientPathBase, 'html', res)
}

Expand Down Expand Up @@ -102,6 +108,12 @@ module.exports = Class(function() {
this._getStaticPath = function(version, pathBase, extension) {
return this._staticDir+'/'+version+'/'+pathBase+'.'+extension
}

this._sendJSError = function(res, message) {
res
.cache(3 * time.hours)
.send('<script>alert("'+message+'")</script>', 'text/html')
}
})

// function handleImageRequest(req, res) {
Expand Down

0 comments on commit 3fc4431

Please sign in to comment.