Skip to content

Commit

Permalink
Add a standard method for returning input to the SocketWrapper.
Browse files Browse the repository at this point in the history
Use this to make the code consistent between upgrade processor
implementations.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1637940 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed Nov 10, 2014
1 parent c45f530 commit 966398b
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 34 deletions.
6 changes: 2 additions & 4 deletions java/org/apache/coyote/http11/upgrade/AprProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.jni.Socket;
import org.apache.tomcat.util.net.AprEndpoint.AprSocketWrapper;
import org.apache.tomcat.util.net.SocketWrapperBase;

public class AprProcessor extends AbstractProcessor<Long> {
Expand All @@ -39,7 +37,7 @@ public AprProcessor(SocketWrapperBase<Long> wrapper, ByteBuffer leftOverInput,
super(httpUpgradeHandler,
new UpgradeServletInputStream(wrapper),
new UpgradeServletOutputStream(wrapper, asyncWriteBufferSize));
((AprSocketWrapper) wrapper).setLeftOverInput(leftOverInput);
Socket.timeoutSet(wrapper.getSocket().longValue(), INFINITE_TIMEOUT);
wrapper.unRead(leftOverInput);
wrapper.setTimeout(INFINITE_TIMEOUT);
}
}
5 changes: 1 addition & 4 deletions java/org/apache/coyote/http11/upgrade/Nio2Processor.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ public Nio2Processor(SocketWrapperBase<Nio2Channel> wrapper, ByteBuffer leftover
super(httpUpgradeHandler,
new UpgradeServletInputStream(wrapper),
new UpgradeServletOutputStream(wrapper, asyncWriteBufferSize));

wrapper.unRead(leftoverInput);
wrapper.setTimeout(INFINITE_TIMEOUT);
if (leftoverInput != null) {
wrapper.getSocket().getBufHandler().getReadBuffer().put(leftoverInput);
}
}
}
12 changes: 1 addition & 11 deletions java/org/apache/coyote/http11/upgrade/NioProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,7 @@ public NioProcessor(SocketWrapperBase<NioChannel> wrapper, ByteBuffer leftoverIn
super(httpUpgradeHandler,
new UpgradeServletInputStream(wrapper),
new UpgradeServletOutputStream(wrapper, asyncWriteBufferSize));

wrapper.unRead(leftoverInput);
wrapper.setTimeout(INFINITE_TIMEOUT);
if (leftoverInput != null) {
ByteBuffer readBuffer = wrapper.getSocket().getBufHandler().getReadBuffer();
if (readBuffer.remaining() > 0) {
readBuffer.flip();
} else {
readBuffer.clear();
}
readBuffer.put(leftoverInput);
readBuffer.flip();
}
}
}
32 changes: 17 additions & 15 deletions java/org/apache/tomcat/util/net/AprEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -2357,7 +2357,7 @@ public static class AprSocketWrapper extends SocketWrapperBase<Long> {

private final ByteBuffer sslOutputBuffer;

private volatile ByteBuffer leftOverInput;
private volatile ByteBuffer returnedInput;
private volatile boolean eagain = false;
private volatile boolean closed = false;

Expand All @@ -2377,28 +2377,20 @@ public AprSocketWrapper(Long socket, AprEndpoint endpoint) {
}


public void setLeftOverInput(ByteBuffer leftOverInput) {
if (leftOverInput != null) {
this.leftOverInput = ByteBuffer.allocate(leftOverInput.remaining());
this.leftOverInput.put(leftOverInput);
}
}


@Override
public int read(boolean block, byte[] b, int off, int len) throws IOException {

if (closed) {
throw new IOException(sm.getString("socket.apr.closed", getSocket()));
}

if (leftOverInput != null) {
if (leftOverInput.remaining() < len) {
len = leftOverInput.remaining();
if (returnedInput != null) {
if (returnedInput.remaining() < len) {
len = returnedInput.remaining();
}
leftOverInput.get(b, off, len);
if (leftOverInput.remaining() == 0) {
leftOverInput = null;
returnedInput.get(b, off, len);
if (returnedInput.remaining() == 0) {
returnedInput = null;
}
return len;
}
Expand Down Expand Up @@ -2475,6 +2467,16 @@ public boolean isReady() {
}



@Override
public void unRead(ByteBuffer input) {
if (returnedInput != null) {
this.returnedInput = ByteBuffer.allocate(returnedInput.remaining());
this.returnedInput.put(returnedInput);
}
}


@Override
public void close() {
closed = true;
Expand Down
8 changes: 8 additions & 0 deletions java/org/apache/tomcat/util/net/Nio2Endpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,14 @@ public int read(boolean block, byte[] b, int off, int len) throws IOException {
}


@Override
public void unRead(ByteBuffer returnedInput) {
if (returnedInput != null) {
getSocket().getBufHandler().getReadBuffer().put(returnedInput);
}
}


@Override
public void close() throws IOException {
getSocket().close();
Expand Down
15 changes: 15 additions & 0 deletions java/org/apache/tomcat/util/net/NioEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,21 @@ public int read(boolean block, byte[] b, int off, int len)
}


@Override
public void unRead(ByteBuffer returnedInput) {
if (returnedInput != null) {
ByteBuffer readBuffer = getSocket().getBufHandler().getReadBuffer();
if (readBuffer.remaining() > 0) {
readBuffer.flip();
} else {
readBuffer.clear();
}
readBuffer.put(returnedInput);
readBuffer.flip();
}
}


@Override
public void close() throws IOException {
getSocket().close();
Expand Down
13 changes: 13 additions & 0 deletions java/org/apache/tomcat/util/net/SocketWrapperBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.tomcat.util.net;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
Expand Down Expand Up @@ -188,6 +189,18 @@ public String toString() {

public abstract int read(boolean block, byte[] b, int off, int len) throws IOException;
public abstract boolean isReady() throws IOException;
/**
* Return input that has been read to the input buffer for re-reading by the
* correct component. There are times when a component may read more data
* than it needs before it passes control to another component. One example
* of this is during HTTP upgrade. If an (arguably misbehaving client) sends
* data associated with the upgraded protocol before the HTTP upgrade
* completes, the HTTP handler may read it. This method provides a way for
* that data to be returned so it can be processed by the correct component.
*
* @param input The input to return to the input buffer.
*/
public abstract void unRead(ByteBuffer input);
public abstract void close() throws IOException;

public abstract int write(boolean block, byte[] b, int off, int len) throws IOException;
Expand Down

0 comments on commit 966398b

Please sign in to comment.