Skip to content

Commit

Permalink
fix: also clear precondition specifying headers from request
Browse files Browse the repository at this point in the history
  • Loading branch information
Lordfirespeed committed Oct 31, 2024
1 parent edb978b commit 52ae1f8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/proud-chairs-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@otterhttp/response": patch
---

also clear precondition specifying headers from request
17 changes: 11 additions & 6 deletions packages/response/src/preconditions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { noneMatch } from "./if-none-match"
import { rangePrecondition } from "./if-range"
import { isUnmodifiedSince } from "./if-unmodified-since"

function clearPreconditionHeadersFromResponse(response: HasOutgoingHeaders) {
function clearPreconditionHeaders(request: HasIncomingHeaders, response: HasOutgoingHeaders) {
request.headers["if-match"] = undefined
request.headers["if-unmodified-since"] = undefined
request.headers["if-none-match"] = undefined
request.headers["if-modified-since"] = undefined
response.removeHeader("etag")
response.removeHeader("last-modified")
}
Expand Down Expand Up @@ -49,7 +53,8 @@ export function validatePreconditions(
if (ifMatch != null) {
if (!Object.hasOwnProperty.call(current, "etag")) current.etag = res.getHeader("etag")
if (!someMatch(current.etag, ifMatch)) {
clearPreconditionHeadersFromResponse(res)
res.req.headers["if-match"] = undefined
clearPreconditionHeaders(res.req, res)
throw new ClientError("If-Match Precondition Failed", {
statusCode: HttpStatus.PreconditionFailed,
code: "ERR_PRECONDITION_FAILED_IF_MATCH",
Expand All @@ -66,7 +71,7 @@ export function validatePreconditions(
if (ifMatch == null && ifUnmodifiedSince != null) {
if (!Object.hasOwnProperty.call(current, "lastModified")) current.lastModified = res.getHeader("last-modified")
if (!isUnmodifiedSince(current.lastModified, ifUnmodifiedSince)) {
clearPreconditionHeadersFromResponse(res)
clearPreconditionHeaders(res.req, res)
throw new ClientError("If-Unmodified-Since Precondition Failed", {
statusCode: HttpStatus.PreconditionFailed,
code: "ERR_PRECONDITION_FAILED_IF_UNMODIFIED_SINCE",
Expand All @@ -84,15 +89,15 @@ export function validatePreconditions(
const failed = !noneMatch(current.etag, ifNoneMatch)

if (failed && (method === "GET" || method === "HEAD")) {
clearPreconditionHeadersFromResponse(res)
clearPreconditionHeaders(res.req, res)
throw new NotModifiedError("If-None-Match Precondition Failed", {
code: "ERR_PRECONDITION_FAILED_IF_NONE_MATCH",
expected: true,
})
}

if (failed) {
clearPreconditionHeadersFromResponse(res)
clearPreconditionHeaders(res.req, res)
throw new ClientError("If-None-Match Precondition Failed", {
statusCode: HttpStatus.PreconditionFailed,
code: "ERR_PRECONDITION_FAILED_IF_NONE_MATCH",
Expand All @@ -109,7 +114,7 @@ export function validatePreconditions(
if ((method === "GET" || method === "HEAD") && ifNoneMatch == null && ifModifiedSince != null) {
if (!Object.hasOwnProperty.call(current, "lastModified")) current.lastModified = res.getHeader("last-modified")
if (!hasBeenModifiedSince(current.lastModified, ifModifiedSince)) {
clearPreconditionHeadersFromResponse(res)
clearPreconditionHeaders(res.req, res)
throw new NotModifiedError("If-Modified-Since Precondition Failed", {
code: "ERR_PRECONDITION_FAILED_IF_MODIFIED_SINCE",
expected: true,
Expand Down

0 comments on commit 52ae1f8

Please sign in to comment.