http11processor.java

来自「Tomcat 4.1与WebServer集成组件的源代码包.」· Java 代码 · 共 1,300 行 · 第 1/3 页

JAVA
1,300
字号
/* * ==================================================================== *  * The Apache Software License, Version 1.1 * * Copyright (c) 1999 The Apache Software Foundation.  All rights  * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer.  * * 2. Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in *    the documentation and/or other materials provided with the *    distribution. * * 3. The end-user documentation included with the redistribution, if *    any, must include the following acknowlegement:   *       "This product includes software developed by the  *        Apache Software Foundation (http://www.apache.org/)." *    Alternately, this acknowlegement may appear in the software itself, *    if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software *    Foundation" must not be used to endorse or promote products derived *    from this software without prior written permission. For written  *    permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache" *    nor may "Apache" appear in their names without prior written *    permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation.  For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * * [Additional notices, if required by prior licensing conditions] * */ package org.apache.coyote.http11;import java.io.EOFException;import java.io.InterruptedIOException;import java.io.InputStream;import java.io.IOException;import java.io.OutputStream;import java.net.Socket;import java.net.InetAddress;import org.apache.tomcat.util.buf.ByteChunk;import org.apache.tomcat.util.buf.MessageBytes;import org.apache.tomcat.util.http.FastHttpDateFormat;import org.apache.tomcat.util.http.MimeHeaders;import org.apache.tomcat.util.buf.Ascii;import org.apache.tomcat.util.buf.HexUtils;import org.apache.tomcat.util.net.SSLSupport;import org.apache.coyote.ActionHook;import org.apache.coyote.ActionCode;import org.apache.coyote.Adapter;import org.apache.coyote.InputBuffer;import org.apache.coyote.OutputBuffer;import org.apache.coyote.Processor;import org.apache.coyote.Request;import org.apache.coyote.Response;import org.apache.coyote.http11.filters.ChunkedInputFilter;import org.apache.coyote.http11.filters.ChunkedOutputFilter;//import org.apache.coyote.http11.filters.GzipInputFilter;import org.apache.coyote.http11.filters.GzipOutputFilter;import org.apache.coyote.http11.filters.IdentityInputFilter;import org.apache.coyote.http11.filters.IdentityOutputFilter;import org.apache.coyote.http11.filters.VoidInputFilter;import org.apache.coyote.http11.filters.VoidOutputFilter;/** * Processes HTTP requests. *  * @author Remy Maucherat */public class Http11Processor implements Processor, ActionHook {    // ----------------------------------------------------------- Constructors    /**     * Default constructor.     */    public Http11Processor() {        request = new Request();        inputBuffer = new InternalInputBuffer(request);        request.setInputBuffer(inputBuffer);        response = new Response();        response.setHook(this);        outputBuffer = new InternalOutputBuffer(response);        response.setOutputBuffer(outputBuffer);        request.setResponse(response);        initializeFilters();    }    // ----------------------------------------------------- Instance Variables    /**     * Associated adapter.     */    protected Adapter adapter = null;    /**     * Request object.     */    protected Request request = null;    /**     * Response object.     */    protected Response response = null;    /**     * Input.     */    protected InternalInputBuffer inputBuffer = null;    /**     * Output.     */    protected InternalOutputBuffer outputBuffer = null;    /**     * State flag.     */    protected boolean started = false;    /**     * Error flag.     */    protected boolean error = false;    /**     * Keep-alive.     */    protected boolean keepAlive = true;    /**     * HTTP/1.1 flag.     */    protected boolean http11 = true;    /**     * HTTP/0.9 flag.     */    protected boolean http09 = false;    /**     * Content delimitator for the request (if false, the connection will     * be closed at the end of the request).     */    protected boolean contentDelimitation = true;    /**     * List of restricted user agents.     */    protected String[] restrictedUserAgents = null;    /**     * Logger.     */    protected static org.apache.commons.logging.Log log         = org.apache.commons.logging.LogFactory.getLog(Http11Processor.class);    /**     * Maximum number of Keep-Alive requests to honor.     */    protected int maxKeepAliveRequests = -1;    /**     * SSL information.     */    protected SSLSupport sslSupport;    /**     * Socket associated with the current connection.     */    protected Socket socket;    /**     * Remote Address associated with the current connection.     */    protected String remoteAddr = null;    /**     * Remote Host associated with the current connection.     */    protected String remoteHost = null;    /**     * Maximum timeout on uploads.     */    protected int timeout = 300000;   // 5 minutes as in Apache HTTPD server    /**     * Flag to disable setting a different time-out on uploads.     */    protected boolean disableUploadTimeout = false;    /**     * Allowed compression level.     */    protected int compressionLevel = 0;    /**     * Minimum contentsize to make compression.     */    protected int compressionMinSize = 2048;    /**     * List of user agents to not use gzip with     */    protected String[] noCompressionUserAgents = null;    /**     * List of MIMES which could be gzipped     */    protected String[] compressableMimeTypes = { "text/html", "text/xml", "text/plain" };    /**     * Host name (used to avoid useless B2C conversion on the host name).     */    protected char[] hostNameC = new char[0];    // ------------------------------------------------------------- Properties    /**     * Return compression level.     */    public String getCompression() {        switch (compressionLevel) {        case 0:            return "off";        case 1:            return "on";        case 2:            return "force";        }        return "off";    }    /**     * Set compression level.     */    public void setCompression(String compression) {        if (compression.equals("on")) {            this.compressionLevel = 1;        } else if (compression.equals("force")) {            this.compressionLevel = 2;        } else if (compression.equals("off")) {            this.compressionLevel = 0;        } else {            try {                // Try to parse compression as an int, which would give the                // minimum compression size                compressionMinSize = Integer.parseInt(compression);                this.compressionLevel = 1;            } catch (Exception e) {                this.compressionLevel = 0;            }        }    }    /**     * Add user-agent for which gzip compression didn't works     * The user agent String given will be exactly matched     * to the user-agent header submitted by the client.     *      * @param userAgent user-agent string     */    public void addNoCompressionUserAgent(String userAgent) {    	addStringArray(noCompressionUserAgents, userAgent);    }    /**     * Set no compression user agent list (this method is best when used with      * a large number of connectors, where it would be better to have all of      * them referenced a single array).     */    public void setNoCompressionUserAgents(String[] noCompressionUserAgents) {        this.noCompressionUserAgents = noCompressionUserAgents;    }    /**     * Return the list of no compression user agents.     */    public String[] findNoCompressionUserAgents() {        return (noCompressionUserAgents);    }    /**     * Add a mime-type which will be compressable     * The mime-type String will be exactly matched     * in the response mime-type header .     *      * @param userAgent user-agent string     */    public void addCompressableMimeType(String mimeType) {    	addStringArray(compressableMimeTypes, mimeType);    }    /**     * Set compressable mime-type list (this method is best when used with      * a large number of connectors, where it would be better to have all of      * them referenced a single array).     */    public void setCompressableMimeType(String[] compressableMimeTypes) {        this.compressableMimeTypes = compressableMimeTypes;    }    /**     * Return the list of restricted user agents.     */    public String[] findCompressableMimeTypes() {        return (compressableMimeTypes);    }    // --------------------------------------------------------- Public Methods    /**     * Add input or output filter.     *      * @param className class name of the filter     */    protected void addFilter(String className) {        try {            Class clazz = Class.forName(className);            Object obj = clazz.newInstance();            if (obj instanceof InputFilter) {                inputBuffer.addFilter((InputFilter) obj);            } else if (obj instanceof OutputFilter) {                outputBuffer.addFilter((OutputFilter) obj);            } else {                // Not a valid filter: log and ignore            }        } catch (Exception e) {            // Log and ignore        }    }    /**     * General use method     *      * @param sArray the StringArray      * @param value string     */    private void addStringArray(String sArray[], String value) {        if (sArray == null)            sArray = new String[0];        String[] results = new String[sArray.length + 1];        for (int i = 0; i < sArray.length; i++)            results[i] = sArray[i];        results[sArray.length] = value;        sArray = results;    }    /**     * General use method     *      * @param sArray the StringArray      * @param value string     */    private boolean inStringArray(String sArray[], String value) {        for (int i = 0; i < sArray.length; i++) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?