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