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.handlers;
 
 
Handler for requests that require 100-continue responses. If an attempt is made to read from the source channel then a 100 continue response is sent.

Author(s):
Stuart Douglas
 
 public class HttpContinueReadHandler implements HttpHandler {
 
     private static final ConduitWrapper<StreamSourceConduitWRAPPER = new ConduitWrapper<StreamSourceConduit>() {
         @Override
         public StreamSourceConduit wrap(final ConduitFactory<StreamSourceConduitfactoryfinal HttpServerExchange exchange) {
             if(exchange.isRequestChannelAvailable() && !exchange.isResponseStarted()) {
                 return new ContinueConduit(factory.create(), exchange);
             }
             return factory.create();
         }
     };
 
     private final HttpHandler handler;
 
     public HttpContinueReadHandler(final HttpHandler handler) {
         this. = handler;
     }
 
     @Override
     public void handleRequest(final HttpServerExchange exchangethrows Exception {
         if (HttpContinue.requiresContinueResponse(exchange)) {
             exchange.addRequestWrapper();
         }
         .handleRequest(exchange);
     }
 
     private static final class ContinueConduit extends AbstractStreamSourceConduit<StreamSourceConduitimplements StreamSourceConduit {
 
         private boolean sent = false;
         private HttpContinue.ContinueResponseSender response = null;
         private final HttpServerExchange exchange;
 
 
         protected ContinueConduit(final StreamSourceConduit nextfinal HttpServerExchange exchange) {
             super(next);
             this. = exchange;
         }
 
         @Override
         public long transferTo(final long positionfinal long countfinal FileChannel targetthrows IOException {
             if (.getResponseCode() == 417) {
                 //rejected
                 return -1;
             }
             if (!) {
                  = true;
                  = HttpContinue.createResponseSender();
             }
             if ( != null) {
                 if (!.send()) {
                     return 0;
                 }
                  = null;
             }
             return super.transferTo(positioncounttarget);
         }
 
         @Override
         public long transferTo(final long countfinal ByteBuffer throughBufferfinal StreamSinkChannel targetthrows IOException {
            if (.getResponseCode() == 417) {
                //rejected
                return -1;
            }
            if (!) {
                 = true;
                 = HttpContinue.createResponseSender();
            }
            if ( != null) {
                if (!.send()) {
                    return 0;
                }
                 = null;
            }
            return super.transferTo(countthroughBuffertarget);
        }
        @Override
        public int read(final ByteBuffer dstthrows IOException {
            if (.getResponseCode() == 417) {
                //rejected
                return -1;
            }
            if (!) {
                 = true;
                 = HttpContinue.createResponseSender();
            }
            if ( != null) {
                if (!.send()) {
                    return 0;
                }
                 = null;
            }
            return super.read(dst);
        }
        @Override
        public long read(final ByteBuffer[] dstsfinal int offsfinal int lenthrows IOException {
            if (.getResponseCode() == 417) {
                //rejected
                return -1;
            }
            if (!) {
                 = true;
                 = HttpContinue.createResponseSender();
            }
            if ( != null) {
                if (!.send()) {
                    return 0;
                }
                 = null;
            }
            return super.read(dstsoffslen);
        }
        @Override
        public void awaitReadable(final long timefinal TimeUnit timeUnitthrows IOException {
            if (.getResponseCode() == 417) {
                //rejected
                return;
            }
            if (!) {
                 = true;
                 = HttpContinue.createResponseSender();
            }
            long exitTime = System.currentTimeMillis() + timeUnit.toMillis(time);
            if ( != null) {
                while (!.send()) {
                    long currentTime = System.currentTimeMillis();
                    if (currentTime > exitTime) {
                        return;
                    }
                    .awaitWritable(exitTime - currentTime.);
                }
                 = null;
            }
            long currentTime = System.currentTimeMillis();
            super.awaitReadable(exitTime - currentTime.);
        }
        @Override
        public void awaitReadable() throws IOException {
            if (.getResponseCode() == 417) {
                //rejected
                return;
            }
            if (!) {
                 = true;
                 = HttpContinue.createResponseSender();
            }
            if ( != null) {
                while (!.send()) {
                    .awaitWritable();
                }
                 = null;
            }
            super.awaitReadable();
        }
    }
New to GrepCode? Check out our FAQ X