Skip to content

Commit c65a2be

Browse files
authoredDec 10, 2021
Disable body reading on head requests (#95)
Fixes #37 Review by @lolgab
1 parent e983d00 commit c65a2be

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed
 

‎requests/src/requests/Requester.scala

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package requests
33
import java.io.{ByteArrayInputStream, ByteArrayOutputStream, OutputStream}
44
import java.net.{HttpCookie, HttpURLConnection, InetSocketAddress}
55
import java.util.zip.{GZIPInputStream, InflaterInputStream}
6-
76
import javax.net.ssl._
8-
97
import collection.JavaConverters._
108
import scala.collection.mutable
119

@@ -283,7 +281,6 @@ case class Requester(verb: String,
283281

284282
val deGzip = autoDecompress && headerFields.get("content-encoding").toSeq.flatten.exists(_.contains("gzip"))
285283
val deDeflate = autoDecompress && headerFields.get("content-encoding").toSeq.flatten.exists(_.contains("deflate"))
286-
287284
def persistCookies() = {
288285
if (sess.persistCookies) {
289286
headerFields
@@ -333,7 +330,11 @@ case class Requester(verb: String,
333330
else connection.getErrorStream
334331

335332
def processWrappedStream[V](f: java.io.InputStream => V): V = {
336-
if (stream != null) {
333+
// The HEAD method is identical to GET except that the server
334+
// MUST NOT return a message-body in the response.
335+
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html section 9.4
336+
if (verb == "HEAD") f(new ByteArrayInputStream(Array()))
337+
else if (stream != null) {
337338
try f(
338339
if (deGzip) new GZIPInputStream(stream)
339340
else if (deDeflate) new InflaterInputStream(stream)

‎requests/test/src/requests/RequestTests.scala

+8
Original file line numberDiff line numberDiff line change
@@ -260,5 +260,13 @@ object RequestTests extends TestSuite{
260260
)
261261
assert(res.statusCode == 200)
262262
}
263+
test("gzipError"){
264+
val response = requests.head("https://api.github.com/users/lihaoyi")
265+
assert(response.statusCode == 200)
266+
assert(response.statusMessage == "OK")
267+
assert(response.data.array.isEmpty)
268+
assert(response.headers.keySet.map(_.toLowerCase).contains("content-length"))
269+
assert(response.headers.keySet.map(_.toLowerCase).contains("content-type"))
270+
}
263271
}
264272
}

0 commit comments

Comments
 (0)
Please sign in to comment.