--- /dev/null
+/*\r
+ Copyright (c) 2008, Adobe Systems Incorporated\r
+ All rights reserved.\r
+\r
+ Redistribution and use in source and binary forms, with or without \r
+ modification, are permitted provided that the following conditions are\r
+ met:\r
+\r
+ * Redistributions of source code must retain the above copyright notice, \r
+ this list of conditions and the following disclaimer.\r
+ \r
+ * Redistributions in binary form must reproduce the above copyright\r
+ notice, this list of conditions and the following disclaimer in the \r
+ documentation and/or other materials provided with the distribution.\r
+ \r
+ * Neither the name of Adobe Systems Incorporated nor the names of its \r
+ contributors may be used to endorse or promote products derived from \r
+ this software without specific prior written permission.\r
+\r
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS\r
+ IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,\r
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\r
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR \r
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,\r
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,\r
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR\r
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+*/\r
+package com.adobe.net.proxies\r
+{\r
+ import flash.events.Event;\r
+ import flash.events.IOErrorEvent;\r
+ import flash.events.ProgressEvent;\r
+ import flash.net.Socket;\r
+\r
+ /**\r
+ * This class allows TCP socket connections through HTTP proxies in accordance with\r
+ * RFC 2817:\r
+ * \r
+ * ftp://ftp.rfc-editor.org/in-notes/rfc2817.txt\r
+ * \r
+ * It can also be used to make direct connections to a destination, as well. If you\r
+ * pass the host and port into the constructor, no proxy will be used. You can also\r
+ * call connect, passing in the host and the port, and if you didn't set the proxy\r
+ * info, a direct connection will be made. A proxy is only used after you have called\r
+ * the setProxyInfo function.\r
+ * \r
+ * The connection to and negotiation with the proxy is completely hidden. All the\r
+ * same events are thrown whether you are using a proxy or not, and the data you\r
+ * receive from the target server will look exact as it would if you were connected\r
+ * to it directly rather than through a proxy.\r
+ * \r
+ * @author Christian Cantrell\r
+ * \r
+ **/\r
+ public class RFC2817Socket\r
+ extends Socket\r
+ {\r
+ private var proxyHost:String = null;\r
+ private var host:String = null;\r
+ private var proxyPort:int = 0;\r
+ private var port:int = 0;\r
+ private var deferredEventHandlers:Object = new Object();\r
+ private var buffer:String = new String();\r
+\r
+ /**\r
+ * Construct a new RFC2817Socket object. If you pass in the host and the port,\r
+ * no proxy will be used. If you want to use a proxy, instantiate with no\r
+ * arguments, call setProxyInfo, then call connect.\r
+ **/\r
+ public function RFC2817Socket(host:String = null, port:int = 0)\r
+ {\r
+ super(host, port);\r
+ }\r
+ \r
+ /**\r
+ * Set the proxy host and port number. Your connection will only proxied if\r
+ * this function has been called.\r
+ **/\r
+ public function setProxyInfo(host:String, port:int):void\r
+ {\r
+ this.proxyHost = host;\r
+ this.proxyPort = port;\r
+\r
+ var deferredSocketDataHandler:Object = this.deferredEventHandlers[ProgressEvent.SOCKET_DATA];\r
+ var deferredConnectHandler:Object = this.deferredEventHandlers[Event.CONNECT];\r
+\r
+ if (deferredSocketDataHandler != null)\r
+ {\r
+ super.removeEventListener(ProgressEvent.SOCKET_DATA, deferredSocketDataHandler.listener, deferredSocketDataHandler.useCapture);\r
+ }\r
+\r
+ if (deferredConnectHandler != null)\r
+ {\r
+ super.removeEventListener(Event.CONNECT, deferredConnectHandler.listener, deferredConnectHandler.useCapture);\r
+ }\r
+ }\r
+ \r
+ /**\r
+ * Connect to the specified host over the specified port. If you want your\r
+ * connection proxied, call the setProxyInfo function first.\r
+ **/\r
+ public override function connect(host:String, port:int):void\r
+ {\r
+ if (this.proxyHost == null)\r
+ {\r
+ this.redirectConnectEvent();\r
+ this.redirectSocketDataEvent();\r
+ super.connect(host, port);\r
+ }\r
+ else\r
+ {\r
+ this.host = host;\r
+ this.port = port;\r
+ super.addEventListener(Event.CONNECT, this.onConnect);\r
+ super.addEventListener(ProgressEvent.SOCKET_DATA, this.onSocketData);\r
+ super.connect(this.proxyHost, this.proxyPort);\r
+ }\r
+ }\r
+\r
+ private function onConnect(event:Event):void\r
+ {\r
+ this.writeUTFBytes("CONNECT "+this.host+":"+this.port+" HTTP/1.1\n\n");\r
+ this.flush();\r
+ this.redirectConnectEvent();\r
+ }\r
+ \r
+ private function onSocketData(event:ProgressEvent):void\r
+ {\r
+ while (this.bytesAvailable != 0)\r
+ {\r
+ this.buffer += this.readUTFBytes(1);\r
+ if (this.buffer.search(/\r?\n\r?\n$/) != -1)\r
+ {\r
+ this.checkResponse(event);\r
+ break;\r
+ }\r
+ }\r
+ }\r
+ \r
+ private function checkResponse(event:ProgressEvent):void\r
+ {\r
+ var responseCode:String = this.buffer.substr(this.buffer.indexOf(" ")+1, 3);\r
+\r
+ if (responseCode.search(/^2/) == -1)\r
+ {\r
+ var ioError:IOErrorEvent = new IOErrorEvent(IOErrorEvent.IO_ERROR);\r
+ ioError.text = "Error connecting to the proxy ["+this.proxyHost+"] on port ["+this.proxyPort+"]: " + this.buffer;\r
+ this.dispatchEvent(ioError);\r
+ }\r
+ else\r
+ {\r
+ this.redirectSocketDataEvent();\r
+ this.dispatchEvent(new Event(Event.CONNECT));\r
+ if (this.bytesAvailable > 0)\r
+ {\r
+ this.dispatchEvent(event);\r
+ }\r
+ }\r
+ this.buffer = null;\r
+ }\r
+ \r
+ private function redirectConnectEvent():void\r
+ {\r
+ super.removeEventListener(Event.CONNECT, onConnect);\r
+ var deferredEventHandler:Object = this.deferredEventHandlers[Event.CONNECT];\r
+ if (deferredEventHandler != null)\r
+ {\r
+ super.addEventListener(Event.CONNECT, deferredEventHandler.listener, deferredEventHandler.useCapture, deferredEventHandler.priority, deferredEventHandler.useWeakReference); \r
+ }\r
+ }\r
+ \r
+ private function redirectSocketDataEvent():void\r
+ {\r
+ super.removeEventListener(ProgressEvent.SOCKET_DATA, onSocketData);\r
+ var deferredEventHandler:Object = this.deferredEventHandlers[ProgressEvent.SOCKET_DATA];\r
+ if (deferredEventHandler != null)\r
+ {\r
+ super.addEventListener(ProgressEvent.SOCKET_DATA, deferredEventHandler.listener, deferredEventHandler.useCapture, deferredEventHandler.priority, deferredEventHandler.useWeakReference); \r
+ }\r
+ }\r
+ \r
+ public override function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int=0.0, useWeakReference:Boolean=false):void\r
+ {\r
+ if (type == Event.CONNECT || type == ProgressEvent.SOCKET_DATA)\r
+ {\r
+ this.deferredEventHandlers[type] = {listener:listener,useCapture:useCapture, priority:priority, useWeakReference:useWeakReference};\r
+ }\r
+ else\r
+ {\r
+ super.addEventListener(type, listener, useCapture, priority, useWeakReference);\r
+ }\r
+ }\r
+ }\r
+}
\ No newline at end of file