92 lines
1.6 KiB
Go
92 lines
1.6 KiB
Go
|
package http
|
||
|
|
||
|
import (
|
||
|
"strings"
|
||
|
|
||
|
csm "git.loafle.net/commons/service_matcher-go"
|
||
|
)
|
||
|
|
||
|
type HTTPMatcher struct {
|
||
|
csm.Matchers
|
||
|
meta csm.Metadata
|
||
|
}
|
||
|
|
||
|
func (h *HTTPMatcher) Key() string {
|
||
|
return "HTTP"
|
||
|
}
|
||
|
|
||
|
func (h *HTTPMatcher) Name() string {
|
||
|
name := "HTTP"
|
||
|
if v, ok := h.meta["server"]; ok {
|
||
|
name = name + " (" + v + ")"
|
||
|
}
|
||
|
return name
|
||
|
}
|
||
|
|
||
|
func (h *HTTPMatcher) Meta() csm.Metadata {
|
||
|
return h.meta
|
||
|
}
|
||
|
|
||
|
func (h *HTTPMatcher) IsPrePacket() bool {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func (h *HTTPMatcher) HasResponse(index int) bool {
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
func (h *HTTPMatcher) IsError(info csm.MatchInfo, index int, packet *csm.Packet) bool {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func (h *HTTPMatcher) Match(info csm.MatchInfo, index int, packet *csm.Packet) error {
|
||
|
|
||
|
if packet == nil || packet.Buffer == nil || packet.Len == 0 {
|
||
|
return csm.NoPacketReceivedError()
|
||
|
}
|
||
|
|
||
|
str := string(packet.Buffer)
|
||
|
elems := strings.Split(str, "\r\n")
|
||
|
|
||
|
if len(elems) <= 0 || 9 > len(elems[0]) {
|
||
|
return csm.NotMatchedError()
|
||
|
}
|
||
|
|
||
|
protocol := (elems[0])[:8]
|
||
|
|
||
|
if !strings.HasPrefix(protocol, "HTTP/") {
|
||
|
return csm.NotMatchedError()
|
||
|
}
|
||
|
|
||
|
serverName := ""
|
||
|
for _, valueStr := range elems {
|
||
|
tempElems := strings.Split(valueStr, ":")
|
||
|
|
||
|
if 0 == strings.Compare(tempElems[0], "Server") {
|
||
|
serverName = strings.TrimSpace(tempElems[1])
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
|
||
|
h.meta["protocol_version"] = protocol
|
||
|
if len(serverName) > 0 {
|
||
|
h.meta["server"] = serverName
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func NewMatcher() csm.Matcher {
|
||
|
|
||
|
m := &HTTPMatcher{}
|
||
|
m.meta = csm.NewMetadata()
|
||
|
|
||
|
reqStr := "GET / HTTP/1.1\r\n\r\n"
|
||
|
byte := make([]byte, len(reqStr))
|
||
|
copy(byte[:], reqStr)
|
||
|
|
||
|
m.AddPacket(csm.NewPacket(byte, len(reqStr)))
|
||
|
|
||
|
return m
|
||
|
}
|