📄 httpframe.java
字号:
// HTTPFrame.java// $Id: HTTPFrame.java,v 1.110 2004/01/06 10:07:51 ylafon Exp $// (c) COPYRIGHT MIT and INRIA, 1997.// Please first read the full copyright statement in file COPYRIGHT.htmlpackage org.w3c.jigsaw.frames;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.InputStream;import java.io.IOException;import java.net.MalformedURLException;import java.net.URL;import java.net.URLEncoder;import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;import org.w3c.tools.codec.Base64Encoder;import org.w3c.tools.sorter.Sorter;import org.w3c.tools.resources.Attribute;import org.w3c.tools.resources.AttributeRegistry;import org.w3c.tools.resources.BooleanAttribute;import org.w3c.tools.resources.IntegerAttribute;import org.w3c.tools.resources.LongAttribute;import org.w3c.tools.resources.DoubleAttribute;import org.w3c.tools.resources.StringAttribute;import org.w3c.tools.resources.StringArrayAttribute;import org.w3c.tools.resources.ProtocolFrame;import org.w3c.tools.resources.ProtocolFrame;import org.w3c.tools.resources.LookupState;import org.w3c.tools.resources.LookupResult;import org.w3c.tools.resources.ResourceReference;import org.w3c.tools.resources.InvalidResourceException;import org.w3c.tools.resources.MultipleLockException;import org.w3c.tools.resources.Resource;import org.w3c.tools.resources.FramedResource;import org.w3c.tools.resources.ContainerInterface;import org.w3c.tools.resources.ContainerResource;import org.w3c.tools.resources.DirectoryResource;import org.w3c.tools.resources.FileResource;import org.w3c.tools.resources.RequestInterface;import org.w3c.tools.resources.ReplyInterface;import org.w3c.tools.resources.event.AttributeChangedEvent;import org.w3c.jigsaw.http.Client;import org.w3c.jigsaw.http.ClientException;import org.w3c.jigsaw.http.httpd;import org.w3c.jigsaw.http.Request;import org.w3c.jigsaw.http.Reply;import org.w3c.jigsaw.http.HTTPException;import org.w3c.jigsaw.html.HtmlGenerator;import org.w3c.jigsaw.html.HtmlLink;import org.w3c.www.mime.MimeType;import org.w3c.www.http.ByteRangeOutputStream;import org.w3c.www.http.HTTP;import org.w3c.www.http.HttpContentRange;import org.w3c.www.http.HttpDate;import org.w3c.www.http.HttpEntityTag;import org.w3c.www.http.HttpFactory;import org.w3c.www.http.HttpInteger;import org.w3c.www.http.HttpMimeType;import org.w3c.www.http.HttpRange;import org.w3c.www.http.HttpString;import org.w3c.www.http.HttpTokenList;import org.w3c.tools.crypt.Md5;import org.w3c.tools.resources.ProtocolException;import org.w3c.tools.resources.ResourceException;/** * Default class to handle the HTTP protocol, manage FileResource and * DirectoryResource. */public class HTTPFrame extends ProtocolFrame { public static final String STATE_CONTENT_LOCATION = "org.w3c.jigsaw.frames.HTTPFrame.cl"; private static final boolean debug = false; /** * Condition check return code - Condition existed but failed. */ public static final int COND_FAILED = 1; /** * Condition check return code - Condition existed and succeeded. */ public static final int COND_OK = 2; /** * Condition check return code - Condition existed and succeeded * but is a weak validation. */ public static final int COND_WEAK = 3; private static HttpTokenList _accept_ranges = null; static { String accept_ranges[] = { "bytes" }; _accept_ranges = HttpFactory.makeStringList(accept_ranges); } /** * Methods allowed by instances of that class in particular: */ protected HttpTokenList allowed = null; /** * Attributes index - The index for the quality attribute. */ protected static int ATTR_QUALITY = -1 ; /** * Attribute index - The index for the title attribute. */ protected static int ATTR_TITLE = -1 ; /** * Attribute index - The index for the content languages attribute. */ protected static int ATTR_CONTENT_LANGUAGE = -1 ; /** * Attribute index - The index for the content encodings attribute. */ protected static int ATTR_CONTENT_ENCODING = -1 ; /** * Attribute index - The index for the content type attribute. */ protected static int ATTR_CONTENT_TYPE = -1 ; /** * Attribute index - The index for the charset attribute. */ protected static int ATTR_CHARSET = -1 ; /** * Attribute index - The index for the content length attribute. */ protected static int ATTR_CONTENT_LENGTH = -1 ; /** * Attribute index - The icon (if any) associated to the resource. */ protected static int ATTR_ICON = -1 ; /** * Attribute index - Max age: the maximum drift allowed from reality. */ protected static int ATTR_MAXAGE = -1 ; /** * Attribute index - Send MD5 Digest: the md5 digest of the resource sent */ protected static int ATTR_MD5 = -1; /** * Attribute index - delete allowed for the associated resource ? */ protected static int ATTR_ALLOW_DEL = -1; // // Attribute relative to FileResource // /** * Attribute index - Do we allow PUT method on this file. */ protected static int ATTR_PUTABLE = -1 ; // // Attribute relative to DirectoryResource // /** * Attribute index - The index for our relocate attribute. */ protected static int ATTR_RELOCATE = -1 ; /** * Attribute index - our index resource name. */ protected static int ATTR_INDEX = -1 ; /** * Attribute index - our indexes resource name. */ protected static int ATTR_INDEXES = -1 ; /** * Attribute index - The icon directory to use in dir listing. */ protected static int ATTR_ICONDIR = -1 ; /** * Attribute index - Allow the GNN browse method. */ protected static int ATTR_BROWSABLE = -1 ; /** * Attribute index - Style sheet for directory listing */ protected static int ATTR_STYLE_LINK = -1 ; static { Attribute a = null ; Class cls = null ; // Get a pointer to our class: try { cls = Class.forName("org.w3c.jigsaw.frames.HTTPFrame") ; } catch (Exception ex) { ex.printStackTrace() ; System.exit(1) ; } // The quality attribute: a = new DoubleAttribute("quality" , new Double(1.0) , Attribute.EDITABLE); ATTR_QUALITY = AttributeRegistry.registerAttribute(cls, a) ; // The title attribute: a = new StringAttribute("title" , null , Attribute.EDITABLE) ; ATTR_TITLE = AttributeRegistry.registerAttribute(cls, a) ; // The content language attribute: a = new LanguageAttribute("content-language" , null , Attribute.EDITABLE) ; ATTR_CONTENT_LANGUAGE = AttributeRegistry.registerAttribute(cls,a); // The content encoding attribute: a = new EncodingAttribute("content-encoding" , null , Attribute.EDITABLE) ; ATTR_CONTENT_ENCODING = AttributeRegistry.registerAttribute(cls,a); // The content type attribute: a = new MimeTypeAttribute("content-type" , null , Attribute.EDITABLE) ; ATTR_CONTENT_TYPE = AttributeRegistry.registerAttribute(cls,a); // The Charset attribute: a = new StringAttribute("charset" , null , Attribute.EDITABLE) ; ATTR_CHARSET = AttributeRegistry.registerAttribute(cls,a); // The content length attribute: a = new IntegerAttribute("content-length" , null , Attribute.COMPUTED); ATTR_CONTENT_LENGTH = AttributeRegistry.registerAttribute(cls,a); // The icon attribute: a = new StringAttribute("icon" , null , Attribute.EDITABLE) ; ATTR_ICON = AttributeRegistry.registerAttribute(cls, a) ; // The max age attribute (in ms) a = new LongAttribute("maxage" , null , Attribute.EDITABLE) ; ATTR_MAXAGE = AttributeRegistry.registerAttribute(cls, a) ; // Should we send MD5 digest? a = new BooleanAttribute("send-md5" , Boolean.FALSE , Attribute.EDITABLE); ATTR_MD5 = AttributeRegistry.registerAttribute(cls, a) ; // delete allowed for the associated resource ? a = new BooleanAttribute("allow-delete" , Boolean.FALSE , Attribute.EDITABLE); ATTR_ALLOW_DEL = AttributeRegistry.registerAttribute(cls, a) ; // // Attribute relative to a FileResource // // The putable flag: a = new BooleanAttribute("putable" , Boolean.FALSE , Attribute.EDITABLE) ; ATTR_PUTABLE = AttributeRegistry.registerAttribute(cls, a) ; // // Attribute relative to a DirectoryResource // //Should we relocate invalid request to this directory ? a = new BooleanAttribute("relocate" , Boolean.TRUE , Attribute.EDITABLE); ATTR_RELOCATE = AttributeRegistry.registerAttribute(cls, a) ; // Our index resource name (optional). a = new StringAttribute("index" , null , Attribute.EDITABLE) ; ATTR_INDEX = AttributeRegistry.registerAttribute(cls, a) ; // Our indexes resource name a = new StringArrayAttribute("indexes" , null , Attribute.EDITABLE) ; ATTR_INDEXES = AttributeRegistry.registerAttribute(cls, a) ; // Our icon directory. a = new StringAttribute("icondir" , null , Attribute.EDITABLE) ; ATTR_ICONDIR = AttributeRegistry.registerAttribute(cls,a); // The browsable flag: a = new BooleanAttribute("browsable" , Boolean.FALSE , Attribute.EDITABLE) ; ATTR_BROWSABLE = AttributeRegistry.registerAttribute(cls, a) ; // The style sheet attribute: a = new StringAttribute("style-sheet-link" , null , Attribute.EDITABLE) ; ATTR_STYLE_LINK = AttributeRegistry.registerAttribute(cls, a) ; } /** * The associated DirectoryResource (if any) */ protected DirectoryResource dresource = null; /** * The associated FileResource (if any) */ protected FileResource fresource = null; /** * Register this frame to the given resource. * @param resource The resource associated with this frame. */ public void registerResource(FramedResource resource) { super.registerResource(resource); if (resource instanceof FileResource) fresource = (FileResource) resource; else if (resource instanceof DirectoryResource) dresource = (DirectoryResource) resource; } /** * Get the associated FileResource (if any) * @return a FileResource instance or <strong>null</strong> * if no FileResource is associated with this frame. */ public FileResource getFileResource() { return fresource; } /** * Get the associated DirectoryResource (if any) * @return a DirectoryResource instance or <strong>null</strong> * if no DirectoryResource is associated with this frame. */ public DirectoryResource getDirectoryResource() { return dresource; } /** * use this one instead of registerResource if the resource type * doesn't matter or if this is not a file or a directory resource. * In subclasses you should have to do that: * <pre> * public void registerResource(FramedResource resource) { * super.registerOtherResource(resource); * } * </pre> * @param the resource to register. */ public void registerOtherResource(FramedResource resource) { super.registerResource(resource); dresource = null; fresource = null; } // The HTTPResource keeps a cache of ready to use Http values. This // allows to save converting to/from wire rep these objects. Not // much CPU time, but also memory is spared. HttpMimeType contenttype = null; HttpInteger contentlength = null; HttpDate lastmodified = null; HttpTokenList contentencoding = null; HttpTokenList contentlanguage = null; // The Http entity tag for this resource (for FileResource only) HttpEntityTag etag = null; // the MD5 digest for this resource (for FileResource only) HttpString md5Digest = null; /** * Get this resource's help url. * @return An URL, encoded as a String, or <strong>null</strong> if not * available. */ public String getHelpURL() { httpd server = (httpd) getServer(); if ( server == null ) return null; String docurl = server.getDocumentationURL(); if ( docurl == null ) return null; return docurl + "/" + getClass().getName() + ".html"; } /** * Get the help URL for that resource's attribute. * @param topic The topic (can be an attribute name, or a property, etc). * @return A String encoded URL, or <strong>null</strong>. */ public String getHelpURL(String topic) { httpd server = (httpd) getServer(); if ( server == null ) return null; String docurl = server.getDocumentationURL(); if ( docurl == null ) return null; Class defines = AttributeRegistry.getAttributeClass(getClass(), topic); if ( defines != null ) return docurl + "/" + defines.getName() + ".html"; return null; } /** * give the md5 digest from cache or calculate it * @return the HttpString version of the digest */ private HttpString getMd5Digest() { if (md5Digest != null) return md5Digest; // not found, compute it if necessary! Resource r = getResource(); if (r instanceof FileResource) { try { Md5 md5 = new Md5 ( new FileInputStream(((FileResource)r).getFile())); String s = null; try { byte b[] = md5.getDigest(); Base64Encoder b64;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -