main
Last change
on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago |
F4 Finalna Verzija
|
-
Property mode
set to
100644
|
File size:
2.3 KB
|
Rev | Line | |
---|
[79a0317] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | var _parseUrl = require("url").parse;
|
---|
| 4 |
|
---|
| 5 |
|
---|
| 6 |
|
---|
| 7 | /*
|
---|
| 8 | Customize the URL object that Node generates
|
---|
| 9 | because:
|
---|
| 10 |
|
---|
| 11 | * necessary data for later
|
---|
| 12 | * urlObj.host is useless
|
---|
| 13 | * urlObj.hostname is too long
|
---|
| 14 | * urlObj.path is useless
|
---|
| 15 | * urlObj.pathname is too long
|
---|
| 16 | * urlObj.protocol is inaccurate; should be called "scheme"
|
---|
| 17 | * urlObj.search is mostly useless
|
---|
| 18 | */
|
---|
| 19 | function clean(urlObj)
|
---|
| 20 | {
|
---|
| 21 | var scheme = urlObj.protocol;
|
---|
| 22 |
|
---|
| 23 | if (scheme)
|
---|
| 24 | {
|
---|
| 25 | // Remove ":" suffix
|
---|
| 26 | if (scheme.indexOf(":") === scheme.length-1)
|
---|
| 27 | {
|
---|
| 28 | scheme = scheme.substr(0, scheme.length-1);
|
---|
| 29 | }
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | urlObj.host =
|
---|
| 33 | {
|
---|
| 34 | // TODO :: unescape(encodeURIComponent(s)) ? ... http://ecmanaut.blogspot.ca/2006/07/encoding-decoding-utf8-in-javascript.html
|
---|
| 35 | full: urlObj.hostname,
|
---|
| 36 | stripped: null
|
---|
| 37 | };
|
---|
| 38 |
|
---|
| 39 | urlObj.path =
|
---|
| 40 | {
|
---|
| 41 | absolute:
|
---|
| 42 | {
|
---|
| 43 | array: null,
|
---|
| 44 | string: urlObj.pathname
|
---|
| 45 | },
|
---|
| 46 | relative:
|
---|
| 47 | {
|
---|
| 48 | array: null,
|
---|
| 49 | string: null
|
---|
| 50 | }
|
---|
| 51 | };
|
---|
| 52 |
|
---|
| 53 | urlObj.query =
|
---|
| 54 | {
|
---|
| 55 | object: urlObj.query,
|
---|
| 56 | string:
|
---|
| 57 | {
|
---|
| 58 | full: null,
|
---|
| 59 | stripped: null
|
---|
| 60 | }
|
---|
| 61 | };
|
---|
| 62 |
|
---|
| 63 | urlObj.extra =
|
---|
| 64 | {
|
---|
| 65 | hrefInfo:
|
---|
| 66 | {
|
---|
| 67 | minimumPathOnly: null,
|
---|
| 68 | minimumResourceOnly: null,
|
---|
| 69 | minimumQueryOnly: null,
|
---|
| 70 | minimumHashOnly: null,
|
---|
| 71 | empty: null,
|
---|
| 72 |
|
---|
| 73 | separatorOnlyQuery: urlObj.search==="?"
|
---|
| 74 | },
|
---|
| 75 | portIsDefault: null,
|
---|
| 76 | relation:
|
---|
| 77 | {
|
---|
| 78 | maximumScheme: null,
|
---|
| 79 | maximumAuth: null,
|
---|
| 80 | maximumHost: null,
|
---|
| 81 | maximumPort: null,
|
---|
| 82 | maximumPath: null,
|
---|
| 83 | maximumResource: null,
|
---|
| 84 | maximumQuery: null,
|
---|
| 85 | maximumHash: null,
|
---|
| 86 |
|
---|
| 87 | minimumScheme: null,
|
---|
| 88 | minimumAuth: null,
|
---|
| 89 | minimumHost: null,
|
---|
| 90 | minimumPort: null,
|
---|
| 91 | minimumPath: null,
|
---|
| 92 | minimumResource: null,
|
---|
| 93 | minimumQuery: null,
|
---|
| 94 | minimumHash: null,
|
---|
| 95 |
|
---|
| 96 | overridesQuery: null
|
---|
| 97 | },
|
---|
| 98 | resourceIsIndex: null,
|
---|
| 99 | slashes: urlObj.slashes
|
---|
| 100 | };
|
---|
| 101 |
|
---|
| 102 | urlObj.resource = null;
|
---|
| 103 | urlObj.scheme = scheme;
|
---|
| 104 | delete urlObj.hostname;
|
---|
| 105 | delete urlObj.pathname;
|
---|
| 106 | delete urlObj.protocol;
|
---|
| 107 | delete urlObj.search;
|
---|
| 108 | delete urlObj.slashes;
|
---|
| 109 |
|
---|
| 110 | return urlObj;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 |
|
---|
| 114 |
|
---|
| 115 | function validScheme(url, options)
|
---|
| 116 | {
|
---|
| 117 | var valid = true;
|
---|
| 118 |
|
---|
| 119 | options.rejectedSchemes.every( function(rejectedScheme)
|
---|
| 120 | {
|
---|
| 121 | valid = !(url.indexOf(rejectedScheme+":") === 0);
|
---|
| 122 |
|
---|
| 123 | // Break loop
|
---|
| 124 | return valid;
|
---|
| 125 | });
|
---|
| 126 |
|
---|
| 127 | return valid;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 | function parseUrlString(url, options)
|
---|
| 133 | {
|
---|
| 134 | if ( validScheme(url,options) )
|
---|
| 135 | {
|
---|
| 136 | return clean( _parseUrl(url, true, options.slashesDenoteHost) );
|
---|
| 137 | }
|
---|
| 138 | else
|
---|
| 139 | {
|
---|
| 140 | return {href:url, valid:false};
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 | module.exports = parseUrlString;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.