Files
mochi/frontend/http/params.go
T
Lawrence, Rendall d9666685f5 update Go verstion to 1.25
* update dependencies
* fix lint warnings (package naming, ctx cancel, UDP serve close check)
2026-03-24 19:36:49 +03:00

40 lines
1.1 KiB
Go

//revive:disable-next-line:var-naming
package http
import (
"github.com/rs/zerolog"
"github.com/valyala/fasthttp"
"github.com/sot-tech/mochi/bittorrent"
"github.com/sot-tech/mochi/pkg/str2bytes"
)
// queryParams parses a URL Query and implements the Params interface with some
// additional helpers.
type queryParams struct {
*fasthttp.Args
}
// GetString returns a string parsed from a query. Every key can be returned as a
// string because they are encoded in the URL as strings.
func (qp queryParams) GetString(key string) (string, bool) {
v := qp.Peek(key)
return str2bytes.BytesToString(v), v != nil
}
// InfoHashes returns a list of requested infohashes.
func (qp queryParams) InfoHashes() bittorrent.InfoHashes {
var ihs bittorrent.InfoHashes
for _, bb := range qp.PeekMulti("info_hash") {
if ih, err := bittorrent.NewInfoHash(bb); err == nil {
ihs = append(ihs, ih)
}
}
return ihs
}
// MarshalZerologObject writes fields into zerolog event
func (qp queryParams) MarshalZerologObject(e *zerolog.Event) {
e.Str("query", str2bytes.BytesToString(qp.QueryString()))
}