ajp13.java

来自「精通tomcat书籍原代码,希望大家共同学习」· Java 代码 · 共 513 行 · 第 1/2 页

JAVA
513
字号
	}

        // Ajp13 messages
	switch(type) {
	case RequestHandler.JK_AJP13_FORWARD_REQUEST:
	    return reqHandler.decodeRequest(this, hBuf, req);
	    
	case RequestHandler.JK_AJP13_CPING_REQUEST:
		return reqHandler.sendCPong(this, outBuf);
		
	case RequestHandler.JK_AJP13_SHUTDOWN:
	    return -2;
	}

	// logged || loging message
	AjpHandler handler=handlers[type];
	if( handler==null ) {
	    logger.log( "Unknown message " + type + handlerName[type] );
	    return 200;
	}

        if( debug > 0 )
            logger.log( "Ajp14 handler " + handler );
	return handler.handleAjpMessage( type, this, hBuf, req );
    }

    // ==================== Servlet Input Support =================

    /** @deprecated -- Will use reqHandler, make sure nobody else
     calls this */

    
    public int available() throws IOException {
        return reqHandler.available(this);
    }

    public int doRead() throws IOException 
    {
        return reqHandler.doRead( this );
    }
    
    public int doRead(byte[] b, int off, int len) throws IOException 
    {
        return reqHandler.doRead( this, b, off, len );
    }
    
    private boolean refillReadBuffer() throws IOException 
    {
        return reqHandler.refillReadBuffer(this);
    }    

    public void beginSendHeaders(int status,
                                 String statusMessage,
                                 int numHeaders) throws IOException {
        reqHandler.beginSendHeaders( this, outBuf,
                                         status, statusMessage,
                                         numHeaders);
    }        

	public void sendHeader(String name, String value) throws IOException {
		reqHandler.sendHeader(  outBuf, name, value );
	}


    public void endSendHeaders() throws IOException {
        reqHandler.endSendHeaders(this, outBuf);
    }

    public void sendHeaders(int status, MimeHeaders headers)
        throws IOException
    {
        reqHandler.sendHeaders(this, outBuf, status,
                                   HttpMessages.getMessage(status),
                                   headers);
    }

    public void sendHeaders(int status, String statusMessage,
                            MimeHeaders headers)
        throws IOException
    {
        reqHandler.sendHeaders( this, outBuf, status,
                                    statusMessage, headers );
    }

    public void finish() throws IOException {
        reqHandler.finish(this, outBuf );
    }

    public void doWrite(byte b[], int off, int len) throws IOException {
        reqHandler.doWrite( this, outBuf, b, off, len );
    }
    

    // ========= Internal Packet-Handling Methods =================

    /**
     * Read N bytes from the InputStream, and ensure we got them all
     * Under heavy load we could experience many fragmented packets
     * just read Unix Network Programming to recall that a call to
     * read didn't ensure you got all the data you want
     *
     * from read() Linux manual
     *
     * On success, the number of bytes read is returned (zero indicates end of file),
     * and the file position is advanced by this number.
     * It is not an error if this number is smaller than the number of bytes requested;
     * this may happen for example because fewer bytes
     * are actually available right now (maybe because we were close to end-of-file,
     * or because we are reading from a pipe, or  from  a
     * terminal),  or  because  read()  was interrupted by a signal.
     * On error, -1 is returned, and errno is set appropriately. In this
     * case it is left unspecified whether the file position (if any) changes.
     *
     **/
    private int readN(InputStream in, byte[] b, int offset, int len) throws IOException {
        int pos = 0;
        int got;

        while(pos < len) {
            got = in.read(b, pos + offset, len - pos);

            if (debug > 10) {
                logger.log("read got # " + got);
            }

            // connection just closed by remote. 
            if (got <= 0) {
                // This happens periodically, as apache restarts
                // periodically.
                // It should be more gracefull ! - another feature for Ajp14 
                return JK_AJP13_COMM_BROKEN;
            }

            pos += got;
        }
        return pos;
     }

    /**
     * Read in a packet from the web server and store it in the passed-in
     * <CODE>Ajp13Packet</CODE> object.
     *
     * @param msg The object into which to store the incoming packet -- any
     * current contents will be overwritten.
     *
     * @return The number of bytes read on a successful read or -1 if there 
     * was an error.
     **/
    public int receive(Ajp13Packet msg) throws IOException {
        if (debug > 0) {
            logger.log("receive()");
        }

	// XXX If the length in the packet header doesn't agree with the
	// actual number of bytes read, it should probably return an error
	// value.  Also, callers of this method never use the length
	// returned -- should probably return true/false instead.
	byte b[] = msg.getBuff();
	
        int rd = readN(in, b, 0, H_SIZE );
        
        // XXX - connection closed (JK_AJP13_COMM_CLOSED)
        //     - connection broken (JK_AJP13_COMM_BROKEN)
        //
        if(rd < 0) {
            // Most likely normal apache restart.
            return rd;
        }
        
	int len = msg.checkIn();
	if( debug > 5 )
            logger.log( "Received " + rd + " " + len + " " + b[0] );
        
	// XXX check if enough space - it's assert()-ed !!!
        
 	int total_read = 0;
        
        total_read = readN(in, b, H_SIZE, len);

        // it's ok to have read 0 bytes when len=0 -- this means
        // the end of the stream has been reached.
        if (total_read < 0) {
            logger.log("can't read body, waited #" + len);
            return JK_AJP13_BAD_BODY;
        }
        
        if (total_read != len) {
            logger.log( "incomplete read, waited #" + len +
                        " got only " + total_read);
            return JK_AJP13_INCOMPLETE_BODY;
        }
        
        if (debug > 0)
            logger.log("receive:  total read = " + total_read);
	return total_read;
    }
    
    /**
     * Send a packet to the web server.  Works for any type of message.
     *
     * @param msg A packet with accumulated data to send to the server --
     * this method will write out the length in the header.  
     */
    public void send( Ajp13Packet msg ) throws IOException {
        if (debug > 0) {
            logger.log("send()");
        }

	msg.end(); // Write the packet header
	byte b[] = msg.getBuff();
	int len  = msg.getLen();

        if (debug > 5 )
            logger.log("send() " + len + " " + b[0] );

	out.write( b, 0, len );
    }
	
    /**
     * Close the socket connection to the web server.  In general, sockets
     * are maintained across many requests, so this will not be called
     * after finish().  
     */
    public void close() throws IOException {
        if (debug > 0) {
            logger.log("close()");
        }

	if(null != out) {        
	    out.close();
	}
	if(null !=in) {
	    in.close();
	}
        setLogged( false );	// no more logged now 
    }

    // -------------------- Debug --------------------
    protected int debug = 0;
    
    public void setDebug(int debug) {
        this.debug = debug;
        this.reqHandler.setDebug(debug);
    }

    public void setLogger(Logger l) {
        this.logger = l;
        this.reqHandler.setLogger(l);
    }

    /**
     * XXX place holder...
     */
    Logger logger = new Logger();
}

⌨️ 快捷键说明

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