Skip to content

Instantly share code, notes, and snippets.

@litlfred
Created December 2, 2013 19:41
Show Gist options
  • Save litlfred/7756507 to your computer and use it in GitHub Desktop.
Save litlfred/7756507 to your computer and use it in GitHub Desktop.
SSCE for BaseX Issue #799: process XML documents with a charset in the Content-Type
module namespace page = 'http://basex.org/modules/web-page';
import module namespace request = "http://exquery.org/ns/request";
declare variable $page:test_doc := <blah><node/><node/><node/></blah>;
declare
%rest:path("/multi-part")
%rest:GET
%output:method("xhtml")
function page:menu() {
let $url := concat(request:scheme(), "://",request:hostname(),":",request:port(),"/multi-part")
return <html>
<body>
<ul>
<li><a href="{concat($url,'/without-charset')}">Expected Behaviour</a>:posted document is paresed into XML. <br/>"Content-Type" header in multi-part is "application/xml"</li>
<li><a href="{concat($url,'/with-charset')}">Unexpected Behaviour</a>:posted document remains base6Binary. <br/>"Content-Type" header in multi-part is "application/xml; charset=UTF-8" </li>
</ul>
</body>
</html>
};
declare
%rest:path("/multi-part/accept-doc")
%rest:consumes("application/xml", "text/xml", "multipart/form-data")
%rest:POST("{$request}")
%output:method("xhtml")
function page:accept-doc($request)
{
if ($request instance of xs:base64Binary) then
<html><body><h2>Unexpected Behavior</h2>Got request as a Base64 encoded binary string<p/>I really wanted this to be parsed as XML already</body></html>
else
(:Assuming it is an element :)
<html><body><h2>Expected Behavior</h2>Got {count($request//node)} &lt;node/&gt; elements. This is what I wanted</body></html>
};
declare
%rest:path("/multi-part/with-charset")
%rest:GET
function page:withg-charset()
{
let $boundary := concat("----------------", random:uuid())
let $message :=
<http:multipart media-type='multipart/form-data' boundary="{$boundary}" method='xml' accept='*/*'>
<http:header name="Content-Disposition" value="form-data; name=&quot;fileupload&quot;; filename=&quot;soap.xml&quot;"/>
<http:header name="Content-Type" value="application/xml; charset=UTF-8"/>
<http:body media-type="application/xml" method='xml' >
{$page:test_doc}
</http:body>
</http:multipart>
return page:process_message($message)
};
declare
%rest:path("/multi-part/without-charset")
%rest:GET
function page:without-charset()
{
let $boundary := concat("----------------", random:uuid())
let $message :=
<http:multipart media-type='multipart/form-data' boundary="{$boundary}" method='xml' accept='*/*'>
<http:header name="Content-Disposition" value="form-data; name=&quot;fileupload&quot;; filename=&quot;soap.xml&quot;"/>
<http:header name="Content-Type" value="application/xml"/>
<http:body media-type="application/xml" method='xml' >
{$page:test_doc}
</http:body>
</http:multipart>
return page:process_message($message)
};
declare function page:process_message($message) {
let $url := concat(request:scheme(), "://",request:hostname(),":",request:port(),"/multi-part/accept-doc")
let $request := <http:request
href='{$url}'
method='post' >
{$message}
</http:request>
let $response := http:send-request($request)
let $status := text{$response[1]/@status}
return if ($status = "200")
then
$response[2]
else
<html><body>Could not process</body></html>
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment