View | Details | Raw Unified | Return to bug 53960
Collapse All | Expand All

(-)test/org/apache/catalina/startup/SimpleHttpClient.java (-30 / +149 lines)
Lines 39-45 Link Here
39
 */
39
 */
40
public abstract class SimpleHttpClient {
40
public abstract class SimpleHttpClient {
41
    public static final String TEMP_DIR =
41
    public static final String TEMP_DIR =
42
        System.getProperty("java.io.tmpdir");
42
            System.getProperty("java.io.tmpdir");
43
43
44
    public static final String CRLF = "\r\n";
44
    public static final String CRLF = "\r\n";
45
45
Lines 48-61 Link Here
48
    public static final String REDIRECT_302 = "HTTP/1.1 302";
48
    public static final String REDIRECT_302 = "HTTP/1.1 302";
49
    public static final String FAIL_400 = "HTTP/1.1 400";
49
    public static final String FAIL_400 = "HTTP/1.1 400";
50
    public static final String FAIL_404 = "HTTP/1.1 404";
50
    public static final String FAIL_404 = "HTTP/1.1 404";
51
    public static final String TIMEOUT_408 = "HTTP/1.1 408";
51
    public static final String FAIL_413 = "HTTP/1.1 413";
52
    public static final String FAIL_413 = "HTTP/1.1 413";
52
    public static final String FAIL_50X = "HTTP/1.1 50";
53
    public static final String FAIL_50X = "HTTP/1.1 50";
53
    public static final String FAIL_500 = "HTTP/1.1 500";
54
    public static final String FAIL_500 = "HTTP/1.1 500";
54
    public static final String FAIL_501 = "HTTP/1.1 501";
55
    public static final String FAIL_501 = "HTTP/1.1 501";
55
56
57
    private static final String CONTENT_LENGTH_HEADER_PREFIX =
58
            "Content-Length: ";
59
60
    protected static final String SESSION_COOKIE_NAME = "JSESSIONID";
61
    protected static final String SESSION_PARAMETER_NAME =
62
            SESSION_COOKIE_NAME.toLowerCase();
63
64
    private static final String COOKIE_HEADER_PREFIX = "Set-Cookie: ";
56
    private static final String SESSION_COOKIE_HEADER_PREFIX =
65
    private static final String SESSION_COOKIE_HEADER_PREFIX =
57
        "Set-Cookie: JSESSIONID=";
66
            COOKIE_HEADER_PREFIX + SESSION_COOKIE_NAME + "=";
58
67
68
    private static final String REDIRECT_HEADER_PREFIX = "Location: ";
69
    protected static final String SESSION_PATH_PARAMETER_PREFIX =
70
            SESSION_PARAMETER_NAME + "=";
71
    protected static final String SESSION_PATH_PARAMETER_TAILS = CRLF + ";?";
72
73
    private static final String ELEMENT_HEAD = "<";
74
    private static final String ELEMENT_TAIL = ">";
75
    private static final String RESOURCE_TAG = "a";
76
    private static final String LOGIN_TAG = "form";
77
59
    private Socket socket;
78
    private Socket socket;
60
    private Writer writer;
79
    private Writer writer;
61
    private BufferedReader reader;
80
    private BufferedReader reader;
Lines 63-75 Link Here
63
82
64
    private String[] request;
83
    private String[] request;
65
    private boolean useContinue = false;
84
    private boolean useContinue = false;
85
    private boolean useCookies = true;
66
    private int requestPause = 1000;
86
    private int requestPause = 1000;
67
87
68
    private String responseLine;
88
    private String responseLine;
69
    private List<String> responseHeaders = new ArrayList<>();
89
    private List<String> responseHeaders = new ArrayList<>();
70
    private String responseBody;
90
    private String sessionId;
71
    private boolean useContentLength;
91
    private boolean useContentLength;
92
    private int contentLength;
93
    private String redirectUri;
72
94
95
    private String responseBody;
96
    private List<String> bodyUriElments = new ArrayList<>();
97
73
    protected void setPort(int thePort) {
98
    protected void setPort(int thePort) {
74
        port = thePort;
99
        port = thePort;
75
    }
100
    }
Lines 86-91 Link Here
86
        return useContinue;
111
        return useContinue;
87
    }
112
    }
