/
Requester.scala
427 lines (393 loc) · 16.1 KB
/
Requester.scala
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package requests
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, OutputStream}
import java.net.{HttpCookie, HttpURLConnection, InetSocketAddress}
import java.util.zip.{GZIPInputStream, InflaterInputStream}
import javax.net.ssl._
import collection.JavaConverters._
import scala.collection.mutable
trait BaseSession{
def headers: Map[String, String]
def cookies: mutable.Map[String, HttpCookie]
def readTimeout: Int
def connectTimeout: Int
def auth: RequestAuth
def proxy: (String, Int)
def cert: Cert
def sslContext: SSLContext
def maxRedirects: Int
def persistCookies: Boolean
def verifySslCerts: Boolean
def autoDecompress: Boolean
def compress: Compress
def chunkedUpload: Boolean
def check: Boolean
lazy val get = Requester("GET", this)
lazy val post = Requester("POST", this)
lazy val put = Requester("PUT", this)
lazy val delete = Requester("DELETE", this)
lazy val head = Requester("HEAD", this)
lazy val options = Requester("OPTIONS", this)
// unofficial
lazy val patch = Requester("PATCH", this)
def send(method: String) = Requester(method, this)
}
object BaseSession{
val defaultHeaders = Map(
"User-Agent" -> "requests-scala",
"Accept-Encoding" -> "gzip, deflate",
"Connection" -> "keep-alive",
"Accept" -> "*/*"
)
}
object Requester{
val officialHttpMethods = Set("GET", "POST", "HEAD", "OPTIONS", "PUT", "DELETE", "TRACE")
private lazy val methodField: java.lang.reflect.Field = {
val m = classOf[HttpURLConnection].getDeclaredField("method")
m.setAccessible(true)
m
}
}
case class Requester(verb: String,
sess: BaseSession){
private val upperCaseVerb = verb.toUpperCase
/**
* Makes a single HTTP request, and returns a [[Response]] object. Requires
* all uploaded request `data` to be provided up-front, and aggregates all
* downloaded response `data` before returning it in the response. If you
* need streaming access to the upload and download, use the [[Requester.stream]]
* function instead.
*
* @param url The URL to which you want to make this HTTP request
* @param auth HTTP authentication you want to use with this request; defaults to none
* @param params URL params to pass to this request, for GETs and DELETEs
* @param headers Custom headers to use, in addition to the defaults
* @param data Body data to pass to this request, for POSTs and PUTs. Can be a
* Map[String, String] of form data, bulk data as a String or Array[Byte],
* or MultiPart form data.
* @param readTimeout How long to wait for data to be read before timing out
* @param connectTimeout How long to wait for a connection before timing out
* @param proxy Host and port of a proxy you want to use
* @param cert Client certificate configuration
* @param sslContext Client sslContext configuration
* @param cookies Custom cookies to send up with this request
* @param maxRedirects How many redirects to automatically resolve; defaults to 5.
* You can also set it to 0 to prevent Requests from resolving
* redirects for you
* @param verifySslCerts Set this to false to ignore problems with SSL certificates
* @param check Throw an exception on a 4xx or 5xx response code. Defaults to `true`
*/
def apply(url: String,
auth: RequestAuth = sess.auth,
params: Iterable[(String, String)] = Nil,
headers: Iterable[(String, String)] = Nil,
data: RequestBlob = RequestBlob.EmptyRequestBlob,
readTimeout: Int = sess.readTimeout,
connectTimeout: Int = sess.connectTimeout,
proxy: (String, Int) = sess.proxy,
cert: Cert = sess.cert,
sslContext: SSLContext = sess.sslContext,
cookies: Map[String, HttpCookie] = Map(),
cookieValues: Map[String, String] = Map(),
maxRedirects: Int = sess.maxRedirects,
verifySslCerts: Boolean = sess.verifySslCerts,
autoDecompress: Boolean = sess.autoDecompress,
compress: Compress = sess.compress,
keepAlive: Boolean = true,
check: Boolean = sess.check,
chunkedUpload: Boolean = sess.chunkedUpload): Response = {
val out = new ByteArrayOutputStream()
var streamHeaders: StreamHeaders = null
val w = stream(
url, auth, params, data.headers, headers, data, readTimeout,
connectTimeout, proxy, cert, sslContext, cookies, cookieValues, maxRedirects,
verifySslCerts, autoDecompress, compress, keepAlive, check, chunkedUpload,
onHeadersReceived = sh => streamHeaders = sh
)
w.writeBytesTo(out)
Response(
streamHeaders.url,
streamHeaders.statusCode,
streamHeaders.statusMessage,
new geny.Bytes(out.toByteArray),
streamHeaders.headers,
streamHeaders.history
)
}
/**
* Performs a streaming HTTP request. Most of the parameters are the same as
* [[apply]], except that the `data` parameter is missing, and no [[Response]]
* object is returned. Instead, the caller gets access via three callbacks
* (described below). This provides a lower-level API than [[Requester.apply]],
* allowing the caller fine-grained access to the upload/download streams
* so they can direct them where-ever necessary without first aggregating all
* the data into memory.
*
* @param onHeadersReceived the second callback to be called, this provides
* access to the response's status code, status
* message, headers, and any previous re-direct
* responses. Returns a boolean, where `false` can
* be used to
*
* @return a `Writable` that can be used to write the output data to any
* destination
*/
def stream(url: String,
auth: RequestAuth = sess.auth,
params: Iterable[(String, String)] = Nil,
blobHeaders: Iterable[(String, String)] = Nil,
headers: Iterable[(String, String)] = Nil,
data: RequestBlob = RequestBlob.EmptyRequestBlob,
readTimeout: Int = sess.readTimeout,
connectTimeout: Int = sess.connectTimeout,
proxy: (String, Int) = sess.proxy,
cert: Cert = sess.cert,
sslContext: SSLContext = sess.sslContext,
cookies: Map[String, HttpCookie] = Map(),
cookieValues: Map[String, String] = Map(),
maxRedirects: Int = sess.maxRedirects,
verifySslCerts: Boolean = sess.verifySslCerts,
autoDecompress: Boolean = sess.autoDecompress,
compress: Compress = sess.compress,
keepAlive: Boolean = true,
check: Boolean = true,
chunkedUpload: Boolean = false,
redirectedFrom: Option[Response] = None,
onHeadersReceived: StreamHeaders => Unit = null): geny.Readable = new geny.Readable {
def readBytesThrough[T](f: java.io.InputStream => T): T = {
val url0 = new java.net.URL(url)
val url1 = if (params.nonEmpty) {
val encodedParams = Util.urlEncode(params)
val firstSep = if (url0.getQuery != null) "&" else "?"
new java.net.URL(url + firstSep + encodedParams)
} else url0
var connection: HttpURLConnection = null
try {
val conn =
if (proxy == null) url1.openConnection
else {
val (ip, port) = proxy
val p = new java.net.Proxy(
java.net.Proxy.Type.HTTP, new InetSocketAddress(ip, port)
)
url1.openConnection(p)
}
connection = conn match{
case c: HttpsURLConnection =>
if (cert != null) {
c.setSSLSocketFactory(Util.clientCertSocketFactory(cert, verifySslCerts))
if (!verifySslCerts) c.setHostnameVerifier(new HostnameVerifier { def verify(h: String, s: SSLSession) = true })
} else if (sslContext != null) {
c.setSSLSocketFactory(sslContext.getSocketFactory)
if (!verifySslCerts) c.setHostnameVerifier(new HostnameVerifier { def verify(h: String, s: SSLSession) = true })
} else if (!verifySslCerts) {
c.setSSLSocketFactory(Util.noVerifySocketFactory)
c.setHostnameVerifier(new HostnameVerifier { def verify(h: String, s: SSLSession) = true })
}
c
case c: HttpURLConnection => c
}
connection.setInstanceFollowRedirects(false)
if (Requester.officialHttpMethods.contains(upperCaseVerb)) {
connection.setRequestMethod(upperCaseVerb)
} else {
// HttpURLConnection enforces a list of official http METHODs, but not everyone abides by the spec
// this hack allows us set an unofficial http method
connection match {
case cs: HttpsURLConnection =>
cs.getClass.getDeclaredFields.find(_.getName == "delegate").foreach{ del =>
del.setAccessible(true)
Requester.methodField.set(del.get(cs), upperCaseVerb)
}
case c =>
Requester.methodField.set(c, upperCaseVerb)
}
}
for((k, v) <- blobHeaders) connection.setRequestProperty(k, v)
for((k, v) <- sess.headers) connection.setRequestProperty(k, v)
for((k, v) <- headers) connection.setRequestProperty(k, v)
for((k, v) <- compress.headers) connection.setRequestProperty(k, v)
connection.setReadTimeout(readTimeout)
auth.header.foreach(connection.setRequestProperty("Authorization", _))
connection.setConnectTimeout(connectTimeout)
connection.setUseCaches(false)
connection.setDoOutput(true)
val sessionCookieValues = for{
c <- (sess.cookies ++ cookies).valuesIterator
if !c.hasExpired
if c.getDomain == null || c.getDomain == url1.getHost
if c.getPath == null || url1.getPath.startsWith(c.getPath)
} yield (c.getName, c.getValue)
val allCookies = sessionCookieValues ++ cookieValues
if (allCookies.nonEmpty){
connection.setRequestProperty(
"Cookie",
allCookies
.map{case (k, v) => s"""$k="$v""""}
.mkString("; ")
)
}
if (upperCaseVerb == "POST" || upperCaseVerb == "PUT" || upperCaseVerb == "PATCH" || upperCaseVerb == "DELETE") {
if (!chunkedUpload) {
val bytes = new ByteArrayOutputStream()
usingOutputStream(compress.wrap(bytes)) { os => data.write(os) }
val byteArray = bytes.toByteArray
connection.setFixedLengthStreamingMode(byteArray.length)
usingOutputStream(connection.getOutputStream) { os => os.write(byteArray) }
} else {
connection.setChunkedStreamingMode(0)
usingOutputStream(compress.wrap(connection.getOutputStream)) { os => data.write(os) }
}
}
val (responseCode, responseMsg, headerFields) = try {(
connection.getResponseCode,
connection.getResponseMessage,
connection.getHeaderFields.asScala
.filter(_._1 != null)
.map{case (k, v) => (k.toLowerCase(), v.asScala.toSeq)}.toMap
)} catch{
case e: java.net.SocketTimeoutException =>
throw new TimeoutException(url, readTimeout, connectTimeout)
case e: java.net.UnknownHostException =>
throw new UnknownHostException(url, e.getMessage)
case e: javax.net.ssl.SSLHandshakeException =>
throw new InvalidCertException(url, e)
}
val deGzip = autoDecompress && headerFields.get("content-encoding").toSeq.flatten.exists(_.contains("gzip"))
val deDeflate = autoDecompress && headerFields.get("content-encoding").toSeq.flatten.exists(_.contains("deflate"))
def persistCookies() = {
if (sess.persistCookies) {
headerFields
.get("set-cookie")
.iterator
.flatten
.flatMap(HttpCookie.parse(_).asScala)
.foreach(c => sess.cookies(c.getName) = c)
}
}
if (responseCode.toString.startsWith("3") && maxRedirects > 0){
val out = new ByteArrayOutputStream()
Util.transferTo(connection.getInputStream, out)
val bytes = out.toByteArray
val current = Response(
url,
responseCode,
responseMsg,
new geny.Bytes(bytes),
headerFields,
redirectedFrom
)
persistCookies()
val newUrl = current.headers("location").head
stream(
new java.net.URL(url1, newUrl).toString, auth, params, blobHeaders,
headers, data, readTimeout, connectTimeout, proxy, cert, sslContext, cookies,
cookieValues, maxRedirects - 1, verifySslCerts, autoDecompress,
compress, keepAlive, check, chunkedUpload, Some(current),
onHeadersReceived
).readBytesThrough(f)
}else{
persistCookies()
val streamHeaders = StreamHeaders(
url,
responseCode,
responseMsg,
headerFields,
redirectedFrom
)
if (onHeadersReceived != null) onHeadersReceived(streamHeaders)
val stream =
if (connection.getResponseCode.toString.startsWith("2")) connection.getInputStream
else connection.getErrorStream
def processWrappedStream[V](f: java.io.InputStream => V): V = {
// The HEAD method is identical to GET except that the server
// MUST NOT return a message-body in the response.
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html section 9.4
if (upperCaseVerb == "HEAD") f(new ByteArrayInputStream(Array()))
else if (stream != null) {
try f(
if (deGzip) new GZIPInputStream(stream)
else if (deDeflate) new InflaterInputStream(stream)
else stream
) finally if (!keepAlive) stream.close()
}else{
f(new ByteArrayInputStream(Array()))
}
}
if (streamHeaders.is2xx || !check) processWrappedStream(f)
else {
val errorOutput = new ByteArrayOutputStream()
processWrappedStream(geny.Internal.transfer(_, errorOutput))
throw new RequestFailedException(
Response(
streamHeaders.url,
streamHeaders.statusCode,
streamHeaders.statusMessage,
new geny.Bytes(errorOutput.toByteArray),
streamHeaders.headers,
streamHeaders.history
)
)
}
}
} finally if (!keepAlive && connection != null) {
connection.disconnect()
}
}
}
private def usingOutputStream[T](os: OutputStream)(fn: OutputStream => T): Unit =
try fn(os) finally os.close()
/**
* Overload of [[Requester.apply]] that takes a [[Request]] object as configuration
*/
def apply(r: Request, data: RequestBlob, chunkedUpload: Boolean): Response = apply(
r.url,
r.auth,
r.params,
r.headers,
data,
r.readTimeout,
r.connectTimeout,
r.proxy,
r.cert,
r.sslContext,
r.cookies,
r.cookieValues,
r.maxRedirects,
r.verifySslCerts,
r.autoDecompress,
r.compress,
r.keepAlive,
r.check,
chunkedUpload
)
/**
* Overload of [[Requester.stream]] that takes a [[Request]] object as configuration
*/
def stream(r: Request,
data: RequestBlob,
chunkedUpload: Boolean,
onHeadersReceived: StreamHeaders => Unit): geny.Writable = stream(
r.url,
r.auth,
r.params,
Seq.empty[(String, String)],
r.headers,
data,
r.readTimeout,
r.connectTimeout,
r.proxy,
r.cert,
r.sslContext,
r.cookies,
r.cookieValues,
r.maxRedirects,
r.verifySslCerts,
r.autoDecompress,
r.compress,
r.keepAlive,
r.check,
chunkedUpload,
None,
onHeadersReceived
)
}