File: TestCancelledUpload.java

package info (click to toggle)
tomcat9 9.0.115-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 48,140 kB
  • sloc: java: 383,595; xml: 71,225; jsp: 4,682; sh: 1,228; perl: 324; makefile: 18; ansic: 14
file content (174 lines) | stat: -rw-r--r-- 6,463 bytes parent folder | download
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
172
173
174
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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 org.apache.coyote.http2;

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Assert;
import org.junit.Test;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;

public class TestCancelledUpload extends Http2TestBase {

    @Test
    public void testCancelledRequest() throws Exception {
        http2Connect();

        http2Protocol.setAllowedTrailerHeaders(TRAILER_HEADER_NAME);

        int bodySize = 8192;
        int bodyCount = 20;

        byte[] headersFrameHeader = new byte[9];
        ByteBuffer headersPayload = ByteBuffer.allocate(128);
        byte[] dataFrameHeader = new byte[9];
        ByteBuffer dataPayload = ByteBuffer.allocate(bodySize);
        byte[] trailerFrameHeader = new byte[9];
        ByteBuffer trailerPayload = ByteBuffer.allocate(256);

        buildPostRequest(headersFrameHeader, headersPayload, false, dataFrameHeader, dataPayload, null, true, 3);
        buildTrailerHeaders(trailerFrameHeader, trailerPayload, 3);

        // Write the headers
        writeFrame(headersFrameHeader, headersPayload);
        // Body
        for (int i = 0; i < bodyCount; i++) {
            writeFrame(dataFrameHeader, dataPayload);
        }

        // Trailers
        writeFrame(trailerFrameHeader, trailerPayload);

        // The Server will process the request on a separate thread to the
        // incoming frames.
        // The request processing thread will:
        // - read up to 128 bytes of request body
        // (and issue a window update for bytes read)
        // - write a 403 response with no response body
        // The connection processing thread will:
        // - read the request body until the flow control window is exhausted
        // - reset the stream if further DATA frames are received
        parser.readFrame();

        // Check for reset and exit if found
        if (checkReset()) {
            return;
        }

        // Not window update, not reset, must be the headers
        Assert.assertEquals("3-HeadersStart\n" + "3-Header-[:status]-[403]\n" + "3-Header-[content-length]-[0]\n" +
                "3-Header-[date]-[Wed, 11 Nov 2015 19:18:42 GMT]\n" + "3-HeadersEnd\n", output.getTrace());
        output.clearTrace();
        parser.readFrame();

        // Check for reset and exit if found
        if (checkReset()) {
            return;
        }

        // Not window update, not reset, must be the response body
        Assert.assertEquals("3-Body-0\n" + "3-EndOfStream\n", output.getTrace());
        output.clearTrace();
        parser.readFrame();

        Assert.assertTrue(checkReset());

        // If there are any more frames after this, ignore them
    }


    /*
     * Looking for a RST frame with error type 3 (flow control error).
     *
     * If there is a flow control window update for stream 0 it may be followed by one for stream 3.
     *
     * If there is a flow control window update for stream 3 it will always be preceded by one for stream 0.
     */
    private boolean checkReset() throws IOException, Http2Exception {
        int lastConnectionFlowControlWindowUpdate = -1;
        while (true) {
            String trace = output.getTrace();
            if (trace.startsWith("3-RST-[3]\n")) {
                // This is the reset we are looking for
                return true;
            } else if (trace.startsWith("3-RST-[")) {
                // Probably error type 8 (cancel) - ignore
            } else if (trace.startsWith("0-WindowSize-[")) {
                // Connection flow control window update
                lastConnectionFlowControlWindowUpdate = Integer.parseInt(trace.substring(14, trace.length() - 2));
            } else if (trace.startsWith("3-WindowSize-[")) {
                // Stream flow control window update
                // Must be same size as last connection flow control window
                // update. True for Tomcat anyway.
                Assert.assertEquals("3-WindowSize-[" + lastConnectionFlowControlWindowUpdate + "]\n", trace);
            } else {
                return false;
            }
            output.clearTrace();
            parser.readFrame();
        }
    }


    @Override
    protected void configureAndStartWebApplication() throws LifecycleException {
        Tomcat tomcat = getTomcatInstance();

        // Retain '/simple' url-pattern since it enables code re-use
        Context ctxt = getProgrammaticRootContext();
        Tomcat.addServlet(ctxt, "cancel", new CancelServlet());
        ctxt.addServletMappingDecoded("/simple", "cancel");

        tomcat.start();
    }


    private static class CancelServlet extends SimpleServlet {

        private static final long serialVersionUID = 1L;

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            // Read up to 128 bytes and then return a 403 response

            InputStream is = req.getInputStream();
            byte[] buf = new byte[128];
            int toRead = 128;

            int read = is.read(buf);
            while (read != -1 && toRead > 0) {
                toRead -= read;
                read = is.read(buf);
            }

            if (toRead == 0) {
                resp.setStatus(HttpServletResponse.SC_FORBIDDEN);
            } else {
                resp.setStatus(HttpServletResponse.SC_OK);
            }
        }
    }
}