You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by ms...@apache.org on 2002/05/10 15:00:11 UTC
cvs commit: jakarta-jmeter/src/org/apache/jmeter/protocol/http/sampler HTTPSampler.java
mstover1 02/05/10 06:00:10
Modified: src/org/apache/jmeter/protocol/http/control
HeaderManager.java
src/org/apache/jmeter/protocol/http/proxy Daemon.java
HttpReplyHdr.java HttpRequestHdr.java Proxy.java
src/org/apache/jmeter/protocol/http/sampler HTTPSampler.java
Log:
proxy server work
Revision Changes Path
1.10 +2 -2 jakarta-jmeter/src/org/apache/jmeter/protocol/http/control/HeaderManager.java
Index: HeaderManager.java
===================================================================
RCS file: /home/cvs/jakarta-jmeter/src/org/apache/jmeter/protocol/http/control/HeaderManager.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- HeaderManager.java 23 Feb 2002 01:21:06 -0000 1.9
+++ HeaderManager.java 10 May 2002 13:00:09 -0000 1.10
@@ -77,7 +77,7 @@
* pass HTTP headers along with a request.
*
* @author <a href="mailto:giacomo@apache.org">Giacomo Pati</a>
- * @version $Revision: 1.9 $ $Date: 2002/02/23 01:21:06 $
+ * @version $Revision: 1.10 $ $Date: 2002/05/10 13:00:09 $
*/
public class HeaderManager implements ConfigElement,JMeterComponentModel,Saveable,
Serializable
@@ -333,7 +333,7 @@
if (header == null) {
continue;
}
- if (header.getName().equals(name)) {
+ if (header.getName().equalsIgnoreCase(name)) {
removeIndices.addElement(new Integer(i));
}
}
1.6 +1 -0 jakarta-jmeter/src/org/apache/jmeter/protocol/http/proxy/Daemon.java
Index: Daemon.java
===================================================================
RCS file: /home/cvs/jakarta-jmeter/src/org/apache/jmeter/protocol/http/proxy/Daemon.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- Daemon.java 4 Mar 2002 23:08:54 -0000 1.5
+++ Daemon.java 10 May 2002 13:00:09 -0000 1.6
@@ -140,6 +140,7 @@
{
// Listen on main socket
Socket ClientSocket = MainSocket.accept();
+ ClientSocket.setSoTimeout(15 * 1000);
// Pass request to new proxy thread
Proxy thd = new Proxy(ClientSocket, cache, config,target,cookieManager);
thd.start();
1.3 +114 -301 jakarta-jmeter/src/org/apache/jmeter/protocol/http/proxy/HttpReplyHdr.java
Index: HttpReplyHdr.java
===================================================================
RCS file: /home/cvs/jakarta-jmeter/src/org/apache/jmeter/protocol/http/proxy/HttpReplyHdr.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- HttpReplyHdr.java 26 Jul 2001 00:34:48 -0000 1.2
+++ HttpReplyHdr.java 10 May 2002 13:00:10 -0000 1.3
@@ -6,6 +6,7 @@
import java.net.*;
import java.io.*;
+import java.util.*;
//
// Class: HttpReplyHdr
@@ -14,306 +15,118 @@
public class HttpReplyHdr
{
-
- static String CR="\r\n";
- static String HTTP_PROTOCOL="HTTP/1.0";
- static String HTTP_SERVER="Java Proxy Server";
-
- String lastModified ="";
- long contentLength=0;
- String extraErrorString ="";
-
-/**
- * Sets the last modified date for a header;
- *
- * @param date A string holding an interner date
- * @return true
- */
-public boolean setModifiedDate(String date)
- {
- lastModified = date;
- return true;
- }
-
-/**
- * Adds an extra explanation. This extra information is
- * Added to the http headers failure explanation.
- *
- * @param str Description to add.
- * @return true.
- */
-public boolean addErrorDescription(String str)
- {
- extraErrorString = str;
- return true;
- }
-
-/**
- * Forms a http ok reply header
- *
- * @param ContentType The mime-type of the content
- * @return A string with the header in it
- */
-public String formOk(String ContentType,long ContentLength)
- {
-
- contentLength = ContentLength;
-
- String out =new String();
-
- out += HTTP_PROTOCOL + " 200 Ok" + CR;
- out += "Server: " + HTTP_SERVER + CR;
- out += "MIME-version: 1.0" + CR;
-
- if (0 < ContentType.length())
- out += "Content-type: " + ContentType + CR;
- else
- out += "Content-Type: text/html" + CR;
-
- if (0 != contentLength)
- out += "Content-Length: " + Long.toString(contentLength) + CR;
-
- if (0 < lastModified.length())
- out +="Last-Modified: " + lastModified + CR;
-
- out +=CR;
-
- return out;
- }
-
-
-/**
- * private! builds an http document describing a headers reason.
- *
- * @param Error Error name.
- * @param Description Errors description.
- * @return A string with the HTML description body
- */
-private String formErrorBody(String Error,String Description)
- {
- String out;
- //Generate Error Body
- out ="<HTML><HEAD><TITLE>";
- out += Error ;
- out +="</TITLE></HEAD>";
- out +="<BODY><H2>" + Error +"</H2>\n";
- out +="</P></H3>";
- out += Description;
- out +="</BODY></HTML>";
- return out;
- }
-
-
-
-/**
- * builds an http document describing an error.
- *
- * @param Error Error name.
- * @param Description Errors description.
- * @return A string with the HTML description body
- */
-private String formError(String Error, String Description)
- {
- /* A HTTP RESPONCE HEADER LOOKS ALOT LIKE:
- *
- * HTTP/1.0 200 OK
- * Date: Wednesday, 02-Feb-94 23:04:12 GMT
- * Server: NCSA/1.1
- * MIME-version: 1.0
- * Last-modified: Monday, 15-Nov-93 23:33:16 GMT
- * Content-type: text/html
- * Content-length: 2345
- * \r\n
- */
-
- String body=formErrorBody(Error,Description);
- String header =new String();
-
- header +=HTTP_PROTOCOL +" " + Error + CR;
- header +="Server: " + HTTP_SERVER + CR;
- header +="MIME-version: 1.0" + CR;
- header +="Content-type: text/html" + CR;
-
- if (0 < lastModified.length())
- header +="Last-Modified: " + lastModified +CR;
-
- header +="Content-Length: " + String.valueOf(body.length())+ CR;
-
- header += CR;
- header += body;
-
- return header;
- }
-
-
-/**
- * Indicates a new file was created.
- *
- * @return The header in a string;
- */
-public String formCreated()
- {
- return formError("201 Created","Object was created");
- }
-
-/**
- * Indicates the document was accepted.
- *
- * @return The header in a string;
- */
-public String formAccepted()
- {
- return formError("202 Accepted","Object checked in");
- }
-
-/**
- * Indicates only a partial responce was sent.
- *
- * @return The header in a string;
- */
-public String formPartial()
- {
- return formError("203 Partial","Only partail document available");
- }
-
-/**
- * Indicates a requested URL has moved to a new address or name.
- *
- * @return The header in a string;
- */
-public String formMoved()
- {
- //300 codes tell client to do actions
- return formError("301 Moved","File has moved");
- }
-
-/**
- * Never seen this used.
- *
- * @return The header in a string;
- */
-public String formFound()
- {
- return formError("302 Found","Object was found");
- }
-
-/**
- * The requested method is not implemented by the server.
- *
- * @return The header in a string;
- */
-public String formMethod()
- {
- return formError("303 Method unseported","Method unseported");
- }
-
-/**
- * Indicates remote copy of the requested object is current.
- *
- * @return The header in a string;
- */
-public String formNotModified()
- {
- return formError("304 Not modified","Use local copy");
- }
-
-/**
- * Client not otherized for the request.
- *
- * @return The header in a string;
- */
-public String formUnautorized()
- {
- return formError("401 Unathorized","Unathorized use of this service");
- }
-
-/**
- * Payment is required for service.
- *
- * @return The header in a string;
- */
-public String formPaymentNeeded()
- {
- return formError("402 Payment required","Payment is required");
- }
-
-/**
- * Client if forbidden to get the request service.
- *
- * @return The header in a string;
- */
-public String formForbidden()
- {
- return formError("403 Forbidden","You need permission for this service");
- }
-
-/**
- * The requested object was not found.
- *
- * @return The header in a string;
- */
-public String formNotFound()
- {
- return formError("404 Not_found","Requested object was not found");
- }
-
-/**
- * The server had a problem and could not fulfill the request.
- *
- * @return The header in a string;
- */
-public String formInternalError()
- {
- return formError("500 Internal server error","Server broke");
- }
-
-/**
- * Server does not do the requested feature.
- *
- * @return The header in a string;
- */
-public String formNotImplemented()
- {
- return formError("501 Method not implemented","Service not implemented, programer was lazy");
- }
-
-/**
- * Server is overloaded, client should try again latter.
- *
- * @return The header in a string;
- */
-public String formOverloaded()
- {
- return formError("502 Server overloaded","Try again latter");
- }
-
-/**
- * Indicates the request took to long.
- *
- * @return The header in a string;
- */
-public String formTimeout()
- {
- return formError("503 Gateway timeout","The connection timed out");
- }
-
-/**
- * Indicates the client's proxies could not locate a server.
- *
- * @return The header in a string;
- */
-public String formServerNotFound()
- {
- return formError("503 Gateway timeout","The requested server was not found");
- }
-
-/**
- * Indicates the client is not allowed to access the object.
- *
- * @return The header in a string;
- */
-public String formNotAllowed()
- {
- return formError("403 Access Denied","Access is not allowed");
+ static final String _CRLF = "\r\n";
+ static final String _CRLF2 = "\r\n\r\n";
+ static final String _LF = "\n";
+ static final String _LF2 = "\n\n";
+ ByteArrayOutputStream record = new ByteArrayOutputStream();
+
+ public HttpReplyHdr()
+ {
+ }
+
+ public byte[] parse(String server,int port,InputStream clientRequest) throws IOException
+ {
+ Socket toServer = null;
+ BufferedOutputStream serverOut = null;
+ BufferedInputStream serverIn = null;
+ byte[] buf = new byte[10000];
+ int readLength = 0;
+ int x = 0;
+ try
+ {
+ toServer = new Socket(server, port);
+ serverOut = new BufferedOutputStream(toServer.getOutputStream());
+ serverIn = new BufferedInputStream(toServer.getInputStream());
+ while ((x = clientRequest.read(buf)) > -1) {
+ serverOut.write(buf,0,x);
+ }
+ serverOut.flush();
+ readHeaders(record,serverIn);
+ int length = parseHeaders(record.toString("8859_1"));
+ System.out.println("reply content length = "+length);
+ if(length > 0)
+ {
+ while(readLength < length)
+ {
+ if((x = serverIn.read(buf)) <= 0) break;
+ record.write(buf,0,x);
+ readLength += x;
+ }
+ }
+ else
+ {
+ while ((x = serverIn.read(buf)) >= 0) {
+ record.write(buf,0,x);
+ }
+ }
+ }
+ finally
+ {
+ try {
+ serverOut.close();
+ } catch(IOException e) {
+ }
+ try {
+ serverIn.close();
+ } catch(IOException e) {
+ }
+ try {
+ toServer.close();
+ } catch(IOException e) {
+ }
+ }
+
+ return record.toByteArray();
+ }
+
+ private int parseHeaders(String headers)
+ {
+ String delimiter = null;
+ if(headers.endsWith(_CRLF)) delimiter = _CRLF;
+ else delimiter = _LF;
+ StringTokenizer st = new StringTokenizer(headers, delimiter);
+ String token;
+ int pos = -1;
+ int contentLength = 0;
+ while( st.hasMoreTokens() ) {
+ token = st.nextToken();
+ if((pos = token.indexOf(": ")) < 0) {
+ continue;
+ }
+ String name = token.substring(0, pos);
+ String value = token.substring(pos +2);
+ // System.out.println(name + "<=>" + value);
+ if(name.equalsIgnoreCase("Content-Length")) {
+ try {
+ value.replace('\r', ' ');
+ value = value.trim();
+ contentLength = Integer.parseInt(value);
+ }
+ catch(NumberFormatException nfe) {
+ System.out.println("Error: Bad Content-Length. ["+value+"]");
+ }
+ }
+ }
+ return contentLength;
+ }
+
+ private void readHeaders(OutputStream record,InputStream serverIn) throws IOException
+ {
+ boolean EOH = false;
+ byte[] onebyte = new byte[1];
+ while(!EOH) {
+ if(serverIn.read(onebyte) < 0) {
+ EOH = true;
+ continue;
+ }
+ else
+ record.write(onebyte);
+
+ if(record.toString().endsWith(_CRLF2) || record.toString().endsWith(_LF2)) {
+ EOH = true;
+ }
+ }
+ System.out.println(record.toString());
}
}
1.10 +3 -3 jakarta-jmeter/src/org/apache/jmeter/protocol/http/proxy/HttpRequestHdr.java
Index: HttpRequestHdr.java
===================================================================
RCS file: /home/cvs/jakarta-jmeter/src/org/apache/jmeter/protocol/http/proxy/HttpRequestHdr.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- HttpRequestHdr.java 29 Apr 2002 17:08:06 -0000 1.9
+++ HttpRequestHdr.java 10 May 2002 13:00:10 -0000 1.10
@@ -23,8 +23,8 @@
* !ToDo (Class description)
*
*@author $Author: mstover1 $
- *@created $Date: 2002/04/29 17:08:06 $
- *@version $Revision: 1.9 $
+ *@created $Date: 2002/05/10 13:00:10 $
+ *@version $Revision: 1.10 $
***************************************/
public class HttpRequestHdr
{
@@ -110,7 +110,7 @@
while(keys.hasNext())
{
String key = (String)keys.next();
- if(!key.equalsIgnoreCase("cookie") && !key.equalsIgnoreCase("content-length"))
+ if(!key.equalsIgnoreCase("proxy-connection") && !key.equalsIgnoreCase("content-length"))
{
Header h = new Header(key,(String)headers.get(key));
manager.add(h);
1.15 +19 -107 jakarta-jmeter/src/org/apache/jmeter/protocol/http/proxy/Proxy.java
Index: Proxy.java
===================================================================
RCS file: /home/cvs/jakarta-jmeter/src/org/apache/jmeter/protocol/http/proxy/Proxy.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- Proxy.java 9 May 2002 01:58:37 -0000 1.14
+++ Proxy.java 10 May 2002 13:00:10 -0000 1.15
@@ -65,6 +65,7 @@
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.samplers.SampleResult;
+import org.apache.jmeter.protocol.http.control.HeaderManager;
//
// Class: Proxy
@@ -138,9 +139,7 @@
{
String serverName = "";
HttpURLConnection url;
-
byte line[];
-
HttpRequestHdr request = new HttpRequestHdr();
HttpReplyHdr reply = new HttpReplyHdr();
FileInputStream fileInputStream = null;
@@ -152,16 +151,21 @@
byte[] clientRequest = request.parse(
new BufferedInputStream(
ClientSocket.getInputStream()));
+ OutputStream clientOut = new BufferedOutputStream(
+ ClientSocket.getOutputStream());
Entry entry = request.getEntry();
- entry.addConfigElement(cookieManager);
+ HeaderManager headers = request.getHeaderManager();
+ entry.addConfigElement(headers);
+ headers.removeHeaderNamed("cookie");
System.out.println("Delivering urlconfig to test tree");
target.deliverUrlConfig((UrlConfig)entry.getConfigElement(UrlConfig.class),
- new JMeterComponentModel[]{request.getHeaderManager()});
+ new JMeterComponentModel[]{headers});
+ Sampler sampler = (Sampler)entry.getSamplerClass().newInstance();
+ byte[] serverResponse = (byte[])sampler.sample(entry).getValue(
+ SampleResult.TEXT_RESPONSE);
writeToClient(
- request.serverName(),
- request.serverPort(),
- new BufferedInputStream(new ByteArrayInputStream(clientRequest)),
- new BufferedOutputStream(ClientSocket.getOutputStream()));
+ serverResponse,
+ clientOut);
}
catch (UnknownHostException uhe)
{
@@ -175,7 +179,7 @@
// Notify client that server not found
DataOutputStream out =
new DataOutputStream(ClientSocket.getOutputStream());
- out.writeBytes(reply.formServerNotFound());
+ out.writeBytes("Server not found");
out.flush();
}
catch (Exception uhe2)
@@ -185,6 +189,7 @@
catch (Exception e)
{
+ e.printStackTrace();
try
{
if (TakenFromCache)
@@ -199,7 +204,7 @@
// Notify client that internal error accured in proxy
DataOutputStream out =
new DataOutputStream(ClientSocket.getOutputStream());
- out.writeBytes(reply.formTimeout());
+ out.writeBytes("Proxy error "+e.getMessage());
out.flush();
}
@@ -232,114 +237,21 @@
- //
- // Private methods
- //
-
- //
- // Send to administrator web page containing reference to applet
- //
- private void sendAppletWebPage()
- {
- System.out.println("Sending the applet...");
- String page = "";
- try
- {
- File appletHtmlPage = new File(config.getAdminPath() +
- File.separator + "Admin.html");
- BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(appletHtmlPage)));
-
- String s = null;
-
- while ((s = in.readLine()) != null)
- {
- page += s;
- }
-
- page = page.substring(0, page.indexOf("PORT")) +
- config.getAdminPort() +
- page.substring(page.indexOf("PORT") + 4);
-
- in.close();
- DataOutputStream out = new DataOutputStream(ClientSocket.getOutputStream());
- out.writeBytes(page);
- out.flush();
- out.close();
- }
- catch (Exception e)
- {
- System.out.println("Error: can't open applet html page");
- }
-
- }
-
-
- //
- // Send the applet to administrator
- //
- private void sendAppletClass(String className)
- {
- try
- {
- byte data[] = new byte[2000];
- int count;
- HttpReplyHdr reply = new HttpReplyHdr();
- File appletFile = new File(adminPath + File.separatorChar + className);
- long length = appletFile.length();
-
- FileInputStream in = new FileInputStream(appletFile);
- DataOutputStream out = new DataOutputStream(ClientSocket.getOutputStream());
-
- out.writeBytes(reply.formOk("application/octet-stream", length));
-
- while (-1 < (count = in.read(data)))
- {
- out.write(data, 0, count);
- }
- out.flush();
- in.close();
- out.close();
- }
- catch (Exception e)
- {
- }
- }
private void writeToClient(
- String server,
- int serverPort,
- InputStream in,
+ byte[] in,
OutputStream out)
throws IOException {
- Socket toServer = null;
- BufferedInputStream serverIn = null;
- BufferedOutputStream serverOut = null;
- byte[] buf = new byte[10000];
try {
- int x = 0;
- toServer = new Socket(server, serverPort);
- serverOut = new BufferedOutputStream(toServer.getOutputStream());
- serverIn = new BufferedInputStream(toServer.getInputStream());
- while ((x = in.read(buf)) > 0) {
- serverOut.write(buf,0,x);
- }
- serverOut.flush();
- while ((x = serverIn.read(buf)) > 0) {
- out.write(buf,0,x);
- out.flush();
- }
- out.write(-1);
+ out.write(in);
out.flush();
+ System.out.println("Wrote everything to browser");
} catch (IOException e) {
e.printStackTrace();
} finally {
System.out.println("closing everything");
- try {
- in.close();
+ try{
out.close();
- serverIn.close();
- serverOut.close();
- toServer.close();
} catch (Exception ex) {ex.printStackTrace();}
}
}
1.36 +25 -9 jakarta-jmeter/src/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java
Index: HTTPSampler.java
===================================================================
RCS file: /home/cvs/jakarta-jmeter/src/org/apache/jmeter/protocol/http/sampler/HTTPSampler.java,v
retrieving revision 1.35
retrieving revision 1.36
diff -u -r1.35 -r1.36
--- HTTPSampler.java 29 Apr 2002 17:08:06 -0000 1.35
+++ HTTPSampler.java 10 May 2002 13:00:10 -0000 1.36
@@ -75,18 +75,15 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.ArrayList;
+import java.util.*;
/**
* A sampler which understands all the parts necessary to read statistics about
* HTTP requests, including cookies and authentication.
*
*@author Michael Stover
- *@created $Date: 2002/04/29 17:08:06 $
- *@version $Revision: 1.35 $
+ *@created $Date: 2002/05/10 13:00:10 $
+ *@version $Revision: 1.36 $
*/
public class HTTPSampler implements Sampler
{
@@ -467,9 +464,12 @@
{
byte[] ret = readResponse(conn);
time = System.currentTimeMillis() - time;
- res.putValue(SampleResult.TEXT_RESPONSE, ret);
res.putValue(SampleResult.SUCCESS, new Boolean(true));
- getResponseHeaders(conn, res);
+ byte[] headers = getResponseHeaders(conn, res);
+ byte[] complete = new byte[headers.length+ret.length];
+ System.arraycopy(headers,0,complete,0,headers.length);
+ System.arraycopy(ret,0,complete,headers.length,ret.length);
+ res.putValue(SampleResult.TEXT_RESPONSE,complete);
}
else if (errorLevel == 3)
{
@@ -556,14 +556,30 @@
*@param conn connection from which the headers are read
*@param res where the headers read are stored
*/
- protected void getResponseHeaders(HttpURLConnection conn, SampleResult res)
+ protected byte[] getResponseHeaders(HttpURLConnection conn,
+ SampleResult res) throws IOException
{
+ ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+ bytes.write("HTTP/1.1 ".getBytes("8859_1"));
+ bytes.write(Integer.toString(conn.getResponseCode()).getBytes("8859_1"));
+ bytes.write(" ".getBytes("8859_1"));
+ bytes.write(conn.getResponseMessage().getBytes("8859_1"));
+ bytes.write("\n".getBytes("8859_1"));
HashMap hValues = new HashMap(20);
for (int i = 1; conn.getHeaderFieldKey(i) != null; i++)
{
hValues.put(conn.getHeaderFieldKey(i), conn.getHeaderField(i));
+ if(!conn.getHeaderFieldKey(i).equalsIgnoreCase("transfer-encoding"))
+ {
+ bytes.write(conn.getHeaderFieldKey(i).getBytes("8859_1"));
+ bytes.write(": ".getBytes("8859_1"));
+ bytes.write(conn.getHeaderField(i).getBytes("8859_1"));
+ bytes.write("\n".getBytes("8859_1"));
+ }
}
+ bytes.write("\n".getBytes("8859_1"));
res.putValue(Sampler.HEADER, hValues);
+ return bytes.toByteArray();
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>