Rev 1348 | 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"; |
||
| 1348 | linlun | 35 | this.myRequest += "User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.2) Gecko/20090730 SUSE/3.5.2-1.1 Firefox/3.5.2\r\n"; |
| 983 | runge | 36 | this.myRequest += "Connection: close\r\n"; |
| 37 | this.myRequest += "\r\n"; |
||
| 976 | runge | 38 | } |
| 39 | |||
| 40 | HTTP.prototype.parseUrl = function(url) |
||
| 41 | { |
||
| 42 | var result = new Array(); |
||
| 43 | result['port'] = 80; |
||
| 44 | result['path'] = "/"; |
||
| 45 | |||
| 46 | var firstSlash = url.indexOf('/'); |
||
| 47 | |||
| 48 | if (firstSlash == -1) |
||
| 49 | { |
||
| 50 | result['hostname'] = url; |
||
| 51 | } |
||
| 52 | else |
||
| 53 | { |
||
| 54 | result['hostname'] = url.substr(0, firstSlash); |
||
| 55 | result['path'] = url.substr(firstSlash); |
||
| 56 | } |
||
| 57 | |||
| 58 | var firstColon = result['hostname'].indexOf(':'); |
||
| 59 | |||
| 60 | if (firstColon != -1) |
||
| 61 | { |
||
| 62 | result['port'] = result['hostname'].substr(firstColon+1); |
||
| 63 | result['hostname'] = result['hostname'].substr(0, firstColon); |
||
| 64 | } |
||
| 65 | |||
| 66 | return result; |
||
| 67 | } |
||
| 68 | |||
| 69 | HTTP.prototype.socketCallback = function(event, data) |
||
| 70 | { |
||
| 71 | switch (event) |
||
| 72 | { |
||
| 984 | runge | 73 | case "CONNECTED": |
| 983 | runge | 74 | this.mySocket.send(this.myRequest); |
| 75 | break; |
||
| 76 | |||
| 984 | runge | 77 | case "DATA": |
| 976 | runge | 78 | this.myBuffer += unescape(data); |
| 79 | break; |
||
| 80 | |||
| 984 | runge | 81 | case "RESET": |
| 82 | case "CLOSED": |
||
| 83 | case "DIED": |
||
| 976 | runge | 84 | this.mySocket.stop(); |
| 85 | this.processBuffer(); |
||
| 1203 | arune | 86 | this.mySocket = null; |
| 976 | runge | 87 | break; |
| 88 | } |
||
| 89 | } |
||
| 90 | |||
| 91 | HTTP.prototype.processBuffer = function() |
||
| 92 | { |
||
| 981 | runge | 93 | ///FIXME: This is a bit ugly code... also not fully HTTP/1.1 compliant look at http://www.jmarshall.com/easy/http/ |
| 94 | |||
| 976 | runge | 95 | var pos = this.myBuffer.indexOf("\n\n"); |
| 96 | |||
| 97 | var headerData = this.myBuffer.substr(0, pos); |
||
| 98 | var contentData = this.myBuffer.substr(pos+2); |
||
| 99 | |||
| 100 | var headerLines = headerData.split("\n"); |
||
| 101 | |||
| 102 | var header = new Array(); |
||
| 103 | var result = headerLines[0]; |
||
| 104 | |||
| 105 | for (var n = 1; n < headerLines.length; n++) |
||
| 106 | { |
||
| 107 | var headerParts = headerLines[n].split(":", 2); |
||
| 108 | header[headerParts[0]] = headerParts[1].trim(' '); |
||
| 109 | } |
||
| 110 | |||
| 977 | runge | 111 | this.myBuffer = ""; |
| 112 | |||
| 1402 | runge | 113 | if (this.myUserCallback != null) |
| 114 | { |
||
| 115 | this.myUserCallback(result, header, contentData); |
||
| 116 | this.myUserCallback = null; |
||
| 117 | } |
||
| 976 | runge | 118 | } |