001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/server/TransparentProxyRequestHandler.java,v 1.7 2004/12/11 22:35:26 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient.server;
032:
033: import java.io.IOException;
034: import java.io.InputStream;
035: import java.io.InterruptedIOException;
036: import java.io.OutputStream;
037: import java.net.Socket;
038:
039: import org.apache.commons.httpclient.Header;
040: import org.apache.commons.httpclient.HttpStatus;
041: import org.apache.commons.httpclient.HttpVersion;
042:
043: /**
044: * This request handler can handle the CONNECT method. It does nothing for any
045: * other HTTP methods.
046: *
047: * @author Ortwin Glueck
048: */
049: public class TransparentProxyRequestHandler implements
050: HttpRequestHandler {
051:
052: /*
053: * (non-Javadoc)
054: *
055: * @see org.apache.commons.httpclient.server.HttpRequestHandler#processRequest(org.apache.commons.httpclient.server.SimpleHttpServerConnection)
056: */
057: public boolean processRequest(
058: final SimpleHttpServerConnection conn,
059: final SimpleRequest request) throws IOException {
060:
061: RequestLine line = request.getRequestLine();
062: HttpVersion ver = line.getHttpVersion();
063: String method = line.getMethod();
064: if (!"CONNECT".equalsIgnoreCase(method)) {
065: return false;
066: }
067: Socket targetSocket = null;
068: try {
069: targetSocket = connect(line.getUri());
070: } catch (IOException e) {
071: SimpleResponse response = new SimpleResponse();
072: response.setStatusLine(ver, HttpStatus.SC_NOT_FOUND);
073: response.setHeader(new Header("Server", "test proxy"));
074: response
075: .setBodyString("Cannot connect to " + line.getUri());
076: conn.writeResponse(response);
077: return true;
078: }
079: SimpleResponse response = new SimpleResponse();
080: response.setHeader(new Header("Server", "test proxy"));
081: response.setStatusLine(ver, HttpStatus.SC_OK,
082: "Connection established");
083: conn.writeResponse(response);
084:
085: SimpleHttpServerConnection target = new SimpleHttpServerConnection(
086: targetSocket);
087: pump(conn, target);
088: return true;
089: }
090:
091: private void pump(final SimpleHttpServerConnection source,
092: final SimpleHttpServerConnection target) throws IOException {
093:
094: source.setSocketTimeout(100);
095: target.setSocketTimeout(100);
096:
097: InputStream sourceIn = source.getInputStream();
098: OutputStream sourceOut = source.getOutputStream();
099: InputStream targetIn = target.getInputStream();
100: OutputStream targetOut = target.getOutputStream();
101:
102: byte[] tmp = new byte[1024];
103: int l;
104: for (;;) {
105: if (!source.isOpen() || !target.isOpen()) {
106: break;
107: }
108: try {
109: l = sourceIn.read(tmp);
110: if (l == -1) {
111: break;
112: }
113: targetOut.write(tmp, 0, l);
114: } catch (InterruptedIOException ignore) {
115: if (Thread.interrupted()) {
116: break;
117: }
118: }
119: try {
120: l = targetIn.read(tmp);
121: if (l == -1) {
122: break;
123: }
124: sourceOut.write(tmp, 0, l);
125: } catch (InterruptedIOException ignore) {
126: if (Thread.interrupted()) {
127: break;
128: }
129: }
130: }
131: }
132:
133: private static Socket connect(final String host) throws IOException {
134: String hostname = null;
135: int port;
136: int i = host.indexOf(':');
137: if (i != -1) {
138: hostname = host.substring(0, i);
139: try {
140: port = Integer.parseInt(host.substring(i + 1));
141: } catch (NumberFormatException ex) {
142: throw new IOException("Invalid host address: " + host);
143: }
144: } else {
145: hostname = host;
146: port = 80;
147: }
148: return new Socket(hostname, port);
149: }
150:
151: }
|