From db6628d1a532cf54a1e1a21d2dc6644a73d643f7 Mon Sep 17 00:00:00 2001 From: Lucas Roesler Date: Sat, 22 Jun 2019 21:30:40 +0200 Subject: [PATCH] Remove Hyjacker log handler **What** - Remove the hyjacker based logs handler implementation because it is not needed Signed-off-by: Lucas Roesler --- gateway/handlers/logs.go | 115 --------------------------------------- 1 file changed, 115 deletions(-) diff --git a/gateway/handlers/logs.go b/gateway/handlers/logs.go index d0a6e5fa..360eaf51 100644 --- a/gateway/handlers/logs.go +++ b/gateway/handlers/logs.go @@ -4,12 +4,10 @@ import ( "context" "io" "log" - "net" "net/http" "net/url" "os" "strings" - "time" ) const crlf = "\r\n" @@ -82,93 +80,6 @@ func NewLogHandlerFunc(logProvider url.URL) http.HandlerFunc { } } -// NewHijackLogHandlerFunc creates and http HandlerFunc from the supplied log Requestor. -// This remains in the package for reference purposes, providers should instead use NewLogHandlerFunc -func NewHijackLogHandlerFunc(logProvider url.URL) http.HandlerFunc { - - writeRequestURI := false - if _, exists := os.LookupEnv("write_request_uri"); exists { - writeRequestURI = exists - } - - upstreamLogProviderBase := strings.TrimSuffix(logProvider.String(), "/") - - return func(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() - if r.Body != nil { - defer r.Body.Close() - } - - logRequest := buildUpstreamRequest(r, upstreamLogProviderBase, upstreamLogsEndpoint) - if logRequest.Body != nil { - defer logRequest.Body.Close() - } - - hijacker, ok := w.(http.Hijacker) - if !ok { - log.Println("LogProxy: response is not a Hijacker, required for streaming response") - http.NotFound(w, r) - return - } - - clientConn, buf, err := hijacker.Hijack() - if err != nil { - log.Println("LogProxy: failed to hijack connection for streaming response") - return - } - - defer clientConn.Close() - // A zero value for t means Write will not time out, allowing us to stream the logs while - // following even if there is a large gap in logs - clientConn.SetWriteDeadline(time.Time{}) - // flush the headers and the initial 200 status code to the client - buf.Flush() - - if writeRequestURI { - log.Printf("LogProxy: proxying request to %s %s\n", logRequest.Host, logRequest.URL.String()) - } - - ctx, cancel := context.WithCancel(ctx) - logRequest = logRequest.WithContext(ctx) - defer cancel() - - logResp, err := http.DefaultTransport.RoundTrip(logRequest) - if err != nil { - log.Printf("LogProxy: forwarding request failed: %s\n", err.Error()) - buf.WriteString("HTTP/1.1 500 Server Error" + crlf) - buf.Flush() - return - } - // Body is always closeable if err is nil - defer logResp.Body.Close() - - // write response headers directly to the buffer per RFC 2616 - // https://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html - buf.WriteString("HTTP/1.1 " + logResp.Status + crlf) - logResp.Header.Write(buf) - buf.WriteString(crlf) - buf.Flush() - - // watch for connection closures and stream data - // connections and contexts should have cancel methods deferred already - select { - case err := <-copyNotify(buf, logResp.Body): - if err != nil { - log.Printf("LogProxy: error while copy: %s", err.Error()) - return - } - logResp.Trailer.Write(buf) - case err := <-closeNotify(ctx, clientConn): - if err != nil { - log.Printf("LogProxy: client connection closed: %s", err.Error()) - } - return - } - - return - } -} - type writerFlusher interface { io.Writer http.Flusher @@ -197,29 +108,3 @@ func copyNotify(destination io.Writer, source io.Reader) <-chan error { }() return done } - -// closeNotify will watch the connection and notify when then connection is closed -func closeNotify(ctx context.Context, c net.Conn) <-chan error { - notify := make(chan error, 1) - - go func() { - buf := make([]byte, 1) - // blocks until non-zero read or error. From the fd.Read docs: - // If the caller wanted a zero byte read, return immediately - // without trying (but after acquiring the readLock). - // Otherwise syscall.Read returns 0, nil which looks like - // io.EOF. - // It is important that `buf` is allocated a non-zero size - n, err := c.Read(buf) - if err != nil { - log.Printf("LogProxy: test connection: %s\n", err) - notify <- err - return - } - if n > 0 { - log.Printf("LogProxy: unexpected data: %s\n", buf[:n]) - return - } - }() - return notify -}