| 1 |
"use strict"; |
| 2 |
|
| 3 |
function isDirectoryIndex(resource, options) |
| 4 |
{ |
| 5 |
var verdict = false; |
| 6 |
|
| 7 |
options.directoryIndexes.every( function(index) |
| 8 |
{ |
| 9 |
if (index === resource) |
| 10 |
{ |
| 11 |
verdict = true; |
| 12 |
return false; |
| 13 |
} |
| 14 |
|
| 15 |
return true; |
| 16 |
}); |
| 17 |
|
| 18 |
return verdict; |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
function parsePath(urlObj, options) |
| 24 |
{ |
| 25 |
var path = urlObj.path.absolute.string; |
| 26 |
|
| 27 |
if (path) |
| 28 |
{ |
| 29 |
var lastSlash = path.lastIndexOf("/"); |
| 30 |
|
| 31 |
if (lastSlash > -1) |
| 32 |
{ |
| 33 |
if (++lastSlash < path.length) |
| 34 |
{ |
| 35 |
var resource = path.substr(lastSlash); |
| 36 |
|
| 37 |
if (resource!=="." && resource!=="..") |
| 38 |
{ |
| 39 |
urlObj.resource = resource; |
| 40 |
path = path.substr(0, lastSlash); |
| 41 |
} |
| 42 |
else |
| 43 |
{ |
| 44 |
path += "/"; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
urlObj.path.absolute.string = path; |
| 49 |
urlObj.path.absolute.array = splitPath(path); |
| 50 |
} |
| 51 |
else if (path==="." || path==="..") |
| 52 |
{ |
| 53 |
// "..?var", "..#anchor", etc ... not "..index.html" |
| 54 |
path += "/"; |
| 55 |
|
| 56 |
urlObj.path.absolute.string = path; |
| 57 |
urlObj.path.absolute.array = splitPath(path); |
| 58 |
} |
| 59 |
else |
| 60 |
{ |
| 61 |
// Resource-only |
| 62 |
urlObj.resource = path; |
| 63 |
urlObj.path.absolute.string = null; |
| 64 |
} |
| 65 |
|
| 66 |
urlObj.extra.resourceIsIndex = isDirectoryIndex(urlObj.resource, options); |
| 67 |
} |
| 68 |
// Else: query/hash-only or empty |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
function splitPath(path) |
| 74 |
{ |
| 75 |
// TWEAK :: condition only for speed optimization |
| 76 |
if (path !== "/") |
| 77 |
{ |
| 78 |
var cleaned = []; |
| 79 |
|
| 80 |
path.split("/").forEach( function(dir) |
| 81 |
{ |
| 82 |
// Cleanup -- splitting "/dir/" becomes ["","dir",""] |
| 83 |
if (dir !== "") |
| 84 |
{ |
| 85 |
cleaned.push(dir); |
| 86 |
} |
| 87 |
}); |
| 88 |
|
| 89 |
return cleaned; |
| 90 |
} |
| 91 |
else |
| 92 |
{ |
| 93 |
// Faster to skip the above block and just create an array |
| 94 |
return []; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
module.exports = parsePath; |