1 jls.loader.provide('jls.net.URL'); 2 3 // TODO Fix inter dependence 4 jls.loader.require('jls.net.http.HttpURLConnection'); 5 6 jls.net.URL = jls.lang.Class.create( /** @lends jls.net.URL.prototype */ 7 { 8 /** 9 * Creates an URL. 10 * 11 * @param {String} [protocol] The protocol or the URL as a string. 12 * @param {String} [host] The host name. 13 * @param {Number} [port] The port number. 14 * @param {String} [file] The file part of the URL. 15 * @constructs 16 * @class This class represents an Uniform Resource Locator. 17 * scheme://username:password@domain:port/path?query_string#anchor 18 */ 19 initialize : function(protocol, host, port, file) { 20 this._protocol = null; 21 this._host = null; 22 this._port = -1; 23 this._file = null; 24 this._path = null; 25 this._query = null; 26 this._ref = null; 27 if (typeof protocol == 'string') { 28 if (typeof host == 'undefined') { 29 this._fromExternalForm(protocol); 30 } else { 31 this._protocol = protocol; 32 this._host = host; 33 this._port = port; 34 this._file = file; 35 } 36 } else if (typeof protocol == 'object') { 37 this._protocol = protocol.protocol; 38 this._host = protocol.host; 39 this._port = protocol.port; 40 this._file = protocol.file; 41 } else { 42 throw new jls.lang.Exception('Invalid arguments'); 43 } 44 }, 45 /** 46 * Returns the protocol. 47 * 48 * @returns {String} The protocol. 49 */ 50 getProtocol : function() { 51 return this._protocol; 52 }, 53 /** 54 * Returns the host. 55 * 56 * @returns {String} The host. 57 */ 58 getHost : function() { 59 return this._host; 60 }, 61 /** 62 * Returns the port. 63 * 64 * @returns {Number} The port. 65 */ 66 getPort : function() { 67 return this._port; 68 }, 69 /** 70 * Returns the default port associated protocol. 71 * 72 * @returns {String} The default port associated protocol. 73 */ 74 getDefaultPort : function() { 75 if (this._protocol == null) { 76 return -1; 77 } 78 var cname = this._protocol.toUpperCase() + '_PORT'; 79 if (cname in jls.net.URL) { 80 return jls.net.URL[cname]; 81 } 82 return -1; 83 }, 84 /** 85 * Returns the file. 86 * 87 * @returns {String} The file. 88 */ 89 getFile : function() { 90 // URL: <protocol>://<host>:<port>/<file> 91 // file: <path>(#<ref>|?<query>) 92 return this._file; 93 }, 94 /** 95 * Returns the path. 96 * 97 * @returns {String} The path. 98 */ 99 getPath : function() { 100 return this._path; 101 }, 102 /** 103 * Returns the query. 104 * 105 * @returns {String} The query. 106 */ 107 getQuery : function() { 108 return this._query; 109 }, 110 /** 111 * Returns the anchor. 112 * 113 * @returns {String} The anchor. 114 */ 115 getRef : function() { 116 return this._ref; 117 }, 118 /** 119 * Opens a connection. 120 * 121 * @returns {Object} The connection. 122 */ 123 openConnection : function() { 124 if (this._protocol.toLowerCase() == 'http') { 125 return new jls.net.http.HttpURLConnection(this); 126 } 127 return null; 128 }, 129 _fromExternalForm : function(s) { 130 var i = s.indexOf('://'); 131 if (i == -1) { 132 throw new jls.lang.Exception('Invalid URL'); 133 } 134 this._protocol = s.substring(0, i); 135 i += 3; 136 var j = s.indexOf('/', i); 137 if ((j != -1) && (j < s.length)) { 138 this._file = s.substring(j + 1); 139 } else { 140 this._file = ''; 141 } 142 var k = s.indexOf(':', i); 143 if ((k != -1) && (k < j)) { 144 this._host = s.substring(i, k); 145 this._port = parseInt(s.substring(k + 1, j)); 146 } else { 147 this._host = s.substring(i, j); 148 this._port = -1; 149 } 150 i = this._file.indexOf('?'); 151 if (i > 0) { 152 this._path = this._file.substring(0, i); 153 this._query = this._file.substring(i + 1); 154 this._ref = null; 155 } else { 156 i = this._file.indexOf('#'); 157 if (i > 0) { 158 this._ref = this._file.substring(i + 1); 159 this._file = this._file.substring(0, i); 160 } else { 161 this._ref = null; 162 } 163 this._path = this._file; 164 this._query = null; 165 } 166 return this; 167 }, 168 /** 169 * Returns the external form of the URL. 170 * 171 * @returns {String} The URL as a string. 172 */ 173 toExternalForm : function() { 174 var s = this._protocol; 175 s += '://' + this._host; 176 if (this._port >= 0) { 177 s += ':' + this._port; 178 } 179 s += '/'; 180 if (this._file) { 181 s += this._file; 182 } 183 if (this._ref) { 184 s += '#' + this._ref; 185 } 186 return s; 187 }, 188 /** 189 * Returns the string value of the URL that is its external form. 190 * 191 * @returns {String} The URL as a string. 192 */ 193 toString : function() { 194 return this.toExternalForm(); 195 } 196 }); 197 198 Object.extend(jls.net.URL, /** @lends jls.net.URL */ 199 { 200 HTTP_PORT : 80, 201 HTTPS_PORT : 443 202 }); 203