DataPower

 View Only
Expand all | Collapse all

dp:decode('test%11',url)

  • 1.  dp:decode('test%11',url)

    Posted Tue February 27, 2024 05:52 AM

    Hello

    Just a quick question about this command : dp:decode('test%11',url)

    This command thrown an error because the % is not escaped  (%25 expected)

    Is there a way to catch this error and avoid to have an error rule triggered ?

    Some time client send double escapement (%25C3%25A9 first decode--> %C3A9 seconde decode --> é) but sometime, client just send %c3a9.

    So it will be nice if dp:decode can be catched in case of failure

    Any workaround ? thanks :)



    ------------------------------
    D@viiid
    ------------------------------


  • 2.  RE: dp:decode('test%11',url)

    IBM Champion
    Posted Tue February 27, 2024 10:02 AM

    Clear this up for us:

    >> Is there a way to catch this error and avoid to have an error rule triggered?

    and then:

    >> So it will be nice if dp:decode can be catched in case of failure

    In my mind, the error rule is DataPower giving you the opportunity to handle the error in case of decode failure.   In the error rule, you'll likely want to return some kind of error to the client, maybe an HTTP 400 if you want to be explicit about a bad request, or an HTTP 404 if you want to indicate the resource at the URL cannot be found, prompting the client to check the manner in which their URL is constructed.

    Do you want DataPower not to fire off the error rule if there is an error?   You can always use an on-error action with it set to continue.  Not sure what you plan to do after that, but at least the error rule won't be fired.



    ------------------------------
    Joseph Morgan
    ------------------------------



  • 3.  RE: dp:decode('test%11',url)

    Posted Tue February 27, 2024 10:26 AM

    hello

    Thanks for response :)

    In my ideal world, i want to decode and url and even if an error in generated, i don't want datapower triggered an error rule because my treatment has to be finished.

    in other words, I'm looking to know if there is some kind of try/catch for this kind of command :)



    ------------------------------
    D@viiid
    ------------------------------



  • 4.  RE: dp:decode('test%11',url)

    IBM Champion
    Posted Tue February 27, 2024 11:05 AM

    I honestly don't know ( and would be very surprised if ) DataPower supports XSLT 3, where there is a try/catch.

    As I stated before, you can use an on-error action set to continue, but that's not a try/catch.  You may need to change the XSLT to set a context variable to indicate if the URL is successfully decoded.  Then, after the XSLT containing the decode use another to handle any error.

    Can you consider changing over from XSLT to GWS, where you can handle the error, even if only for decoding the URL?   Honestly, you could consider using GWS to scan the URL for double-encoding.



    ------------------------------
    Joseph Morgan
    ------------------------------



  • 5.  RE: dp:decode('test%11',url)

    Posted Tue February 27, 2024 04:25 PM

    Hi D@viiid, 
    First, the 2nd argument to the dp:decode is a string, so your statement should have url in single quotes as a string literal

    <xsl:variable name="decodedUrl" select="dp:decode('test%11','url')"/>

    After this is done, I logged to the console the string-length($decodedUrl) and received an answer of 5, so the decode did work. Now a %11 in ascii is actually a DC1 (Device Control 1 - x11), but my question is why are you having any invalid ascii character %11 in the url string to be decoded??? By the way, don't try to write that variable to the console as a x11 causes a failure of the logging subsystem and thus the xslt will get a runtime error. If I change that value to have a valid ascii character I would expect to see in a url, say %2B (a + character), then the decoded string is test+ as expected.

    Bottom line, dp:decode, assuming the 2nd argument is properly specified, is translating %xx to the associated ascii xx character and is not failing, but the character it is translating may cause the logging system heartburn if you try to do an xsl:message and write its value to the default logs.  What is your use case that %11 is trying to handle?

    Regards,

    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 6.  RE: dp:decode('test%11',url)

    Posted Wed February 28, 2024 03:59 AM

    Hello Steve

    sorry for confusion : yes my example is not relevant.

    What i received is 'test%2525´ and in this case I had to double escape (first to pass from %2525 to %25, second to pass from %25 to %)

    but since couple of Week, I also receive %25. (Or other caracters escape only on Time)

    In my code, i used a couple of dp:decode and it works fine for %2525 but not for %25.

    Is it more clear ?

    maybe I can use gw function call from my stylesheet to decode string and use try/catch to avoid error and continue process in the stylesheet 

    what do you mean ?

    :)



    ------------------------------
    D@viiid
    ------------------------------



  • 7.  RE: dp:decode('test%11',url)

    Posted Wed February 28, 2024 06:16 AM
    Edited by Hermann Stamm-Wilbrandt Wed February 28, 2024 06:16 AM

    > I honestly don't know ( and would be very surprised if ) DataPower supports XSLT 3, where there is a try/catch.
    >
    Since more than 20 years now DataPower supports XSLT 1.0 only:
    https://www.ibm.com/docs/en/datapower-gateway/10.5.0?topic=overview-release-notes

    ...

    • XPath 1.0
    • XSLT 1.0
    • XQuery 1.0

    ...

    Not explicitlely stated, but since XPath 2.0 is part of XQuery 1.0, DataPower supports the >200 XPath 2.0 functions and operators as well (compared to <40 for XPath 1.0):
    https://www.w3.org/TR/xquery-operators/



    ------------------------------
    Hermann Stamm-Wilbrandt
    Compiler Level 3 support, IBM DataPower Gateways
    IBM
    Boeblingen Germany
    ------------------------------



  • 8.  RE: dp:decode('test%11',url)

    Posted Wed February 28, 2024 06:26 AM

    Hi D@viiid,

    > What i received is 'test%2525´ and in this case I had to double escape (first to pass from %2525 to %25, second to pass from %25 to %)
    > but since couple of Week, I also receive %25. (Or other caracters escape only on Time)
    >
    first, DataPower implements standards, and if that would be my service, I would just error out on double encoded input.
    This would force clients breaking standards to fix their application.
    That is for an ideal world.

    Before URL decoding to handle double encoded as well as normal encoded:
    Use "contains($url,'%2525')" to decide whether you want double decode or not:
    https://www.w3.org/TR/1999/REC-xpath-19991116/#function-contains



    ------------------------------
    Hermann Stamm-Wilbrandt
    Compiler Level 3 support, IBM DataPower Gateways
    IBM
    Boeblingen Germany
    ------------------------------



  • 9.  RE: dp:decode('test%11',url)

    Posted Wed February 28, 2024 06:32 AM

    Just realized that title of this thread is dangerous!
    %11 is DC1 ASCII control character:
    https://en.wikipedia.org/wiki/ASCII#Control_code_chart

    And that is not an allowed XML character:
    https://www.w3.org/TR/xml/#charsets

    [2]    Char    ::=    #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]


    ------------------------------
    Hermann Stamm-Wilbrandt
    Compiler Level 3 support, IBM DataPower Gateways
    IBM
    Boeblingen Germany
    ------------------------------



  • 10.  RE: dp:decode('test%11',url)

    Posted Wed February 28, 2024 05:26 PM

    Hi D@viiid,
    I agree with Hermann (of course :-) )
    When your url is test%2525, the first decode result will be test%25 (the %25 is changed to simply the %), and then the 2nd decode result will be test%, but if you only had test%25, then the first decode will result in test%, but that naked percent with nothing following is illegal for the 2nd decode, which fails with 

    Invalid url in dp:decode 'test%'

    Even if that wasn't at the end of the string, no telling if %xx would do something or fail.  If those two following characters happen to be valid hex and hopefully a valid utf-8 character it might work, but you'd get the same error as above if you had test%25test, ie what followed the % was not valid 2 digit hex, for example

    Invalid url in dp:decode 'test%test'

    Since there is no catching of this xslt runtime error, you'll need to test for it prior to doing the decode.  Hermann provided a contains example, but if you're really getting dynamic data where you are not necessarily getting a %2525, you may also need to revert to a regular expression that would match any % and a non two digit hex character and not attempt the decode if you find one of those, perhaps a regular expression test with

    regexp:test($stringtodecode, '%(?!([0-9|A-F][0-9|A-F]))')


    which will return a match if it finds any percent character not followed by two hex digits, including the percent character at the end of a string.

    Regards,

    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------