- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1k
[UNDERTOW-2605] ensure ReadTimeoutStreamSourceConduit is cleaned up after exact read of content length #1792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            aogburn
  wants to merge
  2
  commits into
  undertow-io:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
aogburn:EAPSUP-1996main
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +148
        
        
          −0
        
        
          
        
      
    
  
  
     Open
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            143 changes: 143 additions & 0 deletions
          
          143 
        
  core/src/test/java/io/undertow/server/ExactLengthReadTimeoutTestCase.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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. | ||
| */ | ||
|  | ||
| 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(); | ||
| } | ||
| } | ||
| } | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!