Write intercepts any writes to the response writer. The first write will extract the Content-Type as the mediatype. Otherwise it falls back to the RequestURI extension.
(b []byte)
| 319 | // Write intercepts any writes to the response writer. |
| 320 | // The first write will extract the Content-Type as the mediatype. Otherwise it falls back to the RequestURI extension. |
| 321 | func (w *responseWriter) Write(b []byte) (int, error) { |
| 322 | if w.z == nil { |
| 323 | // first write |
| 324 | if mediatype := w.ResponseWriter.Header().Get("Content-Type"); mediatype != "" { |
| 325 | w.mediatype = mediatype |
| 326 | } |
| 327 | if _, params, minifier := w.m.Match(w.mediatype); minifier != nil { |
| 328 | pr, pw := io.Pipe() |
| 329 | z := &writer{pw, sync.WaitGroup{}, false, nil} |
| 330 | z.wg.Add(1) |
| 331 | go func() { |
| 332 | defer z.wg.Done() |
| 333 | defer pr.Close() |
| 334 | if err := minifier(w.m, w.ResponseWriter, pr, params); err != nil { |
| 335 | z.err = err |
| 336 | } |
| 337 | }() |
| 338 | w.z = z |
| 339 | } else { |
| 340 | w.z = w.ResponseWriter |
| 341 | } |
| 342 | } |
| 343 | return w.z.Write(b) |
| 344 | } |
| 345 | |
| 346 | // Close must be called when writing has finished. It returns the error from the minifier. |
| 347 | func (w *responseWriter) Close() error { |