2 Adobe Systems Incorporated(r) Source Code License Agreement
3 Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved.
5 Please read this Source Code License Agreement carefully before using
8 Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive,
9 no-charge, royalty-free, irrevocable copyright license, to reproduce,
10 prepare derivative works of, publicly display, publicly perform, and
11 distribute this source code and such derivative works in source or
12 object code form without any attribution requirements.
14 The name "Adobe Systems Incorporated" must not be used to endorse or promote products
15 derived from the source code without prior written permission.
17 You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and
18 against any loss, damage, claims or lawsuits, including attorney's
19 fees that arise or result from your use or distribution of the source
22 THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT
23 ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
24 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF
26 NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA
27 OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF
33 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 package com.adobe.net.proxies
37 import flash.events.Event;
38 import flash.events.IOErrorEvent;
39 import flash.events.ProgressEvent;
40 import flash.net.Socket;
43 * This class allows TCP socket connections through HTTP proxies in accordance with
46 * ftp://ftp.rfc-editor.org/in-notes/rfc2817.txt
48 * It can also be used to make direct connections to a destination, as well. If you
49 * pass the host and port into the constructor, no proxy will be used. You can also
50 * call connect, passing in the host and the port, and if you didn't set the proxy
51 * info, a direct connection will be made. A proxy is only used after you have called
52 * the setProxyInfo function.
54 * The connection to and negotiation with the proxy is completely hidden. All the
55 * same events are thrown whether you are using a proxy or not, and the data you
56 * receive from the target server will look exact as it would if you were connected
57 * to it directly rather than through a proxy.
59 * @author Christian Cantrell
62 public class RFC2817Socket
65 private var proxyHost:String = null;
66 private var host:String = null;
67 private var proxyPort:int = 0;
68 private var port:int = 0;
69 private var deferredEventHandlers:Object = new Object();
70 private var buffer:String = new String();
73 * Construct a new RFC2817Socket object. If you pass in the host and the port,
74 * no proxy will be used. If you want to use a proxy, instantiate with no
75 * arguments, call setProxyInfo, then call connect.
77 public function RFC2817Socket(host:String = null, port:int = 0)
79 if (host != null && port != 0)
86 * Set the proxy host and port number. Your connection will only proxied if
87 * this function has been called.
89 public function setProxyInfo(host:String, port:int):void
91 this.proxyHost = host;
92 this.proxyPort = port;
94 var deferredSocketDataHandler:Object = this.deferredEventHandlers[ProgressEvent.SOCKET_DATA];
95 var deferredConnectHandler:Object = this.deferredEventHandlers[Event.CONNECT];
97 if (deferredSocketDataHandler != null)
99 super.removeEventListener(ProgressEvent.SOCKET_DATA, deferredSocketDataHandler.listener, deferredSocketDataHandler.useCapture);
102 if (deferredConnectHandler != null)
104 super.removeEventListener(Event.CONNECT, deferredConnectHandler.listener, deferredConnectHandler.useCapture);
109 * Connect to the specified host over the specified port. If you want your
110 * connection proxied, call the setProxyInfo function first.
112 public override function connect(host:String, port:int):void
114 if (this.proxyHost == null)
116 this.redirectConnectEvent();
117 this.redirectSocketDataEvent();
118 super.connect(host, port);
124 super.addEventListener(Event.CONNECT, this.onConnect);
125 super.addEventListener(ProgressEvent.SOCKET_DATA, this.onSocketData);
126 super.connect(this.proxyHost, this.proxyPort);
130 private function onConnect(event:Event):void
132 this.writeUTFBytes("CONNECT "+this.host+":"+this.port+" HTTP/1.1\n\n");
134 this.redirectConnectEvent();
137 private function onSocketData(event:ProgressEvent):void
139 while (this.bytesAvailable != 0)
141 this.buffer += this.readUTFBytes(1);
142 if (this.buffer.search(/\r?\n\r?\n$/) != -1)
144 this.checkResponse(event);
150 private function checkResponse(event:ProgressEvent):void
152 var responseCode:String = this.buffer.substr(this.buffer.indexOf(" ")+1, 3);
154 if (responseCode.search(/^2/) == -1)
156 var ioError:IOErrorEvent = new IOErrorEvent(IOErrorEvent.IO_ERROR);
157 ioError.text = "Error connecting to the proxy ["+this.proxyHost+"] on port ["+this.proxyPort+"]: " + this.buffer;
158 this.dispatchEvent(ioError);
162 this.redirectSocketDataEvent();
163 this.dispatchEvent(new Event(Event.CONNECT));
164 if (this.bytesAvailable > 0)
166 this.dispatchEvent(event);
172 private function redirectConnectEvent():void
174 super.removeEventListener(Event.CONNECT, onConnect);
175 var deferredEventHandler:Object = this.deferredEventHandlers[Event.CONNECT];
176 if (deferredEventHandler != null)
178 super.addEventListener(Event.CONNECT, deferredEventHandler.listener, deferredEventHandler.useCapture, deferredEventHandler.priority, deferredEventHandler.useWeakReference);
182 private function redirectSocketDataEvent():void
184 super.removeEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
185 var deferredEventHandler:Object = this.deferredEventHandlers[ProgressEvent.SOCKET_DATA];
186 if (deferredEventHandler != null)
188 super.addEventListener(ProgressEvent.SOCKET_DATA, deferredEventHandler.listener, deferredEventHandler.useCapture, deferredEventHandler.priority, deferredEventHandler.useWeakReference);
192 public override function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int=0.0, useWeakReference:Boolean=false):void
194 if (type == Event.CONNECT || type == ProgressEvent.SOCKET_DATA)
196 this.deferredEventHandlers[type] = {listener:listener,useCapture:useCapture, priority:priority, useWeakReference:useWeakReference};
200 super.addEventListener(type, listener, useCapture, priority, useWeakReference);