| Method from org.apache.coyote.ajp.AjpAprProcessor Detail: |
public void action(ActionCode actionCode,
Object param) {
if (actionCode == ActionCode.ACTION_COMMIT) {
if (response.isCommitted())
return;
// Validate and write response headers
try {
prepareResponse();
} catch (IOException e) {
// Set error flag
error = true;
}
} else if (actionCode == ActionCode.ACTION_CLIENT_FLUSH) {
if (!response.isCommitted()) {
// Validate and write response headers
try {
prepareResponse();
} catch (IOException e) {
// Set error flag
error = true;
return;
}
}
try {
flush();
// Send explicit flush message
if (Socket.sendb(socket, flushMessageBuffer, 0,
flushMessageBuffer.position()) < 0) {
error = true;
}
} catch (IOException e) {
// Set error flag
error = true;
}
} else if (actionCode == ActionCode.ACTION_CLOSE) {
// Close
// End the processing of the current request, and stop any further
// transactions with the client
try {
finish();
} catch (IOException e) {
// Set error flag
error = true;
}
} else if (actionCode == ActionCode.ACTION_START) {
started = true;
} else if (actionCode == ActionCode.ACTION_STOP) {
started = false;
} else if (actionCode == ActionCode.ACTION_REQ_SSL_ATTRIBUTE ) {
if (!certificates.isNull()) {
ByteChunk certData = certificates.getByteChunk();
X509Certificate jsseCerts[] = null;
ByteArrayInputStream bais =
new ByteArrayInputStream(certData.getBytes(),
certData.getStart(),
certData.getLength());
// Fill the first element.
try {
CertificateFactory cf =
CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate)
cf.generateCertificate(bais);
jsseCerts = new X509Certificate[1];
jsseCerts[0] = cert;
request.setAttribute(AprEndpoint.CERTIFICATE_KEY, jsseCerts);
} catch (java.security.cert.CertificateException e) {
log.error(sm.getString("ajpprocessor.certs.fail"), e);
return;
}
}
} else if (actionCode == ActionCode.ACTION_REQ_HOST_ATTRIBUTE) {
// Get remote host name using a DNS resolution
if (request.remoteHost().isNull()) {
try {
request.remoteHost().setString(InetAddress.getByName
(request.remoteAddr().toString()).getHostName());
} catch (IOException iex) {
// Ignore
}
}
} else if (actionCode == ActionCode.ACTION_REQ_LOCAL_ADDR_ATTRIBUTE) {
// Copy from local name for now, which should simply be an address
request.localAddr().setString(request.localName().toString());
} else if (actionCode == ActionCode.ACTION_REQ_SET_BODY_REPLAY) {
// Set the given bytes as the content
ByteChunk bc = (ByteChunk) param;
int length = bc.getLength();
bodyBytes.setBytes(bc.getBytes(), bc.getStart(), length);
request.setContentLength(length);
first = false;
empty = false;
replay = true;
}
}
Send an action to the connector. |
protected void finish() throws IOException {
if (!response.isCommitted()) {
// Validate and write response headers
try {
prepareResponse();
} catch (IOException e) {
// Set error flag
error = true;
}
}
if (finished)
return;
finished = true;
// Add the end message
if (outputBuffer.position() + endMessageArray.length > outputBuffer.capacity()) {
flush();
}
outputBuffer.put(endMessageArray);
flush();
}
|
protected void flush() throws IOException {
if (outputBuffer.position() > 0) {
if (Socket.sendbb(socket, 0, outputBuffer.position()) < 0) {
throw new IOException(sm.getString("ajpprocessor.failedsend"));
}
outputBuffer.clear();
}
}
Callback to write data from the buffer. |
public Adapter getAdapter() {
return adapter;
}
Get the associated adapter. |
public Request getRequest() {
return request;
}
Get the request associated with this processor. |
public boolean getTomcatAuthentication() {
return tomcatAuthentication;
}
|
public void parseHost(MessageBytes valueMB) {
if (valueMB == null || (valueMB != null && valueMB.isNull()) ) {
// HTTP/1.0
// Default is what the socket tells us. Overriden if a host is
// found/parsed
request.setServerPort(endpoint.getPort());
return;
}
ByteChunk valueBC = valueMB.getByteChunk();
byte[] valueB = valueBC.getBytes();
int valueL = valueBC.getLength();
int valueS = valueBC.getStart();
int colonPos = -1;
if (hostNameC.length < valueL) {
hostNameC = new char[valueL];
}
boolean ipv6 = (valueB[valueS] == '[");
boolean bracketClosed = false;
for (int i = 0; i < valueL; i++) {
char b = (char) valueB[i + valueS];
hostNameC[i] = b;
if (b == ']") {
bracketClosed = true;
} else if (b == ':") {
if (!ipv6 || bracketClosed) {
colonPos = i;
break;
}
}
}
if (colonPos < 0) {
if (request.scheme().equalsIgnoreCase("https")) {
// 443 - Default HTTPS port
request.setServerPort(443);
} else {
// 80 - Default HTTTP port
request.setServerPort(80);
}
request.serverName().setChars(hostNameC, 0, valueL);
} else {
request.serverName().setChars(hostNameC, 0, colonPos);
int port = 0;
int mult = 1;
for (int i = valueL - 1; i > colonPos; i--) {
int charValue = HexUtils.DEC[(int) valueB[i + valueS]];
if (charValue == -1) {
// Invalid character
error = true;
// 400 - Bad request
response.setStatus(400);
break;
}
port = port + (charValue * mult);
mult = 10 * mult;
}
request.setServerPort(port);
}
}
|
protected void prepareRequest() {
// Translate the HTTP method code to a String.
byte methodCode = requestHeaderMessage.getByte();
if (methodCode != Constants.SC_M_JK_STORED) {
String methodName = Constants.methodTransArray[(int)methodCode - 1];
request.method().setString(methodName);
}
requestHeaderMessage.getBytes(request.protocol());
requestHeaderMessage.getBytes(request.requestURI());
requestHeaderMessage.getBytes(request.remoteAddr());
requestHeaderMessage.getBytes(request.remoteHost());
requestHeaderMessage.getBytes(request.localName());
request.setLocalPort(requestHeaderMessage.getInt());
boolean isSSL = requestHeaderMessage.getByte() != 0;
if (isSSL) {
request.scheme().setString("https");
}
// Decode headers
MimeHeaders headers = request.getMimeHeaders();
int hCount = requestHeaderMessage.getInt();
for(int i = 0 ; i < hCount ; i++) {
String hName = null;
// Header names are encoded as either an integer code starting
// with 0xA0, or as a normal string (in which case the first
// two bytes are the length).
int isc = requestHeaderMessage.peekInt();
int hId = isc & 0xFF;
MessageBytes vMB = null;
isc &= 0xFF00;
if(0xA000 == isc) {
requestHeaderMessage.getInt(); // To advance the read position
hName = Constants.headerTransArray[hId - 1];
vMB = headers.addValue(hName);
} else {
// reset hId -- if the header currently being read
// happens to be 7 or 8 bytes long, the code below
// will think it's the content-type header or the
// content-length header - SC_REQ_CONTENT_TYPE=7,
// SC_REQ_CONTENT_LENGTH=8 - leading to unexpected
// behaviour. see bug 5861 for more information.
hId = -1;
requestHeaderMessage.getBytes(tmpMB);
ByteChunk bc = tmpMB.getByteChunk();
vMB = headers.addValue(bc.getBuffer(),
bc.getStart(), bc.getLength());
}
requestHeaderMessage.getBytes(vMB);
if (hId == Constants.SC_REQ_CONTENT_LENGTH ||
(hId == -1 && tmpMB.equalsIgnoreCase("Content-Length"))) {
// just read the content-length header, so set it
long cl = vMB.getLong();
if(cl < Integer.MAX_VALUE)
request.setContentLength( (int)cl );
} else if (hId == Constants.SC_REQ_CONTENT_TYPE ||
(hId == -1 && tmpMB.equalsIgnoreCase("Content-Type"))) {
// just read the content-type header, so set it
ByteChunk bchunk = vMB.getByteChunk();
request.contentType().setBytes(bchunk.getBytes(),
bchunk.getOffset(),
bchunk.getLength());
}
}
// Decode extra attributes
boolean secret = false;
byte attributeCode;
while ((attributeCode = requestHeaderMessage.getByte())
!= Constants.SC_A_ARE_DONE) {
switch (attributeCode) {
case Constants.SC_A_REQ_ATTRIBUTE :
requestHeaderMessage.getBytes(tmpMB);
String n = tmpMB.toString();
requestHeaderMessage.getBytes(tmpMB);
String v = tmpMB.toString();
request.setAttribute(n, v);
break;
case Constants.SC_A_CONTEXT :
requestHeaderMessage.getBytes(tmpMB);
// nothing
break;
case Constants.SC_A_SERVLET_PATH :
requestHeaderMessage.getBytes(tmpMB);
// nothing
break;
case Constants.SC_A_REMOTE_USER :
if (tomcatAuthentication) {
// ignore server
requestHeaderMessage.getBytes(tmpMB);
} else {
requestHeaderMessage.getBytes(request.getRemoteUser());
}
break;
case Constants.SC_A_AUTH_TYPE :
if (tomcatAuthentication) {
// ignore server
requestHeaderMessage.getBytes(tmpMB);
} else {
requestHeaderMessage.getBytes(request.getAuthType());
}
break;
case Constants.SC_A_QUERY_STRING :
requestHeaderMessage.getBytes(request.queryString());
break;
case Constants.SC_A_JVM_ROUTE :
requestHeaderMessage.getBytes(request.instanceId());
break;
case Constants.SC_A_SSL_CERT :
request.scheme().setString("https");
// SSL certificate extraction is lazy, moved to JkCoyoteHandler
requestHeaderMessage.getBytes(certificates);
break;
case Constants.SC_A_SSL_CIPHER :
request.scheme().setString("https");
requestHeaderMessage.getBytes(tmpMB);
request.setAttribute(AprEndpoint.CIPHER_SUITE_KEY,
tmpMB.toString());
break;
case Constants.SC_A_SSL_SESSION :
request.scheme().setString("https");
requestHeaderMessage.getBytes(tmpMB);
request.setAttribute(AprEndpoint.SESSION_ID_KEY,
tmpMB.toString());
break;
case Constants.SC_A_SSL_KEY_SIZE :
request.setAttribute(AprEndpoint.KEY_SIZE_KEY,
new Integer(requestHeaderMessage.getInt()));
break;
case Constants.SC_A_STORED_METHOD:
requestHeaderMessage.getBytes(request.method());
break;
case Constants.SC_A_SECRET:
requestHeaderMessage.getBytes(tmpMB);
if (requiredSecret != null) {
secret = true;
if (!tmpMB.equals(requiredSecret)) {
response.setStatus(403);
error = true;
}
}
break;
default:
// Ignore unknown attribute for backward compatibility
break;
}
}
// Check if secret was submitted if required
if ((requiredSecret != null) && !secret) {
response.setStatus(403);
error = true;
}
// Check for a full URI (including protocol://host:port/)
ByteChunk uriBC = request.requestURI().getByteChunk();
if (uriBC.startsWithIgnoreCase("http", 0)) {
int pos = uriBC.indexOf("://", 0, 3, 4);
int uriBCStart = uriBC.getStart();
int slashPos = -1;
if (pos != -1) {
byte[] uriB = uriBC.getBytes();
slashPos = uriBC.indexOf('/", pos + 3);
if (slashPos == -1) {
slashPos = uriBC.getLength();
// Set URI as "/"
request.requestURI().setBytes
(uriB, uriBCStart + pos + 1, 1);
} else {
request.requestURI().setBytes
(uriB, uriBCStart + slashPos,
uriBC.getLength() - slashPos);
}
MessageBytes hostMB = headers.setValue("host");
hostMB.setBytes(uriB, uriBCStart + pos + 3,
slashPos - pos - 3);
}
}
MessageBytes valueMB = request.getMimeHeaders().getValue("host");
parseHost(valueMB);
}
After reading the request headers, we have to setup the request filters. |
protected void prepareResponse() throws IOException {
response.setCommitted(true);
responseHeaderMessage.reset();
responseHeaderMessage.appendByte(Constants.JK_AJP13_SEND_HEADERS);
// HTTP header contents
responseHeaderMessage.appendInt(response.getStatus());
String message = response.getMessage();
if (message == null){
message = HttpMessages.getMessage(response.getStatus());
} else {
message = message.replace('\n", ' ").replace('\r", ' ");
}
tmpMB.setString(message);
responseHeaderMessage.appendBytes(tmpMB);
// Special headers
MimeHeaders headers = response.getMimeHeaders();
String contentType = response.getContentType();
if (contentType != null) {
headers.setValue("Content-Type").setString(contentType);
}
String contentLanguage = response.getContentLanguage();
if (contentLanguage != null) {
headers.setValue("Content-Language").setString(contentLanguage);
}
long contentLength = response.getContentLengthLong();
if (contentLength >= 0) {
headers.setValue("Content-Length").setLong(contentLength);
}
// Other headers
int numHeaders = headers.size();
responseHeaderMessage.appendInt(numHeaders);
for (int i = 0; i < numHeaders; i++) {
MessageBytes hN = headers.getName(i);
int hC = Constants.getResponseAjpIndex(hN.toString());
if (hC > 0) {
responseHeaderMessage.appendInt(hC);
}
else {
responseHeaderMessage.appendBytes(hN);
}
MessageBytes hV=headers.getValue(i);
responseHeaderMessage.appendBytes(hV);
}
// Write to buffer
responseHeaderMessage.end();
outputBuffer.put(responseHeaderMessage.getBuffer(), 0, responseHeaderMessage.getLen());
}
When committing the response, we have to validate the set of headers, as
well as setup the response filters. |
public boolean process(long socket) throws IOException {
RequestInfo rp = request.getRequestProcessor();
rp.setStage(org.apache.coyote.Constants.STAGE_PARSE);
// Setting up the socket
this.socket = socket;
Socket.setrbb(this.socket, inputBuffer);
Socket.setsbb(this.socket, outputBuffer);
// Error flag
error = false;
boolean openSocket = true;
boolean keptAlive = false;
while (started && !error) {
// Parsing the request header
try {
// Get first message of the request
if (!readMessage(requestHeaderMessage, true, keptAlive)) {
// This means that no data is available right now
// (long keepalive), so that the processor should be recycled
// and the method should return true
rp.setStage(org.apache.coyote.Constants.STAGE_ENDED);
break;
}
// Check message type, process right away and break if
// not regular request processing
int type = requestHeaderMessage.getByte();
if (type == Constants.JK_AJP13_CPING_REQUEST) {
if (Socket.sendb(socket, pongMessageBuffer, 0,
pongMessageBuffer.position()) < 0) {
error = true;
}
continue;
} else if(type != Constants.JK_AJP13_FORWARD_REQUEST) {
// Usually the servlet didn't read the previous request body
if(log.isDebugEnabled()) {
log.debug("Unexpected message: "+type);
}
continue;
}
keptAlive = true;
request.setStartTime(System.currentTimeMillis());
} catch (IOException e) {
error = true;
break;
} catch (Throwable t) {
log.debug(sm.getString("ajpprocessor.header.error"), t);
// 400 - Bad Request
response.setStatus(400);
error = true;
}
// Setting up filters, and parse some request headers
rp.setStage(org.apache.coyote.Constants.STAGE_PREPARE);
try {
prepareRequest();
} catch (Throwable t) {
log.debug(sm.getString("ajpprocessor.request.prepare"), t);
// 400 - Internal Server Error
response.setStatus(400);
error = true;
}
// Process the request in the adapter
if (!error) {
try {
rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);
adapter.service(request, response);
} catch (InterruptedIOException e) {
error = true;
} catch (Throwable t) {
log.error(sm.getString("ajpprocessor.request.process"), t);
// 500 - Internal Server Error
response.setStatus(500);
error = true;
}
}
// Finish the response if not done yet
if (!finished) {
try {
finish();
} catch (Throwable t) {
error = true;
}
}
// If there was an error, make sure the request is counted as
// and error, and update the statistics counter
if (error) {
response.setStatus(500);
}
request.updateCounters();
rp.setStage(org.apache.coyote.Constants.STAGE_KEEPALIVE);
recycle();
}
// Add the socket to the poller
if (!error) {
endpoint.getPoller().add(socket);
} else {
openSocket = false;
}
rp.setStage(org.apache.coyote.Constants.STAGE_ENDED);
recycle();
return openSocket;
}
Process pipelined HTTP requests using the specified input and output
streams. |
protected boolean read(int n) throws IOException {
if (inputBuffer.capacity() - inputBuffer.limit() < =
n - inputBuffer.remaining()) {
inputBuffer.compact();
inputBuffer.limit(inputBuffer.position());
inputBuffer.position(0);
}
int nRead;
while (inputBuffer.remaining() < n) {
nRead = Socket.recvbb
(socket, inputBuffer.limit(),
inputBuffer.capacity() - inputBuffer.limit());
if (nRead > 0) {
inputBuffer.limit(inputBuffer.limit() + nRead);
} else {
throw new IOException(sm.getString("ajpprotocol.failedread"));
}
}
return true;
}
Read at least the specified amount of bytes, and place them
in the input buffer. |
protected boolean readMessage(AjpMessage message,
boolean first,
boolean useAvailableData) throws IOException {
byte[] buf = message.getBuffer();
int headerLength = message.getHeaderLength();
if (first) {
if (!readt(headerLength, useAvailableData)) {
return false;
}
} else {
read(headerLength);
}
inputBuffer.get(message.getBuffer(), 0, headerLength);
message.processHeader();
read(message.getLen());
inputBuffer.get(message.getBuffer(), headerLength, message.getLen());
return true;
}
|
protected boolean readt(int n,
boolean useAvailableData) throws IOException {
if (useAvailableData && inputBuffer.remaining() == 0) {
return false;
}
if (inputBuffer.capacity() - inputBuffer.limit() < =
n - inputBuffer.remaining()) {
inputBuffer.compact();
inputBuffer.limit(inputBuffer.position());
inputBuffer.position(0);
}
int nRead;
while (inputBuffer.remaining() < n) {
nRead = Socket.recvbb
(socket, inputBuffer.limit(),
inputBuffer.capacity() - inputBuffer.limit());
if (nRead > 0) {
inputBuffer.limit(inputBuffer.limit() + nRead);
} else {
if ((-nRead) == Status.ETIMEDOUT || (-nRead) == Status.TIMEUP) {
return false;
} else {
throw new IOException(sm.getString("ajpprotocol.failedread"));
}
}
}
return true;
}
Read at least the specified amount of bytes, and place them
in the input buffer. |
public boolean receive() throws IOException {
first = false;
bodyMessage.reset();
readMessage(bodyMessage, false, false);
// No data received.
if (bodyMessage.getLen() == 0) {
// just the header
// Don't mark 'end of stream' for the first chunk.
return false;
}
int blen = bodyMessage.peekInt();
if (blen == 0) {
return false;
}
bodyMessage.getBytes(bodyBytes);
empty = false;
return true;
}
Receive a chunk of data. Called to implement the
'special' packet in ajp13 and to receive the data
after we send a GET_BODY packet |
public void recycle() {
// Recycle Request object
first = true;
endOfStream = false;
empty = true;
replay = false;
finished = false;
request.recycle();
response.recycle();
certificates.recycle();
inputBuffer.clear();
inputBuffer.limit(0);
outputBuffer.clear();
}
|
public void setAdapter(Adapter adapter) {
this.adapter = adapter;
}
Set the associated adapter. |
public void setRequiredSecret(String requiredSecret) {
this.requiredSecret = requiredSecret;
}
|
public void setTomcatAuthentication(boolean tomcatAuthentication) {
this.tomcatAuthentication = tomcatAuthentication;
}
|