2024-09-06 01:40:57 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2024-09-11 18:32:55 +02:00
|
|
|
"io/ioutil"
|
2024-09-06 01:40:57 +02:00
|
|
|
"net/http"
|
2024-09-12 04:30:23 +02:00
|
|
|
"strconv"
|
2024-09-06 01:40:57 +02:00
|
|
|
|
|
|
|
"git.defalsify.org/vise.git/logging"
|
2024-09-11 18:53:12 +02:00
|
|
|
|
2024-09-12 04:30:23 +02:00
|
|
|
"git.grassecon.net/urdt/ussd/internal/handlers"
|
2024-09-06 01:40:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
logg = logging.NewVanilla().WithDomain("httpserver")
|
|
|
|
)
|
|
|
|
|
2024-09-11 00:09:10 +02:00
|
|
|
type DefaultRequestParser struct {
|
|
|
|
}
|
|
|
|
|
2024-09-12 04:30:23 +02:00
|
|
|
|
|
|
|
func(rp *DefaultRequestParser) GetSessionId(rq any) (string, error) {
|
|
|
|
rqv, ok := rq.(*http.Request)
|
|
|
|
if !ok {
|
|
|
|
return "", handlers.ErrInvalidRequest
|
|
|
|
}
|
|
|
|
v := rqv.Header.Get("X-Vise-Session")
|
2024-09-11 18:32:55 +02:00
|
|
|
if v == "" {
|
2024-09-12 04:30:23 +02:00
|
|
|
return "", handlers.ErrSessionMissing
|
2024-09-06 01:40:57 +02:00
|
|
|
}
|
2024-09-11 18:32:55 +02:00
|
|
|
return v, nil
|
2024-09-06 01:40:57 +02:00
|
|
|
}
|
|
|
|
|
2024-09-12 04:30:23 +02:00
|
|
|
func(rp *DefaultRequestParser) GetInput(rq any) ([]byte, error) {
|
|
|
|
rqv, ok := rq.(*http.Request)
|
|
|
|
if !ok {
|
|
|
|
return nil, handlers.ErrInvalidRequest
|
|
|
|
}
|
|
|
|
defer rqv.Body.Close()
|
|
|
|
v, err := ioutil.ReadAll(rqv.Body)
|
2024-09-11 18:32:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-09-06 01:40:57 +02:00
|
|
|
}
|
2024-09-11 18:32:55 +02:00
|
|
|
return v, nil
|
2024-09-06 01:40:57 +02:00
|
|
|
}
|
|
|
|
|
2024-09-13 15:03:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
type SessionHandlerOption func(*SessionHandler)
|
|
|
|
|
|
|
|
func WithAtOutput() SessionHandlerOption {
|
|
|
|
return func(sh *SessionHandler) {
|
|
|
|
sh.useAtOutput = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-06 01:40:57 +02:00
|
|
|
type SessionHandler struct {
|
2024-09-12 05:07:55 +02:00
|
|
|
handlers.RequestHandler
|
2024-09-13 15:03:31 +02:00
|
|
|
useAtOutput bool
|
2024-09-06 01:40:57 +02:00
|
|
|
}
|
|
|
|
|
2024-09-13 15:03:31 +02:00
|
|
|
func ToSessionHandler(h handlers.RequestHandler, opts ...SessionHandlerOption) *SessionHandler {
|
|
|
|
sh := &SessionHandler{
|
2024-09-12 05:07:55 +02:00
|
|
|
RequestHandler: h,
|
2024-09-13 15:03:31 +02:00
|
|
|
useAtOutput: false,
|
2024-09-06 01:40:57 +02:00
|
|
|
}
|
2024-09-13 15:03:31 +02:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(sh)
|
|
|
|
}
|
|
|
|
return sh
|
2024-09-06 01:40:57 +02:00
|
|
|
}
|
|
|
|
|
2024-09-12 04:30:23 +02:00
|
|
|
func(f *SessionHandler) writeError(w http.ResponseWriter, code int, err error) {
|
|
|
|
s := err.Error()
|
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(len(s)))
|
2024-09-06 01:40:57 +02:00
|
|
|
w.WriteHeader(code)
|
|
|
|
_, err = w.Write([]byte{})
|
|
|
|
if err != nil {
|
2024-09-12 04:30:23 +02:00
|
|
|
logg.Errorf("error writing error!!", "err", err, "olderr", s)
|
2024-09-06 01:40:57 +02:00
|
|
|
w.WriteHeader(500)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-12 04:30:23 +02:00
|
|
|
func(f *SessionHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|
|
|
var code int
|
|
|
|
var err error
|
|
|
|
|
|
|
|
rqs := handlers.RequestSession{
|
|
|
|
Ctx: req.Context(),
|
|
|
|
Writer: w,
|
2024-09-06 01:40:57 +02:00
|
|
|
}
|
|
|
|
|
2024-09-12 05:07:55 +02:00
|
|
|
rp := f.GetRequestParser()
|
|
|
|
cfg := f.GetConfig()
|
|
|
|
cfg.SessionId, err = rp.GetSessionId(req)
|
2024-09-06 01:40:57 +02:00
|
|
|
if err != nil {
|
2024-09-12 04:30:23 +02:00
|
|
|
logg.ErrorCtxf(rqs.Ctx, "", "header processing error", err)
|
|
|
|
f.writeError(w, 400, err)
|
|
|
|
}
|
|
|
|
rqs.Config = cfg
|
2024-09-12 05:07:55 +02:00
|
|
|
rqs.Input, err = rp.GetInput(req)
|
2024-09-12 04:30:23 +02:00
|
|
|
if err != nil {
|
|
|
|
logg.ErrorCtxf(rqs.Ctx, "", "header processing error", err)
|
|
|
|
f.writeError(w, 400, err)
|
2024-09-06 01:40:57 +02:00
|
|
|
return
|
|
|
|
}
|
2024-09-12 04:30:23 +02:00
|
|
|
|
|
|
|
rqs, err = f.Process(rqs)
|
|
|
|
switch err {
|
|
|
|
case handlers.ErrStorage:
|
|
|
|
code = 500
|
|
|
|
case handlers.ErrEngineInit:
|
|
|
|
code = 500
|
|
|
|
case handlers.ErrEngineExec:
|
|
|
|
code = 500
|
|
|
|
default:
|
|
|
|
code = 200
|
2024-09-06 01:40:57 +02:00
|
|
|
}
|
2024-09-12 04:30:23 +02:00
|
|
|
|
|
|
|
if code != 200 {
|
|
|
|
f.writeError(w, 500, err)
|
2024-09-06 01:40:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(200)
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
2024-09-13 15:03:31 +02:00
|
|
|
if f.useAtOutput {
|
|
|
|
rqs, err = f.AtOutput(rqs)
|
|
|
|
} else {
|
|
|
|
rqs, err = f.Output(rqs)
|
|
|
|
}
|
2024-09-06 01:40:57 +02:00
|
|
|
if err != nil {
|
2024-09-12 04:30:23 +02:00
|
|
|
f.writeError(w, 500, err)
|
2024-09-06 01:40:57 +02:00
|
|
|
return
|
|
|
|
}
|
2024-09-12 04:30:23 +02:00
|
|
|
|
2024-09-12 05:07:55 +02:00
|
|
|
rqs, err = f.Reset(rqs)
|
2024-09-06 01:40:57 +02:00
|
|
|
if err != nil {
|
2024-09-12 04:30:23 +02:00
|
|
|
f.writeError(w, 500, err)
|
2024-09-06 01:40:57 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|