1 18 19 package org.apache.jmeter.protocol.http.proxy; 20 21 import java.io.BufferedInputStream ; 22 import java.io.BufferedOutputStream ; 23 import java.io.DataOutputStream ; 24 import java.io.IOException ; 25 import java.io.OutputStream ; 26 import java.net.Socket ; 27 import java.net.UnknownHostException ; 28 29 import org.apache.jmeter.protocol.http.control.HeaderManager; 30 import org.apache.jmeter.protocol.http.sampler.HTTPSampler; 31 import org.apache.jmeter.samplers.SampleResult; 32 import org.apache.jmeter.testelement.TestElement; 33 import org.apache.jorphan.logging.LoggingManager; 34 import org.apache.log.Logger; 35 36 46 public class Proxy extends Thread 47 { 48 49 private static transient Logger log = LoggingManager.getLoggerForClass(); 50 51 52 private Socket clientSocket = null; 53 54 55 private ProxyControl target; 56 57 58 private boolean captureHttpHeaders; 59 60 63 public Proxy() 64 { 65 } 66 67 74 Proxy(Socket clientSocket, ProxyControl target) 75 { 76 configure(clientSocket, target); 77 } 78 79 86 void configure( 87 Socket clientSocket, 88 ProxyControl target) 89 { 90 this.target = target; 91 this.clientSocket = clientSocket; 92 this.captureHttpHeaders = target.getCaptureHttpHeaders(); 93 } 94 95 98 public void run() 99 { 100 HttpRequestHdr request = new HttpRequestHdr(); 101 SampleResult result = null; 102 HeaderManager headers = null; 103 104 HTTPSampler sampler = new HTTPSampler(); 105 try 106 { 107 request.parse(new BufferedInputStream (clientSocket.getInputStream())); 108 109 sampler = request.getSampler(); 110 111 115 headers = request.getHeaderManager(); 116 sampler.setHeaderManager(headers); 117 result = sampler.sample(); 118 writeToClient( 119 result, 120 new BufferedOutputStream (clientSocket.getOutputStream())); 121 124 headers.removeHeaderNamed("cookie"); } 126 catch (UnknownHostException uhe) 127 { 128 log.warn("Server Not Found.", uhe); 129 writeErrorToClient(HttpReplyHdr.formServerNotFound()); 130 } 131 catch (Exception e) 132 { 133 log.error("",e); 134 writeErrorToClient(HttpReplyHdr.formTimeout()); 135 } 136 finally 137 { 138 target.deliverSampler( 139 sampler, 140 new TestElement[] { 141 captureHttpHeaders ? headers : null 142 }, 143 result); 144 try 145 { 146 clientSocket.close(); 147 } 148 catch (Exception e) 149 { 150 log.error("",e); 151 } 152 } 153 } 154 155 162 private void writeToClient( 163 SampleResult res, 164 OutputStream out) 165 throws IOException 166 { 167 try 168 { 169 String responseHeaders = massageResponseHeaders(res,res.getResponseHeaders()); 170 out.write((responseHeaders+"\n").getBytes()); 171 out.write(res.getResponseData()); 172 out.flush(); 173 log.debug("Done writing to client"); 174 } 175 catch (IOException e) 176 { 177 log.error("",e); 178 throw e; 179 } 180 finally 181 { 182 try 183 { 184 out.close(); 185 } 186 catch (Exception ex) 187 { 188 log.warn("Error while closing socket", ex); 189 } 190 } 191 } 192 193 200 private String massageResponseHeaders(SampleResult res,String headers) 201 { 202 int encodingHeaderLoc = headers.indexOf(": gzip"); 203 String newHeaders = headers; 204 if(encodingHeaderLoc > -1) 205 { 206 int end = headers.indexOf("\n",encodingHeaderLoc); 207 int begin = headers.lastIndexOf("\n",encodingHeaderLoc); 208 newHeaders = newHeaders.substring(0,begin) + newHeaders.substring(end); 209 int lengthIndex = newHeaders.indexOf("ength: "); 210 end = newHeaders.indexOf("\n",lengthIndex); 211 newHeaders = newHeaders.substring(0,lengthIndex+7) + res.getResponseData().length + 212 newHeaders.substring(end); 213 } 214 return newHeaders; 215 } 216 217 223 private void writeErrorToClient(String message) 224 { 225 try 226 { 227 OutputStream sockOut = clientSocket.getOutputStream(); 228 DataOutputStream out = new DataOutputStream (sockOut); 229 out.writeBytes(message); 230 out.flush(); 231 } 232 catch (Exception e) 233 { 234 log.warn("Exception while writing error", e); 235 } 236 } 237 } | Popular Tags |