~elektito/hodhod

254c57e5e17411efc3676a9bef347253994d12a7 — Mostafa Razavi 1 year, 3 months ago 4b698dc
Update request logging
5 files changed, 25 insertions(+), 9 deletions(-)

M main.go
M pkg/hodhod/cgi.go
M pkg/hodhod/error_resp.go
M pkg/hodhod/response.go
M pkg/hodhod/static.go
M main.go => main.go +6 -5
@@ 78,8 78,6 @@ func getResponseForRequest(req hodhod.Request, cfg *hodhod.Config) (resp hodhod.
func handleConn(conn net.Conn, cfg *hodhod.Config) {
	defer conn.Close()

	log.Println("Accepted connection.")

	err := conn.SetDeadline(time.Now().Add(ConnectionTimeout))
	if err != nil {
		log.Println("Error setting connection deadline:", err)


@@ 96,16 94,17 @@ func handleConn(conn net.Conn, cfg *hodhod.Config) {
	}

	urlStr := s.Text()
	log.Println("Request:", urlStr)

	urlParsed, err := url.Parse(urlStr)
	if err != nil {
		log.Printf("Request: remote=%s resp=59 url=%s\n", conn.RemoteAddr().String(), urlStr)
		conn.Write([]byte("59 Bad Request\r\n"))
		return
	}

	tlsConn := conn.(*tls.Conn)
	if tlsConn.ConnectionState().ServerName != urlParsed.Hostname() {
		log.Printf("Request: remote=%s resp=53 url=%s\n", conn.RemoteAddr().String(), urlStr)
		conn.Write([]byte("53 URL hostname does not match SNI\r\n"))
		return
	}


@@ 116,6 115,7 @@ func handleConn(conn net.Conn, cfg *hodhod.Config) {
	}
	resp, err := getResponseForRequest(req, cfg)
	if errors.Is(err, ErrNotFound{}) {
		log.Printf("Request: remote=%s backend=none resp=51 url=%s\n", conn.RemoteAddr().String(), urlStr)
		conn.Write([]byte("51 Not Found\r\n"))
		return
	} else if err != nil {


@@ 123,8 123,11 @@ func handleConn(conn net.Conn, cfg *hodhod.Config) {
		return
	}

	log.Printf("Request: remote=%s backend=%s url=%s\n", conn.RemoteAddr().String(), resp.Backend(), urlStr)

	err = resp.Init(&req)
	if err != nil {
		log.Printf("Request: remote=%s resp=40 url=%s\n", conn.RemoteAddr().String(), urlStr)
		conn.Write([]byte("40 Internal error\r\n"))
		return
	}


@@ 164,8 167,6 @@ func handleConn(conn net.Conn, cfg *hodhod.Config) {
	}()

	wg.Wait()

	log.Println("Closed connection.")
}

func loadCertificates(cfg *hodhod.Config) (certs []tls.Certificate, err error) {

M pkg/hodhod/cgi.go => pkg/hodhod/cgi.go +8 -4
@@ 35,7 35,11 @@ func cgiError(exitCode int) CgiError {
	}
}

func (resp CgiResponse) Init(req *Request) (err error) {
func (resp *CgiResponse) Backend() string {
	return "cgi"
}

func (resp *CgiResponse) Init(req *Request) (err error) {
	reqLine := []byte(req.Url.String())
	reqLine = append(reqLine, '\r', '\n')
	_, err = resp.stdin.Write(reqLine)


@@ 43,7 47,7 @@ func (resp CgiResponse) Init(req *Request) (err error) {
	return
}

func (resp CgiResponse) Read(p []byte) (n int, err error) {
func (resp *CgiResponse) Read(p []byte) (n int, err error) {
	if resp.cmd.ProcessState != nil {
		if resp.cmd.ProcessState.ExitCode() != 0 {
			err = cgiError(resp.cmd.ProcessState.ExitCode())


@@ 58,7 62,7 @@ func (resp CgiResponse) Read(p []byte) (n int, err error) {
	return
}

func (resp CgiResponse) Close() {
func (resp *CgiResponse) Close() {
	if resp.cmd.ProcessState == nil {
		resp.cancelScript()
	}


@@ 134,7 138,7 @@ func NewCgiResp(req Request, scriptPath string, cfg *Config) (resp Response) {
		}
	}()

	resp = CgiResponse{
	resp = &CgiResponse{
		cmd:          cmd,
		stdin:        wStdin,
		stdout:       rStdout,

M pkg/hodhod/error_resp.go => pkg/hodhod/error_resp.go +4 -0
@@ 11,6 11,10 @@ type ErrorResponse struct {
	returnedStatusLine bool
}

func (resp *ErrorResponse) Backend() string {
	return "error"
}

func (resp *ErrorResponse) Init(req *Request) (err error) {
	return
}

M pkg/hodhod/response.go => pkg/hodhod/response.go +3 -0
@@ 14,6 14,9 @@ type Response interface {

	// Release any resources related to this response
	Close()

	// Returns the backend name for this response
	Backend() string
}

var _ io.Reader = Response(nil)

M pkg/hodhod/static.go => pkg/hodhod/static.go +4 -0
@@ 13,6 13,10 @@ type StaticResponse struct {
	returnedStatusLine bool
}

func (resp *StaticResponse) Backend() string {
	return "static"
}

func (resp *StaticResponse) Init(req *Request) (err error) {
	return
}