MCPcopy Create free account
hub / github.com/cortesi/devd / Sniff

Method Sniff

inject/inject.go:86–113  ·  view source on GitHub ↗

Sniff reads the first SniffLen bytes of the source, and checks for the marker. Returns an Injector instance.

(src io.Reader, contentType string)

Source from the content-addressed store, hash-verified

84// Sniff reads the first SniffLen bytes of the source, and checks for the
85// marker. Returns an Injector instance.
86func (ci *CopyInject) Sniff(src io.Reader, contentType string) (Injector, error) {
87 if !strings.Contains(contentType, ci.ContentType) {
88 return &nopInjector{src: src}, nil
89 }
90
91 injector := &realInjector{
92 conf: ci,
93 src: src,
94 }
95 if ci.Within == 0 || ci.Marker == nil {
96 return injector, nil
97 }
98 buf := make([]byte, ci.Within+len(ci.Payload))
99 n, err := io.ReadFull(src, buf)
100 if err != nil && err != io.ErrUnexpectedEOF && err != io.EOF {
101 return nil, fmt.Errorf("inject could not read data to sniff: %s", err)
102 }
103 injector.sniffedData = buf[:n]
104 if bytes.Index(buf, ci.Payload) > -1 {
105 return injector, nil
106 }
107 loc := ci.Marker.FindIndex(injector.sniffedData[:min(n, ci.Within)])
108 if loc != nil {
109 injector.found = true
110 injector.offset = loc[0]
111 }
112 return injector, nil
113}
114
115// ServeTemplate renders and serves a template to an http.ResponseWriter
116func (ci *CopyInject) ServeTemplate(statuscode int, w http.ResponseWriter, t *template.Template, data interface{}) error {

Callers 5

TestNilFunction · 0.95
ServeTemplateMethod · 0.95
injectFunction · 0.80
ServeHTTPContextMethod · 0.80
serveContentFunction · 0.80

Calls 1

minFunction · 0.85

Tested by 2

TestNilFunction · 0.76
injectFunction · 0.64