Forum Discussion

philotas's avatar
philotas
Roku Guru
9 years ago

Escape (UrlEncode) within render thread without using roUrlTransfer?

I need to use the Escape function of roUrlTransfer to create a working URL.
However since the Escape function is tied to  roUrlTransfer I cannot create an object on the SG render thread.
And it is overkill to have to create a roUrlTransfer anyways .. especially in another thread just to manipulate a string.

So how can I use the Escape function? Shouldn't that function be a global function instead of belonging to ifUrlTransfer?

9 Replies

  • Hahaha, that's a good one!
    You can't - sorry. 

    As an excercise of ridiculousness, maybe create a persistent "UrlEncoder" Task thread, with "input" and "output" field, then put observer on the output and shove the URL in the input, wait for a call back... that'd be a good Karnapidasana
  • RokuKC's avatar
    RokuKC
    Roku Employee
    "philotas" wrote:
    I need to use the Escape function of roUrlTransfer to create a working URL.
    However since the Escape function is tied to  roUrlTransfer I cannot create an object on the SG render thread.
    And it is overkill to have to create a roUrlTransfer anyways .. especially in another thread just to manipulate a string.

    So how can I use the Escape function? Shouldn't that function be a global function instead of belonging to ifUrlTransfer?


    Thanks for raising the issue. I've filed a feature request for this.
  • "EnTerr" wrote:
    Hahaha, that's a good one!
    You can't - sorry. 

    As an excercise of ridiculousness, maybe create a persistent "UrlEncoder" Task thread, with "input" and "output" field, then put observer on the output and shove the URL in the input, wait for a call back... that'd be a good Karnapidasana

    haha, good one too! Sometimes feeling the same!
  • "philotas" wrote:
    haha, good one too! Sometimes feeling the same!

    Sadly, i could not come with a short and elegant workaround - or i would have suggested it. The fact that the (un)usually resourceful RokuKC and theEndless didn't either, also speaks.

    You could write your own function that goes over the string character by character - and %-escape the ones that are not "unreserved". A roRegEx might come handy to check against, something like "[\w\d\-_.~]". And not to forget UTF8-escaping the ones with ASC() > 255 ... so nevermind, probably better to do roByteArray.FromAsciiString() and then toHexString() everything and insert % before every two chars... a rather bad way.
  • Here, i whipped a band-aid:
    function url_encode(s):
    unreserved = createObject("roRegex", "[\w\d\-_.]", "")
    ba = createObject("roByteArray")
    res = ""
    for each ch in s.split(""):
    if unreserved.isMatch(ch):
    res += ch
    else:
    ba.fromAsciiString(ch): hex = ba.toHexString()
    for i = 1 to len(hex) step 2: res += "%" + mid(hex,i,2): next
    end if
    next
    return res
    end function
  • TheEndless's avatar
    TheEndless
    Channel Surfer
    I'm curious about the use-case.  What is the situation in which you need to URL encode a value in a separate thread from the actual web request?
  • "TheEndless" wrote:
    I'm curious about the use-case.  What is the situation in which you need to URL encode a value in a separate thread from the actual web request?

    Umm... i can easily see the need if using one of the media playback noids - "Audio" and "Video" (AKA the Roku bread and butter 8-) )?  

    Say the media server for whatever reasons (security, analytics, accounting) requires you to pass it some varying magic either as a query or a cookie.or a header - and needing that url-encoded. Such needs will incur in the render thread, would it not?
  • "TheEndless" wrote:
    I'm curious about the use-case.  What is the situation in which you need to URL encode a value in a separate thread from the actual web request?

    I load data from a server in order to show images and text in a long row list.
    Until the server is updated to deliver working URLs I need to construct the URL for the Poster component in the sg thread.
    I could probably do the when loading the data, but then most of theses genereted URLs might never be used so it is not a good idea to prepare them "just in case".