Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

fix

  • Loading branch information...
commit 7dae8a05a1f079cfae014b5e9ff87305abef3d64 1 parent 47eddb8
Tokuhiro Matsuno authored

Showing 3 changed files with 22 additions and 12 deletions. Show diff stats Hide diff stats

  1. +2 2 README.mkdn
  2. +2 2 eg/server.js
  3. +18 8 mobileagent.js
4 README.mkdn
Source Rendered
@@ -7,11 +7,11 @@ But this module can work on browsers.
7 7 Usage
8 8 ------
9 9
10   - var ma = require('mobileagent.js'),
  10 + var MobileAgent = require('../mobileagent.js'),
11 11 http = require('http');
12 12
13 13 http.createServer(function (req, res) {
14   - var ma = ma.getMobileAgent(req);
  14 + var ma = MobileAgent.getMobileAgent(req);
15 15
16 16 res.writeHead(200, {'Content-Type': 'text/plain'});
17 17 res.end('Hello ' + ma.getCarrierLongName() + '\n');
4 eg/server.js
... ... @@ -1,8 +1,8 @@
1   -var ma = require('../mobileagent.js'),
  1 +var MobileAgent = require('../mobileagent.js'),
2 2 http = require('http');
3 3
4 4 http.createServer(function (req, res) {
5   - var ma = ma.getMobileAgent(req);
  5 + var ma = MobileAgent.getMobileAgent(req);
6 6
7 7 res.writeHead(200, {'Content-Type': 'text/plain'});
8 8 res.end('Hello ' + ma.getCarrierLongName() + '\n');
26 mobileagent.js
@@ -455,19 +455,29 @@ var MobileAgentNonMobile = MobileAgentBase.extend({
455 455 * @param req request object. It is normally node.js' HTTP request.
456 456 **/
457 457 function getMobileAgent(req) {
458   - var ua = req['user-agent'],
459   - carrier = detectCarrier(ua);
460   - switch (carrier) {
  458 + var ua,
  459 + headers,
  460 + carrier;
  461 + if (typeof req === 'object') {
  462 + if ('headers' in req) {
  463 + headers = req['headers'];
  464 + } else {
  465 + headers = req;
  466 + }
  467 + } else {
  468 + headers = { 'user-agent': req }; // plain string
  469 + }
  470 + switch (detectCarrier(headers['user-agent'])) {
461 471 case 'I':
462   - return new MobileAgentDoCoMo(req);
  472 + return new MobileAgentDoCoMo(headers);
463 473 case 'V':
464   - return new MobileAgentSoftBank(req);
  474 + return new MobileAgentSoftBank(headers);
465 475 case 'E':
466   - return new MobileAgentEZWeb(req);
  476 + return new MobileAgentEZWeb(headers);
467 477 case 'H':
468   - return new MobileAgentAirHPhone(req);
  478 + return new MobileAgentAirHPhone(headers);
469 479 default:
470   - return new MobileAgentNonMobile(req);
  480 + return new MobileAgentNonMobile(headers);
471 481 }
472 482 }
473 483

0 comments on commit 7dae8a0

Please sign in to comment.
Something went wrong with that request. Please try again.