001: /*
002: * HttpFile.java
003: *
004: * Copyright (C) 2000-2002 Peter Graves
005: * $Id: HttpFile.java,v 1.1.1.1 2002/09/24 16:08:24 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: public final class HttpFile extends File {
025: private File cache;
026: private String headers;
027: private String contentType;
028:
029: private HttpFile() {
030: isRemote = true;
031: }
032:
033: private HttpFile(String hostName, String path, int protocol,
034: int port) {
035: this ();
036: this .hostName = hostName;
037: this .canonicalPath = path;
038: this .protocol = protocol;
039: this .port = port;
040: }
041:
042: public static HttpFile getHttpFile(String name) {
043: HttpFile file = new HttpFile();
044: if (name.startsWith(PREFIX_HTTP)) {
045: name = name.substring(PREFIX_HTTP.length());
046: file.protocol = PROTOCOL_HTTP;
047: } else if (name.startsWith(PREFIX_HTTPS)) {
048: name = name.substring(PREFIX_HTTPS.length());
049: file.protocol = PROTOCOL_HTTPS;
050: }
051: int index = name.indexOf('/');
052: if (index < 0) {
053: file.hostName = name;
054: file.canonicalPath = "/";
055: } else {
056: file.hostName = name.substring(0, index);
057: file.canonicalPath = name.substring(index);
058: }
059: index = file.hostName.indexOf(':');
060: if (index >= 0) {
061: try {
062: file.port = Integer.parseInt(file.hostName
063: .substring(index + 1));
064: file.hostName = file.hostName.substring(0, index);
065: } catch (NumberFormatException e) {
066: Log.error(e);
067: return null;
068: }
069: }
070: if (file.port == 0)
071: file.port = file.protocol == PROTOCOL_HTTPS ? 443 : 80;
072: return file;
073: }
074:
075: public static HttpFile getHttpFile(HttpFile directory, String name) {
076: if (name.startsWith("http://") || name.startsWith("https://")) {
077: // Ignore directory.
078: return getHttpFile(name);
079: } else if (name.startsWith("//")) {
080: switch (directory.protocol) {
081: case PROTOCOL_HTTP:
082: return getHttpFile("http:" + name);
083: case PROTOCOL_HTTPS:
084: return getHttpFile("https:" + name);
085: default:
086: Debug.assertTrue(false);
087: return null;
088: }
089: } else if (name.startsWith("/")) {
090: return new HttpFile(directory.hostName, name,
091: directory.protocol, directory.port);
092: } else {
093: return new HttpFile(directory.hostName, canonicalize(
094: appendNameToPath(directory.canonicalPath(), name,
095: '/'), "/"), directory.protocol,
096: directory.port);
097: }
098: }
099:
100: public final File getCache() {
101: return cache;
102: }
103:
104: public final void setCache(File cache) {
105: this .cache = cache;
106: }
107:
108: public final String getHeaders() {
109: return headers;
110: }
111:
112: public final void setHeaders(String s) {
113: headers = s;
114: }
115:
116: public final String getContentType() {
117: return contentType;
118: }
119:
120: public final void setContentType(String s) {
121: contentType = s;
122: }
123:
124: public final File getRoot() {
125: return new HttpFile(hostName, "/", protocol, port);
126: }
127:
128: public final String getSeparator() {
129: return "/";
130: }
131:
132: public final char getSeparatorChar() {
133: return '/';
134: }
135:
136: public File getParentFile() {
137: if (canonicalPath() == null || canonicalPath.equals("/")) {
138: // The file might really be "http://www.cnn.com/index.html", but
139: // it might appear to be "http://www.cnn.com/".
140: return new HttpFile(hostName, "/", protocol, port);
141: }
142: // Strip query (if any).
143: int index = canonicalPath.indexOf('?');
144: String stripped = index >= 0 ? canonicalPath
145: .substring(0, index) : canonicalPath;
146: index = stripped.lastIndexOf('/');
147: if (index < 0) {
148: // No parent.
149: return null;
150: } else if (index == 0) {
151: // "/index.html"
152: return new HttpFile(hostName, "/", protocol, port);
153: } else {
154: return new HttpFile(hostName, stripped.substring(0, index),
155: protocol, port);
156: }
157: }
158:
159: public String netPath() {
160: FastStringBuffer sb = new FastStringBuffer(256);
161: if (protocol == PROTOCOL_HTTP) {
162: sb.append(PREFIX_HTTP);
163: } else if (protocol == PROTOCOL_HTTPS) {
164: sb.append(PREFIX_HTTPS);
165: } else {
166: Debug.assertTrue(false);
167: return null;
168: }
169: sb.append(hostName);
170: int defaultPort = protocol == PROTOCOL_HTTPS ? 443 : 80;
171: if (port != defaultPort) {
172: sb.append(':');
173: sb.append(port);
174: }
175: sb.append(canonicalPath);
176: return sb.toString();
177: }
178:
179: public String getName() {
180: int index = canonicalPath.lastIndexOf('/');
181: String name = index >= 0 ? canonicalPath.substring(index + 1)
182: : canonicalPath;
183: index = name.indexOf('?');
184: if (index >= 0)
185: return name.substring(0, index);
186: else
187: return name;
188: }
189: }
|