mirror of
https://github.com/openfaas/faas.git
synced 2025-06-22 23:03:24 +00:00
Remove Hyjacker log handler
**What** - Remove the hyjacker based logs handler implementation because it is not needed Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
This commit is contained in:
committed by
Alex Ellis
parent
e07a61fd0c
commit
db6628d1a5
@ -4,12 +4,10 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const crlf = "\r\n"
|
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 {
|
type writerFlusher interface {
|
||||||
io.Writer
|
io.Writer
|
||||||
http.Flusher
|
http.Flusher
|
||||||
@ -197,29 +108,3 @@ func copyNotify(destination io.Writer, source io.Reader) <-chan error {
|
|||||||
}()
|
}()
|
||||||
return done
|
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
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user