📄 ajp13.java
字号:
/* * ==================================================================== * * 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.ajp;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.io.InputStream;import java.io.OutputStream;import java.io.ByteArrayInputStream;import java.net.Socket;import java.util.Enumeration;import java.security.cert.X509Certificate;import java.security.cert.CertificateFactory;import org.apache.tomcat.util.buf.MessageBytes;import org.apache.tomcat.util.buf.ByteChunk;import org.apache.tomcat.util.http.MimeHeaders;import org.apache.tomcat.util.http.HttpMessages;import org.apache.tomcat.util.http.BaseRequest;/** * Represents a single, persistent connection between the web server and * the servlet container. Uses the Apache JServ Protocol version 1.3 for * communication. Because this protocal does not multiplex requests, this * connection can only be associated with a single request-handling cycle * at a time.<P> * * This class contains knowledge about how an individual packet is laid out * (via the <CODE>Ajp13Packet</CODE> class), and also about the * stages of communicaton between the server and the servlet container. It * translates from Tomcat's internal servlet support methods * (e.g. doWrite) to the correct packets to send to the web server. * * @see Ajp13Interceptor * * @author Dan Milstein [danmil@shore.net] * @author Keith Wannamaker [Keith@Wannamaker.org] * @author Kevin Seguin [seguin@apache.org] * @author Henri Gomez [hgomez@slib.fr] * @author Costin Manolache */public class Ajp13 { public static final int MAX_PACKET_SIZE=8192; public static final int H_SIZE=4; // Size of basic packet header public static final int MAX_READ_SIZE = MAX_PACKET_SIZE - H_SIZE - 2; public static final int MAX_SEND_SIZE = MAX_PACKET_SIZE - H_SIZE - 4; // Prefix codes for message types from server to container public static final byte JK_AJP13_SHUTDOWN = 7; // Error code for Ajp13 public static final int JK_AJP13_BAD_HEADER = -100; public static final int JK_AJP13_NO_HEADER = -101; public static final int JK_AJP13_COMM_CLOSED = -102; public static final int JK_AJP13_COMM_BROKEN = -103; public static final int JK_AJP13_BAD_BODY = -104; public static final int JK_AJP13_INCOMPLETE_BODY = -105; // ============ Instance Properties ==================== OutputStream out; InputStream in; // Buffer used of output body and headers public Ajp13Packet outBuf = new Ajp13Packet( MAX_PACKET_SIZE ); // Buffer used for input body Ajp13Packet inBuf = new Ajp13Packet( MAX_PACKET_SIZE ); // Buffer used for request head ( and headers ) Ajp13Packet hBuf=new Ajp13Packet( MAX_PACKET_SIZE ); // Holds incoming reads of request body data (*not* header data) byte []bodyBuff = new byte[MAX_READ_SIZE]; int blen; // Length of current chunk of body data in buffer int pos; // Current read position within that buffer boolean end_of_stream; // true if we've received an empty packet // Required handler - essential request processing public RequestHandler reqHandler; // AJP14 - detect protocol,set communication parameters, login // If no password is set, use only Ajp13 messaging boolean backwardCompat=true; boolean logged=false; String secret=null; public Ajp13() { super(); initBuf(); reqHandler=new RequestHandler(); reqHandler.init( this ); } public Ajp13(RequestHandler reqHandler ) { super(); initBuf(); this.reqHandler=reqHandler; reqHandler.init( this ); } /** Will be overriden */ public void initBuf() { outBuf = new Ajp13Packet( MAX_PACKET_SIZE ); inBuf = new Ajp13Packet( MAX_PACKET_SIZE ); hBuf=new Ajp13Packet( MAX_PACKET_SIZE ); } public void recycle() { if (debug > 0) { logger.log("recycle()"); } // This is a touch cargo-cultish, but I think wise. blen = 0; pos = 0; end_of_stream = false; logged=false; } /** * Associate an open socket with this instance. */ public void setSocket( Socket socket ) throws IOException { if (debug > 0) { logger.log("setSocket()"); } socket.setSoLinger( true, 100); out = socket.getOutputStream(); in = socket.getInputStream(); pos = 0; } /** * Backward compat mode, no login needed */ public void setBackward(boolean b) { backwardCompat=b; } public boolean isLogged() { return logged; } void setLogged( boolean b ) { logged=b; } public void setSecret( String s ) { secret=s; } public String getSecret() { return secret; } // -------------------- Handlers registry -------------------- static final int MAX_HANDLERS=32; static final int RESERVED=16; // reserved names, backward compat // Note that we don't make distinction between in and out // messages ( i.e. one id is used only in one direction ) AjpHandler handlers[]=new AjpHandler[MAX_HANDLERS]; String handlerName[]=new String[MAX_HANDLERS]; int currentId=RESERVED; public int registerMessageType( int id, String name, AjpHandler h, String sig[] ) { if( id < 0 ) { // try to find it by name for( int i=0; i< handlerName.length; i++ ) if( name.equals( handlerName[i] ) ) return i; handlerName[currentId]=name; handlers[currentId]=h; currentId++; return currentId; } // fixed id handlerName[id]=name; handlers[id]=h; return id; } // -------------------- Main dispatch -------------------- /** * Read a new packet from the web server and decode it. If it's a * forwarded request, store its properties in the passed-in AjpRequest * object. * * @param req An empty (newly-recycled) request object. * * @return 200 in case of a successful read of a forwarded request, 500 * if there were errors in the reading of the request, and -2 if the * server is asking the container to shut itself down. */ public int receiveNextRequest(BaseRequest req) throws IOException { if (debug > 0) { logger.log("receiveNextRequest()"); } // XXX The return values are awful. int err = 0; // if we receive an IOException here, it must be because // the remote just closed the ajp13 connection, and it's not // an error, we just need to close the AJP13 connection try { err = receive(hBuf); } catch (IOException ioe) { if(debug >0 ) logger.log( "IOException receiving message "); return -1; // Indicate it's a disconnection from the remote end } if(err < 0) { if(debug >0 ) logger.log( "Error receiving message "); return 500; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -