1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */

17 package org.apache.coyote.http11.filters;
18
19 import java.io.IOException;
20 import java.nio.charset.StandardCharsets;
21
22 import org.apache.coyote.InputBuffer;
23 import org.apache.coyote.Request;
24 import org.apache.coyote.http11.InputFilter;
25 import org.apache.tomcat.util.buf.ByteChunk;
26 import org.apache.tomcat.util.net.ApplicationBufferHandler;
27
28 /**
29  * Void input filter, which returns -1 when attempting a read. Used with a GET,
30  * HEAD, or a similar request.
31  *
32  * @author Remy Maucherat
33  */

34 public class VoidInputFilter implements InputFilter {
35
36
37     // -------------------------------------------------------------- Constants
38
39     protected static final String ENCODING_NAME = "void";
40     protected static final ByteChunk ENCODING = new ByteChunk();
41
42
43     // ----------------------------------------------------- Static Initializer
44
45     static {
46         ENCODING.setBytes(ENCODING_NAME.getBytes(StandardCharsets.ISO_8859_1),
47                 0, ENCODING_NAME.length());
48     }
49
50
51     // ---------------------------------------------------- InputBuffer Methods
52
53     @Override
54     public int doRead(ApplicationBufferHandler handler) throws IOException {
55         return -1;
56     }
57
58
59     // ---------------------------------------------------- InputFilter Methods
60
61     /**
62      * Set the associated request.
63      */

64     @Override
65     public void setRequest(Request request) {
66         // NOOP: Request isn't used so ignore it
67     }
68
69
70     /**
71      * Set the next buffer in the filter pipeline.
72      */

73     @Override
74     public void setBuffer(InputBuffer buffer) {
75         // NOOP: No body to read
76     }
77
78
79     /**
80      * Make the filter ready to process the next request.
81      */

82     @Override
83     public void recycle() {
84         // NOOP
85     }
86
87
88     /**
89      * Return the name of the associated encoding; Here, the value is
90      * "void".
91      */

92     @Override
93     public ByteChunk getEncodingName() {
94         return ENCODING;
95     }
96
97
98     /**
99      * End the current request. It is acceptable to write extra bytes using
100      * buffer.doWrite during the execution of this method.
101      *
102      * @return Should return 0 unless the filter does some content length
103      * delimitation, in which case the number is the amount of extra bytes or
104      * missing bytes, which would indicate an error.
105      * Note: It is recommended that extra bytes be swallowed by the filter.
106      */

107     @Override
108     public long end() throws IOException {
109         return 0;
110     }
111
112
113     @Override
114     public int available() {
115         return 0;
116     }
117
118
119     @Override
120     public boolean isFinished() {
121         return true;
122     }
123 }
124