Name: js-handler/node_modules/restify/lib/request.js 
1:
// Copyright 2012 Mark Cavage, Inc.  All rights reserved.
2:
 
3:
var http = require('http');
4:
var url = require('url');
5:
var sprintf = require('util').format;
6:
 
7:
var assert = require('assert-plus');
8:
var mime = require('mime');
9:
var Negotatior = require('negotiator');
10:
var uuid = require('node-uuid');
11:
 
12:
var utils = require('./utils');
13:
 
14:
 
15:
 
16:
///--- Globals
17:
 
18:
var Request = http.IncomingMessage;
19:
 
20:
var parseAccept = utils.parseAccept;
21:
var sanitizePath = utils.sanitizePath;
22:
 
23:
 
24:
 
25:
///-- Helpers
26:
 
27:
function negotiator(req) {
28:
        var h = req.headers;
29:
        if (!req._negotatiator) {
30:
                req._negotiator = new Negotatior({
31:
                        headers: {
32:
                                accept: h.accept || '*/*',
33:
                                'accept-encoding': h['accept-encoding'] ||
34:
                                        'identity'
35:
                        }
36:
                });
37:
        }
38:
 
39:
        return (req._negotiator);
40:
}
41:
 
42:
 
43:
///--- API
44:
 
45:
///--- Patches
46:
 
47:
Request.prototype.absoluteUri = function absoluteUri(path) {
48:
        assert.string(path, 'path');
49:
 
50:
        var protocol = this.secure ? 'https://' : 'http://';
51:
        var hostname = this.headers['host'];
52:
        return (url.resolve(protocol + hostname + this.path + '/', path));
53:
};
54:
 
55:
 
56:
Request.prototype.accepts = function accepts(types) {
57:
        if (typeof (types) === 'string')
58:
                types = [types];
59:
 
60:
        types = types.map(function (t) {
61:
                assert.string(t, 'type');
62:
                if (t.indexOf('/') === -1)
63:
                        t = mime.lookup(t);
64:
                return (t);
65:
        });
66:
 
67:
        negotiator(this);
68:
 
69:
        return (this._negotiator.preferredMediaType(types));
70:
};
71:
 
72:
 
73:
Request.prototype.acceptsEncoding = function acceptsEncoding(types) {
74:
        if (typeof (types) === 'string')
75:
                types = [types];
76:
 
77:
        assert.arrayOfString(types, 'types');
78:
 
79:
        negotiator(this);
80:
 
81:
        return (this._negotiator.preferredEncoding(types));
82:
};
83:
 
84:
 
85:
Request.prototype.getContentLength = function getContentLength() {
86:
        if (this._clen !== undefined)
87:
                return (this._clen === false ? undefined : this._clen);
88:
 
89:
        // We should not attempt to read and parse the body of an
90:
        // Upgrade request, so force Content-Length to zero:
91:
        if (this.isUpgradeRequest())
92:
                return (0);
93:
 
94:
        var len = this.header('content-length');
95:
        if (!len) {
96:
                this._clen = false;
97:
        } else {
98:
                this._clen = parseInt(len, 10);
99:
        }
100:
 
101:
        return (this._clen === false ? undefined : this._clen);
102:
};
103:
Request.prototype.contentLength = Request.prototype.getContentLength;
104:
 
105:
 
106:
Request.prototype.getContentType = function getContentType() {
107:
        if (this._contentType !== undefined)
108:
                return (this._contentType);
109:
 
110:
        var index;
111:
        var type = this.headers['content-type'];
112:
 
113:
        if (!type) {
114:
                // RFC2616 section 7.2.1
115:
                this._contentType = 'application/octet-stream';
116:
        } else {
117:
                if ((index = type.indexOf(';')) === -1) {
118:
                        this._contentType = type;
119:
                } else {
120:
                        this._contentType = type.substring(0, index);
121:
                }
122:
        }
123:
 
124:
        return (this._contentType);
125:
};
126:
Request.prototype.contentType = Request.prototype.getContentType;
127:
 
128:
 
129:
Request.prototype.date = function date() {
130:
        if (this._date !== undefined)
131:
                return (this._date);
132:
 
133:
        this._date = new Date(this._time);
134:
        return (this._date);
135:
};
136:
 
137:
 
138:
Request.prototype.getHref = function getHref() {
139:
        if (this._href !== undefined)
140:
                return (this._href);
141:
 
142:
        this._href = this.getUrl().href;
143:
        return (this._href);
144:
};
145:
Request.prototype.href = Request.prototype.getHref;
146:
 
147:
 
148:
Request.prototype.getId = function getId() {
149:
        if (this._id !== undefined)
150:
                return (this._id);
151:
 
152:
        this._id = this.headers['request-id'] ||
153:
                this.headers['x-request-id'] ||
154:
                uuid.v1();
155:
 
156:
        return (this._id);
157:
};
158:
Request.prototype.id = Request.prototype.getId;
159:
 
