~emersion/kimchi

f43d2f1dceed591bfb6358e3cfdd008d3179cd2a — delthas 2 months ago b5c6ce5
Support Hijack, Flush when using access logs

This fixes the following error when using WebSockets with access logs
enabled:

    http: proxy error: can't switch protocols using non-Hijacker ResponseWriter type *main.interceptRW

We also add an Unwrap method for upcoming (Go 1.20) ResponseController
support.
1 files changed, 25 insertions(+), 0 deletions(-)

M interceptor.go
M interceptor.go => interceptor.go +25 -0
@@ 1,6 1,9 @@
package main

import (
	"bufio"
	"fmt"
	"net"
	"net/http"
)



@@ 10,6 13,28 @@ type interceptRW struct {
	size   int
}

var (
	_ http.Flusher  = (*interceptRW)(nil)
	_ http.Hijacker = (*interceptRW)(nil)
)

func (w *interceptRW) Unwrap() http.ResponseWriter {
	return w.ResponseWriter
}

func (w *interceptRW) Flush() {
	if f, ok := w.ResponseWriter.(http.Flusher); ok {
		f.Flush()
	}
}

func (w *interceptRW) Hijack() (net.Conn, *bufio.ReadWriter, error) {
	if h, ok := w.ResponseWriter.(http.Hijacker); ok {
		return h.Hijack()
	}
	return nil, nil, fmt.Errorf("connection does not support hijacking")
}

func (w *interceptRW) WriteHeader(status int) {
	w.status = status
	w.ResponseWriter.WriteHeader(status)