Skip to content

Commit

Permalink
fix: removes the content length header on phase 3 interruption so cad…
Browse files Browse the repository at this point in the history
…dy does not fail as per caddyserver/caddy#5952.
  • Loading branch information
jcchavezs committed Nov 27, 2023
1 parent cfa6aba commit 11fb227
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package coraza

import (
"errors"
"fmt"
"io"
"log"
Expand Down Expand Up @@ -43,6 +42,7 @@ func (i *rwInterceptor) WriteHeader(statusCode int) {
i.statusCode = statusCode

if it := i.tx.ProcessResponseHeaders(statusCode, i.proto); it != nil {
i.w.Header().Del("Content-Length")
i.statusCode = obtainStatusCodeFromInterruptionOrDefault(it, i.statusCode)
i.flushWriteHeader()
return
Expand Down Expand Up @@ -74,7 +74,11 @@ func (i *rwInterceptor) Write(b []byte) (int, error) {
// if there is an interruption it must be from at least phase 4 and hence
// WriteHeader or Write should have been called and hence the status code
// has been flushed to the delegated response writer.
return 0, errors.New("response writer is interrupted")
//
// We return the number of bytes as according to the interface io.Writer
// if we don't return an error, the number of bytes written is len(p).
// See https://pkg.go.dev/io#Writer
return len(b), nil
}

if !i.wroteHeader {
Expand All @@ -90,7 +94,11 @@ func (i *rwInterceptor) Write(b []byte) (int, error) {
i.overrideWriteHeader(it.Status)
// We only flush the status code after an interruption.
i.flushWriteHeader()
return 0, errors.New("response writer is interrupted")

// We return the number of bytes as according to the interface io.Writer
// if we don't return an error, the number of bytes written is len(p).
// See https://pkg.go.dev/io#Writer
return len(b), nil
}
return n, err
}
Expand Down

0 comments on commit 11fb227

Please sign in to comment.