~clinton-collins/familyproject/trunk

« back to all changes in this revision

Viewing changes to ZendFramework/externals/dojo/dojox/io/httpParse.js

  • Committer: Clinton Collins
  • Date: 2009-06-26 19:54:58 UTC
  • Revision ID: clinton.collins@gmail.com-20090626195458-5ebba0qcvo15xlpy
Initial Import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
dojo.provide("dojox.io.httpParse");
 
2
dojox.io.httpParse = function(/*String*/httpStream, /*String?*/topHeaders,/*Boolean?*/ partial){
 
3
        // summary:
 
4
        //              Parses an HTTP stream for a message.
 
5
        // httpStream:
 
6
        //              HTTP stream to parse 
 
7
        // topHeaders:
 
8
        //              Extra header information to add to each HTTP request (kind of HTTP inheritance)
 
9
        // partial:
 
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.
 
13
        // return:
 
14
        //              Returns an array of XHR-like object for reading the headers for each message
 
15
        //               
 
16
        var xhrs=[];
 
17
        var streamLength = httpStream.length;
 
18
        do{
 
19
                var headers = {};
 
20
                var httpParts = httpStream.match(/(\n*[^\n]+)/);
 
21
                if(!httpParts){ 
 
22
                        return null;
 
23
                }
 
24
                httpStream = httpStream.substring(httpParts[0].length+1);
 
25
                httpParts = httpParts[1];
 
26
                var headerParts = httpStream.match(/([^\n]+\n)*/)[0];
 
27
                 
 
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
 
37
                }                                                                               
 
38
        
 
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(){ 
 
45
                                return headerStr;
 
46
                        },
 
47
                        getResponseHeader : function(name){
 
48
                                return headers[name];
 
49
                        }
 
50
                }; 
 
51
                var contentLength = headers['Content-Length'];
 
52
                var content; 
 
53
                if(contentLength){
 
54
                        if(contentLength <= httpStream.length){
 
55
                                content = httpStream.substring(0,contentLength);
 
56
                        }else{
 
57
                                return xhrs; // the content is not finished
 
58
                        }
 
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
 
61
                        content = content[0];
 
62
                }else if(!partial || headerFollowingChar == '\n'){
 
63
                        // if we have to finish
 
64
                        content = httpStream;
 
65
                }else{
 
66
                        return xhrs;
 
67
                }
 
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;
 
71
                xhr.readyState = 4;
 
72
                xhr._lastIndex = streamLength - httpStream.length; // need to pick up from where we left on streaming connections 
 
73
        }while(httpStream);
 
74
        return xhrs;
 
75
}
 
 
b'\\ No newline at end of file'