Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable body reading on head requests #95

Merged
merged 9 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))
}
}
}