Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,11 @@ private void exitRead(long consumed, Throwable readError) throws IOException {
}
long newVal = oldVal - consumed;
state = newVal;
if (allAreClear(state, MASK_COUNT)) {
if (allAreClear(state, FLAG_FINISHED)) {
next.suspendReads();
}
}
}

private void invokeFinishListener() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fl4via I'm not sure what practices Undertow followed re these headers before its move to Commonhaus, but FYI re "Copyright Statements" in https://github.com/commonhaus/foundation/blob/main/policies/ip-policy.md

FWIW, unless something has changed Red Hat isn't looking to have this kind of header added.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bstansberry we have just been copying the same headers since always. I will create a Jira to update the Headers in Undertow. Thanks for the link!


package io.undertow.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import io.undertow.server.handlers.BlockingHandler;
import io.undertow.testutils.DefaultServer;
import io.undertow.testutils.HttpOneOnly;
import io.undertow.testutils.TestHttpClient;
import io.undertow.util.Headers;
import io.undertow.util.StatusCodes;
import org.apache.http.HttpResponse;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.xnio.OptionMap;
import org.xnio.Options;

/**
*
* Tests to ensure no read timeout after an exact read of the content-length
*
* @author Stuart Douglas
* @author Flavia Rainone
* @author Aaron Ogburn
*/
@RunWith(DefaultServer.class)
@HttpOneOnly
public class ExactLengthReadTimeoutTestCase {

private static volatile String message;

private static final String DATA = "1234567890ABCDEF";

private static final int DATA_MULTIPLE = 2048;

@BeforeClass
public static void setup() {
final BlockingHandler blockingHandler = new BlockingHandler();
DefaultServer.setRootHandler(blockingHandler);
blockingHandler.setRootHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) {
try {
final OutputStream outputStream = exchange.getOutputStream();
final InputStream inputStream = exchange.getInputStream();

long length = exchange.getRequestContentLength();
byte[] b = new byte[DATA_MULTIPLE * DATA.length()];
int i = 1;
StringBuilder builder = new StringBuilder();
// read exact content length
while (i > 0 && length > 0) {
i = inputStream.read(b);
if (i > 0) {
length -=i;
builder.append(new String(b, 0, i));
}
}

// this shouldn't cause timeout after complete read
try {
Thread.sleep(200);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

Assert.assertEquals(message, builder.toString());
inputStream.close();
outputStream.close();
} catch (IOException e) {
exchange.getResponseHeaders().put(Headers.CONNECTION, "close");
exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
throw new RuntimeException(e);
}
}
});
}

@DefaultServer.BeforeServerStarts
public static void beforeClass() {
DefaultServer.setServerOptions(OptionMap.create(Options.READ_TIMEOUT, 100));
}

@DefaultServer.AfterServerStops
public static void afterClass() {
DefaultServer.setServerOptions(OptionMap.EMPTY);
}

@Test
public void testExactLengthReadTimeout() throws InterruptedException, IOException {
StringBuilder builder = new StringBuilder(1000 * DATA.length());

for (int i = 0; i < DATA_MULTIPLE; ++i) {
try {
builder.append(DATA);
} catch (Throwable e) {
throw new RuntimeException("test failed with i equal to " + i, e);
}
}

message = builder.toString();
final TestHttpClient client = new TestHttpClient();
try {
HttpPost post = new HttpPost(DefaultServer.getDefaultServerURL() + "/path");
post.setEntity(new StringEntity(message));
post.addHeader(Headers.CONNECTION_STRING, "close");
boolean socketFailure = false;
try {
// Request should succeed.
HttpResponse result = client.execute(post);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
} catch (NoHttpResponseException e) {
Assert.fail("No response was received, this was presumably caused by read-timeout closing the connection.");
}
} finally {
client.getConnectionManager().shutdown();
}
}
}