Initial import

This commit is contained in:
2025-09-26 08:49:53 +02:00
commit a76650da35
35 changed files with 4660 additions and 0 deletions

78
proxy/response.go Normal file
View File

@@ -0,0 +1,78 @@
package proxy
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"strconv"
"git.maze.io/maze/styx/internal/log"
)
func NewResponse(code int, body io.Reader, request *http.Request) *http.Response {
if body == nil {
body = new(bytes.Buffer)
}
rc, ok := body.(io.ReadCloser)
if !ok {
rc = io.NopCloser(body)
}
response := &http.Response{
Status: strconv.Itoa(code) + " " + http.StatusText(code),
StatusCode: code,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: make(http.Header),
Body: rc,
Request: request,
}
if request != nil {
response.Close = request.Close
response.Proto = request.Proto
response.ProtoMajor = request.ProtoMajor
response.ProtoMinor = request.ProtoMinor
}
return response
}
type withLen interface {
Len() int
}
type withSize interface {
Size() int64
}
func NewJSONResponse(code int, body io.Reader, request *http.Request) *http.Response {
response := NewResponse(code, body, request)
response.Header.Set(HeaderContentType, "application/json")
if s, ok := body.(withLen); ok {
response.Header.Set(HeaderContentLength, strconv.Itoa(s.Len()))
} else if s, ok := body.(withSize); ok {
response.Header.Set(HeaderContentLength, strconv.FormatInt(s.Size(), 10))
} else {
log.Trace().Str("type", fmt.Sprintf("%T", body)).Msg("can't detemine body size")
}
response.Close = true
return response
}
func ErrorResponse(request *http.Request, err error) *http.Response {
response := NewResponse(http.StatusBadGateway, nil, request)
switch {
case os.IsNotExist(err):
response.StatusCode = http.StatusNotFound
case os.IsPermission(err):
response.StatusCode = http.StatusForbidden
}
response.Status = http.StatusText(response.StatusCode)
response.Close = true
return response
}