Skip to content

Commit

Permalink
wip: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nickajacks1 committed Oct 20, 2023
1 parent 21b4393 commit 8c7e42d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 142 deletions.
80 changes: 24 additions & 56 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ type acceptedType struct {
quality float64
specificity int
order int
params headerParams
params string
}

// 0 = param name, 1 = param value
type headerParams string

// getTLSConfig returns a net listener's tls config
func getTLSConfig(ln net.Listener) *tls.Config {
// Get listener type
Expand Down Expand Up @@ -232,7 +229,7 @@ func getGroupPath(prefix, path string) string {
// acceptsOffer This function determines if an offer matches a given specification.
// It checks if the specification ends with a '*' or if the offer has the prefix of the specification.
// Returns true if the offer matches the specification, false otherwise.
func acceptsOffer(spec, offer string, _ headerParams) bool {
func acceptsOffer(spec, offer, _ string) bool {
if len(spec) >= 1 && spec[len(spec)-1] == '*' {
return true
} else if strings.HasPrefix(spec, offer) {
Expand All @@ -247,7 +244,7 @@ func acceptsOffer(spec, offer string, _ headerParams) bool {
// It checks if the offer MIME type matches the specification MIME type or if the specification is of the form <MIME_type>/* and the offer MIME type has the same MIME type.
// It checks if the offer contains every parameter present in the specification.
// Returns true if the offer type matches the specification, false otherwise.
func acceptsOfferType(spec, offerType string, specParams headerParams) bool {
func acceptsOfferType(spec, offerType, specParams string) bool {
offerMime := ""
var offerParams string

Expand Down Expand Up @@ -287,29 +284,28 @@ func acceptsOfferType(spec, offerType string, specParams headerParams) bool {
// paramsMatch returns whether offerParams contains all parameters present in specParams.
// Matching is case insensitive, and surrounding quotes are stripped.
// See https://www.rfc-editor.org/rfc/rfc9110#name-parameters
func paramsMatch(specParams headerParams, offerParams string) bool {
func paramsMatch(specParamStr, offerParams string) bool {
stillOk := true
messi := make([][2]string, 0, 2)
forHold(string(specParams), func(s1, s2 string) bool {
specParams := make([][2]string, 0, 2)
forEachParameter(string(specParamStr), func(s1, s2 string) bool {
if s1 == "q" || s1 == "Q" {
return false
}
for i := range messi {
if utils.EqualFold(s1, messi[i][0]) {
messi[i][1] = s2
for i := range specParams {
if utils.EqualFold(s1, specParams[i][0]) {
specParams[i][1] = s2
return false
}
}
messi = append(messi, [2]string{s1, s2})
// messi[s1] = s2
specParams = append(specParams, [2]string{s1, s2})
return true
})
for i := range messi {
for i := range specParams {
foundKey := false
forHold(offerParams, func(offerParam, offerVal string) bool {
if utils.EqualFold(messi[i][0], offerParam) {
forEachParameter(offerParams, func(offerParam, offerVal string) bool {
if utils.EqualFold(specParams[i][0], offerParam) {
foundKey = true
stillOk = utils.EqualFold(messi[i][1], offerVal)
stillOk = utils.EqualFold(specParams[i][1], offerVal)
return false
}
return true
Expand All @@ -318,24 +314,6 @@ func paramsMatch(specParams headerParams, offerParams string) bool {
return false
}
}
// forHold(string(specParams), func(s1, s2 string) bool {
// if s1 == "q" || s1 == "Q" {
// return false
// }
// foundKey := false
// forHold(offerParams, func(offerParam, offerVal string) bool {
// if utils.EqualFold(s1, offerParam) {
// foundKey = true
// stillOk = utils.EqualFold(s2, offerVal)
// return false
// }
// return true
// })
// if !foundKey {
// stillOk = false
// }
// return stillOk
// })
return stillOk
}

Expand Down Expand Up @@ -387,12 +365,12 @@ func getSplicedStrList(headerValue string, dst []string, sep rune) []string {
// to attempt to recover from errors in HTTP semantics. Therefor,
// we take the simple approach and exit early when a semantic error
// is detected in the header.
// parameter = parameter-name "=" parameter-value
// parameter-name = token
// parameter-value = ( token / quoted-string )
// parameters = *( OWS ";" OWS [ parameter ] )

func forHold(header string, f func(string, string) bool) {
//
// parameter = parameter-name "=" parameter-value
// parameter-name = token
// parameter-value = ( token / quoted-string )
// parameters = *( OWS ";" OWS [ parameter ] )
func forEachParameter(header string, f func(string, string) bool) {
for len(header) > 0 {
// eat OWS ";" OWS
header = utils.TrimLeft(header, ' ')
Expand Down Expand Up @@ -490,7 +468,7 @@ func validHeaderFieldByte(c byte) bool {
}

// getOffer return valid offer for header negotiation
func getOffer(header string, isAccepted func(spec, offer string, specParams headerParams) bool, offers ...string) string {
func getOffer(header string, isAccepted func(spec, offer, specParams string) bool, offers ...string) string {
if len(offers) == 0 {
return ""
}
Expand All @@ -505,29 +483,19 @@ func getOffer(header string, isAccepted func(spec, offer string, specParams head

forEachAccept(header, func(accept string) {
order++
spec, quality := accept, 1.0
var params headerParams
spec, quality, params := accept, 1.0, ""

if i := strings.IndexByte(accept, ';'); i != -1 {
spec = accept[:i]
params = headerParams(accept[i:])
forHold(accept[i:], func(param, val string) bool {
params = accept[i:]
forEachParameter(accept[i:], func(param, val string) bool {
if param == "q" || param == "Q" {
if q, err := fasthttp.ParseUfloat([]byte(val)); err == nil {
quality = q
}
return false
}
return true
// if params == nil {
// params = make(headerParams, 0, 2)
// }
// for j := range params {
// if utils.EqualFold(param, params[j][0]) {
// params[j][1] = val
// }
// }
// params = append(params, [2]string{param, val})
})
}
spec = utils.TrimRight(spec, ' ')
Expand Down
159 changes: 73 additions & 86 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,104 +155,91 @@ func Benchmark_Utils_GetOffer_WithParams(b *testing.B) {
}

func Test_Utils_ForHold(t *testing.T) {
ses := ""
forHold(` ; josua=1 ; vermant="20tw\",bob;sack o" `, func(p, v string) bool {
ses += fmt.Sprintf("Licka [%s] [%s]\n", p, v)
return true
expectedParams := [][]string{
{"foo", "1"},
{"bar", `20tw\",b\\b;sack o`},
}
n := 0
forEachParameter(` ; foo=1 ; bar="20tw\",b\\b;sack o" ; action=skip `, func(p, v string) bool {
utils.AssertEqual(t, expectedParams[n][0], p)
utils.AssertEqual(t, expectedParams[n][1], v)
n++
return p != "bar"
})
t.Fatal(ses)
// Check that we exited on the second parameter (bar)
utils.AssertEqual(t, 2, n)
}

func Benchmark_Utils_ForHold(b *testing.B) {

for n := 0; n < b.N; n++ {
salem := make([][2]string, 0, 5)
forHold(` ; josua=1 ; vermant="20tw\",bob;sack o" `, func(s1, s2 string) bool {
forEachParameter(` ; josua=1 ; vermant="20tw\",bob;sack o" `, func(s1, s2 string) bool {
salem = append(salem, [2]string{s1, s2})
return true
})
}
}

// func Test_Utils_AcceptsOfferType(t *testing.T) {
// testCases := []struct {
// description string
// spec string
// specParams headerParams
// offerType string
// accepts bool
// }{
// {
// description: "no params, matching",
// spec: "application/json",
// offerType: "application/json",
// accepts: true,
// },
// {
// description: "no params, mismatch",
// spec: "application/json",
// offerType: "application/xml",
// accepts: false,
// },
// {
// description: "params match",
// spec: "application/json",
// specParams: headerParams{
// {"version", "1"},
// {"format", "foo"},
// },
// offerType: "application/json;version=1;format=foo;q=0.1",
// accepts: true,
// },
// {
// description: "spec has extra params",
// spec: "text/html",
// specParams: headerParams{{"charset", "utf-8"}},
// offerType: "text/html",
// accepts: false,
// },
// {
// description: "offer has extra params",
// spec: "text/html",
// offerType: "text/html;charset=utf-8",
// accepts: true,
// },
// {
// description: "ignores optional whitespace",
// spec: "application/json",
// specParams: headerParams{
// {"format", "foo"},
// {"version", "1"},
// },
// offerType: "application/json; version=1 ; format=foo ",
// accepts: true,
// },
// {
// description: "ignores optional whitespace",
// spec: "application/json",
// specParams: headerParams{
// {"format", "foo bar"},
// {"version", "1"},
// },
// offerType: "application/json; version=1 ; format=foo bar ",
// accepts: true,
// },
// {
// description: "ignores optional whitespace",
// spec: "application/json",
// specParams: headerParams{
// {"format", "foo bar"},
// {"version", "1"},
// },
// offerType: `application/json;version="1";format="foo bar"`,
// accepts: true,
// },
// }
// for _, tc := range testCases {
// accepts := acceptsOfferType(tc.spec, tc.offerType, tc.specParams)
// utils.AssertEqual(t, tc.accepts, accepts, tc.description)
// }
// }
func Test_Utils_AcceptsOfferType(t *testing.T) {
testCases := []struct {
description string
spec string
specParams string
offerType string
accepts bool
}{
{
description: "no params, matching",
spec: "application/json",
offerType: "application/json",
accepts: true,
},
{
description: "no params, mismatch",
spec: "application/json",
offerType: "application/xml",
accepts: false,
},
{
description: "params match",
spec: "application/json",
specParams: `; format=foo; version=1`,
offerType: "application/json;version=1;format=foo;q=0.1",
accepts: true,
},
{
description: "spec has extra params",
spec: "text/html",
specParams: "; charset=utf-8",
offerType: "text/html",
accepts: false,
},
{
description: "offer has extra params",
spec: "text/html",
offerType: "text/html;charset=utf-8",
accepts: true,
},
{
description: "ignores optional whitespace",
spec: "application/json",
specParams: `;format=foo; version=1`,
offerType: "application/json; version=1 ; format=foo ",
accepts: true,
},
{
description: "ignores optional whitespace",
spec: "application/json",
specParams: `;format="foo bar"; version=1`,
offerType: `application/json;version="1";format="foo bar"`,
accepts: true,
},
}
for _, tc := range testCases {
accepts := acceptsOfferType(tc.spec, tc.offerType, tc.specParams)
utils.AssertEqual(t, tc.accepts, accepts, tc.description)
}
}

func Test_Utils_GetSplicedStrList(t *testing.T) {
testCases := []struct {
Expand Down

0 comments on commit 8c7e42d

Please sign in to comment.