Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.protocol.http;
 
 
 import java.util.List;
Class that provides support for dealing with HTTP 100 (Continue) responses.

Note that if a client is pipelining some requests and sending continue for others this could cause problems if the pipelining buffer is enabled.

Author(s):
Stuart Douglas
 
 public class HttpContinue {
 
     public static final String CONTINUE = "100-continue";

    
Returns true if this exchange requires the server to send a 100 (Continue) response.

Parameters:
exchange The exchange
Returns:
true if the server needs to send a continue response
 
     public static boolean requiresContinueResponse(final HttpServerExchange exchange) {
         if (!exchange.isHttp11() || exchange.isResponseStarted()) {
             return false;
         }
         if (exchange.getConnection() instanceof HttpServerConnection) {
             if (((HttpServerConnectionexchange.getConnection()).getExtraBytes() != null) {
                 //we have already received some of the request body
                 //so according to the RFC we do not need to send the Continue
                 return false;
             }
         }
         List<Stringexpect = exchange.getRequestHeaders().get(.);
         if (expect != null) {
             for (String header : expect) {
                 if (header.equalsIgnoreCase()) {
                     return true;
                 }
             }
         }
         return false;
     }

    
Sends a continuation using async IO, and calls back when it is complete.

Parameters:
exchange The exchange
callback The completion callback
 
     public static void sendContinueResponse(final HttpServerExchange exchangefinal IoCallback callback) {
         if (!exchange.isResponseChannelAvailable()) {
             callback.onException(exchangenull..cannotSendContinueResponse());
             return;
         }
         internalSendContinueResponse(exchangecallback);
     }

    
Creates a response sender that can be used to send a HTTP 100-continue response.

Parameters:
exchange The exchange
Returns:
The response sender
 
     public static ContinueResponseSender createResponseSender(final HttpServerExchange exchangethrows IOException {
         if (!exchange.isResponseChannelAvailable()) {
             throw ..cannotSendContinueResponse();
         }
        HttpServerExchange newExchange = exchange.getConnection().sendOutOfBandResponse(exchange);
        newExchange.setResponseCode(100);
        newExchange.getResponseHeaders().put(., 0);
        final StreamSinkChannel responseChannel = newExchange.getResponseChannel();
        return new ContinueResponseSender() {
            boolean shutdown = false;
            @Override
            public boolean send() throws IOException {
                if (!) {
                     = true;
                    responseChannel.shutdownWrites();
                }
                return responseChannel.flush();
            }
            @Override
            public void awaitWritable() throws IOException {
                responseChannel.awaitWritable();
            }
            @Override
            public void awaitWritable(final long timefinal TimeUnit timeUnitthrows IOException {
                responseChannel.awaitWritable(timetimeUnit);
            }
        };
    }

    
Sends a continue response using blocking IO

Parameters:
exchange The exchange
    public static void sendContinueResponseBlocking(final HttpServerExchange exchangethrows IOException {
        if (!exchange.isResponseChannelAvailable()) {
            throw ..cannotSendContinueResponse();
        }
        HttpServerExchange newExchange = exchange.getConnection().sendOutOfBandResponse(exchange);
        newExchange.setResponseCode(100);
        newExchange.getResponseHeaders().put(., 0);
        newExchange.startBlocking();
        newExchange.getOutputStream().close();
        newExchange.getInputStream().close();
    }

    
Sets a 417 response code and ends the exchange.

Parameters:
exchange The exchange to reject
    public static void rejectExchange(final HttpServerExchange exchange) {
        exchange.setResponseCode(417);
        exchange.setPersistent(false);
        exchange.endExchange();
    }
    private static void internalSendContinueResponse(final HttpServerExchange exchangefinal IoCallback callback) {
        HttpServerExchange newExchange = exchange.getConnection().sendOutOfBandResponse(exchange);
        newExchange.setResponseCode(100);
        newExchange.getResponseHeaders().put(., 0);
        final StreamSinkChannel responseChannel = newExchange.getResponseChannel();
        try {
            responseChannel.shutdownWrites();
            if (!responseChannel.flush()) {
                responseChannel.getWriteSetter().set(ChannelListeners.flushingChannelListener(
                        new ChannelListener<StreamSinkChannel>() {
                            @Override
                            public void handleEvent(StreamSinkChannel channel) {
                                channel.suspendWrites();
                                exchange.dispatch(new HttpHandler() {
                                    @Override
                                    public void handleRequest(HttpServerExchange exchangethrows Exception {
                                        callback.onComplete(exchangenull);
                                    }
                                });
                            }
                        }, new ChannelExceptionHandler<Channel>() {
                            @Override
                            public void handleException(Channel channelfinal IOException e) {
                                exchange.dispatch(new HttpHandler() {
                                    @Override
                                    public void handleRequest(HttpServerExchange exchangethrows Exception {
                                        callback.onException(exchangenulle);
                                    }
                                });
                            }}));
                            responseChannel.resumeWrites();
                            exchange.dispatch();
                        }else {
                callback.onComplete(exchangenull);
            }
        } catch (IOException e) {
            callback.onException(exchangenulle);
        }
    }

    
A continue response that is in the process of being sent.
    public interface ContinueResponseSender {

        
Continue sending the response.

Returns:
true if the response is fully sent, false otherwise.
        boolean send() throws IOException;
        void awaitWritable() throws IOException;
        void awaitWritable(long timefinal TimeUnit timeUnitthrows IOException;
    }
New to GrepCode? Check out our FAQ X