Sniff reads the first SniffLen bytes of the source, and checks for the marker. Returns an Injector instance.
(src io.Reader, contentType string)
| 84 | // Sniff reads the first SniffLen bytes of the source, and checks for the |
| 85 | // marker. Returns an Injector instance. |
| 86 | func (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 |
| 116 | func (ci *CopyInject) ServeTemplate(statuscode int, w http.ResponseWriter, t *template.Template, data interface{}) error { |