Skip to content

Commit

Permalink
Disable body reading on head requests (#95)
Browse files Browse the repository at this point in the history
Fixes #37

Review by @lolgab
  • Loading branch information
lihaoyi authored Dec 10, 2021
1 parent e983d00 commit c65a2be
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 5 additions & 4 deletions requests/src/requests/Requester.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ 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

Expand Down Expand Up @@ -283,7 +281,6 @@ case class Requester(verb: String,

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
Expand Down Expand Up @@ -333,7 +330,11 @@ case class Requester(verb: String,
else connection.getErrorStream

def processWrappedStream[V](f: java.io.InputStream => V): V = {
if (stream != null) {
// 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 (verb == "HEAD") f(new ByteArrayInputStream(Array()))
else if (stream != null) {
try f(
if (deGzip) new GZIPInputStream(stream)
else if (deDeflate) new InflaterInputStream(stream)
Expand Down
8 changes: 8 additions & 0 deletions requests/test/src/requests/RequestTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -260,5 +260,13 @@ object RequestTests extends TestSuite{
)
assert(res.statusCode == 200)
}
test("gzipError"){
val response = requests.head("https://api.github.com/users/lihaoyi")
assert(response.statusCode == 200)
assert(response.statusMessage == "OK")
assert(response.data.array.isEmpty)
assert(response.headers.keySet.map(_.toLowerCase).contains("content-length"))
assert(response.headers.keySet.map(_.toLowerCase).contains("content-type"))
}
}
}

0 comments on commit c65a2be

Please sign in to comment.