88
113
114
    public void setUseCookies(boolean theUseCookiesFlag) {
115
        useCookies = theUseCookiesFlag;
116
    }
117
118
    public boolean getUseCookies() {
119
        return useCookies;
120
    }
121
89
    public void setRequestPause(int theRequestPause) {
122
    public void setRequestPause(int theRequestPause) {
90
        requestPause = theRequestPause;
123
        requestPause = theRequestPause;
91
    }
124
    }
Lines 106-123 Link Here
106
        useContentLength = b;
139
        useContentLength = b;
107
    }
140
    }
108
141
142
    public void setSessionId(String theSessionId) {
143
        sessionId = theSessionId;
144
    }
145
109
    public String getSessionId() {
146
    public String getSessionId() {
110
        for (String header : responseHeaders) {
147
        return sessionId;
111
            if (header.startsWith(SESSION_COOKIE_HEADER_PREFIX)) {
112
                header = header.substring(SESSION_COOKIE_HEADER_PREFIX.length());
113
                header = header.substring(0, header.indexOf(';'));
114
                return header;
115
            }
116
        }
117
        return null;
118
    }
148
    }
119
149
120
    public void connect(int connectTimeout, int soTimeout) throws UnknownHostException, IOException {
150
    public String getRedirectUri() {
151
        return redirectUri;
152
    }
153
154
    public void connect(int connectTimeout, int soTimeout)
155
           throws UnknownHostException, IOException {
121
        final String encoding = "ISO-8859-1";
156
        final String encoding = "ISO-8859-1";
122
        SocketAddress addr = new InetSocketAddress("localhost", port);
157
        SocketAddress addr = new InetSocketAddress("localhost", port);
123
        socket = new Socket();
158
        socket = new Socket();
Lines 158-170 Link Here
158
        }
193
        }
159
    }
194
    }
160
195
161
    public void readResponse(boolean readBody) throws IOException {
196
    public void readResponse(boolean wantBody) throws IOException {
162
        // Reset fields use to hold response
197
        // clear any residual data before starting on this response
163
        responseLine = null;
164
        responseHeaders.clear();
198
        responseHeaders.clear();
165
        responseBody = null;
199
        responseBody = null;
200
        bodyUriElments.clear();
166
201
167
        // Read the response
202
        // Read the response status line
168
        responseLine = readLine();
203
        responseLine = readLine();
169
204
170
        // Is a 100 continue response expected?
205
        // Is a 100 continue response expected?
Lines 179-213 Link Here
179
            }
214
            }
180
        }
215
        }
181
216
182
        // Put the headers into the map
217
        // Put the headers into a map, and process interesting ones
218
        processHeaders();
219
220
        // Read the body, if requested and if one exists
221
        processBody(wantBody);
222
223
        if (isResponse408()) {
224
            // the session has timed out
225
            sessionId = null;
226
        }
227
    }
228
229
    /*
230
     * Accumulate the response headers into a map, and extract
231
     * interesting details at the  same time
232
     */
233
    private void processHeaders() throws IOException {
234
        // Reset response fields
235
        redirectUri = null;
236
        contentLength = -1;
237
183
        String line = readLine();
238
        String line = readLine();
184
        int cl = -1;
239
        while ((line != null) && (line.length() > 0)) {
185
        while (line!=null && line.length() > 0) {
186
            responseHeaders.add(line);
240
            responseHeaders.add(line);
241
            if (line.startsWith(CONTENT_LENGTH_HEADER_PREFIX)) {
242
                contentLength = Integer.parseInt(line.substring(16));
243
            }
244
            else if (line.startsWith(COOKIE_HEADER_PREFIX)) {
245
                if (useCookies) {
246
                    String temp = line.substring(
247
                            SESSION_COOKIE_HEADER_PREFIX.length());
248
                    temp = temp.substring(0, temp.indexOf(';'));
249
                    setSessionId(temp);
250
                }
251
            }
252
            else if (line.startsWith(REDIRECT_HEADER_PREFIX)) {
253
                redirectUri = line.substring(REDIRECT_HEADER_PREFIX.length());
254
            }
187
            line = readLine();
255
            line = readLine();
188
            if (line != null && line.startsWith("Content-Length: ")) {
189
                cl = Integer.parseInt(line.substring(16));
190
            }
191
        }
256
        }
257
    }
