View Javadoc
1   /*
2    * SPDX-License-Identifier: MIT
3    * See LICENSE file for details.
4    *
5    * Copyright 2010-2026 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
6    */
7   package waffle.apache.catalina;
8   
9   import jakarta.servlet.http.HttpSession;
10  
11  import java.security.Principal;
12  import java.util.HashMap;
13  import java.util.Map;
14  
15  import mockit.Mocked;
16  
17  import org.apache.catalina.connector.Request;
18  
19  /**
20   * Simple HTTP Request.
21   */
22  public class SimpleHttpRequest extends Request {
23  
24      /** The remote port s. */
25      private static int remotePortS;
26  
27      /**
28       * Next remote port.
29       *
30       * @return the int
31       */
32      public static synchronized int nextRemotePort() {
33          return ++SimpleHttpRequest.remotePortS;
34      }
35  
36      /**
37       * Reset remote port.
38       */
39      public static synchronized void resetRemotePort() {
40          SimpleHttpRequest.remotePortS = 0;
41      }
42  
43      /** The request uri. */
44      private String requestURI;
45  
46      /** The query string. */
47      private String queryString;
48  
49      /** The remote user. */
50      private String remoteUser;
51  
52      /** The method. */
53      private String method = "GET";
54  
55      /** The headers. */
56      private final Map<String, String> headers = new HashMap<>();
57  
58      /** The parameters. */
59      private final Map<String, String> parameters = new HashMap<>();
60  
61      /** The content. */
62      private byte[] content;
63  
64      /** The http session. */
65      @Mocked
66      private HttpSession httpSession;
67  
68      /** The principal. */
69      private Principal principal;
70  
71      /**
72       * Instantiates a new simple http request.
73       */
74      public SimpleHttpRequest() {
75          // Tomcat notes that null on connector here may be ok for testing
76          super(null);
77          this.remotePort = SimpleHttpRequest.nextRemotePort();
78      }
79  
80      /**
81       * Adds the header.
82       *
83       * @param headerName
84       *            the header name
85       * @param headerValue
86       *            the header value
87       */
88      public void addHeader(final String headerName, final String headerValue) {
89          this.headers.put(headerName, headerValue);
90      }
91  
92      /**
93       * Adds the parameter.
94       *
95       * @param parameterName
96       *            the parameter name
97       * @param parameterValue
98       *            the parameter value
99       */
100     public void addParameter(final String parameterName, final String parameterValue) {
101         this.parameters.put(parameterName, parameterValue);
102     }
103 
104     @Override
105     public int getContentLength() {
106         return this.content == null ? -1 : this.content.length;
107     }
108 
109     @Override
110     public String getHeader(final String headerName) {
111         return this.headers.get(headerName);
112     }
113 
114     @Override
115     public String getMethod() {
116         return this.method;
117     }
118 
119     @Override
120     public String getParameter(final String parameterName) {
121         return this.parameters.get(parameterName);
122     }
123 
124     @Override
125     public String getQueryString() {
126         return this.queryString;
127     }
128 
129     @Override
130     public String getRemoteAddr() {
131         return this.remoteAddr;
132     }
133 
134     @Override
135     public String getRemoteHost() {
136         return this.remoteHost;
137     }
138 
139     @Override
140     public int getRemotePort() {
141         return this.remotePort;
142     }
143 
144     @Override
145     public String getRemoteUser() {
146         return this.remoteUser;
147     }
148 
149     @Override
150     public String getRequestURI() {
151         return this.requestURI;
152     }
153 
154     @Override
155     public HttpSession getSession() {
156         return this.httpSession;
157     }
158 
159     @Override
160     public HttpSession getSession(final boolean create) {
161         return this.httpSession;
162     }
163 
164     @Override
165     public Principal getUserPrincipal() {
166         return this.principal;
167     }
168 
169     /**
170      * Sets the content length.
171      *
172      * @param length
173      *            the new content length
174      */
175     public void setContentLength(final int length) {
176         this.content = new byte[length];
177     }
178 
179     /**
180      * Sets the method.
181      *
182      * @param value
183      *            the new method
184      */
185     public void setMethod(final String value) {
186         this.method = value;
187     }
188 
189     /**
190      * Sets the query string.
191      *
192      * @param queryValue
193      *            the new query string
194      */
195     public void setQueryString(final String queryValue) {
196         this.queryString = queryValue;
197         if (this.queryString != null) {
198             for (final String eachParameter : this.queryString.split("[&]", -1)) {
199                 final String[] pair = eachParameter.split("=", -1);
200                 final String value = pair.length == 2 ? pair[1] : "";
201                 this.addParameter(pair[0], value);
202             }
203         }
204     }
205 
206     @Override
207     public void setRemoteAddr(final String value) {
208         this.remoteAddr = value;
209     }
210 
211     @Override
212     public void setRemoteHost(final String value) {
213         this.remoteHost = value;
214     }
215 
216     /**
217      * Sets the remote user.
218      *
219      * @param value
220      *            the new remote user
221      */
222     public void setRemoteUser(final String value) {
223         this.remoteUser = value;
224     }
225 
226     /**
227      * Sets the request uri.
228      *
229      * @param value
230      *            the new request uri
231      */
232     public void setRequestURI(final String value) {
233         this.requestURI = value;
234     }
235 
236     @Override
237     public void setUserPrincipal(final Principal value) {
238         this.principal = value;
239     }
240 
241 }