[Xsltforms-support] How to implement http-header pasing on submission?
Brought to you by:
alain-couthures
|
From: Kostis A. <ank...@gm...> - 2010-05-11 16:49:46
|
Hi Alain,
i saw that you incorporated the event() code i had submitted a while ago,
so now i'm reworking it, to fix certain issues with it, such as:
* checking for event-context items case-insesitivly,
* returning '' when not context items do not exist (insntead of 'undefined').
Among others, i try to implement the parsing of xml-http-headers,
as defined by chapter 11.4 [1].
Therefore, given the 3 response headers below:
----------------
Date: Tue, 11 May 2010 16:26:46 GMT
Server: Apache
Content-Type: text/html; charset=iso-8859-1
----------------
we should return thw following nodeset:
----------------
<header xmlns="">
<name>Date</name><value>Tue, 11 May 2010 16:26:46 GMT</value
</header>
<header xmlns="">
<name>Server</name>Apache<value></value
</header>
<header xmlns="">
<name>Content-Type</name><value>text/html; charset=iso-8859-1</value
</header>
----------------
Based on [2], I crafted the following code to parse the http-headers:
----------------
/** @headers similar to the headers above */
XFSubmission.prototype.parseHttpHeaders_ = function(headers) {
if (!headers)
return '';
var hdoc = new XDocument();
var nodes = [];
try {
var hlines = headers.split("\r\n");
for(var i in hlines) {
var hline = hlines[i];
if (!hline.length)
continue;
var hpair = hline.split(":");
//alert("name="+hpair[0]+", value="+hpair[1]);
var name = hpair[0];
var value = hpair[1].replace("/^\\s*/", "");
var hel = hdoc.createElementNS("", "header");
var nel = hdoc.createElementNS("", "name");
var vel = hdoc.createElementNS("", "value");
var ntxt = hdoc.createTextNode(name);
var vtxt = hdoc.createTextNode(value);
nel.appendChild(ntxt);
vel.appendChild(vtxt);
hel.appendChild(nel);
hel.appendChild(vel);
hdoc.appendChild(hel);// Is it necessary?
nodes.push(hel);
}
} catch(ex) {
DebugConsole.write("Error while parsing http-headers: " + ex);
hdoc = headers;
}
return hdoc; // should i had returned 'nodes' var instead?
}
----------------
Now the questions:
1. Is there i simpler way?
(ie return an associative array?)
2) Why the above code does not work when using the XPath expressions below?
event('response-headers')/header[2]/name
event('response-headers')/header[1]/value
...etc
What should i change with regard to the return value of the
parseHttpHeaders_() function?
NOTE
1. that in the above code i diregarded completely any header-encoding issues,
and
2. that i'm planning to split this function in two,
one that parses headers to produce an associative array, to be used elsewhere
(i.e. to decide based on the 'Content-type' whether to parse the
response-body or not, see [2] again)
and anotherone, that produces the xml-nodeset.
Regards,
Kostis Anagnostopoulos
[1] http://www.w3.org/TR/xforms11/#submit-evt-submit-error
[2] http://www.w3.org/TR/XMLHttpRequest/
|