⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httpadapter.java

📁 这是一个法律事务所系统源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * ==================================================================== * * 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.tomcat.adapter;import org.apache.tomcat.core.*;import org.apache.tomcat.util.*;import java.io.*;import java.net.*;import java.util.*;import javax.servlet.http.*;// We implement Response part of the adapter first, Request will be next/** */public final class HttpAdapter implements BufferListener {    protected OutputStream sout;    static int debug=0;    ContextManager contextM;    // hack - quick solution to keep only one object in pool    RequestImpl req;    ResponseImpl res;    RecycleBufferedInputStream in;        public static final int MAX_HEAD_LEN = 8 * 1024;    byte buf[]=new byte[ MAX_HEAD_LEN ];    int off=0;    int count=0;    // input stream    int readLimit=-1;    int readCount=0;        private Socket socket;    public static final String DEFAULT_CHARACTER_ENCODING = "8859_1";            public HttpAdapter() {        super();    }    // -------------------- Methods used by ConnectionHandler    public void init(ContextManager contextM) {	ByteBuffer ob=new ByteBuffer();	ByteBuffer ib=new ByteBuffer();	ob.addBufferListener( this );	ib.addBufferListener( this );	req=new RequestImpl();	res=new ResponseImpl();	res.setOutputBuffer( ob );	req.setInputBuffer( ib );	this.contextM=contextM;	contextM.initRequest( req, res );    }        public void readNextRequest() throws IOException {	readNextRequest( req, res );	int contentLength = req.getContentLength();	if (contentLength != -1) {	    readLimit=contentLength;	}    }    public void setSocket(Socket socket) throws IOException {	if( in == null )	    in =  new RecycleBufferedInputStream( socket.getInputStream());	else	    in.setInputStream( socket.getInputStream() );	sout= socket.getOutputStream();	        this.socket = socket;    }    public Request getRequest() {	return req;    }    public Response getResponse() {	return res;    }        // -------------------- Other util methods    public void recycle() { 	bufferCount=0;	readLimit=-1;	readCount=0;    }    void log( String s ) {	System.out.println("HttpAdapter: " + s );    }    public void setDebug(int debug) {	this.debug=debug;    }    // -------------------- Buffer listener - implement IN/OUT        /** Read from the input stream to fill a buffer.     *  We read _once_ ( it may be a partial read, if not enough     *  data available). Note that we read at least on byte -     *  it's blocking IO.     */    public void bufferEmpty( BufferEvent ev ) {	try {	    // Find if this is the first chunk , if so do send head 	    if( debug > 0 ) log( "Buffer empty event ");	    	    ByteBuffer bb=(ByteBuffer)ev.getSource();	    ResponseImpl resp=(ResponseImpl)bb.getParent();	    int len=ev.getLength();	    if( readLimit >=0 ) {		if( readCount + len >= readLimit ) {		    len = readLimit - readCount;		}	    }	    int n=0;	    if( len >= 0 )		n=in.read( ev.getByteBuffer(), ev.getOffset(), len );	    ev.setLength( n );	    readCount +=n;	    if( debug > 0 ) log( "Read: " + readLimit + " " + readCount + " " + n + " " + len + " " + ev.getLength());	} catch( IOException ex ) {	    ex.printStackTrace();	}    }    // Called when  we need to send  data    public void bufferFull( BufferEvent ev ) {	try {	    // Find if this is the first chunk , if so do send head 	    if( debug > 0 ) log( "Buffer full event ");	    	    ByteBuffer bb=(ByteBuffer)ev.getSource();	    ResponseImpl resp=(ResponseImpl)bb.getParent();	    	    if( ! resp.isBufferCommitted() ) {		// notify -		resp.notifyEndHeaders();		sendHead( resp );		resp.setBufferCommitted( true );	    }	    sout.write( ev.getByteBuffer(), ev.getOffset(), ev.getLength() );	} catch( IOException ex ) {	    if( ! "Broken pipe".equals(ex.getMessage())) {		ex.printStackTrace();	    }	}    }    // -------------------- Implementation methods--------------------    // Response - sending the head        protected static final int DEFAULT_HEAD_BUFFER_SIZE = 1024;    protected byte[] headBuffer = new byte[DEFAULT_HEAD_BUFFER_SIZE];    protected int bufferCount = 0;        private void sendHead( ResponseImpl resp )  throws IOException {	//	super.endHeaders(); // CM will notify endHeaders(), we just need to	// do our job	if( debug > 0 ) log( "Sending head ");	Request request=resp.getRequest();	int status= resp.getStatus();	// XXX inefficient - will create 2 strings and a hashtable lookup	String message=	ResponseImpl.getMessage( status );		MimeHeaders headers=resp.getMimeHeaders();		headAppend("HTTP/1.0 ");	headAppend(String.valueOf(status));	if(message!=null) {	    headAppend(" ");	    headAppend(message);	}	headAppend("\r\n");	// Hack: set Date header. We need to implement append(date)	MimeHeaderField dateH= headers.find( "Date" );	if( false && dateH == null ) {	    // no date header set by user	    dateH=headers.putHeader();	    dateH.setName("Date");	    dateH.setDateValue(System.currentTimeMillis());	    // will reuse the HttpDate instance	}		// Servlet Engine header will be set per/adapter - smarter adapters will	// not send it every time ( have it in C side ), and we may also want	// to add informations about the adapter used 

⌨️ 快捷键说明

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