Skip to content

Instantly share code, notes, and snippets.

@coderofsalvation
Last active May 17, 2024 08:02
Show Gist options
  • Save coderofsalvation/b2b111a2631fbdc8e76d6cab3bea8f17 to your computer and use it in GitHub Desktop.
Save coderofsalvation/b2b111a2631fbdc8e76d6cab3bea8f17 to your computer and use it in GitHub Desktop.

Revisions

  1. coderofsalvation revised this gist May 17, 2024. No changes.
  2. coderofsalvation revised this gist May 16, 2024. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion URI.gd
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,9 @@
    # date: 16-05-2024
    # comments: https://gist.github.com/coderofsalvation/b2b111a2631fbdc8e76d6cab3bea8f17
    #
    # Participants of the Open Web think & communicate in URI's, with this class we can do that in godot too.
    # Participants of the Open Web think & communicate in URI's: now with Godot (>=4) too!
    # This class will not win the beauty contest related to RFC 3986, but here goes:
    #
    # USAGE:
    #
    # xrf = preload("res://URI.gd").new()
  3. coderofsalvation revised this gist May 16, 2024. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions URI.gd
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ func parse(url: String) -> Dictionary:
    URI[ parts[i] ] = match.strings[i] if match.strings[i] else ""
    if URI["path"]:
    var pathParts:Array = URI["path"].split("/")
    if pathParts.size() > 1 and pathParts[0].find(".") != -1:
    if pathParts.size() > 1 and (pathParts[0].find(".") != -1 || pathParts[0].find(":") != -1):
    URI["domain"] = pathParts.pop_front()
    URI["path"] = "/".join(pathParts)
    pathParts = URI["path"].split("/")
    @@ -44,7 +44,6 @@ func parse(url: String) -> Dictionary:
    URI["query"] = parseArgs( URI["query"].substr(1) ) if URI["query"] else {}
    URI["URN"] = URI["string"].replace("\\?.*","") if URI["domain"] else ""
    URI["isLocal"] = true if !URI["domain"] else false
    return URI

    func parseArgs(fragment: String) -> Dictionary:
    var ARG = {}
  4. coderofsalvation revised this gist May 16, 2024. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions URI.gd
    Original file line number Diff line number Diff line change
    @@ -39,8 +39,10 @@ func parse(url: String) -> Dictionary:
    if pathParts[-1].find(".") != -1:
    URI["file"] = pathParts[-1]
    URI["path"] = "/".join(pathParts)
    URI["fragment"] = parseArgs( URI["hash"].substr(1) ) if URI["hash"] else ""
    URI["query"] = parseArgs( URI["query"].substr(1) ) if URI["query"] else ""
    URI["protocol"] = URI["protocol"].replace("://","") if URI["protocol"] else ""
    URI["fragment"] = parseArgs( URI["hash"].substr(1) ) if URI["hash"] else {}
    URI["query"] = parseArgs( URI["query"].substr(1) ) if URI["query"] else {}
    URI["URN"] = URI["string"].replace("\\?.*","") if URI["domain"] else ""
    URI["isLocal"] = true if !URI["domain"] else false
    return URI

  5. coderofsalvation renamed this gist May 16, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.txt → URI.gd
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    # author: Leon van Kammen (coderofsalvation)
    # SPDX-License-Identifier: MIT
    # date: 16-05-2024
    # comments: https://gist.github.com/coderofsalvation/b2b111a2631fbdc8e76d6cab3bea8f17
    #
    # Participants of the Open Web think & communicate in URI's, with this class we can do that in godot too.
    # USAGE:
  6. coderofsalvation created this gist May 16, 2024.
    55 changes: 55 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    # URI/URL parser
    #
    # author: Leon van Kammen (coderofsalvation)
    # SPDX-License-Identifier: MIT
    # date: 16-05-2024
    #
    # Participants of the Open Web think & communicate in URI's, with this class we can do that in godot too.
    # USAGE:
    #
    # xrf = preload("res://URI.gd").new()
    # print( URI.parse("https://foo.com/a/bc.gltf?bar=2#foo=2") )
    # print( URI.parse("a/bc.gltf?a=1#foo=2") )
    #
    # OUTPUT:
    # { "domain": "foo.com", "fragment": { "foo": { "string": "2", "x": <null>, "y": <null>, "color": <null>, "float": <null>, "int": <null> } }, "file": "bc.gltf", "URN": "https://foo.com/a/bc.gltf?bar=2#foo=2", "string": "https://foo.com/a/bc.gltf?bar=2#foo=2", "protocol": "https", "path": "a/bc.gltf", "query": { "bar": { "string": "2", "x": <null>, "y": <null>, "color": <null>, "float": <null>, "int": <null> } }, "hash": "#foo=2", "isLocal": false }
    # { "domain": "", "fragment": { "foo": { "string": "2", "x": <null>, "y": <null>, "color": <null>, "float": <null>, "int": <null> } }, "file": "bc.gltf", "URN": "", "string": "a/bc.gltf?a=1#foo=2", "protocol": "", "path": "a/bc.gltf", "query": { "a": { "string": "1", "x": <null>, "y": <null>, "color": <null>, "float": <null>, "int": <null> } }, "hash": "#foo=2", "isLocal": true }
    #

    class_name URI

    ####################################################################################################
    # URI Class
    ####################################################################################################
    func parse(url: String) -> Dictionary:
    var URI = {"domain":"","fragment":"","file":""}
    var parts = ["string","protocol","path","query","hash"]
    var urlregex = RegEx.new()
    urlregex.compile("(\\w+:\\/\\/)?([^#\\?]+)?(\\?[^#]+)?(#.*)?")
    var match = urlregex.search(url)
    for i in range(0,parts.size()):
    URI[ parts[i] ] = match.strings[i] if match.strings[i] else ""
    if URI["path"]:
    var pathParts:Array = URI["path"].split("/")
    if pathParts.size() > 1 and pathParts[0].find(".") != -1:
    URI["domain"] = pathParts.pop_front()
    URI["path"] = "/".join(pathParts)
    pathParts = URI["path"].split("/")
    if pathParts[-1].find(".") != -1:
    URI["file"] = pathParts[-1]
    URI["path"] = "/".join(pathParts)
    URI["fragment"] = parseArgs( URI["hash"].substr(1) ) if URI["hash"] else ""
    URI["query"] = parseArgs( URI["query"].substr(1) ) if URI["query"] else ""
    URI["isLocal"] = true if !URI["domain"] else false
    return URI

    func parseArgs(fragment: String) -> Dictionary:
    var ARG = {}
    var items = fragment.split("&")
    for item in items:
    var key_value = item.split("=")
    if key_value.size() > 1:
    ARG[key_value[0]] = guess_type(key_value[1])
    else:
    ARG[key_value[0]] = ""
    return ARG