This is an automated email from the ASF dual-hosted git repository.

gregdove pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-asjs.git


The following commit(s) were added to refs/heads/develop by this push:
     new 5e48fef  Community request - adding CORS support to URLStream, similar 
to URLLoader.
     new 8131b98  Merge branch 'develop' of 
https://github.com/apache/royale-asjs into develop
5e48fef is described below

commit 5e48feffd57623201f733810a577946efd2cf869
Author: greg-dove <[email protected]>
AuthorDate: Fri Feb 11 16:27:48 2022 +1300

    Community request - adding CORS support to URLStream, similar to URLLoader.
---
 .../Network/src/main/resources/basic-manifest.xml  |   1 +
 .../main/royale/org/apache/royale/net/URLStream.as |  22 ++++
 .../royale/net/beads/GlobalCORSCredentialsBead.as  | 129 +++++++++++++++++++++
 3 files changed, 152 insertions(+)

diff --git a/frameworks/projects/Network/src/main/resources/basic-manifest.xml 
b/frameworks/projects/Network/src/main/resources/basic-manifest.xml
index 5218ee1..ffa702c 100644
--- a/frameworks/projects/Network/src/main/resources/basic-manifest.xml
+++ b/frameworks/projects/Network/src/main/resources/basic-manifest.xml
@@ -28,6 +28,7 @@
     <component id="BinaryUploader" 
class="org.apache.royale.net.BinaryUploader"/>
     <component id="CORSCredentialsBead" 
class="org.apache.royale.net.beads.CORSCredentialsBead"/>
     <component id="URLLoaderCORSCredentialsBead" 
class="org.apache.royale.net.beads.URLLoaderCORSCredentialsBead"/>
+    <component id="GlobalCORSCredentialsBead" 
class="org.apache.royale.net.beads.GlobalCORSCredentialsBead"/>
     <component id="FileProxy" class="org.apache.royale.file.FileProxy"/>
     <component id="FileBrowser" 
class="org.apache.royale.file.beads.FileBrowser"/>
     <component id="FileBrowserWithFilter" 
class="org.apache.royale.file.beads.FileBrowserWithFilter"/>
diff --git 
a/frameworks/projects/Network/src/main/royale/org/apache/royale/net/URLStream.as
 
b/frameworks/projects/Network/src/main/royale/org/apache/royale/net/URLStream.as
index d72bf64..c6485d3 100644
--- 
a/frameworks/projects/Network/src/main/royale/org/apache/royale/net/URLStream.as
+++ 
b/frameworks/projects/Network/src/main/royale/org/apache/royale/net/URLStream.as
@@ -52,6 +52,25 @@ package org.apache.royale.net
                {
                        protected var xhr:XMLHttpRequest;
                }