192
258
193
        // Read the body, if any
259
    /*
260
     * Read the body of the server response and save it if requested
261
     */
262
    private void processBody(boolean wantBody) throws IOException {
263
        // Read the body, if one exists
194
        StringBuilder builder = new StringBuilder();
264
        StringBuilder builder = new StringBuilder();
195
        if (readBody) {
265
        if (wantBody) {
196
            if (cl > -1 && useContentLength) {
266
            if (useContentLength && (contentLength > -1)) {
197
                char[] body = new char[cl];
267
                char[] body = new char[contentLength];
198
                reader.read(body);
268
                reader.read(body);
199
                builder.append(body);
269
                builder.append(body);
200
            } else {
270
            }
201
                line = readLine();
271
            else {
202
                while (line != null) {
272
                // not usisng content length, so just read it line by line
273
                String line = null;
274
                while ((line = readLine()) != null) {
203
                    builder.append(line);
275
                    builder.append(line);
204
                    line = readLine();
205
                }
276
                }
206
            }
277
            }
207
        }
278
        }
208
        responseBody = builder.toString();
279
        responseBody = builder.toString();
280
        extractUriElements(responseBody);
209
    }
281
    }
210
282
283
    /*
284
     * Scan an html body for useful html uri elements. If any are found,
285
     * then accumulate them. Test classes might not use them, but they
286
     * are collected anyway.
287
     */
288
    private void extractUriElements(String body) {
289
        if (body.length() > 0) {
290
            int ix = 0;
291
            while ((ix = extractUriElement(body, ix, RESOURCE_TAG)) > 0){}
292
            ix = 0;
293
            while ((ix = extractUriElement(body, ix, LOGIN_TAG)) > 0){}
294
        }
295
    }
296
297
    /*
298
     * Scan an html body for a given html uri element, starting from the
299
     * given index into the source string. If any are found, simply
300
     * accumulate them as literal strings, including angle brackets.
301
     * note: nested elements will not be collected.
302
     *
303
     * @param body HTTP body of the response
304
     * @param startIx offset into the body to resume the scan (for iteration)
305
     * @param elementName to scan for (only one at a time)
306
     * @returns the index into the body to continue the scan (for iteration)
307
     */
308
    private int extractUriElement(String body, int startIx, String elementName) {
309
        String detector = ELEMENT_HEAD + elementName + " ";
310
        int iStart = body.indexOf(detector, startIx);
311
        if (iStart > -1) {
312
            int iEnd = body.indexOf(ELEMENT_TAIL, iStart);
313
            if (iEnd < 0) {
314
                throw new IllegalArgumentException(
315
                        "Response body check failure.\n"
316
                        + "element [" + detector + "] is not terminated with ["
317
                        + ELEMENT_TAIL + "]\nActual: [" + body + "]");
318
            }
319
            String element = body.substring(iStart, iEnd);
320
            bodyUriElments.add(element);
321
            iStart += element.length();
322
        }
323
        return iStart;
324
    }
325
211
    public String readLine() throws IOException {
326
    public String readLine() throws IOException {
212
        return reader.readLine();
327
        return reader.readLine();
213
    }
328
    }
Lines 253-258 Link Here
253
        return getResponseLine().startsWith(FAIL_404);
368
        return getResponseLine().startsWith(FAIL_404);
254
    }
369
    }
255
370
371
    public boolean isResponse408() {
372
        return getResponseLine().startsWith(TIMEOUT_408);
373
    }
374
256
    public boolean isResponse413() {
375
    public boolean isResponse413() {
257
        return getResponseLine().startsWith(FAIL_413);
376
        return getResponseLine().startsWith(FAIL_413);
258
    }
377
    }

Return to bug 53960