1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/TransparentProxyRequestHandler.java,v 1.1.2.2 2004/02/22 18:21:18 olegk Exp $
3    * $Revision: 1.1.2.2 $
4    * $Date: 2004/02/22 18:21:18 $
5    *
6    * ====================================================================
7    *
8    *  Copyright 1999-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   * [Additional notices, if required by prior licensing conditions]
29   *
30   */
31  
32  package org.apache.commons.httpclient.server;
33  
34  import java.io.IOException;
35  import java.io.InputStream;
36  import java.io.OutputStream;
37  import java.io.OutputStreamWriter;
38  import java.io.UnsupportedEncodingException;
39  import java.io.Writer;
40  import java.net.Socket;
41  
42  import org.apache.commons.httpclient.Header;
43  import org.apache.commons.httpclient.HttpConstants;
44  import org.apache.commons.httpclient.HttpURL;
45  import org.apache.commons.httpclient.URI;
46  
47  /***
48   * This request handler can handle the CONNECT method. It does
49   * nothing for any other HTTP methods.
50   * 
51   * @author Ortwin Glueck
52   */
53  public class TransparentProxyRequestHandler implements HttpRequestHandler {
54  
55  	/* (non-Javadoc)
56  	 * @see org.apache.commons.httpclient.server.HttpRequestHandler#processRequest(org.apache.commons.httpclient.server.SimpleHttpServerConnection)
57  	 */
58  	public boolean processRequest(SimpleHttpServerConnection conn) throws IOException {
59  		RequestLine line = conn.getRequestLine();
60  		String method = line.getMethod();
61  		if (!"CONNECT".equalsIgnoreCase(method)) return false;
62  		URI url = new HttpURL(line.getUri());
63  		handshake(conn, url);
64  		return true;
65  	}
66  
67  	private void handshake(SimpleHttpServerConnection conn, URI url) throws IOException  {
68  		Socket targetSocket = new Socket(url.getHost(), url.getPort());
69  		InputStream sourceIn = conn.getInputStream();
70  		OutputStream sourceOut = conn.getOutputStream();
71  		InputStream targetIn = targetSocket.getInputStream();
72  		OutputStream targetOut = targetSocket.getOutputStream();
73  
74  		ResponseWriter out = conn.getWriter();
75  		out.println("HTTP/1.1 200 Connection established");
76  		out.flush();
77  
78  		BidiStreamProxy bdsp = new BidiStreamProxy(sourceIn, sourceOut, targetIn, targetOut);
79  		bdsp.start();
80  		try {
81  			bdsp.block();
82  		} catch (InterruptedException e) {
83  			throw new IOException(e.toString());
84  		}
85  	}
86  	
87  	private void sendHeaders(Header[] headers, OutputStream os) throws IOException {
88  		Writer out;
89  		try {
90  			out = new OutputStreamWriter(os, HttpConstants.HTTP_ELEMENT_CHARSET);
91  		} catch (UnsupportedEncodingException e) {
92  			throw new RuntimeException(e.toString());
93  		}
94  		for (int i=0; i<headers.length; i++) {
95  			out.write(headers[i].toExternalForm());
96  		}
97  	}
98  }