Skip to content

Commit

Permalink
467603 - Response 401 from server hangs client.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbordet committed May 19, 2015
1 parent e5fac30 commit c7cff6e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ private HttpConversation newConversation()
return new HttpConversation();
}

protected List<ProtocolHandler> getProtocolHandlers()
public List<ProtocolHandler> getProtocolHandlers()
{
return handlers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeoutException;

import javax.servlet.AsyncContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
Expand Down Expand Up @@ -235,10 +236,10 @@ protected HttpClient createHttpClient() throws ServletException

HttpClient client = newHttpClient();

// Redirects must be proxied as is, not followed
// Redirects must be proxied as is, not followed.
client.setFollowRedirects(false);

// Must not store cookies, otherwise cookies of different clients will mix
// Must not store cookies, otherwise cookies of different clients will mix.
client.setCookieStore(new HttpCookieStore.Empty());

Executor executor;
Expand Down Expand Up @@ -289,9 +290,12 @@ protected HttpClient createHttpClient() throws ServletException
{
client.start();

// Content must not be decoded, otherwise the client gets confused
// Content must not be decoded, otherwise the client gets confused.
client.getContentDecoderFactories().clear();

// No protocol handlers, pass everything to the client.
client.getProtocolHandlers().clear();

return client;
}
catch (Exception x)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Queue;
import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPOutputStream;

import javax.servlet.AsyncContext;
import javax.servlet.ReadListener;
import javax.servlet.ServletConfig;
Expand Down Expand Up @@ -420,7 +421,7 @@ public void onContent(final Response serverResponse, ByteBuffer content, final C

length += contentBytes;

boolean finished = contentLength > 0 && length == contentLength;
boolean finished = contentLength >= 0 && length == contentLength;
transform(transformer, content, finished, buffers);

int newContentBytes = 0;
Expand Down Expand Up @@ -452,7 +453,7 @@ public void onContent(final Response serverResponse, ByteBuffer content, final C
}
else
{
if (contentLength > 0)
if (contentLength >= 0)
proxyResponse.setContentLength(-1);

// Setting the WriteListener triggers an invocation to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

Expand Down Expand Up @@ -1241,6 +1242,45 @@ public boolean transform(Source source, Sink sink) throws IOException
Assert.assertEquals(value1, obj.get(key1));
}

@Test
public void testServer401() throws Exception
{
startServer(new HttpServlet()
{
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setStatus(HttpStatus.UNAUTHORIZED_401);
response.setHeader(HttpHeader.WWW_AUTHENTICATE.asString(), "Basic realm=\"test\"");
}
});
final AtomicBoolean transformed = new AtomicBoolean();
startProxy(new AsyncMiddleManServlet()
{
@Override
protected ContentTransformer newServerResponseContentTransformer(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse)
{
return new AfterContentTransformer()
{
@Override
public boolean transform(Source source, Sink sink) throws IOException
{
transformed.set(true);
return false;
}
};
}
});
startClient();

ContentResponse response = client.newRequest("localhost", serverConnector.getLocalPort())
.timeout(5, TimeUnit.SECONDS)
.send();

Assert.assertEquals(HttpStatus.UNAUTHORIZED_401, response.getStatus());
Assert.assertFalse(transformed.get());
}

private Path prepareTargetTestsDir() throws IOException
{
final Path targetTestsDir = MavenTestingUtils.getTargetTestingDir().toPath();
Expand Down

0 comments on commit c7cff6e

Please sign in to comment.