2 Copyright (c) 2008, Adobe Systems Incorporated
\r
5 Redistribution and use in source and binary forms, with or without
\r
6 modification, are permitted provided that the following conditions are
\r
9 * Redistributions of source code must retain the above copyright notice,
\r
10 this list of conditions and the following disclaimer.
\r
12 * Redistributions in binary form must reproduce the above copyright
\r
13 notice, this list of conditions and the following disclaimer in the
\r
14 documentation and/or other materials provided with the distribution.
\r
16 * Neither the name of Adobe Systems Incorporated nor the names of its
\r
17 contributors may be used to endorse or promote products derived from
\r
18 this software without specific prior written permission.
\r
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
\r
21 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
\r
22 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
\r
23 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
\r
24 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
\r
25 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
\r
26 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
\r
27 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
\r
28 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r
29 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
\r
30 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
32 package com.adobe.net.proxies
\r
34 import flash.events.Event;
\r
35 import flash.events.IOErrorEvent;
\r
36 import flash.events.ProgressEvent;
\r
37 import flash.net.Socket;
\r
40 * This class allows TCP socket connections through HTTP proxies in accordance with
\r
43 * ftp://ftp.rfc-editor.org/in-notes/rfc2817.txt
\r
45 * It can also be used to make direct connections to a destination, as well. If you
\r
46 * pass the host and port into the constructor, no proxy will be used. You can also
\r
47 * call connect, passing in the host and the port, and if you didn't set the proxy
\r
48 * info, a direct connection will be made. A proxy is only used after you have called
\r
49 * the setProxyInfo function.
\r
51 * The connection to and negotiation with the proxy is completely hidden. All the
\r
52 * same events are thrown whether you are using a proxy or not, and the data you
\r
53 * receive from the target server will look exact as it would if you were connected
\r
54 * to it directly rather than through a proxy.
\r
56 * @author Christian Cantrell
\r
59 public class RFC2817Socket
\r
62 private var proxyHost:String = null;
\r
63 private var host:String = null;
\r
64 private var proxyPort:int = 0;
\r
65 private var port:int = 0;
\r
66 private var deferredEventHandlers:Object = new Object();
\r
67 private var buffer:String = new String();
\r
70 * Construct a new RFC2817Socket object. If you pass in the host and the port,
\r
71 * no proxy will be used. If you want to use a proxy, instantiate with no
\r
72 * arguments, call setProxyInfo, then call connect.
\r
74 public function RFC2817Socket(host:String = null, port:int = 0)
\r
80 * Set the proxy host and port number. Your connection will only proxied if
\r
81 * this function has been called.
\r
83 public function setProxyInfo(host:String, port:int):void
\r
85 this.proxyHost = host;
\r
86 this.proxyPort = port;
\r
88 var deferredSocketDataHandler:Object = this.deferredEventHandlers[ProgressEvent.SOCKET_DATA];
\r
89 var deferredConnectHandler:Object = this.deferredEventHandlers[Event.CONNECT];
\r
91 if (deferredSocketDataHandler != null)
\r
93 super.removeEventListener(ProgressEvent.SOCKET_DATA, deferredSocketDataHandler.listener, deferredSocketDataHandler.useCapture);
\r
96 if (deferredConnectHandler != null)
\r
98 super.removeEventListener(Event.CONNECT, deferredConnectHandler.listener, deferredConnectHandler.useCapture);
\r
103 * Connect to the specified host over the specified port. If you want your
\r
104 * connection proxied, call the setProxyInfo function first.
\r
106 public override function connect(host:String, port:int):void
\r
108 if (this.proxyHost == null)
\r
110 this.redirectConnectEvent();
\r
111 this.redirectSocketDataEvent();
\r
112 super.connect(host, port);
\r
118 super.addEventListener(Event.CONNECT, this.onConnect);
\r
119 super.addEventListener(ProgressEvent.SOCKET_DATA, this.onSocketData);
\r
120 super.connect(this.proxyHost, this.proxyPort);
\r
124 private function onConnect(event:Event):void
\r
126 this.writeUTFBytes("CONNECT "+this.host+":"+this.port+" HTTP/1.1\n\n");
\r
128 this.redirectConnectEvent();
\r
131 private function onSocketData(event:ProgressEvent):void
\r
133 while (this.bytesAvailable != 0)
\r
135 this.buffer += this.readUTFBytes(1);
\r
136 if (this.buffer.search(/\r?\n\r?\n$/) != -1)
\r
138 this.checkResponse(event);
\r
144 private function checkResponse(event:ProgressEvent):void
\r
146 var responseCode:String = this.buffer.substr(this.buffer.indexOf(" ")+1, 3);
\r
148 if (responseCode.search(/^2/) == -1)
\r
150 var ioError:IOErrorEvent = new IOErrorEvent(IOErrorEvent.IO_ERROR);
\r
151 ioError.text = "Error connecting to the proxy ["+this.proxyHost+"] on port ["+this.proxyPort+"]: " + this.buffer;
\r
152 this.dispatchEvent(ioError);
\r
156 this.redirectSocketDataEvent();
\r
157 this.dispatchEvent(new Event(Event.CONNECT));
\r
158 if (this.bytesAvailable > 0)
\r
160 this.dispatchEvent(event);
\r
163 this.buffer = null;
\r
166 private function redirectConnectEvent():void
\r
168 super.removeEventListener(Event.CONNECT, onConnect);
\r
169 var deferredEventHandler:Object = this.deferredEventHandlers[Event.CONNECT];
\r
170 if (deferredEventHandler != null)
\r
172 super.addEventListener(Event.CONNECT, deferredEventHandler.listener, deferredEventHandler.useCapture, deferredEventHandler.priority, deferredEventHandler.useWeakReference);
\r
176 private function redirectSocketDataEvent():void
\r
178 super.removeEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
\r
179 var deferredEventHandler:Object = this.deferredEventHandlers[ProgressEvent.SOCKET_DATA];
\r
180 if (deferredEventHandler != null)
\r
182 super.addEventListener(ProgressEvent.SOCKET_DATA, deferredEventHandler.listener, deferredEventHandler.useCapture, deferredEventHandler.priority, deferredEventHandler.useWeakReference);
\r
186 public override function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int=0.0, useWeakReference:Boolean=false):void
\r
188 if (type == Event.CONNECT || type == ProgressEvent.SOCKET_DATA)
\r
190 this.deferredEventHandlers[type] = {listener:listener,useCapture:useCapture, priority:priority, useWeakReference:useWeakReference};
\r
194 super.addEventListener(type, listener, useCapture, priority, useWeakReference);
\r