2 // ========================================================================
3 // Copyright (c) 1995-2014 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 LOG.debug("{} block readable on {}",this,blocker);
68 Object content=getNextContent();
69 if (content!=null || isFinished())
75 public String toString()
77 return String.format("%s@%x",getClass().getSimpleName(),hashCode());
81 protected ByteBuffer nextContent() throws IOException
83 // If we have some content available, return it
84 if (BufferUtil.hasContent(_content))
87 // No - then we are going to need to parse some more content
89 _httpConnection.parseContent();
91 // If we have some content available, return it
92 if (BufferUtil.hasContent(_content))
100 protected int remaining(ByteBuffer item)
102 return item.remaining();
106 protected int get(ByteBuffer item, byte[] buffer, int offset, int length)
108 int l = Math.min(item.remaining(), length);
109 item.get(buffer, offset, l);
114 protected void consume(ByteBuffer item, int length)
116 item.position(item.position()+length);
120 public void content(ByteBuffer item)
122 if (BufferUtil.hasContent(_content))
123 throw new IllegalStateException();
128 protected void unready()
130 _httpConnection.fillInterested(this);
134 public void succeeded()
136 _httpConnection.getHttpChannel().getState().onReadPossible();
140 public void failed(Throwable x)
143 _httpConnection.getHttpChannel().getState().onReadPossible();