(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler)
| 436 | } |
| 437 | |
| 438 | func (t *Templates) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error { |
| 439 | buf := bufPool.Get().(*bytes.Buffer) |
| 440 | buf.Reset() |
| 441 | defer bufPool.Put(buf) |
| 442 | |
| 443 | // shouldBuf determines whether to execute templates on this response, |
| 444 | // since generally we will not want to execute for images or CSS, etc. |
| 445 | shouldBuf := func(status int, header http.Header) bool { |
| 446 | ct := header.Get("Content-Type") |
| 447 | for _, mt := range t.MIMETypes { |
| 448 | if strings.Contains(ct, mt) { |
| 449 | return true |
| 450 | } |
| 451 | } |
| 452 | return false |
| 453 | } |
| 454 | |
| 455 | rec := caddyhttp.NewResponseRecorder(w, buf, shouldBuf) |
| 456 | |
| 457 | err := next.ServeHTTP(rec, r) |
| 458 | if err != nil { |
| 459 | return err |
| 460 | } |
| 461 | if !rec.Buffered() { |
| 462 | return nil |
| 463 | } |
| 464 | |
| 465 | err = t.executeTemplate(rec, r) |
| 466 | if err != nil { |
| 467 | return err |
| 468 | } |
| 469 | |
| 470 | rec.Header().Set("Content-Length", strconv.Itoa(buf.Len())) |
| 471 | rec.Header().Del("Accept-Ranges") // we don't know ranges for dynamically-created content |
| 472 | rec.Header().Del("Last-Modified") // useless for dynamic content since it's always changing |
| 473 | |
| 474 | // we don't know a way to quickly generate etag for dynamic content, |
| 475 | // and weak etags still cause browsers to rely on it even after a |
| 476 | // refresh, so disable them until we find a better way to do this |
| 477 | rec.Header().Del("Etag") |
| 478 | |
| 479 | return rec.WriteResponse() |
| 480 | } |
| 481 | |
| 482 | // executeTemplate executes the template contained in wb.buf and replaces it with the results. |
| 483 | func (t *Templates) executeTemplate(rr caddyhttp.ResponseRecorder, r *http.Request) error { |
nothing calls this directly
no test coverage detected