Skip to content

Commit

Permalink
Merge pull request indigo-web#89 from fakefloordiv/dev
Browse files Browse the repository at this point in the history
Parser optimizations
  • Loading branch information
flrdv authored Jul 29, 2023
2 parents 068e7e8 + 4f161ab commit b1819e6
Show file tree
Hide file tree
Showing 14 changed files with 209 additions and 309 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/dchest/uniuri v1.2.0
github.com/indigo-web/utils v0.1.1
github.com/indigo-web/utils v0.1.3
github.com/stretchr/testify v1.8.4
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/dchest/uniuri v1.2.0/go.mod h1:fSzm4SLHzNZvWLvWJew423PhAzkpNQYq+uNLq4
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/indigo-web/utils v0.1.1 h1:6368QFWfenf/9kqZxDF8oWCGGiUmcxJTNSTjra9GS/c=
github.com/indigo-web/utils v0.1.1/go.mod h1:fBdCfyNyprkgC0FvqaLE1B43CZgByQyhjDRxz4pNt8U=
github.com/indigo-web/utils v0.1.3 h1:eLlNmB+J+OJOX12TxhhIXKohvBUT+knr9cxpCVrwbS0=
github.com/indigo-web/utils v0.1.3/go.mod h1:fBdCfyNyprkgC0FvqaLE1B43CZgByQyhjDRxz4pNt8U=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
Expand Down
10 changes: 6 additions & 4 deletions http/headers/headers.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package headers

import "github.com/indigo-web/indigo/internal/strcomp"

type Iterator[T any] func() (element T, continue_ bool)

// Headers is a struct that encapsulates headers map from user, allowing only
Expand Down Expand Up @@ -48,7 +50,7 @@ func (h *Headers) Value(key string) string {
// ValueOr returns a header value
func (h *Headers) ValueOr(key, or string) string {
for i := 0; i < len(h.headers); i += 2 {
if h.headers[i] == key {
if strcomp.EqualFold(h.headers[i], key) {
return h.headers[i+1]
}
}
Expand All @@ -65,7 +67,7 @@ func (h *Headers) ValuesIter(key string) Iterator[string] {
}

for ; offset < len(h.headers); offset += 2 {
if h.headers[offset] == key {
if strcomp.EqualFold(h.headers[offset], key) {
value := h.headers[offset+1]
offset += 2

Expand Down Expand Up @@ -97,7 +99,7 @@ func (h *Headers) Add(key, value string) {
// Has returns true or false depending on whether such a key exists
func (h *Headers) Has(key string) bool {
for i := 0; i < len(h.headers); i += 2 {
if h.headers[i] == key {
if strcomp.EqualFold(h.headers[i], key) {
return true
}
}
Expand Down Expand Up @@ -137,7 +139,7 @@ func collectIterator(iter Iterator[string]) (values []string) {

func contains(elements []string, key string) bool {
for i := range elements {
if elements[i] == key {
if strcomp.EqualFold(elements[i], key) {
return true
}
}
Expand Down
10 changes: 3 additions & 7 deletions http/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ type (
Params = map[string]string

Path struct {
String string
Params Params
Query query.Query
Fragment Fragment
String string
Params Params
Query query.Query
}

Fragment = string
)

// Request struct represents http request
Expand Down Expand Up @@ -124,7 +121,6 @@ func (r *Request) WasHijacked() bool {
// Clear resets request headers and reads body into nowhere until completed.
// It is implemented to clear the request object between requests
func (r *Request) Clear() (err error) {
r.Path.Fragment = ""
r.Path.Query.Set(nil)
r.Ctx = context.Background()
r.response = r.response.Clear()
Expand Down
15 changes: 6 additions & 9 deletions indigo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func getStaticRouter(t *testing.T) router.Router {
require.Equal(t, method.GET, request.Method)
_, err := request.Path.Query.Get("some non-existing query key")
require.Error(t, err)
require.Empty(t, request.Path.Fragment)
require.Equal(t, proto.HTTP11, request.Proto)

return http.RespondTo(request)
Expand All @@ -91,7 +90,7 @@ func getStaticRouter(t *testing.T) router.Router {
})

r.Get("/get-read-body", func(request *http.Request) http.Response {
require.Contains(t, request.Headers.Unwrap(), testHeaderKey)
require.True(t, request.Headers.Has(testHeaderKey))
requestHeader := strings.Join(request.Headers.Values(testHeaderKey), ",")
require.Equal(t, testHeaderValue, requestHeader)

Expand Down Expand Up @@ -370,15 +369,13 @@ func TestServer_Static(t *testing.T) {
// are empty strings. Remove them
headerLines = headerLines[:len(headerLines)-2]
wantHeaderLines := []string{
"hello: World!",
"host: " + addr,
"user-agent: Go-http-client/1.1",
"accept-encoding: gzip",
"content-length: 0",
"Hello: World!",
"Host: " + addr,
"User-Agent: Go-http-client/1.1",
"Accept-Encoding: gzip",
"Content-Length: 0",
}

require.Equal(t, len(wantHeaderLines), len(headerLines))

for _, line := range headerLines {
require.True(t, contains(wantHeaderLines, line), "unwanted header line: "+line)
}
Expand Down
6 changes: 3 additions & 3 deletions initializers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func newClient(tcpSettings settings.TCP, conn net.Conn) tcp.Client {
return tcp.NewClient(conn, tcpSettings.ReadTimeout, readBuff)
}

func newKeyValueArenas(s settings.Headers) (*arena.Arena, *arena.Arena) {
keyArena := arena.NewArena(
func newKeyValueArenas(s settings.Headers) (*arena.Arena[byte], *arena.Arena[byte]) {
keyArena := arena.NewArena[byte](
s.MaxKeyLength*s.Number.Default,
s.MaxKeyLength*s.Number.Maximal,
)
valArena := arena.NewArena(
valArena := arena.NewArena[byte](
s.ValueSpace.Default,
s.ValueSpace.Maximal,
)
Expand Down
Loading

0 comments on commit b1819e6

Please sign in to comment.