Repository: flex-asjs Updated Branches: refs/heads/develop 946589ef2 -> cc22300be
Refactored classes Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/cc22300b Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/cc22300b Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/cc22300b Branch: refs/heads/develop Commit: cc22300beb1ecc44b6a858539105431016a9c3c4 Parents: 946589e Author: Harbs <[email protected]> Authored: Tue Jul 5 12:38:19 2016 +0300 Committer: Harbs <[email protected]> Committed: Tue Jul 5 12:38:19 2016 +0300 ---------------------------------------------------------------------- .../Network/src/main/flex/NetworkClasses.as | 2 +- .../flex/org/apache/flex/net/BinaryUploader.as | 6 +- .../flex/org/apache/flex/net/HTTPService.as | 7 +- .../flex/org/apache/flex/net/URLBinaryLoader.as | 116 ++++++++++++++ .../main/flex/org/apache/flex/net/URLLoader.as | 90 +++++++++++ .../main/flex/org/apache/flex/net/URLRequest.as | 48 ++++++ .../main/flex/org/apache/flex/net/URLStream.as | 154 ++++++++++++++++++ .../org/apache/flex/net/url/BinaryURLLoader.as | 83 ---------- .../flex/org/apache/flex/net/url/URLRequest.as | 48 ------ .../flex/org/apache/flex/net/url/URLStream.as | 156 ------------------- 10 files changed, 415 insertions(+), 295 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/cc22300b/frameworks/projects/Network/src/main/flex/NetworkClasses.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Network/src/main/flex/NetworkClasses.as b/frameworks/projects/Network/src/main/flex/NetworkClasses.as index 3477a84..e201410 100644 --- a/frameworks/projects/Network/src/main/flex/NetworkClasses.as +++ b/frameworks/projects/Network/src/main/flex/NetworkClasses.as @@ -28,7 +28,7 @@ package */ internal class NetworkClasses { - import org.apache.flex.net.url.BinaryURLLoader; BinaryURLLoader; + import org.apache.flex.net.URLBinaryLoader; URLBinaryLoader; } } http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/cc22300b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/BinaryUploader.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/BinaryUploader.as b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/BinaryUploader.as index 0a9a58f..3ca3950 100644 --- a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/BinaryUploader.as +++ b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/BinaryUploader.as @@ -487,7 +487,7 @@ package org.apache.flex.net } COMPILE::SWF - private var urlLoader:URLLoader; + private var urlLoader:flash.net.URLLoader; /** * Starts the upload to the server. Events @@ -503,8 +503,8 @@ package org.apache.flex.net COMPILE::SWF { if (!urlLoader) - urlLoader = new URLLoader(); - var request:URLRequest = new URLRequest(url); + urlLoader = new flash.net.URLLoader(); + var request:flash.net.URLRequest = new flash.net.URLRequest(url); request.method = method; if ("idleTimeout" in request) { http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/cc22300b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/HTTPService.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/HTTPService.as b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/HTTPService.as index e46ee14..d4a5d0d 100644 --- a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/HTTPService.as +++ b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/HTTPService.as @@ -23,7 +23,6 @@ package org.apache.flex.net import flash.events.HTTPStatusEvent; import flash.events.IOErrorEvent; import flash.net.URLLoader; - import flash.net.URLRequest; import flash.net.URLRequestHeader; } COMPILE::JS @@ -495,7 +494,7 @@ package org.apache.flex.net } COMPILE::SWF - private var urlLoader:URLLoader; + private var urlLoader:flash.net.URLLoader; /** * Sends the headers and contentData to the server. @@ -517,8 +516,8 @@ package org.apache.flex.net COMPILE::SWF { if (!urlLoader) - urlLoader = new URLLoader(); - var request:URLRequest = new URLRequest(url); + urlLoader = new flash.net.URLLoader(); + var request:flash.net.URLRequest = new flash.net.URLRequest(url); request.method = method; if ("idleTimeout" in request) { http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/cc22300b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLBinaryLoader.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLBinaryLoader.as b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLBinaryLoader.as new file mode 100644 index 0000000..2dfc490 --- /dev/null +++ b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLBinaryLoader.as @@ -0,0 +1,116 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////////// +package org.apache.flex.net +{ + + import org.apache.flex.events.Event; + import org.apache.flex.events.EventDispatcher; + import org.apache.flex.events.ProgressEvent; + import org.apache.flex.utils.BinaryData; + + + /** + * The BinaryUploader class is a relatively low-level class designed to get + * binary data over HTTP the intent is to create similar classes for text and URL vars. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.7.0 + */ + public class URLBinaryLoader extends URLLoader + { + + /** + * The binary result of the request. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.7.0 + */ + public var data:BinaryData; + + private var stream:URLStream; + + /** + * The number of bytes loaded so far. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.7.0 + */ + public var bytesLoaded:uint = 0; + + /** + * The total number of bytes (if avaailable). + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.7.0 + */ + public var bytesTotal:uint = 0; + + public function URLBinaryLoader(request:URLRequest = null) + { + super(); + stream = new URLStream(); + stream.addEventListener(HTTPConstants.COMPLETE, onComplete); + } + + public function load(request:URLRequest):void + { + stream.load(request); + } + + public function close():void + { + stream.close(); + } + + private function redirectEvent(event:Event):void + { + dispatchEvent(event); + } + + private function onComplete(event:Event):void + { + data = stream.response; + if (data) + { + dispatchEvent(event); + } + else + { + // TODO dipatch error event? + dispatchEvent(new Event(HTTPConstants.IO_ERROR)); + } + } + + private function onProgress(event:ProgressEvent):void + { + this.bytesLoaded = event.current + this.bytesTotal = event.total; + dispatchEvent(event); + } + } +} + http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/cc22300b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLLoader.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLLoader.as b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLLoader.as new file mode 100644 index 0000000..809e120 --- /dev/null +++ b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLLoader.as @@ -0,0 +1,90 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////////// + +package org.apache.flex.net +{ + import org.apache.flex.events.EventDispatcher; + + //-------------------------------------- + // Events + //-------------------------------------- + + /** + * Dispatched when the request is complete. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + */ + [Event(name="complete", type="org.apache.flex.events.Event")] + + /** + * Dispatched if an error occurs in the server communication. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + */ + [Event(name="ioError", type="org.apache.flex.events.Event")] + + /** + * Dispatched when an httpStatus code is received from the server. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + */ + [Event(name="httpStatus", type="org.apache.flex.events.Event")] + + /** + * Dispatched if Adobe AIR is able to detect and return the status + * code for the request. Unlike the httpStatus event, the httpResponseStatus + * event is delivered before any response data. Also, the httpResponseStatus + * event includes values for the responseHeaders and responseURL properties + * (which are undefined for an httpStatus event. Note that the + * httpResponseStatus event (if any) will be sent before + * (and in addition to) any complete or error event. + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.0 + */ + [Event(name="httpResponseStatus", type="org.apache.flex.events.Event")] + + /** + * The URLLoader class is a base class for the specific flavors of loaders such as binary, + * text or variables + * + * @langversion 3.0 + * @playerversion Flash 10.2 + * @playerversion AIR 2.6 + * @productversion FlexJS 0.7.0 + */ + public class URLLoader extends EventDispatcher + { + public function URLLoader() + { + throw new Error("URLLoader should not be instantiated. Use a derived class instead.") + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/cc22300b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLRequest.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLRequest.as b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLRequest.as new file mode 100644 index 0000000..930ae8e --- /dev/null +++ b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLRequest.as @@ -0,0 +1,48 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////////// +package org.apache.flex.net +{ + public final class URLRequest extends Object + { + public var url:String; + public var data:Object; + public var contentType:String = "application/x-www-form-urlencoded"; + + public function URLRequest(url:String = null) + { + super(); + if(url != null) + { + this.url = url; + } + this.requestHeaders = []; + } + + public function get requestHeaders():Array + { + return null; + } + + public function set requestHeaders(value:Array) : void + { + } + + } +} + http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/cc22300b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLStream.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLStream.as b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLStream.as new file mode 100644 index 0000000..7b5ff89 --- /dev/null +++ b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/URLStream.as @@ -0,0 +1,154 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//////////////////////////////////////////////////////////////////////////////// +package org.apache.flex.net +{ + + + + import org.apache.flex.events.Event; + import org.apache.flex.events.EventDispatcher; + import org.apache.flex.events.ProgressEvent; + import org.apache.flex.utils.BinaryData; + + COMPILE::SWF + { + import flash.net.URLRequestHeader; + import flash.events.IOErrorEvent; + import flash.net.URLVariables; + import flash.events.Event; + import flash.events.ProgressEvent; + import flash.net.URLRequest; + import flash.net.URLStream; + import flash.utils.ByteArray; + } + + + + public class URLStream extends EventDispatcher + { + COMPILE::JS + { + private var xhr:XMLHttpRequest; + } + + COMPILE::SWF + { + private var flashUrlStream:flash.net.URLStream + } + + public function URLStream() + { + super(); + } + + public function get response():BinaryData + { + COMPILE::JS + { + return new BinaryData(xhr.response as ArrayBuffer); + } + COMPILE::SWF + { + var ba:ByteArray = new ByteArray(); + flashUrlStream.readBytes(ba); + return new BinaryData(ba); + } + } + + public function load(urlRequest:org.apache.flex.net.URLRequest):void + { + COMPILE::JS { + xhr = new XMLHttpRequest(); + xhr.open("POST", urlRequest.url); + xhr.responseType = "arraybuffer"; + xhr.addEventListener("readystatechange", xhr_onreadystatechange,false); + xhr.addEventListener("progress", xhr_progress, false); + xhr.setRequestHeader("Content-type", urlRequest.contentType); + xhr.send(HTTPUtils.encodeUrlVariables(urlRequest.data)); + } + COMPILE::SWF + { + flashUrlStream = new flash.net.URLStream(); + var req:flash.net.URLRequest = new flash.net.URLRequest(urlRequest.url); + var hdr:URLRequestHeader = new URLRequestHeader("Content-type", urlRequest.contentType); + req.requestHeaders.push(hdr); + req.data = new flash.net.URLVariables(HTTPUtils.encodeUrlVariables(urlRequest.data)); + req.method = HTTPConstants.POST; + flashUrlStream.addEventListener(flash.events.ProgressEvent.PROGRESS, flash_progress); + flashUrlStream.addEventListener(flash.events.Event.COMPLETE, flash_complete); + flashUrlStream.addEventListener(IOErrorEvent.IO_ERROR, flash_onIoError); + flashUrlStream.load(req); + } + } + + COMPILE::SWF + protected function flash_onIoError(event:IOErrorEvent):void + { + trace("io error: " + event.text); + } + + COMPILE::SWF + protected function flash_complete(event:flash.events.Event):void + { + dispatchEvent(new org.apache.flex.events.Event(HTTPConstants.COMPLETE)); + } + COMPILE::SWF + protected function flash_progress(event:flash.events.ProgressEvent):void + { + var progressEvent:org.apache.flex.events.ProgressEvent = new org.apache.flex.events.ProgressEvent(org.apache.flex.events.ProgressEvent.PROGRESS); + progressEvent.current = event.bytesLoaded; + progressEvent.total = event.bytesTotal; + dispatchEvent(progressEvent); + } + + + private function xhr_progress(e:Object):void + { + dispatchEvent(new org.apache.flex.events.ProgressEvent(org.apache.flex.events.ProgressEvent.PROGRESS, false, false, e.loaded, e.total)); + } + + COMPILE::JS + private function xhr_onreadystatechange(e:*):void + { + if (xhr.readyState == 4 && xhr.status == 200) + { + dispatchEvent(new org.apache.flex.events.Event(HTTPConstants.COMPLETE)); + }else if (xhr.readyState==4&&xhr.status==404){ + // dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR)); + } + } + + public function close():void + { + COMPILE::SWF + { + flashUrlStream.close(); + } + COMPILE::JS + { + xhr.abort(); + } + + //TODO send an event that it's been aborted + } + public var onComplete:Function; + public var onError:Function; + } +} + http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/cc22300b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/url/BinaryURLLoader.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/url/BinaryURLLoader.as b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/url/BinaryURLLoader.as deleted file mode 100644 index c98645d..0000000 --- a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/url/BinaryURLLoader.as +++ /dev/null @@ -1,83 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// -// Licensed to the Apache Software Foundation (ASF) under one or more -// contributor license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright ownership. -// The ASF licenses this file to You under the Apache License, Version 2.0 -// (the "License"); you may not use this file except in compliance with -// the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -//////////////////////////////////////////////////////////////////////////////// -package org.apache.flex.net.url -{ - - import org.apache.flex.events.Event; - import org.apache.flex.events.EventDispatcher; - import org.apache.flex.events.ProgressEvent; - import org.apache.flex.net.HTTPConstants; - import org.apache.flex.utils.BinaryData; - - public class BinaryURLLoader extends EventDispatcher - { - - public var data:BinaryData; - - private var stream:URLStream; - - public var bytesLoaded:uint = 0; - - public var bytesTotal:uint = 0; - - public function BinaryURLLoader(request:URLRequest = null) - { - super(); - stream = new URLStream(); - stream.addEventListener(HTTPConstants.COMPLETE, onComplete); - } - - public function load(request:URLRequest):void - { - stream.load(request); - } - - public function close():void - { - stream.close(); - } - - private function redirectEvent(event:Event):void - { - dispatchEvent(event); - } - - private function onComplete(event:Event):void - { - data = stream.response; - if (data) - { - dispatchEvent(event); - } - else - { - // TODO dipatch error event? - dispatchEvent(new Event(HTTPConstants.IO_ERROR)); - } - } - - private function onProgress(event:ProgressEvent):void - { - this.bytesLoaded = event.current - this.bytesTotal = event.total; - dispatchEvent(event); - } - } -} - http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/cc22300b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/url/URLRequest.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/url/URLRequest.as b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/url/URLRequest.as deleted file mode 100644 index b4596e0..0000000 --- a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/url/URLRequest.as +++ /dev/null @@ -1,48 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// -// Licensed to the Apache Software Foundation (ASF) under one or more -// contributor license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright ownership. -// The ASF licenses this file to You under the Apache License, Version 2.0 -// (the "License"); you may not use this file except in compliance with -// the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -//////////////////////////////////////////////////////////////////////////////// -package org.apache.flex.net.url -{ - public final class URLRequest extends Object - { - public var url:String; - public var data:Object; - public var contentType:String = "application/x-www-form-urlencoded"; - - public function URLRequest(url:String = null) - { - super(); - if(url != null) - { - this.url = url; - } - this.requestHeaders = []; - } - - public function get requestHeaders():Array - { - return null; - } - - public function set requestHeaders(value:Array) : void - { - } - - } -} - http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/cc22300b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/url/URLStream.as ---------------------------------------------------------------------- diff --git a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/url/URLStream.as b/frameworks/projects/Network/src/main/flex/org/apache/flex/net/url/URLStream.as deleted file mode 100644 index 9e69b47..0000000 --- a/frameworks/projects/Network/src/main/flex/org/apache/flex/net/url/URLStream.as +++ /dev/null @@ -1,156 +0,0 @@ -//////////////////////////////////////////////////////////////////////////////// -// -// Licensed to the Apache Software Foundation (ASF) under one or more -// contributor license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright ownership. -// The ASF licenses this file to You under the Apache License, Version 2.0 -// (the "License"); you may not use this file except in compliance with -// the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -//////////////////////////////////////////////////////////////////////////////// -package org.apache.flex.net.url -{ - - - - import org.apache.flex.events.Event; - import org.apache.flex.events.EventDispatcher; - import org.apache.flex.events.ProgressEvent; - import org.apache.flex.net.HTTPConstants; - import org.apache.flex.net.HTTPUtils; - import org.apache.flex.utils.BinaryData; - - COMPILE::SWF - { - import flash.net.URLRequestHeader; - import flash.events.IOErrorEvent; - import flash.net.URLVariables; - import flash.events.Event; - import flash.events.ProgressEvent; - import flash.net.URLRequest; - import flash.net.URLStream; - import flash.utils.ByteArray; - } - - - - public class URLStream extends EventDispatcher - { - COMPILE::JS - { - private var xhr:XMLHttpRequest; - } - - COMPILE::SWF - { - private var flashUrlStream:flash.net.URLStream - } - - public function URLStream() - { - super(); - } - - public function get response():BinaryData - { - COMPILE::JS - { - return new BinaryData(xhr.response as ArrayBuffer); - } - COMPILE::SWF - { - var ba:ByteArray = new ByteArray(); - flashUrlStream.readBytes(ba); - return new BinaryData(ba); - } - } - - public function load(urlRequest:org.apache.flex.net.url.URLRequest):void - { - COMPILE::JS { - xhr = new XMLHttpRequest(); - xhr.open("POST", urlRequest.url); - xhr.responseType = "arraybuffer"; - xhr.addEventListener("readystatechange", xhr_onreadystatechange,false); - xhr.addEventListener("progress", xhr_progress, false); - xhr.setRequestHeader("Content-type", urlRequest.contentType); - xhr.send(HTTPUtils.encodeUrlVariables(urlRequest.data)); - } - COMPILE::SWF - { - flashUrlStream = new flash.net.URLStream(); - var req:flash.net.URLRequest = new flash.net.URLRequest(urlRequest.url); - var hdr:URLRequestHeader = new URLRequestHeader("Content-type", urlRequest.contentType); - req.requestHeaders.push(hdr); - req.data = new flash.net.URLVariables(HTTPUtils.encodeUrlVariables(urlRequest.data)); - req.method = HTTPConstants.POST; - flashUrlStream.addEventListener(flash.events.ProgressEvent.PROGRESS, flash_progress); - flashUrlStream.addEventListener(flash.events.Event.COMPLETE, flash_complete); - flashUrlStream.addEventListener(IOErrorEvent.IO_ERROR, flash_onIoError); - flashUrlStream.load(req); - } - } - - COMPILE::SWF - protected function flash_onIoError(event:IOErrorEvent):void - { - trace("io error: " + event.text); - } - - COMPILE::SWF - protected function flash_complete(event:flash.events.Event):void - { - dispatchEvent(new org.apache.flex.events.Event(HTTPConstants.COMPLETE)); - } - COMPILE::SWF - protected function flash_progress(event:flash.events.ProgressEvent):void - { - var progressEvent:org.apache.flex.events.ProgressEvent = new org.apache.flex.events.ProgressEvent(org.apache.flex.events.ProgressEvent.PROGRESS); - progressEvent.current = event.bytesLoaded; - progressEvent.total = event.bytesTotal; - dispatchEvent(progressEvent); - } - - - private function xhr_progress(e:Object):void - { - dispatchEvent(new org.apache.flex.events.ProgressEvent(org.apache.flex.events.ProgressEvent.PROGRESS, false, false, e.loaded, e.total)); - } - - COMPILE::JS - private function xhr_onreadystatechange(e:*):void - { - if (xhr.readyState == 4 && xhr.status == 200) - { - dispatchEvent(new org.apache.flex.events.Event(HTTPConstants.COMPLETE)); - }else if (xhr.readyState==4&&xhr.status==404){ - // dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR)); - } - } - - public function close():void - { - COMPILE::SWF - { - flashUrlStream.close(); - } - COMPILE::JS - { - xhr.abort(); - } - - //TODO send an event that it's been aborted - } - public var onComplete:Function; - public var onError:Function; - } -} -
