oglueck 2003/12/11 01:19:19
Modified: httpclient/src/test/org/apache/commons/httpclient/server
TransparentProxyRequestHandler.java
ProxyAuthRequestHandler.java SimpleProxy.java
SimpleHttpServerConnection.java
httpclient/src/test/org/apache/commons/httpclient
TestProxy.java
Log:
code formatting, fixed TABs
Revision Changes Path
1.3 +51 -48
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/TransparentProxyRequestHandler.java
Index: TransparentProxyRequestHandler.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/TransparentProxyRequestHandler.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TransparentProxyRequestHandler.java 5 Dec 2003 21:23:14 -0000 1.2
+++ TransparentProxyRequestHandler.java 11 Dec 2003 09:19:18 -0000 1.3
@@ -77,54 +77,57 @@
import org.apache.commons.httpclient.URI;
/**
- * This request handler can handle the CONNECT method. It does
- * nothing for any other HTTP methods.
+ * This request handler can handle the CONNECT method. It does nothing for any
+ * other HTTP methods.
*
* @author Ortwin Glueck
*/
public class TransparentProxyRequestHandler implements HttpRequestHandler {
- /* (non-Javadoc)
- * @see
org.apache.commons.httpclient.server.HttpRequestHandler#processRequest(org.apache.commons.httpclient.server.SimpleHttpServerConnection)
- */
- public boolean processRequest(SimpleHttpServerConnection conn) throws
IOException {
- RequestLine line = conn.getRequestLine();
- String method = line.getMethod();
- if (!"CONNECT".equalsIgnoreCase(method)) return false;
- URI url = new HttpURL(line.getUri());
- handshake(conn, url);
- return true;
- }
+ /*
+ * (non-Javadoc)
+ *
+ * @see
org.apache.commons.httpclient.server.HttpRequestHandler#processRequest(org.apache.commons.httpclient.server.SimpleHttpServerConnection)
+ */
+ public boolean processRequest(SimpleHttpServerConnection conn) throws
IOException {
+ RequestLine line = conn.getRequestLine();
+ String method = line.getMethod();
+ if (!"CONNECT".equalsIgnoreCase(method))
+ return false;
+ URI url = new HttpURL(line.getUri());
+ handshake(conn, url);
+ return true;
+ }
- private void handshake(SimpleHttpServerConnection conn, URI url) throws
IOException {
- Socket targetSocket = new Socket(url.getHost(), url.getPort());
- InputStream sourceIn = conn.getInputStream();
- OutputStream sourceOut = conn.getOutputStream();
- InputStream targetIn = targetSocket.getInputStream();
- OutputStream targetOut = targetSocket.getOutputStream();
+ private void handshake(SimpleHttpServerConnection conn, URI url) throws
IOException {
+ Socket targetSocket = new Socket(url.getHost(), url.getPort());
+ InputStream sourceIn = conn.getInputStream();
+ OutputStream sourceOut = conn.getOutputStream();
+ InputStream targetIn = targetSocket.getInputStream();
+ OutputStream targetOut = targetSocket.getOutputStream();
- ResponseWriter out = conn.getWriter();
- out.println("HTTP/1.1 200 Connection established");
- out.flush();
+ ResponseWriter out = conn.getWriter();
+ out.println("HTTP/1.1 200 Connection established");
+ out.flush();
- BidiStreamProxy bdsp = new BidiStreamProxy(sourceIn, sourceOut,
targetIn, targetOut);
- bdsp.start();
- try {
- bdsp.block();
- } catch (InterruptedException e) {
- throw new IOException(e.toString());
- }
- }
-
- private void sendHeaders(Header[] headers, OutputStream os) throws IOException
{
- Writer out;
- try {
- out = new OutputStreamWriter(os,
HttpConstants.HTTP_ELEMENT_CHARSET);
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException(e.toString());
- }
- for (int i=0; i<headers.length; i++) {
- out.write(headers[i].toExternalForm());
- }
- }
+ BidiStreamProxy bdsp = new BidiStreamProxy(sourceIn, sourceOut, targetIn,
targetOut);
+ bdsp.start();
+ try {
+ bdsp.block();
+ } catch (InterruptedException e) {
+ throw new IOException(e.toString());
+ }
+ }
+
+ private void sendHeaders(Header[] headers, OutputStream os) throws IOException {
+ Writer out;
+ try {
+ out = new OutputStreamWriter(os, HttpConstants.HTTP_ELEMENT_CHARSET);
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException(e.toString());
+ }
+ for (int i = 0; i < headers.length; i++) {
+ out.write(headers[i].toExternalForm());
+ }
+ }
}
1.4 +93 -88
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ProxyAuthRequestHandler.java
Index: ProxyAuthRequestHandler.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ProxyAuthRequestHandler.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ProxyAuthRequestHandler.java 10 Dec 2003 21:04:13 -0000 1.3
+++ ProxyAuthRequestHandler.java 11 Dec 2003 09:19:18 -0000 1.4
@@ -73,95 +73,100 @@
import org.apache.commons.httpclient.auth.MalformedChallengeException;
/**
- * This request handler guards access to a proxy when used in a
- * request handler chain. It checks the headers for valid credentials
- * and performs the authentication handshake if necessary.
+ * This request handler guards access to a proxy when used in a request handler
+ * chain. It checks the headers for valid credentials and performs the
+ * authentication handshake if necessary.
*
* @author Ortwin Glueck
*/
public class ProxyAuthRequestHandler implements HttpRequestHandler {
- private Credentials credentials;
-
- /**
- * TODO replace creds parameter with a class specific to an auth scheme
encapsulating all required information for a specific scheme
- * @param creds
- */
- public ProxyAuthRequestHandler(Credentials creds) {
- if (creds == null) throw new IllegalArgumentException("Credentials can
not be null");
- this.credentials = creds;
- }
-
- public boolean processRequest(SimpleHttpServerConnection conn)
- throws IOException {
- Header[] headers = conn.getHeaders();
- Header clientAuth = findHeader(headers,
HttpAuthenticator.PROXY_AUTH_RESP);
- if (clientAuth != null) {
- boolean ok = checkAuthorization(clientAuth);
- if (ok) conn.connectionKeepAlive();
- return !ok;
- } else {
- performHandshake(conn);
- }
- return true;
- }
-
- /**
- * @param conn
- */
- private void performHandshake(SimpleHttpServerConnection conn) throws
IOException {
- Header challenge = createChallenge();
- ResponseWriter out = conn.getWriter();
- out.println("HTTP/1.1 407 Proxy Authentication Required");
- out.print(challenge.toExternalForm());
- out.print(new Header("Proxy-Connection",
"Keep-Alive").toExternalForm());
- out.print(new Header("Content-Length", "0").toExternalForm());
- out.println();
- out.flush();
- conn.connectionKeepAlive();
- }
-
- /**
- *
- * @return
- */
- private Header createChallenge() {
- Header header = new Header();
- header.setName(HttpAuthenticator.PROXY_AUTH);
- //TODO add more auth schemes
- String challenge = "basic realm=test";
- header.setValue(challenge);
- return header;
- }
-
- /**
- * Checks if the credentials provided by the client match the required
credentials
- * @return true if the client is authorized, false if not.
- * @param clientAuth
- */
- private boolean checkAuthorization(Header clientAuth) {
- // TODO Auto-generated method stub
- BasicScheme scheme;
- try {
- scheme = new BasicScheme();
+ private Credentials credentials;
+
+ /**
+ * TODO replace creds parameter with a class specific to an auth scheme
+ * encapsulating all required information for a specific scheme
+ *
+ * @param creds
+ */
+ public ProxyAuthRequestHandler(Credentials creds) {
+ if (creds == null)
+ throw new IllegalArgumentException("Credentials can not be null");
+ this.credentials = creds;
+ }
+
+ public boolean processRequest(SimpleHttpServerConnection conn) throws
IOException {
+ Header[] headers = conn.getHeaders();
+ Header clientAuth = findHeader(headers, HttpAuthenticator.PROXY_AUTH_RESP);
+ if (clientAuth != null) {
+ boolean ok = checkAuthorization(clientAuth);
+ if (ok)
+ conn.connectionKeepAlive();
+ return !ok;
+ } else {
+ performHandshake(conn);
+ }
+ return true;
+ }
+
+ /**
+ * @param conn
+ */
+ private void performHandshake(SimpleHttpServerConnection conn) throws
IOException {
+ Header challenge = createChallenge();
+ ResponseWriter out = conn.getWriter();
+ out.println("HTTP/1.1 407 Proxy Authentication Required");
+ out.print(challenge.toExternalForm());
+ out.print(new Header("Proxy-Connection", "Keep-Alive").toExternalForm());
+ out.print(new Header("Content-Length", "0").toExternalForm());
+ out.println();
+ out.flush();
+ conn.connectionKeepAlive();
+ }
+
+ /**
+ * @return
+ */
+ private Header createChallenge() {
+ Header header = new Header();
+ header.setName(HttpAuthenticator.PROXY_AUTH);
+ //TODO add more auth schemes
+ String challenge = "basic realm=test";
+ header.setValue(challenge);
+ return header;
+ }
+
+ /**
+ * Checks if the credentials provided by the client match the required
+ * credentials
+ *
+ * @return true if the client is authorized, false if not.
+ * @param clientAuth
+ */
+ private boolean checkAuthorization(Header clientAuth) {
+ // TODO Auto-generated method stub
+ BasicScheme scheme;
+ try {
+ scheme = new BasicScheme();
scheme.processChallenge("basic realm=test");
- String expectedAuthString = scheme.authenticate(credentials,
null, null);
- return expectedAuthString.equals(clientAuth.getValue());
- } catch (MalformedChallengeException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (AuthenticationException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return false;
- }
-
- private Header findHeader(Header[] headers, String name) {
- for(int i=0; i<headers.length; i++) {
- Header header = headers[i];
- if (header.getName().equalsIgnoreCase(name)) return header;
- }
- return null;
- }
+ String expectedAuthString = scheme.authenticate(credentials, null,
null);
+ return expectedAuthString.equals(clientAuth.getValue());
+ } catch (MalformedChallengeException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (AuthenticationException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+ private Header findHeader(Header[] headers, String name) {
+ for (int i = 0; i < headers.length; i++) {
+ Header header = headers[i];
+ if (header.getName().equalsIgnoreCase(name))
+ return header;
+ }
+ return null;
+ }
}
1.3 +31 -32
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleProxy.java
Index: SimpleProxy.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleProxy.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SimpleProxy.java 5 Dec 2003 21:23:15 -0000 1.2
+++ SimpleProxy.java 11 Dec 2003 09:19:18 -0000 1.3
@@ -68,39 +68,38 @@
import org.apache.commons.httpclient.Credentials;
/**
- * Simple server that registers default request handlers to act as
- * a proxy.
+ * Simple server that registers default request handlers to act as a proxy.
+ *
* @author Ortwin Glueck
*/
public class SimpleProxy extends SimpleHttpServer {
- private HttpRequestHandlerChain chain = new SimpleChain();
-
+ private HttpRequestHandlerChain chain = new SimpleChain();
+
+ /**
+ * @throws IOException
+ */
+ public SimpleProxy() throws IOException {
+ setRequestHandler(chain);
+ }
+
/**
- * @throws IOException
- */
- public SimpleProxy() throws IOException {
- setRequestHandler(chain);
- }
-
- /**
- *
- * @param port The local TCP port to listen on
- * @throws IOException
- */
- public SimpleProxy(int port) throws IOException {
- super(port);
- }
-
- public void requireCredentials(Credentials creds) {
- chain.prependHandler(new ProxyAuthRequestHandler(creds));
- }
-
- private class SimpleChain extends HttpRequestHandlerChain {
- public SimpleChain() {
- appendHandler(new TransparentProxyRequestHandler());
- appendHandler(new ProxyRequestHandler());
- }
- }
+ * @param port
+ * The local TCP port to listen on
+ * @throws IOException
+ */
+ public SimpleProxy(int port) throws IOException {
+ super(port);
+ }
+
+ public void requireCredentials(Credentials creds) {
+ chain.prependHandler(new ProxyAuthRequestHandler(creds));
+ }
+ private class SimpleChain extends HttpRequestHandlerChain {
+ public SimpleChain() {
+ appendHandler(new TransparentProxyRequestHandler());
+ appendHandler(new ProxyRequestHandler());
+ }
+ }
}
1.3 +45 -44
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java
Index: SimpleHttpServerConnection.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleHttpServerConnection.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SimpleHttpServerConnection.java 5 Dec 2003 21:23:15 -0000 1.2
+++ SimpleHttpServerConnection.java 11 Dec 2003 09:19:18 -0000 1.3
@@ -77,26 +77,26 @@
import org.apache.commons.logging.LogFactory;
/**
- * A connection to the SimpleHttpServer.
+ * A connection to the SimpleHttpServer.
*
* @author Christian Kohlschuetter
*/
public class SimpleHttpServerConnection implements Runnable {
-
+
private static final Log LOG =
LogFactory.getLog(SimpleHttpServerConnection.class);
-
+
private SimpleHttpServer server;
private Socket socket;
private InputStream in;
private OutputStream out;
private int requestNo = 0;
-
+
private boolean keepAlive = false;
- private RequestLine requestLine;
+ private RequestLine requestLine;
- private Header[] headers;
+ private Header[] headers;
public SimpleHttpServerConnection(SimpleHttpServer server, Socket socket)
throws IOException {
this.server = server;
@@ -107,13 +107,13 @@
public void destroy() {
try {
- if(socket != null) {
+ if (socket != null) {
in.close();
out.close();
socket.close();
socket = null;
- }
- } catch(IOException e) {
+ }
+ } catch (IOException e) {
// fail("Unexpected exception: " + e);
}
server.removeConnection(this);
@@ -124,10 +124,10 @@
try {
do {
keepAlive = false;
-
+
++this.requestNo;
readRequest();
- } while(keepAlive);
+ } while (keepAlive);
} catch (SocketException ignore) {
} catch (IOException e) {
LOG.error("ServerConnection read error", e);
@@ -136,7 +136,7 @@
destroy();
}
}
-
+
/**
* Requests to close connection after processing this request.
*/
@@ -146,43 +146,44 @@
/**
* Requests to keep the connection alive after processing this request
- * (must be re-issued for every request if permanent keep-alive is desired).
- *
+ * (must be re-issued for every request if permanent keep-alive is
+ * desired).
+ *
*/
public void connectionKeepAlive() {
keepAlive = true;
}
-
+
/**
* Returns the ResponseWriter used to write the output to the socket.
*
- * @return This connection's ResponseWriter
+ * @return This connection's ResponseWriter
*/
public ResponseWriter getWriter() {
try {
- return new ResponseWriter(out);
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException(e.toString());
- }
+ return new ResponseWriter(out);
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException(e.toString());
+ }
}
-
+
/**
- * Returns the number of requests processed (including the current one)
- * for this connection.
+ * Returns the number of requests processed (including the current one) for
+ * this connection.
*
* @return
*/
public int getRequestNumber() {
return requestNo;
}
-
+
private void readRequest() throws IOException {
String line;
do {
line = HttpParser.readLine(in);
- } while(line != null && line.length() == 0);
-
- if(line == null) {
+ } while (line != null && line.length() == 0);
+
+ if (line == null) {
connectionClose();
return;
}
@@ -190,30 +191,30 @@
try {
requestLine = RequestLine.parseLine(line);
headers = HttpParser.parseHeaders(in);
- } catch(IOException e) {
+ } catch (IOException e) {
connectionClose();
ErrorResponse.getInstance().getResponse(HttpStatus.SC_BAD_REQUEST).processRequest(this);
- return;
+ return;
}
server.processRequest(this);
out.flush();
}
-
+
public Header[] getHeaders() {
- Header[] copy = new Header[headers.length];
- System.arraycopy(headers, 0, copy, 0, headers.length);
- return copy;
+ Header[] copy = new Header[headers.length];
+ System.arraycopy(headers, 0, copy, 0, headers.length);
+ return copy;
}
-
+
public RequestLine getRequestLine() {
- return requestLine;
+ return requestLine;
}
- public InputStream getInputStream() {
- return in;
- }
+ public InputStream getInputStream() {
+ return in;
+ }
- public OutputStream getOutputStream() {
- return out;
- }
+ public OutputStream getOutputStream() {
+ return out;
+ }
}
1.3 +63 -55
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestProxy.java
Index: TestProxy.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestProxy.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestProxy.java 5 Dec 2003 21:23:14 -0000 1.2
+++ TestProxy.java 11 Dec 2003 09:19:19 -0000 1.3
@@ -71,60 +71,68 @@
/**
* Tests for proxied connections.
+ *
* @author Ortwin Glueck
*/
public class TestProxy extends TestCase {
- public TestProxy(String testName) {
- super(testName);
- }
-
- public static Test suite() {
- return new TestSuite(TestProxy.class);
- }
-
- protected void setUp() throws Exception {
- /*
- System.setProperty("org.apache.commons.logging.Log",
"org.apache.commons.logging.impl.SimpleLog");
-
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
-
System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire",
"debug");
-
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient",
"debug");
- */
- }
-
- public void testSimpleGet() throws Exception {
- SimpleProxy proxy = new SimpleProxy();
-
- HttpClient client = new HttpClient();
- HostConfiguration hc = new HostConfiguration();
- hc.setHost(System.getProperty("httpclient.test.localHost",
"localhost"),
-
Integer.parseInt(System.getProperty("httpclient.test.localPort", "8080")),
- Protocol.getProtocol("http"));
- hc.setProxy(proxy.getLocalAddress(), proxy.getLocalPort());
- client.setHostConfiguration(hc);
-
- GetMethod get = new GetMethod("/");
- client.executeMethod(get);
- assertEquals(200, get.getStatusCode());
- get.releaseConnection();
- }
-
- public void testAuthGet() throws Exception {
- Credentials creds = new UsernamePasswordCredentials("user",
"password");
- SimpleProxy proxy = new SimpleProxy();
- proxy.requireCredentials(creds);
-
- HttpClient client = new HttpClient();
- HostConfiguration hc = new HostConfiguration();
- hc.setHost(System.getProperty("httpclient.test.localHost",
"localhost"),
-
Integer.parseInt(System.getProperty("httpclient.test.localPort", "8080")),
- Protocol.getProtocol("http"));
hc.setProxy(proxy.getLocalAddress(), proxy.getLocalPort());
- client.setHostConfiguration(hc);
- client.getState().setProxyCredentials(null, null, creds);
-
- GetMethod get = new GetMethod("/");
- client.executeMethod(get);
- assertEquals(200, get.getStatusCode());
- get.releaseConnection();
- }
+ public TestProxy(String testName) {
+ super(testName);
+ }
+
+ public static Test suite() {
+ return new TestSuite(TestProxy.class);
+ }
+
+ protected void setUp() throws Exception {
+ /*
+ * System.setProperty("org.apache.commons.logging.Log",
+ * "org.apache.commons.logging.impl.SimpleLog");
+ * System.setProperty("org.apache.commons.logging.simplelog.showdatetime",
+ * "true");
+ *
System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire",
+ * "debug");
+ *
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient",
+ * "debug");
+ */
+ }
+
+ public void testSimpleGet() throws Exception {
+ SimpleProxy proxy = new SimpleProxy();
+
+ HttpClient client = new HttpClient();
+ HostConfiguration hc = new HostConfiguration();
+ hc.setHost(
+ System.getProperty("httpclient.test.localHost", "localhost"),
+ Integer.parseInt(System.getProperty("httpclient.test.localPort", "8080")),
+ Protocol.getProtocol("http"));
+ hc.setProxy(proxy.getLocalAddress(), proxy.getLocalPort());
+ client.setHostConfiguration(hc);
+
+ GetMethod get = new GetMethod("/");
+ client.executeMethod(get);
+ assertEquals(200, get.getStatusCode());
+ get.releaseConnection();
+ }
+
+ public void testAuthGet() throws Exception {
+ Credentials creds = new UsernamePasswordCredentials("user", "password");
+ SimpleProxy proxy = new SimpleProxy();
+ proxy.requireCredentials(creds);
+
+ HttpClient client = new HttpClient();
+ HostConfiguration hc = new HostConfiguration();
+ hc.setHost(
+ System.getProperty("httpclient.test.localHost", "localhost"),
+ Integer.parseInt(System.getProperty("httpclient.test.localPort", "8080")),
+ Protocol.getProtocol("http"));
+ hc.setProxy(proxy.getLocalAddress(), proxy.getLocalPort());
+ client.setHostConfiguration(hc);
+ client.getState().setProxyCredentials(null, null, creds);
+
+ GetMethod get = new GetMethod("/");
+ client.executeMethod(get);
+ assertEquals(200, get.getStatusCode());
+ get.releaseConnection();
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]