Skip to content

Commit 40275df

Browse files
committed
chromesocketxhr support https
1 parent 63acfb4 commit 40275df

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

chromesocketxhr.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
this.responseLength = null
5151
this.responseBytesRead = null
5252
this.requestBody = null
53+
54+
this.secured = false
5355
}
5456

5557
ChromeSocketXMLHttpRequest.prototype = {
@@ -58,7 +60,7 @@
5860
url:url,
5961
async:true }
6062
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
6264
},
6365
setRequestHeader: function(key, val) {
6466
this.extraHeaders[key] = val
@@ -107,11 +109,12 @@
107109
}
108110
},
109111
error: function(data) {
112+
this._finished = true
113+
//console.log('error:',data)
110114
this.haderror = true
111115
if (! this.stream.closed) {
112116
this.stream.close()
113117
}
114-
this._finished = true
115118
if (this.onerror) {
116119
this.onerror(data)
117120
}
@@ -131,12 +134,13 @@
131134
var host = this.getHost()
132135
var port = this.getPort()
133136
//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))
135140
},
136141
onConnect: function(result) {
137142
//console.log('connected to',this.getHost())
138143
var lasterr = chrome.runtime.lastError
139-
140144
if (this.closed) { return }
141145
this.connecting = false
142146
if (this.timedOut) {
@@ -147,6 +151,12 @@
147151
this.error({error:'connection error',
148152
code:result})
149153
} 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+
}
150160
var headers = this.createRequestHeaders()
151161
//console.log('request to',this.getHost(),headers)
152162
this.stream.writeBuffer.add( new TextEncoder('utf-8').encode(headers).buffer )
@@ -156,13 +166,18 @@
156166
}
157167
this.stream.tryWrite()
158168
this.stream.readUntil('\r\n\r\n', this.onHeaders.bind(this))
169+
chrome.sockets.tcp.setPaused( this.sockInfo.socketId, false, function(){})
159170
}
160171
},
161172
getHost: function() {
162173
return this.uri.hostname
163174
},
164175
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+
}
166181
},
167182
onHeaders: function(data) {
168183
// not sure what encoding for headers is exactly, latin1 or something? whatever.
@@ -180,20 +195,20 @@
180195
if (response.headers['transfer-encoding'] &&
181196
response.headers['transfer-encoding'] == 'chunked') {
182197
this.chunks = new WSC.Buffer
183-
console.log('looking for an \\r\\n')
198+
//console.log('looking for an \\r\\n')
184199
this.stream.readUntil("\r\n", this.getNewChunk.bind(this))
185200
//this.error('chunked encoding')
186201
} else {
187202
if (! response.headers['content-length']) {
188203
this.error("no content length in response")
189204
} else {
205+
console.log('read bytes',this.responseLength)
190206
this.stream.readBytes(this.responseLength, this.onBody.bind(this))
191207
}
192208
}
193209
},
194210
onChunkDone: function(data) {
195211
this.chunks.add(data)
196-
console.log(this.stream.readBuffer)
197212
this.stream.readUntil("\r\n", this.getNewChunk.bind(this))
198213
},
199214
getNewChunk: function(data) {
@@ -203,9 +218,9 @@
203218
this.error('invalid chunked encoding response')
204219
return
205220
}
206-
console.log('looking for new chunk of len',len)
221+
//console.log('looking for new chunk of len',len)
207222
if (len == 0) {
208-
console.log('got all chunks',this.chunks)
223+
//console.log('got all chunks',this.chunks)
209224
var body = this.chunks.flatten()
210225
this.onBody(body)
211226
} else {
@@ -258,9 +273,9 @@
258273
console.log('creating XHR')
259274
var xhr = new ChromeSocketXMLHttpRequest
260275
xhr.open("GET","https://www.google.com")
261-
xhr.timeout = 4000
276+
xhr.timeout = 8000
262277
xhr.onload = xhr.onerror = xhr.ontimeout = function(evt) {
263-
console.log('xhr test load',evt)
278+
console.log('xhr result:',evt)
264279
}
265280
xhr.send()
266281
window.txhr = xhr

stream.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@
8585
onWrite: function(callback, evt) {
8686
var err = chrome.runtime.lastError
8787
if (err) {
88-
console.log('socket.send lastError',err)
88+
//console.log('socket.send lastError',err)
8989
//this.tryClose()
9090
this.close('writeerr'+err)
9191
return
9292
}
9393

9494
// look at evt!
9595
if (evt.bytesWritten <= 0) {
96-
console.log('onwrite fail, closing',evt)
96+
//console.log('onwrite fail, closing',evt)
9797
this.close('writerr<0')
9898
return
9999
}
@@ -114,7 +114,7 @@
114114
this.close('read tcp lasterr'+lasterr)
115115
return
116116
}
117-
//console.log('onRead',evt)
117+
//console.log('onRead',WSC.ui82str(new Uint8Array(evt.data)))
118118
if (evt.resultCode == 0) {
119119
//this.error({message:'remote closed connection'})
120120
this.log('remote closed connection (halfduplex)')
@@ -131,7 +131,6 @@
131131
this.checkBuffer()
132132
}
133133
},
134-
135134
log: function(msg,msg2,msg3) {
136135
console.log(this.sockId,msg,msg2,msg3)
137136
},
@@ -148,7 +147,7 @@
148147
this.readCallback = null
149148
callback(toret)
150149
}
151-
} else if (this.pleaseReadBytes) {
150+
} else if (this.pleaseReadBytes !== null) {
152151
if (this.readBuffer.size() >= this.pleaseReadBytes) {
153152
var data = this.readBuffer.consume(this.pleaseReadBytes)
154153
var callback = this.readCallback

0 commit comments

Comments
 (0)