1
dojo.provide("dojox.io.httpParse");
2
dojox.io.httpParse = function(/*String*/httpStream, /*String?*/topHeaders,/*Boolean?*/ partial){
4
// Parses an HTTP stream for a message.
6
// HTTP stream to parse
8
// Extra header information to add to each HTTP request (kind of HTTP inheritance)
10
// A true value indicates that the stream may not be finished, it may end arbitrarily in mid stream.
11
// The last XHR object will have a special property _lastIndex that indicates the how far along
12
// the httpStream could be successfully parsed into HTTP messages.
14
// Returns an array of XHR-like object for reading the headers for each message
17
var streamLength = httpStream.length;
20
var httpParts = httpStream.match(/(\n*[^\n]+)/);
24
httpStream = httpStream.substring(httpParts[0].length+1);
25
httpParts = httpParts[1];
26
var headerParts = httpStream.match(/([^\n]+\n)*/)[0];
28
httpStream = httpStream.substring(headerParts.length);
29
var headerFollowingChar = httpStream.substring(0,1);
30
httpStream = httpStream.substring(1);
31
headerParts = (topHeaders || "") + headerParts;
32
var headerStr = headerParts;
33
headerParts = headerParts.match(/[^:\n]+:[^\n]+\n/g); // parse the containing and contained response headers with the contained taking precedence (by going last)
34
for(var j = 0; j < headerParts.length; j++){
35
var colonIndex = headerParts[j].indexOf(':');
36
headers[headerParts[j].substring(0,colonIndex)] = headerParts[j].substring(colonIndex+1).replace(/(^[ \r\n]*)|([ \r\n]*)$/g,''); // trim
39
httpParts = httpParts.split(' ');
40
var xhr = { // make it look like an xhr object, at least for the response part of the API
41
status : parseInt(httpParts[1],10),
42
statusText : httpParts[2],
43
readyState : 3, // leave it at 3 until we get a full body
44
getAllResponseHeaders : function(){
47
getResponseHeader : function(name){
51
var contentLength = headers['Content-Length'];
54
if(contentLength <= httpStream.length){
55
content = httpStream.substring(0,contentLength);
57
return xhrs; // the content is not finished
59
}else if((content = httpStream.match(/(.*)HTTP\/\d\.\d \d\d\d[\w\s]*\n/))){ // assign content
60
// if we spot another HTTP message coming up, we will just assign all the in between text to the content
62
}else if(!partial || headerFollowingChar == '\n'){
63
// if we have to finish
68
xhrs.push(xhr); // add it to the list, since it is a full HTTP message
69
httpStream = httpStream.substring(content.length); // move along the stream
70
xhr.responseText = content;
72
xhr._lastIndex = streamLength - httpStream.length; // need to pick up from where we left on streaming connections
b'\\ No newline at end of file'