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 6 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
13 changes: 9 additions & 4 deletions requests/src/requests/Requester.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package requests

import requests.Requester.methodField
Copy link
Member

@lolgab lolgab Dec 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a spurious change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, will remove


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 @@ -34,6 +34,8 @@ trait BaseSession{
lazy val options = Requester("OPTIONS", this)
// unofficial
lazy val patch = Requester("PATCH", this)

def send(method: String) = Requester(method, this)
}

object BaseSession{
Expand Down Expand Up @@ -281,7 +283,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 @@ -331,7 +332,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
21 changes: 21 additions & 0 deletions requests/test/src-2/requests/Scala2RequestTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package requests

import utest._
import ujson._

object Scala2RequestTests extends TestSuite{
val tests = Tests{
test("send"){
requests.send("get")("https://httpbin.org/get?hello=world&foo=baz")

// This doesn't compile right in Scala3 for some reason
val res1 = requests.send("put")(
"https://httpbin.org/put",
data = Map("hello" -> "world", "foo" -> "baz"),
chunkedUpload = true
).text

assert(read(res1).obj("form") == Obj("foo" -> "baz", "hello" -> "world"))
}
}
}
8 changes: 8 additions & 0 deletions requests/test/src/requests/RequestTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -280,5 +280,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.contains("content-length"))
assert(response.headers.keySet.contains("content-type"))
}
}
}