Skip to content

Commit

Permalink
Fix handling of HTTP 304 (#159)
Browse files Browse the repository at this point in the history
#105

304 is not a redirect, so it does not have a `location` header like
other 3xx return codes, and is not a failure

Added a unit test to confirm the fix
  • Loading branch information
lihaoyi authored May 30, 2024
1 parent 335eb4c commit b6ade99
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions requests/src/requests/Requester.scala
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,9 @@ case class Requester(verb: String,
}
}

if (responseCode.toString.startsWith("3") && maxRedirects > 0){
if (responseCode.toString.startsWith("3") &&
responseCode.toString != "304" &&
maxRedirects > 0){
val out = new ByteArrayOutputStream()
Util.transferTo(connection.getInputStream, out)
val bytes = out.toByteArray
Expand Down Expand Up @@ -344,7 +346,7 @@ case class Requester(verb: String,
}
}

if (streamHeaders.is2xx || !check) processWrappedStream(f)
if (streamHeaders.statusCode == 304 || streamHeaders.is2xx || !check) processWrappedStream(f)
else {
val errorOutput = new ByteArrayOutputStream()
processWrappedStream(geny.Internal.transfer(_, errorOutput))
Expand Down
4 changes: 4 additions & 0 deletions requests/test/src/requests/RequestTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ object RequestTests extends TestSuite{
}
}

test("test_reproduction"){
requests.get("http://httpbin.org/status/304").text()

}
test("streaming"){
val res1 = requests.get("http://httpbin.org/stream/5").text()
assert(res1.linesIterator.length == 5)
Expand Down

0 comments on commit b6ade99

Please sign in to comment.