http11processor.java

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

JAVA
1,300
字号
            if (sArray[i].equals(value)) {                return true;            }        }        return false;    }    /**     * Add restricted user-agent (which will downgrade the connector      * to HTTP/1.0 mode). 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 addRestrictedUserAgent(String userAgent) {    	addStringArray(restrictedUserAgents, userAgent);    }    /**     * Set restricted 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 setRestrictedUserAgents(String[] restrictedUserAgents) {        this.restrictedUserAgents = restrictedUserAgents;    }    /**     * Return the list of restricted user agents.     */    public String[] findRestrictedUserAgents() {        return (restrictedUserAgents);    }    /**     * Set the maximum number of Keep-Alive requests to honor.     * This is to safeguard from DoS attacks.  Setting to a negative     * value disables the check.     */    public void setMaxKeepAliveRequests(int mkar) {        maxKeepAliveRequests = mkar;    }    /**     * Return the number of Keep-Alive requests that we will honor.     */    public int getMaxKeepAliveRequests() {        return maxKeepAliveRequests;    }    /**     * Set the SSL information for this HTTP connection.     */    public void setSSLSupport(SSLSupport sslSupport) {        this.sslSupport = sslSupport;    }    /**     * Set the socket associated with this HTTP connection.     */    public void setSocket(Socket socket)        throws IOException {        this.socket = socket;    }    /**     * Set the flag to control upload time-outs.     */    public void setDisableUploadTimeout(boolean isDisabled) {        disableUploadTimeout = isDisabled;    }    /**     * Get the flag that controls upload time-outs.     */    public boolean getDisableUploadTimeout() {        return disableUploadTimeout;    }    /**     * Set the upload timeout.     */    public void setTimeout( int timeouts ) {        timeout = timeouts ;    }    /**     * Get the upload timeout.     */    public int getTimeout() {        return timeout;    }    /**     * Process pipelined HTTP requests using the specified input and output     * streams.     *      * @param inputStream stream from which the HTTP requests will be read     * @param outputStream stream which will be used to output the HTTP      * responses     * @throws IOException error during an I/O operation     */    public void process(InputStream input, OutputStream output)        throws IOException {        // Set the remote address        remoteAddr = null;        remoteHost = null;        // Setting up the I/O        inputBuffer.setInputStream(input);        outputBuffer.setOutputStream(output);        // Error flag        error = false;        keepAlive = true;        int keepAliveLeft = maxKeepAliveRequests;        int soTimeout = socket.getSoTimeout();        boolean keptAlive = false;        while (started && !error && keepAlive) {            try {                if( !disableUploadTimeout && keptAlive && soTimeout > 0 ) {                    socket.setSoTimeout(soTimeout);                }                inputBuffer.parseRequestLine();                keptAlive = true;                if (!disableUploadTimeout) {                    socket.setSoTimeout(timeout);                }                inputBuffer.parseHeaders();            } catch (IOException e) {                error = true;                break;            } catch (Exception e) {                log.warn("Error parsing HTTP request", e);                // 500 - Bad Request                response.setStatus(400);                error = true;            }            // Setting up filters, and parse some request headers            prepareRequest();            if (maxKeepAliveRequests > 0 && --keepAliveLeft == 0)                keepAlive = false;            // Process the request in the adapter            if (!error) {                try {                    adapter.service(request, response);                } catch (InterruptedIOException e) {                    error = true;                } catch (Throwable t) {                    log.error("Error processing request", t);                    // 500 - Internal Server Error                    response.setStatus(500);                    error = true;                }            }            // Finish the handling of the request            try {                inputBuffer.endRequest();            } catch (IOException e) {                error = true;            } catch (Throwable t) {                log.error("Error finishing request", t);                // 500 - Internal Server Error                response.setStatus(500);                error = true;            }            try {                outputBuffer.endRequest();            } catch (IOException e) {                error = true;            } catch (Throwable t) {                log.error("Error finishing response", t);                error = true;            }            // Next request            inputBuffer.nextRequest();            outputBuffer.nextRequest();        }        // Recycle        inputBuffer.recycle();        outputBuffer.recycle();        // Recycle ssl info        sslSupport = null;    }    // ----------------------------------------------------- ActionHook Methods    /**     * Send an action to the connector.     *      * @param actionCode Type of the action     * @param param Action parameter     */    public void action(ActionCode actionCode, Object param) {        if (actionCode == ActionCode.ACTION_COMMIT) {            // Commit current response            if (response.isCommitted())                return;            // Validate and write response headers            prepareResponse();            try {                outputBuffer.commit();            } catch (IOException e) {                // Set error flag                error = true;            }        } else if (actionCode == ActionCode.ACTION_ACK) {            // Acknowlege request            // Send a 100 status back if it makes sense (response not committed            // yet, and client specified an expectation for 100-continue)            if (response.isCommitted())                return;            MessageBytes expectMB =                 request.getMimeHeaders().getValue("expect");            if ((expectMB != null)                && (expectMB.indexOfIgnoreCase("100-continue", 0) != -1)) {                try {                    outputBuffer.sendAck();                } catch (IOException e) {                    // Set error flag                    error = true;                }            }        } else if (actionCode == ActionCode.ACTION_CLOSE) {            // Close            // End the processing of the current request, and stop any further            // transactions with the client            try {                outputBuffer.endRequest();            } catch (IOException e) {                // Set error flag                error = true;            }        } else if (actionCode == ActionCode.ACTION_RESET) {            // Reset response            // Note: This must be called before the response is committed            outputBuffer.reset();        } else if (actionCode == ActionCode.ACTION_CUSTOM) {            // Do nothing        } else if (actionCode == ActionCode.ACTION_START) {            started = true;        } else if (actionCode == ActionCode.ACTION_STOP) {            started = false;        } else if (actionCode == ActionCode.ACTION_REQ_SSL_ATTRIBUTE ) {            try {                if (sslSupport != null) {                    Object sslO = sslSupport.getCipherSuite();                    if (sslO != null)                        request.setAttribute                            (SSLSupport.CIPHER_SUITE_KEY, sslO);                    sslO = sslSupport.getPeerCertificateChain(false);                    if (sslO != null)                        request.setAttribute                            (SSLSupport.CERTIFICATE_KEY, sslO);                    sslO = sslSupport.getKeySize();                    if (sslO != null)                        request.setAttribute                            (SSLSupport.KEY_SIZE_KEY, sslO);                    sslO = sslSupport.getSessionId();                    if (sslO != null)                        request.setAttribute                            (SSLSupport.SESSION_ID_KEY, sslO);                }            } catch (Exception e) {                log.warn("Exception getting SSL attributes " ,e);            }        } else if (actionCode == ActionCode.ACTION_REQ_HOST_ADDR_ATTRIBUTE) {            if (remoteAddr == null) {                remoteAddr = socket.getInetAddress().getHostAddress();                request.remoteAddr().setString(remoteAddr);            }        } else if (actionCode == ActionCode.ACTION_REQ_HOST_ATTRIBUTE) {            if (remoteAddr == null) {                remoteAddr = socket.getInetAddress().getHostAddress();                request.remoteAddr().setString(remoteAddr);            }            if (remoteHost == null) {                remoteHost = socket.getInetAddress().getHostName();                request.remoteHost().setString(remoteHost);            }        } else if (actionCode == ActionCode.ACTION_REQ_SSL_CERTIFICATE) {            try {                Object sslO = sslSupport.getPeerCertificateChain(true);                if( sslO != null) {                    request.setAttribute                        (SSLSupport.CERTIFICATE_KEY, sslO);                }            } catch (Exception e) {                log.warn("Exception getting SSL Cert",e);            }        }    }    // ------------------------------------------------------ Connector Methods    /**     * Set the associated adapter.     *      * @param adapter the new adapter     */    public void setAdapter(Adapter adapter) {        this.adapter = adapter;    }    /**     * Get the associated adapter.     *      * @return the associated adapter     */    public Adapter getAdapter() {        return adapter;    }    // ------------------------------------------------------ Protected Methods    /**     * After reading the request headers, we have to setup the request filters.     */    protected void prepareRequest() {        http11 = true;        http09 = false;        contentDelimitation = false;        if (sslSupport != null) {            request.scheme().setString("https");        }        MessageBytes protocolMB = request.protocol();        if (protocolMB.equals(Constants.HTTP_11)) {            http11 = true;            protocolMB.setString(Constants.HTTP_11);        } else if (protocolMB.equals(Constants.HTTP_10)) {            http11 = false;            keepAlive = false;            protocolMB.setString(Constants.HTTP_10);        } else if (protocolMB.equals("")) {            // HTTP/0.9            http09 = true;            http11 = false;            keepAlive = false;        } else {            // Unsupported protocol            http11 = false;            error = true;            // Send 505; Unsupported HTTP version            response.setStatus(505);        }        MessageBytes methodMB = request.method();        if (methodMB.equals(Constants.GET)) {            methodMB.setString(Constants.GET);        } else if (methodMB.equals(Constants.POST)) {            methodMB.setString(Constants.POST);        }        // Check connection header        MessageBytes connectionValueMB =             request.getMimeHeaders().getValue("connection");        if (connectionValueMB != null) {            ByteChunk connectionValueBC = connectionValueMB.getByteChunk();            if (findBytes(connectionValueBC, Constants.CLOSE_BYTES) != -1) {                keepAlive = false;            } else if (findBytes(connectionValueBC,                                  Constants.KEEPALIVE_BYTES) != -1) {                keepAlive = true;            }        }        // Check user-agent header        if ((restrictedUserAgents != null) && ((http11) || (keepAlive))) {            MessageBytes userAgentValueMB =                  request.getMimeHeaders().getValue("user-agent");            // Check in the restricted list, and adjust the http11             // and keepAlive flags accordingly            String userAgentValue = userAgentValueMB.toString();

⌨️ 快捷键说明

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