mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-03 19:17:29 -05:00 
			
		
		
		
	templates: Merge pull request #1482 from crvv/master
templates: Set MIME type
This commit is contained in:
		
						commit
						2be0dc40f0
					
				@ -4,6 +4,7 @@ package templates
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
 | 
						"mime"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"path"
 | 
						"path"
 | 
				
			||||||
@ -62,6 +63,16 @@ func (t Templates) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
 | 
				
			|||||||
					return http.StatusInternalServerError, err
 | 
										return http.StatusInternalServerError, err
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									// If Content-Type isn't set here, http.ResponseWriter.Write
 | 
				
			||||||
 | 
									// will set it according to response body. But other middleware
 | 
				
			||||||
 | 
									// such as gzip can modify response body, then Content-Type
 | 
				
			||||||
 | 
									// detected by http.ResponseWriter.Write is wrong.
 | 
				
			||||||
 | 
									ctype := mime.TypeByExtension(ext)
 | 
				
			||||||
 | 
									if ctype == "" {
 | 
				
			||||||
 | 
										ctype = http.DetectContentType(buf.Bytes())
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									w.Header().Set("Content-Type", ctype)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				templateInfo, err := os.Stat(templatePath)
 | 
									templateInfo, err := os.Stat(templatePath)
 | 
				
			||||||
				if err == nil {
 | 
									if err == nil {
 | 
				
			||||||
					// add the Last-Modified header if we were able to read the stamp
 | 
										// add the Last-Modified header if we were able to read the stamp
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user