[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: http_url_post_raw encoding Issue


There isn't one in HTTPAPI but below is one I wrote you are welcome to use.


     P utWebv$XmlCharsCvtIllegal...

     P                 B                   Export

     D                 PI         65535A   Varying


      * Convert XML Characters (Illegal To Special)


      * This procedure performs manipulation of XML characters so they can

      * be assigned as an XML value or attribute. This includes replacing

      * certain illegal characters with their special value replacements.


      * Parameters


     D v@@Chars                   65535A   Const Varying


      * Variables                                              
     D a@ScanFor       S              1A                       
     D i@ScanForPos    S             10I 0                     
     D i@ScanStart     S             10I 0                     
     D i@ReplaceLen    S             10I 0                     
     D v@ReplaceWith   S              6A   Varying Inz(*Blanks)
     D v@Chars         S          65535A   Varying             
       // assign initial value, trimming trailing blanks       
       // but adding in enough blanks to allow for processing  
       v@Chars = %TrimR(v@@Chars) + v@ReplaceWith;             
       // if nothing in value return                           
       If %Len(v@Chars) = %Len(v@ReplaceWith);                 
         Return '';                                            
       // handle &              
       a@ScanFor = '&';         
       v@ReplaceWith = '&'; 
       ExSr $Replace;           
       // handle <              
       a@ScanFor = '<';         
       v@ReplaceWith = '&lt;';  
       ExSr $Replace;           
       // handle >              
       a@ScanFor = '>';         
       v@ReplaceWith = '&gt;';  
       ExSr $Replace;           
       // handle "              
       a@ScanFor = '"';         
       v@ReplaceWith = '&quot;';
       ExSr $Replace;           
       // handle '

       a@ScanFor = '''';

       v@ReplaceWith = '&apos;';

       ExSr $Replace;


       // return new value

       Return %TrimR(v@Chars);



       BegSr $Replace;


       // Replace Special Value


         // scan for first occurance, if none found then leave

         i@ScanForPos = %Scan(a@ScanFor:v@Chars);

         If i@ScanForPos = *Zeros;



         // get length of replacement                                    
         i@ReplaceLen = %Len(v@ReplaceWith);                             
         // loop until no illegal values found                           
         DoU i@ScanForPos = *Zeros;                                      
           // if original string is not already the                      
           // replacement value then perform replacement                 
           If %Subst(v@Chars:i@ScanForPos:i@ReplaceLen) <> v@ReplaceWith;
             v@Chars = %Replace(v@ReplaceWith:v@Chars:i@ScanForPos:1);   
           // update scan starting position                              
           i@ScanStart = (i@ScanForPos + i@ReplaceLen);                  
           // scan for next occurance                                    
           i@ScanForPos = %Scan(a@ScanFor:v@Chars:i@ScanStart);          
     P                 E

-----Original Message-----
From: ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx
[mailto:ftpapi-bounces@xxxxxxxxxxxxxxxxxxxxxx] On Behalf Of Natarajan Palani
Sent: Friday, October 08, 2010 4:17 PM
To: scott klement
Subject: RE: http_url_post_raw encoding Issue

Hi Scott,
Thanks for the prompt response. Is there a API that I can use to make an XML
document into a valid (well-formed) XML document.
Please let me know and Thanks for your help.
Raj From: Scott Klement <sk@xxxxxxxxxxxxxxxx>To: HTTPAPI and FTPAPI Projects
<ftpapi@xxxxxxxxxxxxxxxxxxxxxx>Subject: Re: http_url_post_raw encoding
IssueDate: Fri, 08 Oct 2010 15:02:19 -0500Hi Raj,
There are two levels of encoding needed here:

1) You need to make your XML document into a valid (well-formed) XML 
document by encoding characters that are special to XML.

2) In some circumstances (and I'm guessing yours is one) you have to 
encode your data so that it's valid as an HTTP URL.  (Called "URL Encoding.)

You are doing the 2nd level, but are missing the first.

Let's see how that affects you.  You start out with this:


This is _not_ a valid XML document.  Then you URL encode it, and you end 
up with this:


That's perfectly valid as HTML form data, and can be legitimately put 
into a URL.  HTTPAPI's encoder has done it's job!  Hurray!  So it's sent 
over the network to the computer on the opposite end.

That computer receives the URL encoded string, and decodes it (since XML 
parsers have no concept of URL encoding.) and the result is this once again:


Now the XML parser tries to parse it... and fails because it's not a 
well-formed (valid) XML document.  You can't have a lone & in the middle 
of chardata in XML.

That's what's happening... and HTTPAPI is doing exactly what it should, 
it's encoding your data so it's valid for HTTP.  HTTPAPI doesn't even 
know that your data is XML!  And even if it somehow was smart enough to 
figure that out for you, how would it know whether & was intended to be 
the literal & character, or whether it's intended to be an entity?  You 
seem to think it can read your mind, but it cannot.

For the process to work, you need to start with a _valid_ XML document. 
  In other words, you need to start with this:


After URL encoding, it will look like this:


Now it goes over HTTP protocol successfully... when it's received, the 
receiving HTTP server decodes the URL encoding back to this:


And the XML parser will now parse it successfully.

From: natarajanpalani@xxxxxxx
To: ftpapi@xxxxxxxxxxxxxxxxxxxxxx
Subject: http_url_post_raw encoding Issue
Date: Fri, 8 Oct 2010 18:50:59 +0000

Hi Scott,
I am using http_url_post_raw. It works like a charm. 
The input to the server is in XML. During our testing we came across a
scenario where the XML data had and "&" char for an element (<DESC>GENERAL &
ACCESSORIES</DESC>).  And we got the error response from the target Server
<ERROR DESCRIPTION>Unable to parse input XML entity reference names can  not
start with character &apos; &apos; (position: START_TAG seen
...&lt;VALUE&gt;GENERAL &amp; ... @1:2497)</ERROR DESCRIPTION>
I thought by using WEBFORM_setPtr & WEBFORM_postData the data gets converted
to URL encoded data. But it did not convert my "&" char to &amp;. Is there
any reason behind this or am I doing something wrong here. Below is my code
for Ur reference:
            form = WEBFORM_open();            WEBFORM_setPtr(form:
'instring': %addr(@XML_Input) :
%len(%trimR(@XML_Input)) );            WEBFORM_postData(form: postData:
postDataSize );
            // Post data and get document            rc =
http_url_post_raw(%trim(@ServerName)                      : postData
: postDataSize                      : 1                      :
%paddr('RECEIVEDATA')                      : @HTTPPostTimeout
: *OMIT                      : %trim(@URLEncodApp) );

            if rc <> 1;              DS_HTTP_StatusData.HTTPErrDesc = 'HTTP
Post Failed. ' +
http_error();              DS_HTTP_StatusData.ReturnCd    = HTTPPostFailed;
Else;              callp Translate(retlen: retdata: 'QTCPEBC');
DS_HTTP_StatusData.XML_Response = %trim(retdata);            endif;

Please let me know.
Thanks for your help. 

This is the FTPAPI mailing list.  To unsubscribe, please go to: