Skip to content

Commit

Permalink
use correct error when matching http
Browse files Browse the repository at this point in the history
  • Loading branch information
WeidiDeng committed Jul 1, 2024
1 parent 46d6187 commit bf7770d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions layer4/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ func (cx *Connection) GetVar(key string) interface{} {

// MatchingBytes returns all bytes currently available for matching. This is only intended for reading.
// Do not write into the slice. It's a view of the internal buffer and you will likely mess up the connection.
// Use of this for matching purpose should be accompanied by corresponding error value,
// ErrConsumedAllPrefetchedBytes and ErrMatchingBufferFull, if not matched.
func (cx *Connection) MatchingBytes() []byte {
return cx.buf[cx.offset:]
}
Expand Down
17 changes: 13 additions & 4 deletions modules/l4http/httpmatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ func (m MatchHTTP) Match(cx *layer4.Connection) (bool, error) {
var err error

data := cx.MatchingBytes()
if !m.isHttp(data) {
needMore, matched := m.isHttp(data)
if needMore {
if len(data) >= layer4.MaxMatchingBytes {
return false, layer4.ErrMatchingBufferFull
}
return false, layer4.ErrConsumedAllPrefetchedBytes
}
if !matched {
return false, nil
}

Expand Down Expand Up @@ -122,11 +129,13 @@ func (m MatchHTTP) Match(cx *layer4.Connection) (bool, error) {
return m.matcherSets.AnyMatch(req), nil
}

func (m MatchHTTP) isHttp(data []byte) bool {
// isHttp test if the buffered data looks like HTTP by looking at the first line.
// first boolean determines if more data is required
func (m MatchHTTP) isHttp(data []byte) (bool, bool) {
// try to find the end of a http request line, for example " HTTP/1.1\r\n"
i := bytes.IndexByte(data, 0x0a) // find first new line
if i < 10 {
return false
return true, false
}
// assume only \n line ending
start := i - 9 // position of space in front of HTTP
Expand All @@ -136,7 +145,7 @@ func (m MatchHTTP) isHttp(data []byte) bool {
start -= 1
end -= 1
}
return bytes.Compare(data[start:end], []byte(" HTTP/")) == 0
return false, bytes.Compare(data[start:end], []byte(" HTTP/")) == 0
}

// Parses information from a http2 request with prior knowledge (RFC 7540 Section 3.4)
Expand Down
2 changes: 1 addition & 1 deletion modules/l4http/httpmatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func TestMatchHTTP_isHttp(t *testing.T) {
},
} {
t.Run(tc.name, func(t *testing.T) {
matched := MatchHTTP{}.isHttp(tc.data)
_, matched := MatchHTTP{}.isHttp(tc.data)
if matched != tc.shouldMatch {
t.Fatalf("test %v | matched: %v != shouldMatch: %v", tc.name, matched, tc.shouldMatch)
}
Expand Down

0 comments on commit bf7770d

Please sign in to comment.