160:
 
161:
Request.prototype.getPath = function getPath() {
162:
        if (this._path !== undefined)
163:
                return (this._path);
164:
 
165:
        this._path = this.getUrl().pathname;
166:
        return (this._path);
167:
};
168:
Request.prototype.path = Request.prototype.getPath;
169:
 
170:
 
171:
Request.prototype.getQuery = function getQuery() {
172:
        if (this._query !== undefined)
173:
                return (this._query);
174:
 
175:
        this._query = this.getUrl().query || {};
176:
        return (this._query);
177:
};
178:
Request.prototype.query = Request.prototype.getQuery;
179:
 
180:
 
181:
Request.prototype.time = function time() {
182:
        return (this._time);
183:
};
184:
 
185:
 
186:
Request.prototype.getUrl = function getUrl() {
187:
        if (this._url !== undefined)
188:
                return (this._url);
189:
 
190:
        this._url = url.parse(this.url);
191:
        return (this._url);
192:
};
193:
 
194:
 
195:
Request.prototype.getVersion = function getVersion() {
196:
        if (this._version !== undefined)
197:
                return (this._version);
198:
 
199:
        this._version =
200:
                this.headers['accept-version'] ||
201:
                this.headers['x-api-version'] ||
202:
                '*';
203:
 
204:
        return (this._version);
205:
};
206:
Request.prototype.version = Request.prototype.getVersion;
207:
 
208:
 
209:
Request.prototype.header = function header(name, value) {
210:
        assert.string(name, 'name');
211:
 
212:
        name = name.toLowerCase();
213:
 
214:
        if (name === 'referer' || name === 'referrer')
215:
                name = 'referer';
216:
 
217:
        return (this.headers[name] || value);
218:
};
219:
 
220:
 
221:
Request.prototype.trailer = function trailer(name, value) {
222:
        assert.string(name, 'name');
223:
        name = name.toLowerCase();
224:
 
225:
        if (name === 'referer' || name === 'referrer')
226:
                name = 'referer';
227:
 
228:
        return ((this.trailers || {})[name] || value);
229:
};
230:
 
231:
 
232:
Request.prototype.is = function is(type) {
233:
        assert.string(type, 'type');
234:
 
235:
        var contentType = this.getContentType();
236:
        var matches = true;
237:
        if (!contentType)
238:
                return (false);
239:
 
240:
        if (type.indexOf('/') === -1)
241:
                type = mime.lookup(type);
242:
 
243:
        if (type.indexOf('*') !== -1) {
244:
                type = type.split('/');
245:
                contentType = contentType.split('/');
246:
                matches &= (type[0] === '*' || type[0] === contentType[0]);
247:
                matches &= (type[1] === '*' || type[1] === contentType[1]);
248:
        } else {
249:
                matches = (contentType === type);
250:
        }
251:
 
252:
        return (matches);
253:
};
254:
 
255:
 
256:
Request.prototype.isChunked = function isChunked() {
257:
        return (this.headers['transfer-encoding'] === 'chunked');
258:
};
259:
 
260:
 
261:
Request.prototype.isKeepAlive = function isKeepAlive() {
262:
        if (this._keepAlive !== undefined)
263:
                return (this._keepAlive);
264:
 
265:
        if (this.headers.connection) {
266:
                this._keepAlive = /keep-alive/i.test(this.headers.connection);
267:
        } else {
268:
                this._keepAlive = this.httpVersion === '1.0' ? false : true;
269:
        }
270:
 
271:
        return (this._keepAlive);
272:
};
273:
 
274:
 
275:
Request.prototype.isSecure = function isSecure() {
276:
        if (this._secure !== undefined)
277:
                return (this._secure);
278:
 
279:
        this._secure = this.connection.encrypted ? true : false;
280:
        return (this._secure);
281:
};
282:
 
283:
 
284:
Request.prototype.isUpgradeRequest = function isUpgradeRequest() {
285:
        if (this._upgradeRequest !== undefined)
286:
                return (this._upgradeRequest);
287:
        else
288:
                return (false);
289:
};
290:
 
291:
 
292:
Request.prototype.isUpload = function isUpload() {
293:
        var m = this.method;
294:
        return (m === 'PATH' || m === 'POST' || m === 'PUT');
295:
};
296:
 
297:
 
298:
Request.prototype.toString = function toString() {
299:
        var headers = '';
300:
        var self = this;
301:
        var str;
302:
 
303:
        Object.keys(this.headers).forEach(function (k) {
304:
                headers += sprintf('%s: %s\n', k, self.headers[k]);
305:
        });
306:
 
307:
        str = sprintf('%s %s HTTP/%s\n%s',
308:
                      this.method,
309:
                      this.url,
310:
                      this.httpVersion,
311:
                      headers);
312:
 
313:
        return (str);
314:
};
315:
 
316:
 
317:
Request.prototype.userAgent = function userAgent() {
318:
        return (this.headers['user-agent']);
319:
};