Subversion Repositories HomeAutomation

Rev

Rev 1203 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
976 runge 1
 
2
function HTTP()
3
{
4
}
5
 
6
HTTP.prototype.mySocket = null;
7
HTTP.prototype.myUserCallback = null;
8
HTTP.prototype.myBuffer = null;
983 runge 9
HTTP.prototype.myRequest = null;
976 runge 10
 
11
HTTP.prototype.request = function(callback, url)
12
{
13
    this.myUserCallback = callback;
14
 
15
    if (this.mySocket)
16
    {
17
        this.mySocket.stop();
18
    }
19
 
20
    var self = this;
21
 
22
    var urlData = this.parseUrl(url);
23
 
24
    /*log("hostname: " + urlData['hostname'] + "\n");
25
    log("path: " + urlData['path'] + "\n");
26
    log("port: " + urlData['port'] + "\n");*/
27
 
28
    this.myBuffer = "";
29
 
30
    this.mySocket = new Socket(function(event, data) { self.socketCallback(event, data); }, urlData['hostname'], urlData['port'], 0);
31
    this.mySocket.start();
32
 
983 runge 33
    this.myRequest = "GET " + urlData['path'] + " HTTP/1.1\r\n";
34
    this.myRequest += "Host: " + urlData['hostname'] + ":" + urlData['port'] + "\r\n";
35
    this.myRequest += "Connection: close\r\n";
36
    this.myRequest += "\r\n";
976 runge 37
}
38
 
39
HTTP.prototype.parseUrl = function(url)
40
{
41
    var result = new Array();
42
    result['port'] = 80;
43
    result['path'] = "/";
44
 
45
    var firstSlash = url.indexOf('/');
46
 
47
    if (firstSlash == -1)
48
    {
49
        result['hostname'] = url;
50
    }
51
    else
52
    {
53
        result['hostname'] = url.substr(0, firstSlash);
54
        result['path'] = url.substr(firstSlash);
55
    }
56
 
57
    var firstColon = result['hostname'].indexOf(':');
58
 
59
    if (firstColon != -1)
60
    {
61
        result['port'] = result['hostname'].substr(firstColon+1);
62
        result['hostname'] = result['hostname'].substr(0, firstColon);
63
    }
64
 
65
    return result;
66
}
67
 
68
HTTP.prototype.socketCallback = function(event, data)
69
{
70
    switch (event)
71
    {
984 runge 72
    case "CONNECTED":
983 runge 73
    this.mySocket.send(this.myRequest);
74
    break;
75
 
984 runge 76
    case "DATA":
976 runge 77
    this.myBuffer += unescape(data);
78
    break;
79
 
984 runge 80
    case "RESET":
81
    case "CLOSED":
82
    case "DIED":
976 runge 83
    this.mySocket.stop();
84
    this.processBuffer();
1203 arune 85
    this.mySocket = null;
976 runge 86
    break;
87
    }
88
}
89
 
90
HTTP.prototype.processBuffer = function()
91
{
981 runge 92
    ///FIXME: This is a bit ugly code... also not fully HTTP/1.1 compliant look at http://www.jmarshall.com/easy/http/
93
 
976 runge 94
    var pos = this.myBuffer.indexOf("\n\n");
95
 
96
    var headerData = this.myBuffer.substr(0, pos);
97
    var contentData = this.myBuffer.substr(pos+2);
98
 
99
    var headerLines = headerData.split("\n");
100
 
101
    var header = new Array();
102
    var result = headerLines[0];
103
 
104
    for (var n = 1; n < headerLines.length; n++)
105
    {
106
        var headerParts = headerLines[n].split(":", 2);
107
        header[headerParts[0]] = headerParts[1].trim(' ');
108
    }
109
 
977 runge 110
    this.myBuffer = "";
111
 
976 runge 112
    this.myUserCallback(result, header, contentData);
113
}