Subversion Repositories HomeAutomation

Rev

Rev 981 | Rev 984 | Go to most recent revision | 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
    {
983 runge 72
    case "EVENT_CONNECTED":
73
    this.mySocket.send(this.myRequest);
74
    break;
75
 
976 runge 76
    case "EVENT_DATA":
77
    this.myBuffer += unescape(data);
78
    break;
79
 
983 runge 80
    case "EVENT_RESET":
976 runge 81
    case "EVENT_CLOSED":
82
    case "EVENT_DIED":
83
    this.mySocket.stop();
84
    this.processBuffer();
85
    break;
86
 
87
    case "EVENT_INACTIVITY":
88
    break;
89
    }
90
}
91
 
92
HTTP.prototype.processBuffer = function()
93
{
981 runge 94
    ///FIXME: This is a bit ugly code... also not fully HTTP/1.1 compliant look at http://www.jmarshall.com/easy/http/
95
 
976 runge 96
    var pos = this.myBuffer.indexOf("\n\n");
97
 
98
    var headerData = this.myBuffer.substr(0, pos);
99
    var contentData = this.myBuffer.substr(pos+2);
100
 
101
    var headerLines = headerData.split("\n");
102
 
103
    var header = new Array();
104
    var result = headerLines[0];
105
 
106
    for (var n = 1; n < headerLines.length; n++)
107
    {
108
        var headerParts = headerLines[n].split(":", 2);
109
        header[headerParts[0]] = headerParts[1].trim(' ');
110
    }
111
 
977 runge 112
    this.myBuffer = "";
113
 
976 runge 114
    this.myUserCallback(result, header, contentData);
115
}