1 | /*
|
---|
2 | Adobe Systems Incorporated(r) Source Code License Agreement
|
---|
3 | Copyright(c) 2005 Adobe Systems Incorporated. All rights reserved.
|
---|
4 |
|
---|
5 | Please read this Source Code License Agreement carefully before using
|
---|
6 | the source code.
|
---|
7 |
|
---|
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.
|
---|
13 |
|
---|
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.
|
---|
16 |
|
---|
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
|
---|
20 | code.
|
---|
21 |
|
---|
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.
|
---|
34 | */
|
---|
35 | package com.adobe.net.proxies
|
---|
36 | {
|
---|
37 | import flash.events.Event;
|
---|
38 | import flash.events.IOErrorEvent;
|
---|
39 | import flash.events.ProgressEvent;
|
---|
40 | import flash.net.Socket;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * This class allows TCP socket connections through HTTP proxies in accordance with
|
---|
44 | * RFC 2817:
|
---|
45 | *
|
---|
46 | * ftp://ftp.rfc-editor.org/in-notes/rfc2817.txt
|
---|
47 | *
|
---|
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.
|
---|
53 | *
|
---|
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.
|
---|
58 | *
|
---|
59 | * @author Christian Cantrell
|
---|
60 | *
|
---|
61 | **/
|
---|
62 | public class RFC2817Socket
|
---|
63 | extends Socket
|
---|
64 | {
|
---|
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();
|
---|
71 |
|
---|
72 | /**
|
---|
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.
|
---|
76 | **/
|
---|
77 | public function RFC2817Socket(host:String = null, port:int = 0)
|
---|
78 | {
|
---|
79 | if (host != null && port != 0)
|
---|
80 | {
|
---|
81 | super(host, port);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Set the proxy host and port number. Your connection will only proxied if
|
---|
87 | * this function has been called.
|
---|
88 | **/
|
---|
89 | public function setProxyInfo(host:String, port:int):void
|
---|
90 | {
|
---|
91 | this.proxyHost = host;
|
---|
92 | this.proxyPort = port;
|
---|
93 |
|
---|
94 | var deferredSocketDataHandler:Object = this.deferredEventHandlers[ProgressEvent.SOCKET_DATA];
|
---|
95 | var deferredConnectHandler:Object = this.deferredEventHandlers[Event.CONNECT];
|
---|
96 |
|
---|
97 | if (deferredSocketDataHandler != null)
|
---|
98 | {
|
---|
99 | super.removeEventListener(ProgressEvent.SOCKET_DATA, deferredSocketDataHandler.listener, deferredSocketDataHandler.useCapture);
|
---|
100 | }
|
---|
101 |
|
---|
102 | if (deferredConnectHandler != null)
|
---|
103 | {
|
---|
104 | super.removeEventListener(Event.CONNECT, deferredConnectHandler.listener, deferredConnectHandler.useCapture);
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Connect to the specified host over the specified port. If you want your
|
---|
110 | * connection proxied, call the setProxyInfo function first.
|
---|
111 | **/
|
---|
112 | public override function connect(host:String, port:int):void
|
---|
113 | {
|
---|
114 | if (this.proxyHost == null)
|
---|
115 | {
|
---|
116 | this.redirectConnectEvent();
|
---|
117 | this.redirectSocketDataEvent();
|
---|
118 | super.connect(host, port);
|
---|
119 | }
|
---|
120 | else
|
---|
121 | {
|
---|
122 | this.host = host;
|
---|
123 | this.port = port;
|
---|
124 | super.addEventListener(Event.CONNECT, this.onConnect);
|
---|
125 | super.addEventListener(ProgressEvent.SOCKET_DATA, this.onSocketData);
|
---|
126 | super.connect(this.proxyHost, this.proxyPort);
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | private function onConnect(event:Event):void
|
---|
131 | {
|
---|
132 | this.writeUTFBytes("CONNECT "+this.host+":"+this.port+" HTTP/1.1\n\n");
|
---|
133 | this.flush();
|
---|
134 | this.redirectConnectEvent();
|
---|
135 | }
|
---|
136 |
|
---|
137 | private function onSocketData(event:ProgressEvent):void
|
---|
138 | {
|
---|
139 | while (this.bytesAvailable != 0)
|
---|
140 | {
|
---|
141 | this.buffer += this.readUTFBytes(1);
|
---|
142 | if (this.buffer.search(/\r?\n\r?\n$/) != -1)
|
---|
143 | {
|
---|
144 | this.checkResponse(event);
|
---|
145 | break;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | private function checkResponse(event:ProgressEvent):void
|
---|
151 | {
|
---|
152 | var responseCode:String = this.buffer.substr(this.buffer.indexOf(" ")+1, 3);
|
---|
153 |
|
---|
154 | if (responseCode.search(/^2/) == -1)
|
---|
155 | {
|
---|
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);
|
---|
159 | }
|
---|
160 | else
|
---|
161 | {
|
---|
162 | this.redirectSocketDataEvent();
|
---|
163 | this.dispatchEvent(new Event(Event.CONNECT));
|
---|
164 | if (this.bytesAvailable > 0)
|
---|
165 | {
|
---|
166 | this.dispatchEvent(event);
|
---|
167 | }
|
---|
168 | }
|
---|
169 | this.buffer = null;
|
---|
170 | }
|
---|
171 |
|
---|
172 | private function redirectConnectEvent():void
|
---|
173 | {
|
---|
174 | super.removeEventListener(Event.CONNECT, onConnect);
|
---|
175 | var deferredEventHandler:Object = this.deferredEventHandlers[Event.CONNECT];
|
---|
176 | if (deferredEventHandler != null)
|
---|
177 | {
|
---|
178 | super.addEventListener(Event.CONNECT, deferredEventHandler.listener, deferredEventHandler.useCapture, deferredEventHandler.priority, deferredEventHandler.useWeakReference);
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | private function redirectSocketDataEvent():void
|
---|
183 | {
|
---|
184 | super.removeEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
|
---|
185 | var deferredEventHandler:Object = this.deferredEventHandlers[ProgressEvent.SOCKET_DATA];
|
---|
186 | if (deferredEventHandler != null)
|
---|
187 | {
|
---|
188 | super.addEventListener(ProgressEvent.SOCKET_DATA, deferredEventHandler.listener, deferredEventHandler.useCapture, deferredEventHandler.priority, deferredEventHandler.useWeakReference);
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | public override function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int=0.0, useWeakReference:Boolean=false):void
|
---|
193 | {
|
---|
194 | if (type == Event.CONNECT || type == ProgressEvent.SOCKET_DATA)
|
---|
195 | {
|
---|
196 | this.deferredEventHandlers[type] = {listener:listener,useCapture:useCapture, priority:priority, useWeakReference:useWeakReference};
|
---|
197 | }
|
---|
198 | else
|
---|
199 | {
|
---|
200 | super.addEventListener(type, listener, useCapture, priority, useWeakReference);
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|