|
50 | 50 | this.responseLength = null |
51 | 51 | this.responseBytesRead = null |
52 | 52 | this.requestBody = null |
| 53 | + |
| 54 | + this.secured = false |
53 | 55 | } |
54 | 56 |
|
55 | 57 | ChromeSocketXMLHttpRequest.prototype = { |
|
58 | 60 | url:url, |
59 | 61 | async:true } |
60 | 62 | this.uri = WSC.parseUri(this.opts.url) |
61 | | - console.assert(this.uri.protocol == 'http:') // https not supported for chrome.socket yet |
| 63 | + //console.assert(this.uri.protocol == 'http:') // https not supported for chrome.socket yet |
62 | 64 | }, |
63 | 65 | setRequestHeader: function(key, val) { |
64 | 66 | this.extraHeaders[key] = val |
|
107 | 109 | } |
108 | 110 | }, |
109 | 111 | error: function(data) { |
| 112 | + this._finished = true |
| 113 | + //console.log('error:',data) |
110 | 114 | this.haderror = true |
111 | 115 | if (! this.stream.closed) { |
112 | 116 | this.stream.close() |
113 | 117 | } |
114 | | - this._finished = true |
115 | 118 | if (this.onerror) { |
116 | 119 | this.onerror(data) |
117 | 120 | } |
|
131 | 134 | var host = this.getHost() |
132 | 135 | var port = this.getPort() |
133 | 136 | //console.log('connecting to',host,port) |
134 | | - chrome.sockets.tcp.connect( sockInfo.socketId, host, port, _.bind(this.onConnect, this) ) |
| 137 | + chrome.sockets.tcp.setPaused( sockInfo.socketId, true, function() { |
| 138 | + chrome.sockets.tcp.connect( sockInfo.socketId, host, port, _.bind(this.onConnect, this) ) |
| 139 | + }.bind(this)) |
135 | 140 | }, |
136 | 141 | onConnect: function(result) { |
137 | 142 | //console.log('connected to',this.getHost()) |
138 | 143 | var lasterr = chrome.runtime.lastError |
139 | | - |
140 | 144 | if (this.closed) { return } |
141 | 145 | this.connecting = false |
142 | 146 | if (this.timedOut) { |
|
147 | 151 | this.error({error:'connection error', |
148 | 152 | code:result}) |
149 | 153 | } else { |
| 154 | + if (this.uri.protocol == 'https:' && ! this.secured) { |
| 155 | + this.secured = true |
| 156 | + //console.log('securing socket',this.sockInfo.socketId) |
| 157 | + chrome.sockets.tcp.secure(this.sockInfo.socketId, this.onConnect.bind(this)) |
| 158 | + return |
| 159 | + } |
150 | 160 | var headers = this.createRequestHeaders() |
151 | 161 | //console.log('request to',this.getHost(),headers) |
152 | 162 | this.stream.writeBuffer.add( new TextEncoder('utf-8').encode(headers).buffer ) |
|
156 | 166 | } |
157 | 167 | this.stream.tryWrite() |
158 | 168 | this.stream.readUntil('\r\n\r\n', this.onHeaders.bind(this)) |
| 169 | + chrome.sockets.tcp.setPaused( this.sockInfo.socketId, false, function(){}) |
159 | 170 | } |
160 | 171 | }, |
161 | 172 | getHost: function() { |
162 | 173 | return this.uri.hostname |
163 | 174 | }, |
164 | 175 | getPort: function() { |
165 | | - return parseInt(this.uri.port) || 80 |
| 176 | + if (this.uri.protocol == 'https:') { |
| 177 | + return parseInt(this.uri.port) || 443 |
| 178 | + } else { |
| 179 | + return parseInt(this.uri.port) || 80 |
| 180 | + } |
166 | 181 | }, |
167 | 182 | onHeaders: function(data) { |
168 | 183 | // not sure what encoding for headers is exactly, latin1 or something? whatever. |
|
180 | 195 | if (response.headers['transfer-encoding'] && |
181 | 196 | response.headers['transfer-encoding'] == 'chunked') { |
182 | 197 | this.chunks = new WSC.Buffer |
183 | | - console.log('looking for an \\r\\n') |
| 198 | + //console.log('looking for an \\r\\n') |
184 | 199 | this.stream.readUntil("\r\n", this.getNewChunk.bind(this)) |
185 | 200 | //this.error('chunked encoding') |
186 | 201 | } else { |
187 | 202 | if (! response.headers['content-length']) { |
188 | 203 | this.error("no content length in response") |
189 | 204 | } else { |
| 205 | + console.log('read bytes',this.responseLength) |
190 | 206 | this.stream.readBytes(this.responseLength, this.onBody.bind(this)) |
191 | 207 | } |
192 | 208 | } |
193 | 209 | }, |
194 | 210 | onChunkDone: function(data) { |
195 | 211 | this.chunks.add(data) |
196 | | - console.log(this.stream.readBuffer) |
197 | 212 | this.stream.readUntil("\r\n", this.getNewChunk.bind(this)) |
198 | 213 | }, |
199 | 214 | getNewChunk: function(data) { |
|
203 | 218 | this.error('invalid chunked encoding response') |
204 | 219 | return |
205 | 220 | } |
206 | | - console.log('looking for new chunk of len',len) |
| 221 | + //console.log('looking for new chunk of len',len) |
207 | 222 | if (len == 0) { |
208 | | - console.log('got all chunks',this.chunks) |
| 223 | + //console.log('got all chunks',this.chunks) |
209 | 224 | var body = this.chunks.flatten() |
210 | 225 | this.onBody(body) |
211 | 226 | } else { |
|
258 | 273 | console.log('creating XHR') |
259 | 274 | var xhr = new ChromeSocketXMLHttpRequest |
260 | 275 | xhr.open("GET","https://www.google.com") |
261 | | - xhr.timeout = 4000 |
| 276 | + xhr.timeout = 8000 |
262 | 277 | xhr.onload = xhr.onerror = xhr.ontimeout = function(evt) { |
263 | | - console.log('xhr test load',evt) |
| 278 | + console.log('xhr result:',evt) |
264 | 279 | } |
265 | 280 | xhr.send() |
266 | 281 | window.txhr = xhr |
|
0 commit comments