-
Notifications
You must be signed in to change notification settings - Fork 271
/
chromesocketxhr.js
285 lines (271 loc) · 11.1 KB
/
chromesocketxhr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
(function() {
function ui8IndexOf(arr, s, startIndex) {
// searches a ui8array for subarray s starting at startIndex
startIndex = startIndex || 0
var match = false
for (var i=startIndex; i<arr.length - s.length + 1; i++) {
if (arr[i] == s[0]) {
match = true
for (var j=1; j<s.length; j++) {
if (arr[i+j] != s[j]) {
match = false
break
}
}
if (match) {
return i
}
}
}
return -1
}
function ChromeSocketXMLHttpRequest() {
this.onload = null
this._finished = false
this.onerror = null
this.opts = null
this.timedOut = false
this.timeout = 0
this.timeoutId = null
this.stream = null
this.connecting = false
this.writing = false
this.haderror = false
this.closed = false
this.sockInfo = null
this.responseType = null
this.extraHeaders = {}
this.headersReceived = false
this.responseHeaders = null
this.responseHeadersParsed = null
this.responseBody = null
this.responseLength = null
this.responseBytesRead = null
this.requestBody = null
this.secured = false
}
ChromeSocketXMLHttpRequest.prototype = {
open: function(method, url, async) {
this.opts = { method:method,
url:url,
async:true }
this.uri = WSC.parseUri(this.opts.url)
//console.assert(this.uri.protocol == 'http:') // https not supported for chrome.socket yet
},
setRequestHeader: function(key, val) {
this.extraHeaders[key] = val
},
cancel: function() {
if (! this.stream.closed) { this.stream.close() }
},
send: function(data) {
//console.log('xhr send payload',this.opts.method, data)
this.requestBody = data
chrome.sockets.tcp.create({}, _.bind(this.onCreate, this))
if (this.timeout !== 0) {
this.timeoutId = setTimeout( _.bind(this.checkTimeout, this), this.timeout )
}
},
createRequestHeaders: function() {
var lines = []
var headers = {//'Connection': 'close',
//'Accept-Encoding': 'identity', // servers will send us chunked encoding even if we dont want it, bastards
// 'Accept-Encoding': 'identity;q=1.0 *;q=0', // servers will send us chunked encoding even if we dont want it, bastards
// 'User-Agent': 'uTorrent/330B(30235)(server)(30235)', // setRequestHeader /extra header is doing this
'Host': this.uri.host}
_.extend(headers, this.extraHeaders)
if (this.opts.method == 'GET') {
// headers['Content-Length'] == '0'
} else if (this.opts.method == 'POST') {
if (this.requestBody) {
headers['Content-Length'] = this.requestBody.byteLength.toString()
} else {
headers['Content-Length'] = '0'
// make sure content-length 0 included ?
}
} else {
this.error('unsupported method')
}
lines.push(this.opts.method + ' ' + this.uri.pathname + this.uri.search + ' HTTP/1.1')
//console.log('making request',lines[0],headers)
for (var key in headers) {
lines.push( key + ': ' + headers[key] )
}
return lines.join('\r\n') + '\r\n\r\n'
},
checkTimeout: function() {
if (! this._finished) {
this.error({error:'timeout'}) // call ontimeout instead
}
},
error: function(data) {
this._finished = true
//console.log('error:',data)
this.haderror = true
if (this.onerror) {
console.assert(typeof data == "object")
data.target = {error:true}
this.onerror(data)
}
if (! this.stream.closed) {
this.stream.close()
}
},
onStreamClose: function(evt) {
//console.log('xhr closed')
if (! this._finished) {
this.error({error:'stream closed'})
}
},
onCreate: function(sockInfo) {
if (this.closed) { return }
this.stream = new WSC.IOStream(sockInfo.socketId)
this.stream.addCloseCallback(this.onStreamClose.bind(this))
this.sockInfo = sockInfo
this.connecting = true
var host = this.getHost()
var port = this.getPort()
//console.log('connecting to',host,port)
chrome.sockets.tcp.setPaused( sockInfo.socketId, true, function() {
chrome.sockets.tcp.connect( sockInfo.socketId, host, port, _.bind(this.onConnect, this) )
}.bind(this))
},
onConnect: function(result) {
//console.log('connected to',this.getHost())
var lasterr = chrome.runtime.lastError
if (this.closed) { return }
this.connecting = false
if (this.timedOut) {
return
} else if (lasterr) {
this.error({error:lasterr.message})
} else if (result < 0) {
this.error({error:'connection error',
code:result})
} else {
if (this.uri.protocol == 'https:' && ! this.secured) {
this.secured = true
//console.log('securing socket',this.sockInfo.socketId)
chrome.sockets.tcp.secure(this.sockInfo.socketId, this.onConnect.bind(this))
return
}
var headers = this.createRequestHeaders()
//console.log('request to',this.getHost(),headers)
this.stream.writeBuffer.add( new TextEncoder('utf-8').encode(headers).buffer )
if (this.requestBody) {
this.stream.writeBuffer.add( this.requestBody )
this.requestBody = null
}
this.stream.tryWrite()
this.stream.readUntil('\r\n\r\n', this.onHeaders.bind(this))
chrome.sockets.tcp.setPaused( this.sockInfo.socketId, false, function(){})
}
},
getHost: function() {
return this.uri.hostname
},
getPort: function() {
if (this.uri.protocol == 'https:') {
return parseInt(this.uri.port) || 443
} else {
return parseInt(this.uri.port) || 80
}
},
onHeaders: function(data) {
// not sure what encoding for headers is exactly, latin1 or something? whatever.
var headers = WSC.ui82str(new Uint8Array(data))
//console.log('found http tracker response headers', headers)
this.headersReceived = true
this.responseHeaders = headers
var response = parseHeaders(this.responseHeaders)
this.responseDataParsed = response
this.responseHeadersParsed = response.headers
//console.log(this.getHost(),'parsed http response headers',response)
this.responseLength = parseInt(response.headers['content-length'])
this.responseBytesRead = this.stream.readBuffer.size()
if (response.headers['transfer-encoding'] &&
response.headers['transfer-encoding'] == 'chunked') {
this.chunks = new WSC.Buffer
//console.log('looking for an \\r\\n')
this.stream.readUntil("\r\n", this.getNewChunk.bind(this))
//this.error('chunked encoding')
} else {
if (! response.headers['content-length']) {
this.error("no content length in response")
} else {
//console.log('read bytes',this.responseLength)
this.stream.readBytes(this.responseLength, this.onBody.bind(this))
}
}
},
onChunkDone: function(data) {
this.chunks.add(data)
this.stream.readUntil("\r\n", this.getNewChunk.bind(this))
},
getNewChunk: function(data) {
var s = WSC.ui82str(new Uint8Array(data.slice(0,data.byteLength-2)))
var len = parseInt(s,16)
if (isNaN(len)) {
this.error('invalid chunked encoding response')
return
}
//console.log('looking for new chunk of len',len)
if (len == 0) {
//console.log('got all chunks',this.chunks)
var body = this.chunks.flatten()
this.onBody(body)
} else {
this.stream.readBytes(len+2, this.onChunkDone.bind(this))
}
},
onBody: function(body) {
this.responseBody = body
var evt = {target: {headers:this.responseDataParsed.headers,
code:this.responseDataParsed.code, /* code is wrong, should be status */
status:this.responseDataParsed.code,
responseHeaders:this.responseHeaders,
responseHeadersParsed:this.responseHeadersParsed,
response:body}
}
if (this.responseType && this.responseType.toLowerCase() == 'xml') {
evt.target.responseXML = (new DOMParser).parseFromString(new TextDecoder('utf-8').decode(body), "text/xml")
}
this.onload(evt)
this._finished = true
if (! this.stream.closed) { this.stream.close() }
// all done!!! (close connection...)
}
}
function parseHeaders(s) {
var lines = s.split('\r\n')
var firstLine = lines[0].split(/ +/)
var proto = firstLine[0]
var code = firstLine[1]
var status = firstLine.slice(2,firstLine.length).join(' ')
var headers = {}
for (var i=1; i<lines.length; i++) {
var line = lines[i]
if (line) {
var j = line.indexOf(':')
var key = line.slice(0,j).toLowerCase()
headers[key] = line.slice(j+1,line.length).trim()
}
}
return {code: code,
status: status,
proto: proto,
headers: headers}
}
WSC.ChromeSocketXMLHttpRequest = ChromeSocketXMLHttpRequest
window.testxhr = function() {
console.log('creating XHR')
var xhr = new ChromeSocketXMLHttpRequest
xhr.open("GET","https://www.google.com")
xhr.timeout = 8000
xhr.onload = xhr.onerror = xhr.ontimeout = function(evt) {
console.log('xhr result:',evt)
}
xhr.send()
window.txhr = xhr
}
})();