MCPcopy
hub / github.com/caddyserver/caddy / ServeHTTP

Method ServeHTTP

modules/caddyhttp/templates/templates.go:438–480  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler)

Source from the content-addressed store, hash-verified

436}
437
438func (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.
483func (t *Templates) executeTemplate(rr caddyhttp.ResponseRecorder, r *http.Request) error {

Callers

nothing calls this directly

Calls 11

BufferedMethod · 0.95
executeTemplateMethod · 0.95
WriteResponseMethod · 0.95
NewResponseRecorderFunction · 0.92
DelMethod · 0.80
GetMethod · 0.65
ResetMethod · 0.65
ServeHTTPMethod · 0.65
SetMethod · 0.45
HeaderMethod · 0.45
LenMethod · 0.45

Tested by

no test coverage detected