Properly isolate http session handler

This commit is contained in:
lash 2025-01-11 08:33:52 +00:00
parent fd6e5caf53
commit 977d14c529
Signed by untrusted user who does not match committer: lash
GPG Key ID: 21D2E7BB88C2A746
4 changed files with 28 additions and 44 deletions

View File

@ -3,8 +3,6 @@ package request
import ( import (
"context" "context"
"io" "io"
"net/http"
"strconv"
"git.defalsify.org/vise.git/resource" "git.defalsify.org/vise.git/resource"
"git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/persist"
@ -27,7 +25,6 @@ type RequestSession struct {
Continue bool Continue bool
} }
// TODO: seems like can remove this. // TODO: seems like can remove this.
type RequestParser interface { type RequestParser interface {
GetSessionId(ctx context.Context, rq any) (string, error) GetSessionId(ctx context.Context, rq any) (string, error)
@ -43,24 +40,3 @@ type RequestHandler interface {
Reset(rs RequestSession) (RequestSession, error) Reset(rs RequestSession) (RequestSession, error)
Shutdown() Shutdown()
} }
type SessionHandler struct {
RequestHandler
}
func (f *SessionHandler) WriteError(w http.ResponseWriter, code int, err error) {
s := err.Error()
w.Header().Set("Content-Length", strconv.Itoa(len(s)))
w.WriteHeader(code)
_, err = w.Write([]byte(s))
if err != nil {
logg.Errorf("error writing error!!", "err", err, "olderr", s)
w.WriteHeader(500)
}
}
func ToSessionHandler(h RequestHandler) *SessionHandler {
return &SessionHandler{
RequestHandler: h,
}
}

View File

@ -5,19 +5,21 @@ import (
"strconv" "strconv"
"git.defalsify.org/vise.git/logging" "git.defalsify.org/vise.git/logging"
"git.grassecon.net/grassrootseconomics/visedriver/internal/handlers"
"git.grassecon.net/grassrootseconomics/visedriver/request" "git.grassecon.net/grassrootseconomics/visedriver/request"
"git.grassecon.net/grassrootseconomics/visedriver/errors"
) )
var ( var (
logg = logging.NewVanilla().WithDomain("httpserver") logg = logging.NewVanilla().WithDomain("visedriver.http.session")
) )
type SessionHandler request.SessionHandler
// TODO: duplicated // HTTPSessionHandler implements the session handler for HTTP
func (f *SessionHandler) WriteError(w http.ResponseWriter, code int, err error) { type HTTPSessionHandler struct {
request.RequestHandler
}
func (f *HTTPSessionHandler) WriteError(w http.ResponseWriter, code int, err error) {
s := err.Error() s := err.Error()
w.Header().Set("Content-Length", strconv.Itoa(len(s))) w.Header().Set("Content-Length", strconv.Itoa(len(s)))
w.WriteHeader(code) w.WriteHeader(code)
@ -28,7 +30,13 @@ func (f *SessionHandler) WriteError(w http.ResponseWriter, code int, err error)
} }
} }
func (f *SessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { func NewHTTPSessionHandler(h request.RequestHandler) *HTTPSessionHandler {
return &HTTPSessionHandler{
RequestHandler: h,
}
}
func (hh *HTTPSessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var code int var code int
var err error var err error
var perr error var perr error
@ -38,48 +46,48 @@ func (f *SessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
Writer: w, Writer: w,
} }
rp := f.GetRequestParser() rp := hh.GetRequestParser()
cfg := f.GetConfig() cfg := hh.GetConfig()
cfg.SessionId, err = rp.GetSessionId(req.Context(), req) cfg.SessionId, err = rp.GetSessionId(req.Context(), req)
if err != nil { if err != nil {
logg.ErrorCtxf(rqs.Ctx, "", "header processing error", err) logg.ErrorCtxf(rqs.Ctx, "", "header processing error", err)
f.WriteError(w, 400, err) hh.WriteError(w, 400, err)
} }
rqs.Config = cfg rqs.Config = cfg
rqs.Input, err = rp.GetInput(req) rqs.Input, err = rp.GetInput(req)
if err != nil { if err != nil {
logg.ErrorCtxf(rqs.Ctx, "", "header processing error", err) logg.ErrorCtxf(rqs.Ctx, "", "header processing error", err)
f.WriteError(w, 400, err) hh.WriteError(w, 400, err)
return return
} }
rqs, err = f.Process(rqs) rqs, err = hh.Process(rqs)
switch err { switch err {
case handlers.ErrStorage: case errors.ErrStorage:
code = 500 code = 500
case handlers.ErrEngineInit: case errors.ErrEngineInit:
code = 500 code = 500
case handlers.ErrEngineExec: case errors.ErrEngineExec:
code = 500 code = 500
default: default:
code = 200 code = 200
} }
if code != 200 { if code != 200 {
f.WriteError(w, 500, err) hh.WriteError(w, 500, err)
return return
} }
w.WriteHeader(200) w.WriteHeader(200)
w.Header().Set("Content-Type", "text/plain") w.Header().Set("Content-Type", "text/plain")
rqs, err = f.Output(rqs) rqs, err = hh.Output(rqs)
rqs, perr = f.Reset(rqs) rqs, perr = hh.Reset(rqs)
if err != nil { if err != nil {
f.WriteError(w, 500, err) hh.WriteError(w, 500, err)
return return
} }
if perr != nil { if perr != nil {
f.WriteError(w, 500, perr) hh.WriteError(w, 500, perr)
return return
} }
} }