3 * Copyright(c) 2014 Jonathan Ong
4 * Copyright(c) 2014 Douglas Christopher Wilson
12 var url = require('url')
17 * Pattern for a simple path case.
18 * See: https://github.com/joyent/node/pull/7878
21 var simplePathRegExp = /^(\/\/?(?!\/)[^\?#\s]*)(\?[^#\s]*)?$/
27 module.exports = parseurl
28 module.exports.original = originalurl
31 * Parse the `req` url with memoization.
33 * @param {ServerRequest} req
38 function parseurl(req) {
41 if (url === undefined) {
46 var parsed = req._parsedUrl
48 if (fresh(url, parsed)) {
49 // Return cached URL parse
54 parsed = fastparse(url)
57 return req._parsedUrl = parsed
61 * Parse the `req` original url with fallback and memoization.
63 * @param {ServerRequest} req
68 function originalurl(req) {
69 var url = req.originalUrl
71 if (typeof url !== 'string') {
76 var parsed = req._parsedOriginalUrl
78 if (fresh(url, parsed)) {
79 // Return cached URL parse
84 parsed = fastparse(url)
87 return req._parsedOriginalUrl = parsed
91 * Parse the `str` url with fast-path short-cut.
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)
103 // Construct simple URL
105 var pathname = simplePath[1]
106 var search = simplePath[2] || null
107 var url = Url !== undefined
112 url.pathname = pathname
114 url.query = search && search.substr(1)
123 * Determine if parsed is still fresh for url.
125 * @param {string} url
126 * @param {object} parsedUrl
131 function fresh(url, parsedUrl) {
132 return typeof parsedUrl === 'object'
133 && parsedUrl !== null
134 && (Url === undefined || parsedUrl instanceof Url)
135 && parsedUrl._raw === url