RFC2817Socket.as 7.1 KB

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