1 7 8 package java.net; 9 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.io.File ; 13 import java.io.OutputStream ; 14 import java.util.Hashtable ; 15 import sun.net.util.IPAddressUtil; 16 import sun.net.www.ParseUtil; 17 18 36 public abstract class URLStreamHandler { 37 55 abstract protected URLConnection openConnection(URL u) throws IOException ; 56 57 79 protected URLConnection openConnection(URL u, Proxy p) throws IOException { 80 throw new UnsupportedOperationException ("Method not implemented."); 81 } 82 83 108 protected void parseURL(URL u, String spec, int start, int limit) { 109 String protocol = u.getProtocol(); 111 String authority = u.getAuthority(); 112 String userInfo = u.getUserInfo(); 113 String host = u.getHost(); 114 int port = u.getPort(); 115 String path = u.getPath(); 116 String query = u.getQuery(); 117 118 String ref = u.getRef(); 120 121 boolean isRelPath = false; 122 boolean queryOnly = false; 123 124 if (start < limit) { 127 int queryStart = spec.indexOf('?'); 128 queryOnly = queryStart == start; 129 if ((queryStart != -1) && (queryStart < limit)) { 130 query = spec.substring(queryStart+1, limit); 131 if (limit > queryStart) 132 limit = queryStart; 133 spec = spec.substring(0, queryStart); 134 } 135 } 136 137 int i = 0; 138 boolean isUNCName = (start <= limit - 4) && 140 (spec.charAt(start) == '/') && 141 (spec.charAt(start + 1) == '/') && 142 (spec.charAt(start + 2) == '/') && 143 (spec.charAt(start + 3) == '/'); 144 if (!isUNCName && (start <= limit - 2) && (spec.charAt(start) == '/') && 145 (spec.charAt(start + 1) == '/')) { 146 start += 2; 147 i = spec.indexOf('/', start); 148 if (i < 0) { 149 i = spec.indexOf('?', start); 150 if (i < 0) 151 i = limit; 152 } 153 154 host = authority = spec.substring(start, i); 155 156 int ind = authority.indexOf('@'); 157 if (ind != -1) { 158 userInfo = authority.substring(0, ind); 159 host = authority.substring(ind+1); 160 } else { 161 userInfo = null; 162 } 163 if (host != null) { 164 if (host.length()>0 && (host.charAt(0) == '[')) { 167 if ((ind = host.indexOf(']')) > 2) { 168 169 String nhost = host ; 170 host = nhost.substring(0,ind+1); 171 if (!IPAddressUtil. 172 isIPv6LiteralAddress(host.substring(1, ind))) { 173 throw new IllegalArgumentException ( 174 "Invalid host: "+ host); 175 } 176 177 port = -1 ; 178 if (nhost.length() > ind+1) { 179 if (nhost.charAt(ind+1) == ':') { 180 ++ind ; 181 if (nhost.length() > (ind + 1)) { 183 port = Integer.parseInt(nhost.substring(ind+1)); 184 } 185 } else { 186 throw new IllegalArgumentException ( 187 "Invalid authority field: " + authority); 188 } 189 } 190 } else { 191 throw new IllegalArgumentException ( 192 "Invalid authority field: " + authority); 193 } 194 } else { 195 ind = host.indexOf(':'); 196 port = -1; 197 if (ind >= 0) { 198 if (host.length() > (ind + 1)) { 200 port = Integer.parseInt(host.substring(ind + 1)); 201 } 202 host = host.substring(0, ind); 203 } 204 } 205 } else { 206 host = ""; 207 } 208 if (port < -1) 209 throw new IllegalArgumentException ("Invalid port number :" + 210 port); 211 start = i; 212 if (authority != null && authority.length() > 0) 215 path = ""; 216 } 217 218 if (host == null) { 219 host = ""; 220 } 221 222 if (start < limit) { 224 if (spec.charAt(start) == '/') { 225 path = spec.substring(start, limit); 226 } else if (path != null && path.length() > 0) { 227 isRelPath = true; 228 int ind = path.lastIndexOf('/'); 229 String seperator = ""; 230 if (ind == -1 && authority != null) 231 seperator = "/"; 232 path = path.substring(0, ind + 1) + seperator + 233 spec.substring(start, limit); 234 235 } else { 236 String seperator = (authority != null) ? "/" : ""; 237 path = seperator + spec.substring(start, limit); 238 } 239 } else if (queryOnly && path != null) { 240 int ind = path.lastIndexOf('/'); 241 if (ind < 0) 242 ind = 0; 243 path = path.substring(0, ind) + "/"; 244 } 245 if (path == null) 246 path = ""; 247 248 if (isRelPath) { 249 while ((i = path.indexOf("/./")) >= 0) { 251 path = path.substring(0, i) + path.substring(i + 2); 252 } 253 i = 0; 255 while ((i = path.indexOf("/../", i)) >= 0) { 256 262 if (i > 0 && (limit = path.lastIndexOf('/', i - 1)) >= 0 && 263 (path.indexOf("/../", limit) != 0)) { 264 path = path.substring(0, limit) + path.substring(i + 3); 265 i = 0; 266 } else { 267 i = i + 3; 268 } 269 } 270 while (path.endsWith("/..")) { 272 i = path.indexOf("/.."); 273 if ((limit = path.lastIndexOf('/', i - 1)) >= 0) { 274 path = path.substring(0, limit+1); 275 } else { 276 break; 277 } 278 } 279 if (path.startsWith("./") && path.length() > 2) 281 path = path.substring(2); 282 283 if (path.endsWith("/.")) 285 path = path.substring(0, path.length() -1); 286 } 287 288 setURL(u, protocol, host, port, authority, userInfo, path, query, ref); 289 } 290 291 297 protected int getDefaultPort() { 298 return -1; 299 } 300 301 313 protected boolean equals(URL u1, URL u2) { 314 String ref1 = u1.getRef(); 315 String ref2 = u2.getRef(); 316 return (ref1 == ref2 || (ref1 != null && ref1.equals(ref2))) && 317 sameFile(u1, u2); 318 } 319 320 328 protected int hashCode(URL u) { 329 int h = 0; 330 331 String protocol = u.getProtocol(); 333 if (protocol != null) 334 h += protocol.hashCode(); 335 336 InetAddress addr = getHostAddress(u); 338 if (addr != null) { 339 h += addr.hashCode(); 340 } else { 341 String host = u.getHost(); 342 if (host != null) 343 h += host.toLowerCase().hashCode(); 344 } 345 346 String file = u.getFile(); 348 if (file != null) 349 h += file.hashCode(); 350 351 if (u.getPort() == -1) 353 h += getDefaultPort(); 354 else 355 h += u.getPort(); 356 357 String ref = u.getRef(); 359 if (ref != null) 360 h += ref.hashCode(); 361 362 return h; 363 } 364 365 376 protected boolean sameFile(URL u1, URL u2) { 377 if (!((u1.getProtocol() == u2.getProtocol()) || 379 (u1.getProtocol() != null && 380 u1.getProtocol().equalsIgnoreCase(u2.getProtocol())))) 381 return false; 382 383 if (!(u1.getFile() == u2.getFile() || 385 (u1.getFile() != null && u1.getFile().equals(u2.getFile())))) 386 return false; 387 388 int port1, port2; 390 port1 = (u1.getPort() != -1) ? u1.getPort() : u1.handler.getDefaultPort(); 391 port2 = (u2.getPort() != -1) ? u2.getPort() : u2.handler.getDefaultPort(); 392 if (port1 != port2) 393 return false; 394 395 if (!hostsEqual(u1, u2)) 397 return false; 398 399 return true; 400 } 401 402 411 protected synchronized InetAddress getHostAddress(URL u) { 412 if (u.hostAddress != null) 413 return u.hostAddress; 414 415 String host = u.getHost(); 416 if (host == null || host.equals("")) { 417 return null; 418 } else { 419 try { 420 u.hostAddress = InetAddress.getByName(host); 421 } catch (UnknownHostException ex) { 422 return null; 423 } catch (SecurityException se) { 424 return null; 425 } 426 } 427 return u.hostAddress; 428 } 429 430 438 protected boolean hostsEqual(URL u1, URL u2) { 439 InetAddress a1 = getHostAddress(u1); 440 InetAddress a2 = getHostAddress(u2); 441 if (a1 != null && a2 != null) { 443 return a1.equals(a2); 444 } else if (u1.getHost() != null && u2.getHost() != null) 446 return u1.getHost().equalsIgnoreCase(u2.getHost()); 447 else 448 return u1.getHost() == null && u2.getHost() == null; 449 } 450 451 458 protected String toExternalForm(URL u) { 459 460 int len = u.getProtocol().length() + 1; 462 if (u.getAuthority() != null && u.getAuthority().length() > 0) 463 len += 2 + u.getAuthority().length(); 464 if (u.getPath() != null) { 465 len += u.getPath().length(); 466 } 467 if (u.getQuery() != null) { 468 len += 1 + u.getQuery().length(); 469 } 470 if (u.getRef() != null) 471 len += 1 + u.getRef().length(); 472 473 StringBuffer result = new StringBuffer (len); 474 result.append(u.getProtocol()); 475 result.append(":"); 476 if (u.getAuthority() != null && u.getAuthority().length() > 0) { 477 result.append("//"); 478 result.append(u.getAuthority()); 479 } 480 if (u.getPath() != null) { 481 result.append(u.getPath()); 482 } 483 if (u.getQuery() != null) { 484 result.append('?'); 485 result.append(u.getQuery()); 486 } 487 if (u.getRef() != null) { 488 result.append("#"); 489 result.append(u.getRef()); 490 } 491 return result.toString(); 492 } 493 494 513 protected void setURL(URL u, String protocol, String host, int port, 514 String authority, String userInfo, String path, 515 String query, String ref) { 516 if (this != u.handler) { 517 throw new SecurityException ("handler for url different from " + 518 "this handler"); 519 } 520 u.set(u.getProtocol(), host, port, authority, userInfo, path, query, ref); 522 } 523 524 540 @Deprecated 541 protected void setURL(URL u, String protocol, String host, int port, 542 String file, String ref) { 543 547 String authority = null; 548 String userInfo = null; 549 if (host != null && host.length() != 0) { 550 authority = (port == -1) ? host : host + ":" + port; 551 int at = host.lastIndexOf('@'); 552 if (at != -1) { 553 userInfo = host.substring(0, at); 554 host = host.substring(at+1); 555 } 556 } 557 558 561 String path = null; 562 String query = null; 563 if (file != null) { 564 int q = file.lastIndexOf('?'); 565 if (q != -1) { 566 query = file.substring(q+1); 567 path = file.substring(0, q); 568 } else 569 path = file; 570 } 571 setURL(u, protocol, host, port, authority, userInfo, path, query, ref); 572 } 573 } 574 | Popular Tags |