feat: get request kind and stream info

This commit is contained in:
2025-04-19 13:31:24 +02:00
parent 6c347e4fa0
commit 5d355b46cc

View File

@ -6,6 +6,8 @@ import (
"fmt"
"net"
"slices"
"strconv"
"strings"
)
type Request struct {
@ -42,6 +44,45 @@ func (req *Request) GetDataFormat() (*DataFormat, error) {
return ParseDataFormat(format)
}
func (req *Request) GetRequestKind() string {
_, ok := req.GetHeader(H_XSTREAM)
if ok {
return "stream"
}
return "single-hit"
}
func (req *Request) GetStreamInfo() (*StreamInfo, error) {
stream, ok := req.GetHeader(H_XSTREAM)
if !ok {
return nil, errors.New("No X-STREAM header presented in request")
}
parts := strings.Split(stream, ":")
if len(parts) != 2 {
return nil, fmt.Errorf("Invalid value of X-STREAM header: '%s'", stream)
}
totalS, bufsizeS := parts[0], parts[1]
total, err := strconv.ParseUint(totalS, 10, 64)
if err != nil {
return nil, err
}
bufsize, err := strconv.ParseUint(bufsizeS, 10, 16)
if err != nil {
return nil, err
}
buf := uint16(bufsize)
return &StreamInfo{
TotalBytes: total,
BufferSize: buf,
}, nil
}
func (req *Request) ExtractText() (string, error) {
df, err := req.GetDataFormat()
if err != nil {