📄 ajp12connectionhandler.java
字号:
// need to be fixed.
// if(contextPath!=null && contextPath.length() >0 )
// lookupPath=requestURI.substring( contextPath.length() + 1 );
if( doLog ) log("AJP: URI: " + requestURI + " CP:" + contextPath + " LP: " + lookupPath);
dummy = ajpin.readString(null); //script filename
// System.out.println("AJP: Script filen=" + dummy);
dummy = ajpin.readString(null); //script name
// System.out.println("AJP: Script name=" + dummy);
serverName = ajpin.readString(""); //server name
if( doLog ) log("AJP: serverName=" + serverName );
try {
serverPort = Integer.parseInt(ajpin.readString("80")); //server port
if (serverPort == 443) {
setScheme("https");
}
} catch (Exception any) {
serverPort = 80;
}
dummy = ajpin.readString(""); //server protocol
// System.out.println("AJP: Server proto=" + dummy);
dummy = ajpin.readString(""); //server signature
// System.out.println("AJP: Server sign=" + dummy);
dummy = ajpin.readString(""); //server software
// System.out.println("AJP: Server softw=" + dummy);
jvmRoute = ajpin.readString(""); //JSERV ROUTE
if(jvmRoute.length() == 0) {
jvmRoute = null;
}
if( doLog ) log("AJP: Server jvmRoute=" + jvmRoute);
/**
* The two following lines are commented out because we don't
* want to depend on unreleased versions of the jserv module.
* - costin
* The two following lines are uncommented because JServ 1.1 final
* is far behind.
* NB: you need a recent mod_jserv to use the latest protocol version.
* Theses env vars are simply ignored. (just here for compatibility)
* - jluc
*/
dummy = ajpin.readString("");
dummy = ajpin.readString("");
// XXX all dummy fields will be used after core is changed to make use
// of them!
break;
/**
* Marker = 5 will be used by mod_jserv to send environment vars
* as key+value (dynamically configurable).
* can be considered as "reserved", and safely ignored by other connectors.
* env_vars is (above in this code) commented out for performance issues.
* so theses env vars are simply ignored. (just here for compatibility)
* but it is where mod_jserv would place SSL_* env vars (by exemple)
* See the new parameter for mod_jserv (version > 1.1):
* ApJServEnvVar localname remotename
* - jluc
*/
case 5: // Environment vars
token1 = ajpin.readString(null);
token2 = ajpin.readString("");
/*
* Env variables should go into the request attributes
* table.
*
* Also, if one of the request attributes is HTTPS=on
* assume that there is an SSL connection.
*/
attributes.put(token1, token2);
if(token1.equals("HTTPS") && token2.equals("on")) {
setScheme("https");
}
break;
case 3: // Header
token1 = ajpin.readString(null);
token2 = ajpin.readString("");
headers.putHeader(token1.toLowerCase(), token2);
break;
case 254: // Signal
signal = ajpin.read();
if (signal == 0) { // PING implemented as signal
try {
// close the socket connection after we send reply
socket.getOutputStream().write(0); // PING reply
sin.close();
} catch (IOException ignored) {
System.err.println(ignored);
}
isPing = true;
return;
} else {
try {
// close the socket connection before handling any signal
// but get the addresses first so they are not corrupted
InetAddress serverAddr = socket.getLocalAddress();
InetAddress clientAddr = socket.getInetAddress();
sin.close();
if ( (signal== 15) &&
isSameAddress(serverAddr, clientAddr) ) {
// Shutdown - probably apache was stoped with apachectl stop
contextM.stop();
// same behavior as in past, because it seems that
// stopping everything doesn't work - need to figure
// out what happens with the threads ( XXX )
System.exit(0);
shutdown=true;
return;
}
} catch (Exception ignored) {
System.err.println(ignored);
}
System.err.println("Signal ignored: " + signal);
}
return;
case 4:
case 255:
more=false;
break;
case -1:
throw new java.io.IOException("Stream closed prematurely");
default:
throw new java.io.IOException("Stream broken");
} // switch
} // while
} catch (IOException ioe) {
throw ioe;
} catch (Exception e) {
System.err.println("Uncaught exception" + e);
e.printStackTrace();
}
// REQUEST_URI includes query string
int indexQ=requestURI.indexOf("?");
int rLen=requestURI.length();
if ( (indexQ >-1) && ( indexQ < rLen) ) {
if(doLog) log("Orig QS " + queryString );
queryString = requestURI.substring(indexQ + 1, requestURI.length());
if(doLog) log("New QS " + queryString );
requestURI = requestURI.substring(0, indexQ);
}
if( doLog ) log("Request: " + requestURI );
if( doLog ) log ("Query: " + queryString );
// System.out.println("ENV: " + env_vars );
// System.out.println("HEADERS: " + headers_in );
// System.out.println("PARAMETERS: " + parameters );
//processCookies();
contentLength = headers.getIntHeader("content-length");
contentType = headers.getHeader("content-type");
// XXX
// detect for real whether or not we have more requests
// coming
// XXX
// Support persistent connection in AJP21
//moreRequests = false;
}
/**
* Return <code>true</code> if the specified client and server addresses
* are the same. This method works around a bug in the IBM 1.1.8 JVM on
* Linux, where the address bytes are returned reversed in some
* circumstances.
*
* @param server The server's InetAddress
* @param client The client's InetAddress
*/
private boolean isSameAddress(InetAddress server, InetAddress client) {
// Compare the byte array versions of the two addresses
byte serverAddr[] = server.getAddress();
byte clientAddr[] = client.getAddress();
if (serverAddr.length != clientAddr.length)
return (false);
boolean match = true;
for (int i = 0; i < serverAddr.length; i++) {
if (serverAddr[i] != clientAddr[i]) {
match = false;
break;
}
}
if (match)
return (true);
// Compare the reversed form of the two addresses
for (int i = 0; i < serverAddr.length; i++) {
if (serverAddr[i] != clientAddr[(serverAddr.length-1)-i])
return (false);
}
return (true);
}
}
// Ajp use Status: instead of Status
class AJP12ResponseAdapter extends HttpResponseAdapter {
/** Override setStatus
*/
protected void sendStatus( int status, String message) throws IOException {
printHead("Status: " );
printHead( String.valueOf( status ));
printHead( " " );
printHead( message );
printHead("\r\n");
// We set it here because we extend HttpResponseAdapter, and this is the
// method that is different.
// 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
if( request.getContext() != null)
setHeader("Servlet-Engine", request.getContext().getEngineHeader());
}
}
class Ajpv12InputStream extends BufferedInputStream {
// UTF8 is a strict superset of ASCII.
final static String CHARSET = "UTF8";
public Ajpv12InputStream(InputStream in) {
super(in);
}
public Ajpv12InputStream(InputStream in, int bufsize) {
super(in,bufsize);
}
public int readWord() throws java.io.IOException {
int b1 = read();
if( b1 == -1)
return -1;
int b2 = read();
if ( b2==-1)
return -1;
return ((int)((b1 << 8) | b2)) & 0xffff;
}
public String readString(String def) throws java.io.IOException {
int len = readWord();
if( len == 0xffff)
return def;
if( len == -1)
throw new java.io.IOException("Stream broken");
byte[] b = new byte[len];
int p = 0;
int r;
while(p<len) {
r = read(b,p, len - p);
if( r< 0) {
throw new java.io.IOException("Stream broken, couldn't demarshal string :"+len+":"+p);
}
p = p+r;
}
return new String(b, CHARSET);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -