3 * Copyright(c) 2014 Jonathan Ong
4 * Copyright(c) 2014 Douglas Christopher Wilson
11 * Module dependencies.
14 var url = require('url')
19 * Pattern for a simple path case.
20 * See: https://github.com/joyent/node/pull/7878
23 var simplePathRegExp = /^(\/\/?(?!\/)[^\?#\s]*)(\?[^#\s]*)?$/
29 module.exports = parseurl
30 module.exports.original = originalurl
33 * Parse the `req` url with memoization.
35 * @param {ServerRequest} req
40 function parseurl(req) {
43 if (url === undefined) {
48 var parsed = req._parsedUrl
50 if (fresh(url, parsed)) {
51 // Return cached URL parse
56 parsed = fastparse(url)
59 return req._parsedUrl = parsed
63 * Parse the `req` original url with fallback and memoization.
65 * @param {ServerRequest} req
70 function originalurl(req) {
71 var url = req.originalUrl
73 if (typeof url !== 'string') {
78 var parsed = req._parsedOriginalUrl
80 if (fresh(url, parsed)) {
81 // Return cached URL parse
86 parsed = fastparse(url)
89 return req._parsedOriginalUrl = parsed
93 * Parse the `str` url with fast-path short-cut.
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)
105 // Construct simple URL
107 var pathname = simplePath[1]
108 var search = simplePath[2] || null
109 var url = Url !== undefined
114 url.pathname = pathname
116 url.query = search && search.substr(1)
125 * Determine if parsed is still fresh for url.
127 * @param {string} url
128 * @param {object} parsedUrl
133 function fresh(url, parsedUrl) {
134 return typeof parsedUrl === 'object'
135 && parsedUrl !== null
136 && (Url === undefined || parsedUrl instanceof Url)
137 && parsedUrl._raw === url