1/*
2 * Copyright (C) 2011 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.mockwebserver;
18
19import java.net.Socket;
20import java.util.List;
21import javax.net.ssl.SSLSocket;
22
23/**
24 * An HTTP request that came into the mock web server.
25 */
26public final class RecordedRequest {
27    private final String requestLine;
28    private final String method;
29    private final String path;
30    private final List<String> headers;
31    private final List<Integer> chunkSizes;
32    private final int bodySize;
33    private final byte[] body;
34    private final int sequenceNumber;
35    private final String sslProtocol;
36
37    RecordedRequest(String requestLine, List<String> headers, List<Integer> chunkSizes,
38            int bodySize, byte[] body, int sequenceNumber, Socket socket) {
39        this.requestLine = requestLine;
40        this.headers = headers;
41        this.chunkSizes = chunkSizes;
42        this.bodySize = bodySize;
43        this.body = body;
44        this.sequenceNumber = sequenceNumber;
45
46        if (socket instanceof SSLSocket) {
47            SSLSocket sslSocket = (SSLSocket) socket;
48            sslProtocol = sslSocket.getSession().getProtocol();
49        } else {
50            sslProtocol = null;
51        }
52
53        if (requestLine != null) {
54            int methodEnd = requestLine.indexOf(' ');
55            int pathEnd = requestLine.indexOf(' ', methodEnd + 1);
56            this.method = requestLine.substring(0, methodEnd);
57            this.path = requestLine.substring(methodEnd + 1, pathEnd);
58        } else {
59            this.method = null;
60            this.path = null;
61        }
62    }
63
64    public String getRequestLine() {
65        return requestLine;
66    }
67
68    public List<String> getHeaders() {
69        return headers;
70    }
71
72    /**
73     * Returns the sizes of the chunks of this request's body, or an empty list
74     * if the request's body was empty or unchunked.
75     */
76    public List<Integer> getChunkSizes() {
77        return chunkSizes;
78    }
79
80    /**
81     * Returns the total size of the body of this POST request (before
82     * truncation).
83     */
84    public int getBodySize() {
85        return bodySize;
86    }
87
88    /**
89     * Returns the body of this POST request. This may be truncated.
90     */
91    public byte[] getBody() {
92        return body;
93    }
94
95    /**
96     * Returns the index of this request on its HTTP connection. Since a single
97     * HTTP connection may serve multiple requests, each request is assigned its
98     * own sequence number.
99     */
100    public int getSequenceNumber() {
101        return sequenceNumber;
102    }
103
104    /**
105     * Returns the connection's SSL protocol like {@code TLSv1}, {@code SSLv3},
106     * {@code NONE} or null if the connection doesn't use SSL.
107     */
108    public String getSslProtocol() {
109        return sslProtocol;
110    }
111
112    @Override public String toString() {
113        return requestLine;
114    }
115
116    public String getMethod() {
117        return method;
118    }
119
120    public String getPath() {
121        return path;
122    }
123}
124