@@ -332,7 +332,14 @@ case class HttpRequest(
332
332
* @param parser function to process the response body InputStream
333
333
*/
334
334
def exec [T ](parser : (Int , Map [String , IndexedSeq [String ]], InputStream ) => T ): HttpResponse [T ] = {
335
- val urlToFetch : URL = new URL (urlBuilder(this ))
335
+ doConnection(parser, new URL (urlBuilder(this )), connectFunc)
336
+ }
337
+
338
+ private def doConnection [T ](
339
+ parser : (Int , Map [String , IndexedSeq [String ]], InputStream ) => T ,
340
+ urlToFetch : URL ,
341
+ connectFunc : (HttpRequest , HttpURLConnection ) => Unit
342
+ ): HttpResponse [T ] = {
336
343
proxyConfig.map(urlToFetch.openConnection).getOrElse(urlToFetch.openConnection) match {
337
344
case conn : HttpURLConnection =>
338
345
conn.setInstanceFollowRedirects(false )
@@ -365,16 +372,23 @@ case class HttpRequest(
365
372
val responseCode : Int = conn.getResponseCode
366
373
val headers : Map [String , IndexedSeq [String ]] = getResponseHeaders(conn)
367
374
val encoding : Option [String ] = headers.get(" Content-Encoding" ).flatMap(_.headOption)
368
- val body : T = {
369
- val shouldDecompress = compress && inputStream != null
370
- val theStream = if (shouldDecompress && encoding.exists(_.equalsIgnoreCase(" gzip" ))) {
371
- new GZIPInputStream (inputStream)
372
- } else if (shouldDecompress && encoding.exists(_.equalsIgnoreCase(" deflate" ))) {
373
- new InflaterInputStream (inputStream)
374
- } else inputStream
375
- parser(responseCode, headers, theStream)
375
+ // HttpURLConnection won't redirect from https <-> http, so we handle manually here
376
+ (if (conn.getInstanceFollowRedirects && (responseCode == 301 || responseCode == 302 )) {
377
+ headers.get(" Location" ).flatMap(_.headOption).map(location => {
378
+ doConnection(parser, new URL (location), DefaultConnectFunc )
379
+ })
380
+ } else None ).getOrElse{
381
+ val body : T = {
382
+ val shouldDecompress = compress && inputStream != null
383
+ val theStream = if (shouldDecompress && encoding.exists(_.equalsIgnoreCase(" gzip" ))) {
384
+ new GZIPInputStream (inputStream)
385
+ } else if (shouldDecompress && encoding.exists(_.equalsIgnoreCase(" deflate" ))) {
386
+ new InflaterInputStream (inputStream)
387
+ } else inputStream
388
+ parser(responseCode, headers, theStream)
389
+ }
390
+ HttpResponse [T ](body, responseCode, headers)
376
391
}
377
- HttpResponse [T ](body, responseCode, headers)
378
392
}
379
393
380
394
private def getResponseHeaders (conn : HttpURLConnection ): Map [String , IndexedSeq [String ]] = {
0 commit comments