Files
mochi/frontend/http/params.go
Lawrence, Rendall 1a3f5b1598 upgrade golangci to v2
* migrate configuration to version 2
* fix new lint warnings
* update lint.yaml to use golangci-lint-action v7
2025-04-22 14:18:18 +03:00

39 lines
1.0 KiB
Go

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()))
}