1   /*
2    * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/TransparentProxyRequestHandler.java,v 1.6 2004/02/27 19:01:34 olegk Exp $
3    * $Revision: 1.6 $
4    * $Date: 2004/02/27 19:01:34 $
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.HttpURL;
44  import org.apache.commons.httpclient.URI;
45  
46  /***
47   * This request handler can handle the CONNECT method. It does nothing for any
48   * other HTTP methods.
49   * 
50   * @author Ortwin Glueck
51   */
52  public class TransparentProxyRequestHandler implements HttpRequestHandler {
53  
54      /*
55       * (non-Javadoc)
56       * 
57       * @see org.apache.commons.httpclient.server.HttpRequestHandler#processRequest(org.apache.commons.httpclient.server.SimpleHttpServerConnection)
58       */
59      public boolean processRequest(
60          final SimpleHttpServerConnection conn,
61          final SimpleRequest request) throws IOException
62      {
63          RequestLine line = request.getRequestLine();
64          String method = line.getMethod();
65          if (!"CONNECT".equalsIgnoreCase(method))
66              return false;
67          URI url = new HttpURL(line.getUri());
68          handshake(conn, url);
69          return true;
70      }
71  
72      private void handshake(SimpleHttpServerConnection conn, URI url) throws IOException {
73          Socket targetSocket = new Socket(url.getHost(), url.getPort());
74          InputStream sourceIn = conn.getInputStream();
75          OutputStream sourceOut = conn.getOutputStream();
76          InputStream targetIn = targetSocket.getInputStream();
77          OutputStream targetOut = targetSocket.getOutputStream();
78  
79          ResponseWriter out = conn.getWriter();
80          out.println("HTTP/1.1 200 Connection established");
81          out.flush();
82  
83          BidiStreamProxy bdsp = new BidiStreamProxy(sourceIn, sourceOut, targetIn, targetOut);
84          bdsp.start();
85          try {
86              bdsp.block();
87          } catch (InterruptedException e) {
88              throw new IOException(e.toString());
89          }
90      }
91  
92      private void sendHeaders(Header[] headers, OutputStream os) throws IOException {
93          Writer out;
94          try {
95              out = new OutputStreamWriter(os, "US-ASCII");
96          } catch (UnsupportedEncodingException e) {
97              throw new RuntimeException(e.toString());
98          }
99          for (int i = 0; i < headers.length; i++) {
100             out.write(headers[i].toExternalForm());
101         }
102     }
103 }