]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/server/HttpInputOverHTTP.java
Importing upstream Jetty jetty-9.2.1.v20140609
[gigi.git] / lib / jetty / org / eclipse / jetty / server / HttpInputOverHTTP.java
1 //
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.
8 //
9 //      The Eclipse Public License is available at
10 //      http://www.eclipse.org/legal/epl-v10.html
11 //
12 //      The Apache License v2.0 is available at
13 //      http://www.opensource.org/licenses/apache2.0.php
14 //
15 //  You may elect to redistribute this code under either of these licenses.
16 //  ========================================================================
17 //
18
19 package org.eclipse.jetty.server;
20
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
23
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;
30
31 public class HttpInputOverHTTP extends HttpInput<ByteBuffer> implements Callback
32 {
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;
37
38     /**
39      * @param httpConnection
40      */
41     public HttpInputOverHTTP(HttpConnection httpConnection)
42     {
43         _httpConnection = httpConnection;
44     }
45
46     @Override
47     public void recycle()
48     {
49         synchronized (lock())
50         {
51             super.recycle();
52             _content=null;
53         }
54     }
55
56     @Override
57     protected void blockForContent() throws IOException
58     {
59         while(true)
60         {
61             try (Blocker blocker=_readBlocker.acquire())
62             {            
63                 _httpConnection.fillInterested(blocker);
64                 LOG.debug("{} block readable on {}",this,blocker);
65                 blocker.block();
66             }
67
68             Object content=getNextContent();
69             if (content!=null || isFinished())
70                 break;
71         }
72     }
73
74     @Override
75     public String toString()
76     {
77         return String.format("%s@%x",getClass().getSimpleName(),hashCode());
78     }
79
80     @Override
81     protected ByteBuffer nextContent() throws IOException
82     {
83         // If we have some content available, return it
84         if (BufferUtil.hasContent(_content))
85             return _content;
86
87         // No - then we are going to need to parse some more content
88         _content=null;
89         _httpConnection.parseContent();
90         
91         // If we have some content available, return it
92         if (BufferUtil.hasContent(_content))
93             return _content;
94
95         return null;
96
97     }
98
99     @Override
100     protected int remaining(ByteBuffer item)
101     {
102         return item.remaining();
103     }
104
105     @Override
106     protected int get(ByteBuffer item, byte[] buffer, int offset, int length)
107     {
108         int l = Math.min(item.remaining(), length);
109         item.get(buffer, offset, l);
110         return l;
111     }
112
113     @Override
114     protected void consume(ByteBuffer item, int length)
115     {
116         item.position(item.position()+length);
117     }
118
119     @Override
120     public void content(ByteBuffer item)
121     {
122         if (BufferUtil.hasContent(_content))
123             throw new IllegalStateException();
124         _content=item;
125     }
126
127     @Override
128     protected void unready()
129     {
130         _httpConnection.fillInterested(this);
131     }
132
133     @Override
134     public void succeeded()
135     {
136         _httpConnection.getHttpChannel().getState().onReadPossible();
137     }
138
139     @Override
140     public void failed(Throwable x)
141     {
142         super.failed(x);
143         _httpConnection.getHttpChannel().getState().onReadPossible();
144     }
145 }