Initial commit
[yaffs-website] / node_modules / parseurl / index.js
1 /*!
2  * parseurl
3  * Copyright(c) 2014 Jonathan Ong
4  * Copyright(c) 2014 Douglas Christopher Wilson
5  * MIT Licensed
6  */
7
8 'use strict'
9
10 /**
11  * Module dependencies.
12  */
13
14 var url = require('url')
15 var parse = url.parse
16 var Url = url.Url
17
18 /**
19  * Pattern for a simple path case.
20  * See: https://github.com/joyent/node/pull/7878
21  */
22
23 var simplePathRegExp = /^(\/\/?(?!\/)[^\?#\s]*)(\?[^#\s]*)?$/
24
25 /**
26  * Exports.
27  */
28
29 module.exports = parseurl
30 module.exports.original = originalurl
31
32 /**
33  * Parse the `req` url with memoization.
34  *
35  * @param {ServerRequest} req
36  * @return {Object}
37  * @api public
38  */
39
40 function parseurl(req) {
41   var url = req.url
42
43   if (url === undefined) {
44     // URL is undefined
45     return undefined
46   }
47
48   var parsed = req._parsedUrl
49
50   if (fresh(url, parsed)) {
51     // Return cached URL parse
52     return parsed
53   }
54
55   // Parse the URL
56   parsed = fastparse(url)
57   parsed._raw = url
58
59   return req._parsedUrl = parsed
60 };
61
62 /**
63  * Parse the `req` original url with fallback and memoization.
64  *
65  * @param {ServerRequest} req
66  * @return {Object}
67  * @api public
68  */
69
70 function originalurl(req) {
71   var url = req.originalUrl
72
73   if (typeof url !== 'string') {
74     // Fallback
75     return parseurl(req)
76   }
77
78   var parsed = req._parsedOriginalUrl
79
80   if (fresh(url, parsed)) {
81     // Return cached URL parse
82     return parsed
83   }
84
85   // Parse the URL
86   parsed = fastparse(url)
87   parsed._raw = url
88
89   return req._parsedOriginalUrl = parsed
90 };
91
92 /**
93  * Parse the `str` url with fast-path short-cut.
94  *
95  * @param {string} str
96  * @return {Object}
97  * @api private
98  */
99
100 function fastparse(str) {
101   // Try fast path regexp
102   // See: https://github.com/joyent/node/pull/7878
103   var simplePath = typeof str === 'string' && simplePathRegExp.exec(str)
104
105   // Construct simple URL
106   if (simplePath) {
107     var pathname = simplePath[1]
108     var search = simplePath[2] || null
109     var url = Url !== undefined
110       ? new Url()
111       : {}
112     url.path = str
113     url.href = str
114     url.pathname = pathname
115     url.search = search
116     url.query = search && search.substr(1)
117
118     return url
119   }
120
121   return parse(str)
122 }
123
124 /**
125  * Determine if parsed is still fresh for url.
126  *
127  * @param {string} url
128  * @param {object} parsedUrl
129  * @return {boolean}
130  * @api private
131  */
132
133 function fresh(url, parsedUrl) {
134   return typeof parsedUrl === 'object'
135     && parsedUrl !== null
136     && (Url === undefined || parsedUrl instanceof Url)
137     && parsedUrl._raw === url
138 }