package http import ( "strings" osm "git.loafle.net/overflow/service_matcher-go" ) type HTTPMatcher struct { osm.Matchers meta osm.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() osm.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 osm.MatchInfo, index int, packet *osm.Packet) bool { return false } func (h *HTTPMatcher) Match(info osm.MatchInfo, index int, packet *osm.Packet) error { if packet == nil || packet.Buffer == nil || packet.Len == 0 { return osm.NoPacketReceivedError() } str := string(packet.Buffer) elems := strings.Split(str, "\r\n") if len(elems) <= 0 || 9 > len(elems[0]) { return osm.NotMatchedError() } protocol := (elems[0])[:8] if !strings.HasPrefix(protocol, "HTTP/") { return osm.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() osm.Matcher { m := &HTTPMatcher{} m.meta = osm.NewMetadata() reqStr := "GET / HTTP/1.1\r\n\r\n" byte := make([]byte, len(reqStr)) copy(byte[:], reqStr) m.AddPacket(osm.NewPacket(byte, len(reqStr))) return m }