mirror of
https://github.com/safing/portbase
synced 2025-09-04 03:29:59 +00:00
Implement review suggestions
This commit is contained in:
parent
08016b16cd
commit
644947a9c9
5 changed files with 30 additions and 26 deletions
|
@ -7,6 +7,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/fxamacker/cbor/v2"
|
"github.com/fxamacker/cbor/v2"
|
||||||
"github.com/vmihailenco/msgpack/v5"
|
"github.com/vmihailenco/msgpack/v5"
|
||||||
|
@ -73,7 +74,7 @@ func loadFormat(data []byte) (format uint8, read int, err error) {
|
||||||
return 0, 0, err
|
return 0, 0, err
|
||||||
}
|
}
|
||||||
if len(data) <= read {
|
if len(data) <= read {
|
||||||
return 0, 0, ErrNoMoreSpace
|
return 0, 0, io.ErrUnexpectedEOF
|
||||||
}
|
}
|
||||||
|
|
||||||
return format, read, nil
|
return format, read, nil
|
||||||
|
|
|
@ -5,7 +5,6 @@ import "errors"
|
||||||
var (
|
var (
|
||||||
ErrIncompatibleFormat = errors.New("dsd: format is incompatible with operation")
|
ErrIncompatibleFormat = errors.New("dsd: format is incompatible with operation")
|
||||||
ErrIsRaw = errors.New("dsd: given data is in raw format")
|
ErrIsRaw = errors.New("dsd: given data is in raw format")
|
||||||
ErrNoMoreSpace = errors.New("dsd: no more space left after reading dsd type")
|
|
||||||
ErrUnknownFormat = errors.New("dsd: format is unknown")
|
ErrUnknownFormat = errors.New("dsd: format is unknown")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,9 +24,6 @@ const (
|
||||||
|
|
||||||
// Special types.
|
// Special types.
|
||||||
LIST = 76 // L
|
LIST = 76 // L
|
||||||
|
|
||||||
// Deprecated: NONE is deprecated, please use RAW instead.
|
|
||||||
NONE = 1
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
|
@ -6,8 +6,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -19,21 +19,14 @@ const (
|
||||||
httpHeaderContentType = "Content-Type"
|
httpHeaderContentType = "Content-Type"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// LoadFromHTTPRequest loads the data from the body into the given interface.
|
||||||
func LoadFromHTTPRequest(r *http.Request, t interface{}) (format uint8, err error) {
|
func LoadFromHTTPRequest(r *http.Request, t interface{}) (format uint8, err error) {
|
||||||
if r.Body == nil {
|
|
||||||
return 0, ErrMissingBody
|
|
||||||
}
|
|
||||||
defer r.Body.Close()
|
|
||||||
|
|
||||||
return loadFromHTTP(r.Body, r.Header.Get(httpHeaderContentType), t)
|
return loadFromHTTP(r.Body, r.Header.Get(httpHeaderContentType), t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoadFromHTTPResponse loads the data from the body into the given interface.
|
||||||
|
// Closing the body is left to the caller.
|
||||||
func LoadFromHTTPResponse(resp *http.Response, t interface{}) (format uint8, err error) {
|
func LoadFromHTTPResponse(resp *http.Response, t interface{}) (format uint8, err error) {
|
||||||
if resp.Body == nil {
|
|
||||||
return 0, ErrMissingBody
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
return loadFromHTTP(resp.Body, resp.Header.Get(httpHeaderContentType), t)
|
return loadFromHTTP(resp.Body, resp.Header.Get(httpHeaderContentType), t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,8 +41,9 @@ func loadFromHTTP(body io.Reader, mimeType string, t interface{}) (format uint8,
|
||||||
if mimeType == "" {
|
if mimeType == "" {
|
||||||
return 0, ErrMissingContentType
|
return 0, ErrMissingContentType
|
||||||
}
|
}
|
||||||
if strings.Contains(mimeType, ";") {
|
mimeType, _, err = mime.ParseMediaType(mimeType)
|
||||||
mimeType = strings.SplitN(mimeType, ";", 2)[0]
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("dsd: failed to parse content type: %w", err)
|
||||||
}
|
}
|
||||||
format, ok := MimeTypeToFormat[mimeType]
|
format, ok := MimeTypeToFormat[mimeType]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -60,6 +54,7 @@ func loadFromHTTP(body io.Reader, mimeType string, t interface{}) (format uint8,
|
||||||
return format, LoadAsFormat(data, format, t)
|
return format, LoadAsFormat(data, format, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RequestHTTPResponseFormat sets the Accept header to the given format.
|
||||||
func RequestHTTPResponseFormat(r *http.Request, format uint8) (mimeType string, err error) {
|
func RequestHTTPResponseFormat(r *http.Request, format uint8) (mimeType string, err error) {
|
||||||
// Get mime type.
|
// Get mime type.
|
||||||
mimeType, ok := FormatToMimeType[format]
|
mimeType, ok := FormatToMimeType[format]
|
||||||
|
@ -73,6 +68,8 @@ func RequestHTTPResponseFormat(r *http.Request, format uint8) (mimeType string,
|
||||||
return mimeType, nil
|
return mimeType, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DumpToHTTPRequest dumps the given data to the HTTP request using the given
|
||||||
|
// format. It also sets the Accept header to the same format.
|
||||||
func DumpToHTTPRequest(r *http.Request, t interface{}, format uint8) error {
|
func DumpToHTTPRequest(r *http.Request, t interface{}, format uint8) error {
|
||||||
mimeType, err := RequestHTTPResponseFormat(r, format)
|
mimeType, err := RequestHTTPResponseFormat(r, format)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -92,13 +89,13 @@ func DumpToHTTPRequest(r *http.Request, t interface{}, format uint8) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DumpToHTTPResponse(w http.ResponseWriter, r *http.Request, t interface{}, fallbackFormat uint8) error {
|
// DumpToHTTPResponse dumpts the given data to the HTTP response, using the
|
||||||
|
// format defined in the request's Accept header.
|
||||||
|
func DumpToHTTPResponse(w http.ResponseWriter, r *http.Request, t interface{}) error {
|
||||||
// Get format from Accept header.
|
// Get format from Accept header.
|
||||||
format, ok := MimeTypeToFormat[r.Header.Get("Accept")]
|
// TODO: Improve parsing of Accept header.
|
||||||
if !ok {
|
mimeType := r.Header.Get("Accept")
|
||||||
format = fallbackFormat
|
format, ok := MimeTypeToFormat[mimeType]
|
||||||
}
|
|
||||||
mimeType, ok := FormatToMimeType[format]
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return ErrIncompatibleFormat
|
return ErrIncompatibleFormat
|
||||||
}
|
}
|
||||||
|
|
5
go.mod
5
go.mod
|
@ -4,7 +4,7 @@ go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/StackExchange/wmi v1.2.1 // indirect
|
github.com/StackExchange/wmi v1.2.1 // indirect
|
||||||
github.com/VictoriaMetrics/metrics v1.18.0
|
github.com/VictoriaMetrics/metrics v1.18.1
|
||||||
github.com/aead/serpent v0.0.0-20160714141033-fba169763ea6
|
github.com/aead/serpent v0.0.0-20160714141033-fba169763ea6
|
||||||
github.com/andyleap/gencode v0.0.0-20171124163308-e1423834d4b4 // indirect
|
github.com/andyleap/gencode v0.0.0-20171124163308-e1423834d4b4 // indirect
|
||||||
github.com/andyleap/parser v0.0.0-20160126201130-db5a13a7cd46 // indirect
|
github.com/andyleap/parser v0.0.0-20160126201130-db5a13a7cd46 // indirect
|
||||||
|
@ -33,8 +33,9 @@ require (
|
||||||
github.com/tidwall/gjson v1.11.0
|
github.com/tidwall/gjson v1.11.0
|
||||||
github.com/tidwall/sjson v1.2.3
|
github.com/tidwall/sjson v1.2.3
|
||||||
github.com/tklauser/go-sysconf v0.3.9 // indirect
|
github.com/tklauser/go-sysconf v0.3.9 // indirect
|
||||||
|
github.com/vmihailenco/msgpack/v5 v5.3.5
|
||||||
go.etcd.io/bbolt v1.3.6
|
go.etcd.io/bbolt v1.3.6
|
||||||
golang.org/x/net v0.0.0-20211013171255-e13a2654a71e // indirect
|
golang.org/x/net v0.0.0-20211116231205-47ca1ff31462 // indirect
|
||||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
|
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
|
||||||
golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c
|
golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c
|
||||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||||
|
|
9
go.sum
9
go.sum
|
@ -25,6 +25,8 @@ github.com/VictoriaMetrics/metrics v1.15.2 h1:w/GD8L9tm+gvx1oZvAofRRXwammiicdI0j
|
||||||
github.com/VictoriaMetrics/metrics v1.15.2/go.mod h1:Z1tSfPfngDn12bTfZSCqArT3OPY3u88J12hSoOhuiRE=
|
github.com/VictoriaMetrics/metrics v1.15.2/go.mod h1:Z1tSfPfngDn12bTfZSCqArT3OPY3u88J12hSoOhuiRE=
|
||||||
github.com/VictoriaMetrics/metrics v1.18.0 h1:vov5NxDHRSXFbdiH4dYLYEjKLoAXXSQ7hcnG8TSD9JQ=
|
github.com/VictoriaMetrics/metrics v1.18.0 h1:vov5NxDHRSXFbdiH4dYLYEjKLoAXXSQ7hcnG8TSD9JQ=
|
||||||
github.com/VictoriaMetrics/metrics v1.18.0/go.mod h1:ArjwVz7WpgpegX/JpB0zpNF2h2232kErkEnzH1sxMmA=
|
github.com/VictoriaMetrics/metrics v1.18.0/go.mod h1:ArjwVz7WpgpegX/JpB0zpNF2h2232kErkEnzH1sxMmA=
|
||||||
|
github.com/VictoriaMetrics/metrics v1.18.1 h1:OZ0+kTTto8oPfHnVAnTOoyl0XlRhRkoQrD2n2cOuRw0=
|
||||||
|
github.com/VictoriaMetrics/metrics v1.18.1/go.mod h1:ArjwVz7WpgpegX/JpB0zpNF2h2232kErkEnzH1sxMmA=
|
||||||
github.com/aead/serpent v0.0.0-20160714141033-fba169763ea6 h1:5L8Mj9Co9sJVgW3TpYk2gxGJnDjsYuboNTcRmbtGKGs=
|
github.com/aead/serpent v0.0.0-20160714141033-fba169763ea6 h1:5L8Mj9Co9sJVgW3TpYk2gxGJnDjsYuboNTcRmbtGKGs=
|
||||||
github.com/aead/serpent v0.0.0-20160714141033-fba169763ea6/go.mod h1:3HgLJ9d18kXMLQlJvIY3+FszZYMxCz8WfE2MQ7hDY0w=
|
github.com/aead/serpent v0.0.0-20160714141033-fba169763ea6/go.mod h1:3HgLJ9d18kXMLQlJvIY3+FszZYMxCz8WfE2MQ7hDY0w=
|
||||||
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||||
|
@ -319,6 +321,11 @@ github.com/valyala/histogram v1.1.2 h1:vOk5VrGjMBIoPR5k6wA8vBaC8toeJ8XO0yfRjFEc1
|
||||||
github.com/valyala/histogram v1.1.2/go.mod h1:CZAr6gK9dbD7hYx2s8WSPh0p5x5wETjC+2b3PJVtEdg=
|
github.com/valyala/histogram v1.1.2/go.mod h1:CZAr6gK9dbD7hYx2s8WSPh0p5x5wETjC+2b3PJVtEdg=
|
||||||
github.com/valyala/histogram v1.2.0 h1:wyYGAZZt3CpwUiIb9AU/Zbllg1llXyrtApRS815OLoQ=
|
github.com/valyala/histogram v1.2.0 h1:wyYGAZZt3CpwUiIb9AU/Zbllg1llXyrtApRS815OLoQ=
|
||||||
github.com/valyala/histogram v1.2.0/go.mod h1:Hb4kBwb4UxsaNbbbh+RRz8ZR6pdodR57tzWUS3BUzXY=
|
github.com/valyala/histogram v1.2.0/go.mod h1:Hb4kBwb4UxsaNbbbh+RRz8ZR6pdodR57tzWUS3BUzXY=
|
||||||
|
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
|
||||||
|
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
|
||||||
|
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
|
||||||
|
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
|
||||||
|
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
|
||||||
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
|
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
|
||||||
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
|
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
|
||||||
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
|
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
|
||||||
|
@ -375,6 +382,8 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxW
|
||||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
golang.org/x/net v0.0.0-20211013171255-e13a2654a71e h1:Xj+JO91noE97IN6F/7WZxzC5QE6yENAQPrwIYhW3bsA=
|
golang.org/x/net v0.0.0-20211013171255-e13a2654a71e h1:Xj+JO91noE97IN6F/7WZxzC5QE6yENAQPrwIYhW3bsA=
|
||||||
golang.org/x/net v0.0.0-20211013171255-e13a2654a71e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
golang.org/x/net v0.0.0-20211013171255-e13a2654a71e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
|
golang.org/x/net v0.0.0-20211116231205-47ca1ff31462 h1:2vmJlzGKvQ7e/X9XT0XydeWDxmqx8DnegiIMRT+5ssI=
|
||||||
|
golang.org/x/net v0.0.0-20211116231205-47ca1ff31462/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||||
|
|
Loading…
Add table
Reference in a new issue