1 21 22 package org.armedbear.j; 23 24 public final class HttpFile extends File 25 { 26 private File cache; 27 private String headers; 28 private String contentType; 29 30 private HttpFile() 31 { 32 isRemote = true; 33 } 34 35 private HttpFile(String hostName, String path, int protocol, int port) 36 { 37 this(); 38 this.hostName = hostName; 39 this.canonicalPath = path; 40 this.protocol = protocol; 41 this.port = port; 42 } 43 44 public static HttpFile getHttpFile(String name) 45 { 46 HttpFile file = new HttpFile(); 47 if (name.startsWith(PREFIX_HTTP)) { 48 name = name.substring(PREFIX_HTTP.length()); 49 file.protocol = PROTOCOL_HTTP; 50 } else if (name.startsWith(PREFIX_HTTPS)) { 51 name = name.substring(PREFIX_HTTPS.length()); 52 file.protocol = PROTOCOL_HTTPS; 53 } 54 int index = name.indexOf('/'); 55 if (index < 0) { 56 file.hostName = name; 57 file.canonicalPath = "/"; 58 } else { 59 file.hostName = name.substring(0, index); 60 file.canonicalPath = name.substring(index); 61 } 62 index = file.hostName.indexOf(':'); 63 if (index >= 0) { 64 try { 65 file.port = Integer.parseInt(file.hostName.substring(index + 1)); 66 file.hostName = file.hostName.substring(0, index); 67 } 68 catch (NumberFormatException e) { 69 Log.error(e); 70 return null; 71 } 72 } 73 if (file.port == 0) 74 file.port = file.protocol == PROTOCOL_HTTPS ? 443 : 80; 75 return file; 76 } 77 78 public static HttpFile getHttpFile(HttpFile directory, String name) 79 { 80 if (name.startsWith("http://") || name.startsWith("https://")) { 81 return getHttpFile(name); 83 } else if (name.startsWith("//")) { 84 switch (directory.protocol) { 85 case PROTOCOL_HTTP: 86 return getHttpFile("http:" + name); 87 case PROTOCOL_HTTPS: 88 return getHttpFile("https:" + name); 89 default: 90 Debug.assertTrue(false); 91 return null; 92 } 93 } else if (name.startsWith("/")) { 94 return new HttpFile(directory.hostName, name, directory.protocol, directory.port); 95 } else { 96 return new HttpFile(directory.hostName, 97 canonicalize(appendNameToPath(directory.canonicalPath(), name, '/'), "/"), 98 directory.protocol, directory.port); 99 } 100 } 101 102 public final File getCache() 103 { 104 return cache; 105 } 106 107 public final void setCache(File cache) 108 { 109 this.cache = cache; 110 } 111 112 public final String getHeaders() 113 { 114 return headers; 115 } 116 117 public final void setHeaders(String s) 118 { 119 headers = s; 120 } 121 122 public final String getContentType() 123 { 124 return contentType; 125 } 126 127 public final void setContentType(String s) 128 { 129 contentType = s; 130 } 131 132 public final File getRoot() 133 { 134 return new HttpFile(hostName, "/", protocol, port); 135 } 136 137 public final String getSeparator() 138 { 139 return "/"; 140 } 141 142 public final char getSeparatorChar() 143 { 144 return '/'; 145 } 146 147 public File getParentFile() 148 { 149 if (canonicalPath() == null || canonicalPath.equals("/")) { 150 return new HttpFile(hostName, "/", protocol, port); 153 } 154 int index = canonicalPath.indexOf('?'); 156 String stripped = index >= 0 ? canonicalPath.substring(0, index) : canonicalPath; 157 index = stripped.lastIndexOf('/'); 158 if (index < 0) { 159 return null; 161 } else if (index == 0) { 162 return new HttpFile(hostName, "/", protocol, port); 164 } else { 165 return new HttpFile(hostName, stripped.substring(0, index), 166 protocol, port); 167 } 168 } 169 170 public String netPath() 171 { 172 FastStringBuffer sb = new FastStringBuffer(256); 173 if (protocol == PROTOCOL_HTTP) { 174 sb.append(PREFIX_HTTP); 175 } else if (protocol == PROTOCOL_HTTPS) { 176 sb.append(PREFIX_HTTPS); 177 } else { 178 Debug.assertTrue(false); 179 return null; 180 } 181 sb.append(hostName); 182 int defaultPort = protocol == PROTOCOL_HTTPS ? 443 : 80; 183 if (port != defaultPort) { 184 sb.append(':'); 185 sb.append(port); 186 } 187 sb.append(canonicalPath); 188 return sb.toString(); 189 } 190 191 public String getName() 192 { 193 int index = canonicalPath.lastIndexOf('/'); 194 String name = index >= 0 ? canonicalPath.substring(index + 1) : canonicalPath; 195 index = name.indexOf('?'); 196 if (index >= 0) 197 return name.substring(0, index); 198 else 199 return name; 200 } 201 } 202 | Popular Tags |