+
+               COMPILE::JS
+               private static var _corsCredentialsChecker:Function;
+               COMPILE::JS
+               /**
+                * Intended as global configuration of CORS withCredentials 
setting on requests
+                * This method is not reflectable, is js-only and is eliminated 
via dead-code-elimination
+                * in js-release builds if it is never used.
+                * URLStream is used a service base in other service classes, 
so this provides
+                * a 'low level' solution for a bead that can work at 
application level.
+                * The 'checker' function parameter should be a function that 
takes a url as its single argument
+                * and returns true or false depending on whether 
'withCredentials' should be set for
+                * that http request. Set it to null to always be false.
+                * @private
+                * @royalesuppressexport
+                */
+               public static function 
setCORSCredentialsChecker(checker:Function):void{
+                       _corsCredentialsChecker = checker;
+               }
                        
                COMPILE::SWF
                {
@@ -117,6 +136,9 @@ package org.apache.royale.net
                        COMPILE::JS {
                                requestStatus = 0;
                                createXmlHttpRequest();
+                               if (_corsCredentialsChecker != null) {
+                                       xhr.withCredentials = 
_corsCredentialsChecker( urlRequest.url);
+                               }
 
                                xhr.open(urlRequest.method, urlRequest.url);
                                xhr.responseType = "arraybuffer";
diff --git 
a/frameworks/projects/Network/src/main/royale/org/apache/royale/net/beads/GlobalCORSCredentialsBead.as
 
b/frameworks/projects/Network/src/main/royale/org/apache/royale/net/beads/GlobalCORSCredentialsBead.as
new file mode 100644
index 0000000..e9c9382
--- /dev/null
+++ 
b/frameworks/projects/Network/src/main/royale/org/apache/royale/net/beads/GlobalCORSCredentialsBead.as
@@ -0,0 +1,129 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  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.royale.net.beads {
+    import org.apache.royale.core.IBead;
+    import org.apache.royale.core.IStrand;
+    import org.apache.royale.net.URLLoader;
+    import org.apache.royale.net.URLStream;
+    
+
+    COMPILE::SWF
+    public class GlobalCORSCredentialsBead implements IBead{
+        public function GlobalCORSCredentialsBead(filter:* = null) {
+            trace("Only needed for JavaScript HTTP Server calls");
+        }
+    
+        public function set strand(value:IStrand):void {
+            //doing nothing with strand
+        }
+    }
+
+    /**
+     *  [JS] Bead to allow passing on user authentication information in a 
XMLHttpRequest request.
+     *  Affects all http support classes that use 
org.apache.royale.net.URLLoader or org.apache.royale.net.URLStream
+     *
+     *  If you don't use this bead any cross domain calls that require user 
authentication
+     *  (via say basic authentication or cookies) will fail.
+     *
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @playerversion AIR 2.6
+     *  @productversion Royale 0.9
+     */
+    COMPILE::JS
+    public class GlobalCORSCredentialsBead implements IBead {
+    
+        private static const alwaysTrue:Function = 
function(url:String):Boolean{return true};
+        private static function getRegexpChecker(regexp:RegExp):Function {
+            return function(url:String):Boolean{
+                return regexp.test(url);
+            }
+        }
+        
+        
+        
+        public function GlobalCORSCredentialsBead(filter:* = null) {
+            if (filter) {
+                withCredentialsFilter = filter;
+            }
+        }
+
+
+        /**
+         *  @copy org.apache.royale.core.IBead#strand
+         *
+         *  @langversion 3.0
+         *  @playerversion Flash 10.2
+         *  @playerversion AIR 2.6
+         *  @productversion Royale 0.8
+         */
+        public function set strand(value:IStrand):void {
+            //doing nothing with strand
+        }
+
+        
+        
+        private var _withCredentialsFilter:* ;
+
+        /**
+         *  configure setting for URLLoader requests
+         *  Can be set to true or false
+         *  Or a Regexp that tests for what should be true in a url string
+         *  Or a String value this is 'always' or 'never'
+         *  Or a String that is used to construct a Regexp
+         *  Or a Function that does its own checking
+         *
+         *  @langversion 3.0
+         *  @playerversion Flash 10.2
+         *  @playerversion AIR 2.6
+         *  @productversion Royale 0.8
+         */
+        public function get withCredentialsFilter():* {
+            return _withCredentialsFilter;
+        }
+
+        public function set withCredentialsFilter(filter:*):void {
+            if (!filter) {
+                _withCredentialsFilter = null;
+            } else {
+                if (typeof filter == 'boolean' || filter == 'always' || filter 
== 'never') { //it must be true because false was already eliminated
+                    _withCredentialsFilter = filter != 'never' ? alwaysTrue : 
null;
+                } else {
+                    if (filter is String) {
+                        //pre-process to Regexp
+                        filter = new RegExp(String(filter));
+                    }
+                    if (filter is RegExp) {
+                        _withCredentialsFilter = getRegexpChecker(filter as 
RegExp);
+                    } else {
+                        if (filter is Function) {
+                            _withCredentialsFilter = filter as Function;
+                        } else  {
+            
+                            throw new Error('Unsupported argument for 
withCredentialsFilter');
+                        }
+                    }
+                }
+            }
+            URLLoader.setCORSCredentialsChecker(_withCredentialsFilter);
+            URLStream.setCORSCredentialsChecker(_withCredentialsFilter);
+        }
+    }
+}

Reply via email to