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.squareup.okhttp.mockwebserver; 18 19import java.io.UnsupportedEncodingException; 20import java.net.Socket; 21import java.util.ArrayList; 22import java.util.List; 23import javax.net.ssl.SSLSocket; 24 25/** An HTTP request that came into the mock web server. */ 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 long bodySize; 33 private final byte[] body; 34 private final int sequenceNumber; 35 private final String sslProtocol; 36 37 public RecordedRequest(String requestLine, List<String> headers, List<Integer> chunkSizes, 38 long 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 this.sslProtocol = socket instanceof SSLSocket 46 ? ((SSLSocket) socket).getSession().getProtocol() 47 : null; 48 49 if (requestLine != null) { 50 int methodEnd = requestLine.indexOf(' '); 51 int pathEnd = requestLine.indexOf(' ', methodEnd + 1); 52 this.method = requestLine.substring(0, methodEnd); 53 this.path = requestLine.substring(methodEnd + 1, pathEnd); 54 } else { 55 this.method = null; 56 this.path = null; 57 } 58 } 59 60 public String getRequestLine() { 61 return requestLine; 62 } 63 64 public String getMethod() { 65 return method; 66 } 67 68 public String getPath() { 69 return path; 70 } 71 72 /** Returns all headers. */ 73 public List<String> getHeaders() { 74 return headers; 75 } 76 77 /** 78 * Returns the first header named {@code name}, or null if no such header 79 * exists. 80 */ 81 public String getHeader(String name) { 82 name += ":"; 83 for (int i = 0, size = headers.size(); i < size; i++) { 84 String header = headers.get(i); 85 if (name.regionMatches(true, 0, header, 0, name.length())) { 86 return header.substring(name.length()).trim(); 87 } 88 } 89 return null; 90 } 91 92 /** Returns the headers named {@code name}. */ 93 public List<String> getHeaders(String name) { 94 List<String> result = new ArrayList<String>(); 95 name += ":"; 96 for (int i = 0, size = headers.size(); i < size; i++) { 97 String header = headers.get(i); 98 if (name.regionMatches(true, 0, header, 0, name.length())) { 99 result.add(header.substring(name.length()).trim()); 100 } 101 } 102 return result; 103 } 104 105 /** 106 * Returns the sizes of the chunks of this request's body, or an empty list 107 * if the request's body was empty or unchunked. 108 */ 109 public List<Integer> getChunkSizes() { 110 return chunkSizes; 111 } 112 113 /** 114 * Returns the total size of the body of this POST request (before 115 * truncation). 116 */ 117 public long getBodySize() { 118 return bodySize; 119 } 120 121 /** Returns the body of this POST request. This may be truncated. */ 122 public byte[] getBody() { 123 return body; 124 } 125 126 /** Returns the body of this POST request decoded as a UTF-8 string. */ 127 public String getUtf8Body() { 128 try { 129 return new String(body, "UTF-8"); 130 } catch (UnsupportedEncodingException e) { 131 throw new AssertionError(); 132 } 133 } 134 135 /** 136 * Returns the index of this request on its HTTP connection. Since a single 137 * HTTP connection may serve multiple requests, each request is assigned its 138 * own sequence number. 139 */ 140 public int getSequenceNumber() { 141 return sequenceNumber; 142 } 143 144 /** 145 * Returns the connection's SSL protocol like {@code TLSv1}, {@code SSLv3}, 146 * {@code NONE} or null if the connection doesn't use SSL. 147 */ 148 public String getSslProtocol() { 149 return sslProtocol; 150 } 151 152 @Override public String toString() { 153 return requestLine; 154 } 155} 156