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