1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-20 09:26:23 -07:00

use Header().Set instead of Header().Add for replacing HTTP headers

This commit is contained in:
Eggbertx 2022-08-05 15:26:22 -07:00
parent 1265576967
commit 4c867d2180
2 changed files with 42 additions and 38 deletions

View file

@ -46,7 +46,6 @@ func (s gochanServer) serveFile(writer http.ResponseWriter, request *http.Reques
}
//the file exists, or there is a folder here
var extension string
if results.IsDir() {
//check to see if one of the specified index pages exists
var found bool
@ -63,49 +62,53 @@ func (s gochanServer) serveFile(writer http.ResponseWriter, request *http.Reques
serverutil.ServeNotFound(writer, request)
return
}
} else {
//the file exists, and is not a folder
extension = strings.ToLower(gcutil.GetFileExtension(request.URL.Path))
switch extension {
case "png":
writer.Header().Add("Content-Type", "image/png")
writer.Header().Add("Cache-Control", "max-age=86400")
case "gif":
writer.Header().Add("Content-Type", "image/gif")
writer.Header().Add("Cache-Control", "max-age=86400")
case "jpg":
fallthrough
case "jpeg":
writer.Header().Add("Content-Type", "image/jpeg")
writer.Header().Add("Cache-Control", "max-age=86400")
case "css":
writer.Header().Add("Content-Type", "text/css")
writer.Header().Add("Cache-Control", "max-age=43200")
case "js":
writer.Header().Add("Content-Type", "text/javascript")
writer.Header().Add("Cache-Control", "max-age=43200")
case "json":
writer.Header().Add("Content-Type", "application/json")
writer.Header().Add("Cache-Control", "max-age=5, must-revalidate")
case "webm":
writer.Header().Add("Content-Type", "video/webm")
writer.Header().Add("Cache-Control", "max-age=86400")
case "htm":
fallthrough
case "html":
writer.Header().Add("Content-Type", "text/html")
writer.Header().Add("Cache-Control", "max-age=5, must-revalidate")
}
gclog.Printf(gclog.LAccessLog, "Success: 200 from %s @ %s", gcutil.GetRealIP(request), request.URL.Path)
}
s.setFileHeaders(filePath, writer)
// serve the index page
writer.Header().Add("Cache-Control", "max-age=5, must-revalidate")
// serve the requested file
fileBytes, _ = ioutil.ReadFile(filePath)
writer.Header().Add("Cache-Control", "max-age=86400")
gclog.Printf(gclog.LAccessLog, "Success: 200 from %s @ %s", gcutil.GetRealIP(request), request.URL.Path)
writer.Write(fileBytes)
}
// set mime type/cache headers according to the file's extension
func (s *gochanServer) setFileHeaders(filename string, writer http.ResponseWriter) {
extension := strings.ToLower(gcutil.GetFileExtension(filename))
switch extension {
case "png":
writer.Header().Set("Content-Type", "image/png")
writer.Header().Set("Cache-Control", "max-age=86400")
case "gif":
writer.Header().Set("Content-Type", "image/gif")
writer.Header().Set("Cache-Control", "max-age=86400")
case "jpg":
fallthrough
case "jpeg":
writer.Header().Set("Content-Type", "image/jpeg")
writer.Header().Set("Cache-Control", "max-age=86400")
case "css":
writer.Header().Set("Content-Type", "text/css")
writer.Header().Set("Cache-Control", "max-age=43200")
case "js":
writer.Header().Set("Content-Type", "text/javascript")
writer.Header().Set("Cache-Control", "max-age=43200")
case "json":
writer.Header().Set("Content-Type", "application/json")
writer.Header().Set("Cache-Control", "max-age=5, must-revalidate")
case "webm":
writer.Header().Set("Content-Type", "video/webm")
writer.Header().Set("Cache-Control", "max-age=86400")
case "htm":
fallthrough
case "html":
writer.Header().Set("Content-Type", "text/html")
writer.Header().Set("Cache-Control", "max-age=5, must-revalidate")
default:
writer.Header().Set("Content-Type", "application/octet-stream")
writer.Header().Set("Cache-Control", "max-age=86400")
}
}
func (s gochanServer) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
for name, namespaceFunction := range s.namespaces {
if request.URL.Path == config.WebPath(name) {

View file

@ -263,6 +263,7 @@ function updateThreadHTML() {
console.log(`added post #${post.no}`);
numAdded++;
}
if(numAdded == 0) return;
console.log(`Added ${numAdded} posts`);
}