PageRenderTime 16ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/socket.io-client/lib/vendor/web-socket-js/flash-src/com/adobe/net/proxies/RFC2817Socket.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 204 lines | 123 code | 14 blank | 67 comment | 25 complexity | e8632f370aa10d170af1fa8972eb475b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /*
  2. Adobe Systems Incorporated(r) Source Code License Agreement
  3. Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved.
  4. Please read this Source Code License Agreement carefully before using
  5. the source code.
  6. Adobe Systems Incorporated grants to you a perpetual, worldwide, non-exclusive,
  7. no-charge, royalty-free, irrevocable copyright license, to reproduce,
  8. prepare derivative works of, publicly display, publicly perform, and
  9. distribute this source code and such derivative works in source or
  10. object code form without any attribution requirements.
  11. The name "Adobe Systems Incorporated" must not be used to endorse or promote products
  12. derived from the source code without prior written permission.
  13. You agree to indemnify, hold harmless and defend Adobe Systems Incorporated from and
  14. against any loss, damage, claims or lawsuits, including attorney's
  15. fees that arise or result from your use or distribution of the source
  16. code.
  17. THIS SOURCE CODE IS PROVIDED "AS IS" AND "WITH ALL FAULTS", WITHOUT
  18. ANY TECHNICAL SUPPORT OR ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
  19. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ALSO, THERE IS NO WARRANTY OF
  21. NON-INFRINGEMENT, TITLE OR QUIET ENJOYMENT. IN NO EVENT SHALL MACROMEDIA
  22. OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  25. OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  27. OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOURCE CODE, EVEN IF
  28. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. package com.adobe.net.proxies
  31. {
  32. import flash.events.Event;
  33. import flash.events.IOErrorEvent;
  34. import flash.events.ProgressEvent;
  35. import flash.net.Socket;
  36. /**
  37. * This class allows TCP socket connections through HTTP proxies in accordance with
  38. * RFC 2817:
  39. *
  40. * ftp://ftp.rfc-editor.org/in-notes/rfc2817.txt
  41. *
  42. * It can also be used to make direct connections to a destination, as well. If you
  43. * pass the host and port into the constructor, no proxy will be used. You can also
  44. * call connect, passing in the host and the port, and if you didn't set the proxy
  45. * info, a direct connection will be made. A proxy is only used after you have called
  46. * the setProxyInfo function.
  47. *
  48. * The connection to and negotiation with the proxy is completely hidden. All the
  49. * same events are thrown whether you are using a proxy or not, and the data you
  50. * receive from the target server will look exact as it would if you were connected
  51. * to it directly rather than through a proxy.
  52. *
  53. * @author Christian Cantrell
  54. *
  55. **/
  56. public class RFC2817Socket
  57. extends Socket
  58. {
  59. private var proxyHost:String = null;
  60. private var host:String = null;
  61. private var proxyPort:int = 0;
  62. private var port:int = 0;
  63. private var deferredEventHandlers:Object = new Object();
  64. private var buffer:String = new String();
  65. /**
  66. * Construct a new RFC2817Socket object. If you pass in the host and the port,
  67. * no proxy will be used. If you want to use a proxy, instantiate with no
  68. * arguments, call setProxyInfo, then call connect.
  69. **/
  70. public function RFC2817Socket(host:String = null, port:int = 0)
  71. {
  72. if (host != null && port != 0)
  73. {
  74. super(host, port);
  75. }
  76. }
  77. /**
  78. * Set the proxy host and port number. Your connection will only proxied if
  79. * this function has been called.
  80. **/
  81. public function setProxyInfo(host:String, port:int):void
  82. {
  83. this.proxyHost = host;
  84. this.proxyPort = port;
  85. var deferredSocketDataHandler:Object = this.deferredEventHandlers[ProgressEvent.SOCKET_DATA];
  86. var deferredConnectHandler:Object = this.deferredEventHandlers[Event.CONNECT];
  87. if (deferredSocketDataHandler != null)
  88. {
  89. super.removeEventListener(ProgressEvent.SOCKET_DATA, deferredSocketDataHandler.listener, deferredSocketDataHandler.useCapture);
  90. }
  91. if (deferredConnectHandler != null)
  92. {
  93. super.removeEventListener(Event.CONNECT, deferredConnectHandler.listener, deferredConnectHandler.useCapture);
  94. }
  95. }
  96. /**
  97. * Connect to the specified host over the specified port. If you want your
  98. * connection proxied, call the setProxyInfo function first.
  99. **/
  100. public override function connect(host:String, port:int):void
  101. {
  102. if (this.proxyHost == null)
  103. {
  104. this.redirectConnectEvent();
  105. this.redirectSocketDataEvent();
  106. super.connect(host, port);
  107. }
  108. else
  109. {
  110. this.host = host;
  111. this.port = port;
  112. super.addEventListener(Event.CONNECT, this.onConnect);
  113. super.addEventListener(ProgressEvent.SOCKET_DATA, this.onSocketData);
  114. super.connect(this.proxyHost, this.proxyPort);
  115. }
  116. }
  117. private function onConnect(event:Event):void
  118. {
  119. this.writeUTFBytes("CONNECT "+this.host+":"+this.port+" HTTP/1.1\n\n");
  120. this.flush();
  121. this.redirectConnectEvent();
  122. }
  123. private function onSocketData(event:ProgressEvent):void
  124. {
  125. while (this.bytesAvailable != 0)
  126. {
  127. this.buffer += this.readUTFBytes(1);
  128. if (this.buffer.search(/\r?\n\r?\n$/) != -1)
  129. {
  130. this.checkResponse(event);
  131. break;
  132. }
  133. }
  134. }
  135. private function checkResponse(event:ProgressEvent):void
  136. {
  137. var responseCode:String = this.buffer.substr(this.buffer.indexOf(" ")+1, 3);
  138. if (responseCode.search(/^2/) == -1)
  139. {
  140. var ioError:IOErrorEvent = new IOErrorEvent(IOErrorEvent.IO_ERROR);
  141. ioError.text = "Error connecting to the proxy ["+this.proxyHost+"] on port ["+this.proxyPort+"]: " + this.buffer;
  142. this.dispatchEvent(ioError);
  143. }
  144. else
  145. {
  146. this.redirectSocketDataEvent();
  147. this.dispatchEvent(new Event(Event.CONNECT));
  148. if (this.bytesAvailable > 0)
  149. {
  150. this.dispatchEvent(event);
  151. }
  152. }
  153. this.buffer = null;
  154. }
  155. private function redirectConnectEvent():void
  156. {
  157. super.removeEventListener(Event.CONNECT, onConnect);
  158. var deferredEventHandler:Object = this.deferredEventHandlers[Event.CONNECT];
  159. if (deferredEventHandler != null)
  160. {
  161. super.addEventListener(Event.CONNECT, deferredEventHandler.listener, deferredEventHandler.useCapture, deferredEventHandler.priority, deferredEventHandler.useWeakReference);
  162. }
  163. }
  164. private function redirectSocketDataEvent():void
  165. {
  166. super.removeEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
  167. var deferredEventHandler:Object = this.deferredEventHandlers[ProgressEvent.SOCKET_DATA];
  168. if (deferredEventHandler != null)
  169. {
  170. super.addEventListener(ProgressEvent.SOCKET_DATA, deferredEventHandler.listener, deferredEventHandler.useCapture, deferredEventHandler.priority, deferredEventHandler.useWeakReference);
  171. }
  172. }
  173. public override function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int=0.0, useWeakReference:Boolean=false):void
  174. {
  175. if (type == Event.CONNECT || type == ProgressEvent.SOCKET_DATA)
  176. {
  177. this.deferredEventHandlers[type] = {listener:listener,useCapture:useCapture, priority:priority, useWeakReference:useWeakReference};
  178. }
  179. else
  180. {
  181. super.addEventListener(type, listener, useCapture, priority, useWeakReference);
  182. }
  183. }
  184. }
  185. }