📄 ajp12connectionhandler.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]
*
*/
/*
Based on Ajp11ConnectionHandler and Ajp12 implementation of JServ
*/
package org.apache.tomcat.service.connector;
import java.io.*;
import java.net.*;
import java.util.*;
import org.apache.tomcat.core.*;
import org.apache.tomcat.util.*;
import org.apache.tomcat.service.http.*;
import org.apache.tomcat.service.http.HttpResponseAdapter;
import org.apache.tomcat.service.http.HttpRequestAdapter;
import org.apache.tomcat.service.*;
import javax.servlet.*;
import javax.servlet.http.*;
/* Deprecated - must be rewriten to the connector model.
*/
public class Ajp12ConnectionHandler implements TcpConnectionHandler {
static StringManager sm = StringManager.getManager("org.apache.tomcat.service");
ContextManager contextM;
public Ajp12ConnectionHandler() {
}
public Object[] init() {
Object thData[]=new Object[2];
AJP12RequestAdapter reqA=new AJP12RequestAdapter();
AJP12ResponseAdapter resA=new AJP12ResponseAdapter();
contextM.initRequest( reqA, resA );
thData[0]=reqA;
thData[1]=resA;
return thData;
}
public void setAttribute(String name, Object value ) {
if("context.manager".equals(name) ) {
contextM=(ContextManager)value;
}
}
public void setServer( Object contextM ) {
this.contextM=(ContextManager )contextM;
}
public void processConnection(TcpConnection connection, Object[] thData) {
try {
// XXX - Add workarounds for the fact that the underlying
// serverSocket.accept() call can now time out. This whole
// architecture needs some serious review.
if (connection == null)
return;
Socket socket=connection.getSocket();
if (socket == null)
return;
socket.setSoLinger( true, 100);
// socket.setSoTimeout( 1000); // or what ?
AJP12RequestAdapter reqA=null;
AJP12ResponseAdapter resA=null;
if( thData != null ) {
reqA=(AJP12RequestAdapter)thData[0];
resA=(AJP12ResponseAdapter)thData[1];
if( reqA!=null ) reqA.recycle();
if( resA!=null ) resA.recycle();
}
if( reqA==null || resA==null ) {
reqA = new AJP12RequestAdapter();
resA=new AJP12ResponseAdapter();
contextM.initRequest( reqA, resA );
}
InputStream in=socket.getInputStream();
OutputStream out=socket.getOutputStream();
reqA.setSocket( socket);
resA.setOutputStream(out);
reqA.readNextRequest();
if( reqA.isPing )
return;
if( reqA.shutdown )
return;
if (resA.getStatus() >= 400) {
resA.finish();
socket.close();
return;
}
// resolve the server that we are for
int contentLength = reqA.getMimeHeaders().getIntHeader("content-length");
if (contentLength != -1) {
BufferedServletInputStream sis =
(BufferedServletInputStream)reqA.getInputStream();
sis.setLimit(contentLength);
}
contextM.service( reqA, resA );
//resA.finish(); // is part of contextM !
socket.close();
} catch (Exception e) {
// XXX
// this isn't what we want, we want to log the problem somehow
System.out.println("HANDLER THREAD PROBLEM: " + e);
e.printStackTrace();
}
}
}
class AJP12RequestAdapter extends RequestImpl {
static StringManager sm = StringManager.getManager("org.apache.tomcat.service");
Socket socket;
InputStream sin;
Ajpv12InputStream ajpin;
boolean shutdown=false;
boolean isPing=false;
boolean doLog;
public int doRead() throws IOException {
return ajpin.read();
}
public int doRead( byte b[], int off, int len ) throws IOException {
return ajpin.read(b,off,len);
}
void log( String s ) {
contextM.log( s );
}
public AJP12RequestAdapter() {
}
public void setContextManager(ContextManager cm ) {
contextM=cm;
doLog=contextM.getDebug() > 10;
}
public void setSocket( Socket s ) throws IOException {
this.socket = s;
sin = s.getInputStream();
in = new BufferedServletInputStream( this );
ajpin = new Ajpv12InputStream(sin);
}
public AJP12RequestAdapter(ContextManager cm, Socket s) throws IOException {
this.socket = s;
this.contextM=cm;
sin = s.getInputStream();
in = new BufferedServletInputStream( this );
ajpin = new Ajpv12InputStream(sin);
doLog=contextM.getDebug() > 10;
}
protected void readNextRequest() throws IOException {
String dummy,token1,token2;
int marker;
int signal;
// Hashtable env_vars=new Hashtable();
try {
boolean more=true;
while (more) {
marker = ajpin.read();
switch (marker) {
case 0: //NOP marker useful for testing if stream is OK
break;
case 1: //beginning of request
method = ajpin.readString(null); //Method
contextPath = ajpin.readString(null); //Zone
// GS, the following commented line causes the Apache + Jserv + Tomcat
// combination to hang with a 404!!!
// if("ROOT".equals( contextPath ) ) contextPath="";
if("ROOT".equalsIgnoreCase( contextPath ) ) contextPath=null;
if( doLog ) log("AJP: CP=" + contextPath);
if( contextPath!= null )
context=contextM.getContext( contextPath );
if( doLog ) log("AJP: context=" + context );
servletName = ajpin.readString(null); //Servlet
if( doLog ) log("AJP: servlet=" + servletName );
serverName = ajpin.readString(null); //Server hostname
if( doLog ) log("AJP: serverName=" + serverName );
dummy = ajpin.readString(null); //Apache document root
pathInfo = ajpin.readString(null); //Apache parsed path-info
if( doLog ) log("AJP: PI=" + pathInfo );
// XXX Bug in mod_jserv !!!!!
pathTranslated = ajpin.readString(null); //Apache parsed path-translated
if( doLog ) log("AJP: PT=" + pathTranslated );
queryString = ajpin.readString(null); //query string
if( doLog ) log("AJP: QS=" + queryString );
remoteAddr = ajpin.readString(""); //remote address
if( doLog ) log("AJP: RA=" + remoteAddr );
remoteHost = ajpin.readString(""); //remote host
if( doLog ) log("AJP: RH=" + remoteHost );
remoteUser = ajpin.readString(null); //remote user
if( doLog ) log("AJP: RU=" + remoteUser);
authType = ajpin.readString(null); //auth type
if( doLog ) log("AJP: AT=" + authType);
dummy = ajpin.readString(null); //remote port
method = ajpin.readString(null); //request method
if( doLog ) log("AJP: Meth=" + method );
requestURI = ajpin.readString(""); //request uri
if( doLog ) log("AJP: URI: " + requestURI + " CP:" + contextPath + " LP: " + lookupPath);
// XXX don't set lookup path - problems with URL rewriting.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -