Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 35943376b5867086b278c0bd51e9e5072f0d5619 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
//  ========================================================================
//  Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//

package org.eclipse.jetty.server;

import java.io.IOException;
import java.nio.ByteBuffer;

import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.SharedBlockingCallback;
import org.eclipse.jetty.util.SharedBlockingCallback.Blocker;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

public class HttpInputOverHTTP extends HttpInput<ByteBuffer> implements Callback
{
    private static final Logger LOG = Log.getLogger(HttpInputOverHTTP.class);
    private final SharedBlockingCallback _readBlocker = new SharedBlockingCallback();
    private final HttpConnection _httpConnection;
    private ByteBuffer _content;

    /**
     * @param httpConnection
     */
    public HttpInputOverHTTP(HttpConnection httpConnection)
    {
        _httpConnection = httpConnection;
    }

    @Override
    public void recycle()
    {
        synchronized (lock())
        {
            super.recycle();
            _content=null;
        }
    }

    @Override
    protected void blockForContent() throws IOException
    {
        while(true)
        {
            try (Blocker blocker=_readBlocker.acquire())
            {            
                _httpConnection.fillInterested(blocker);
                LOG.debug("{} block readable on {}",this,blocker);
            }

            Object content=getNextContent();
            if (content!=null || isFinished())
                break;
        }
    }

    @Override
    public String toString()
    {
        return String.format("%s@%x",getClass().getSimpleName(),hashCode());
    }

    @Override
    protected ByteBuffer nextContent() throws IOException
    {
        // If we have some content available, return it
        if (BufferUtil.hasContent(_content))
            return _content;

        // No - then we are going to need to parse some more content
        _content=null;
        ByteBuffer requestBuffer = _httpConnection.getRequestBuffer();

        while (!_httpConnection.getParser().isComplete())
        {
            // Can the parser progress (even with an empty buffer)
            _httpConnection.getParser().parseNext(requestBuffer==null?BufferUtil.EMPTY_BUFFER:requestBuffer);

            // If we got some content, that will do for now!
            if (BufferUtil.hasContent(_content))
                return _content;

            // No, we can we try reading some content?
            if (BufferUtil.isEmpty(requestBuffer) && _httpConnection.getEndPoint().isInputShutdown())
            {
                _httpConnection.getParser().atEOF();
                continue;
            }

            // OK lets read some data
            int filled=_httpConnection.getEndPoint().fill(requestBuffer);
            if (LOG.isDebugEnabled()) // Avoid boxing of variable 'filled'
                LOG.debug("{} filled {}",this,filled);
            if (filled<=0)
            {
                if (filled<0)
                {
                    _httpConnection.getParser().atEOF();
                    continue;
                }
                return null;
            }
        }

        return null;

    }

    @Override
    protected int remaining(ByteBuffer item)
    {
        return item.remaining();
    }

    @Override
    protected int get(ByteBuffer item, byte[] buffer, int offset, int length)
    {
        int l = Math.min(item.remaining(), length);
        item.get(buffer, offset, l);
        return l;
    }

    @Override
    protected void consume(ByteBuffer item, int length)
    {
        item.position(item.position()+length);
    }

    @Override
    public void content(ByteBuffer item)
    {
        if (BufferUtil.hasContent(_content))
            throw new IllegalStateException();
        _content=item;
    }

    @Override
    protected void unready()
    {
        _httpConnection.fillInterested(this);
    }

    @Override
    public void succeeded()
    {
        _httpConnection.getHttpChannel().getState().onReadPossible();
    }

    @Override
    public void failed(Throwable x)
    {
        super.failed(x);
        _httpConnection.getHttpChannel().getState().onReadPossible();
    }
}

Back to the top