Subversion Repositories HomeAutomation

Rev

Rev 983 | Rev 1203 | 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
	{
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();
85
	break;
86
	}
87
}
88
 
89
HTTP.prototype.processBuffer = function()
90
{
981 runge 91
	///FIXME: This is a bit ugly code... also not fully HTTP/1.1 compliant look at http://www.jmarshall.com/easy/http/
92
 
976 runge 93
	var pos = this.myBuffer.indexOf("\n\n");
94
 
95
	var headerData = this.myBuffer.substr(0, pos);
96
	var contentData = this.myBuffer.substr(pos+2);
97
 
98
	var headerLines = headerData.split("\n");
99
 
100
	var header = new Array();
101
	var result = headerLines[0];
102
 
103
	for (var n = 1; n < headerLines.length; n++)
104
	{
105
		var headerParts = headerLines[n].split(":", 2);
106
		header[headerParts[0]] = headerParts[1].trim(' ');
107
	}
108
 
977 runge 109
	this.myBuffer = "";
110
 
976 runge 111
	this.myUserCallback(result, header, contentData);
112
}