2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.server;
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
24 import org.eclipse.jetty.util.BufferUtil;
25 import org.eclipse.jetty.util.Callback;
26 import org.eclipse.jetty.util.SharedBlockingCallback;
27 import org.eclipse.jetty.util.SharedBlockingCallback.Blocker;
28 import org.eclipse.jetty.util.log.Log;
29 import org.eclipse.jetty.util.log.Logger;
31 public class HttpInputOverHTTP extends HttpInput<ByteBuffer> implements Callback
33 private static final Logger LOG = Log.getLogger(HttpInputOverHTTP.class);
34 private final SharedBlockingCallback _readBlocker = new SharedBlockingCallback();
35 private final HttpConnection _httpConnection;
36 private ByteBuffer _content;
39 * @param httpConnection
41 public HttpInputOverHTTP(HttpConnection httpConnection)
43 _httpConnection = httpConnection;
57 protected void blockForContent() throws IOException
61 try (Blocker blocker=_readBlocker.acquire())
63 _httpConnection.fillInterested(blocker);
64 if (LOG.isDebugEnabled())
65 LOG.debug("{} block readable on {}",this,blocker);
69 Object content=getNextContent();
70 if (content!=null || isFinished())
76 public String toString()
78 return String.format("%s@%x",getClass().getSimpleName(),hashCode());
82 protected ByteBuffer nextContent() throws IOException
84 // If we have some content available, return it
85 if (BufferUtil.hasContent(_content))
88 // No - then we are going to need to parse some more content
90 _httpConnection.parseContent();
92 // If we have some content available, return it
93 if (BufferUtil.hasContent(_content))
101 protected int remaining(ByteBuffer item)
103 return item.remaining();
107 protected int get(ByteBuffer item, byte[] buffer, int offset, int length)
109 int l = Math.min(item.remaining(), length);
110 item.get(buffer, offset, l);
115 protected void consume(ByteBuffer item, int length)
117 item.position(item.position()+length);
121 public void content(ByteBuffer item)
123 if (BufferUtil.hasContent(_content))
124 throw new IllegalStateException();
129 protected void unready()
131 _httpConnection.fillInterested(this);
135 public void succeeded()
137 _httpConnection.getHttpChannel().getState().onReadPossible();
141 public void failed(Throwable x)
144 _httpConnection.getHttpChannel().getState().onReadPossible();