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