Commit 244e1b98 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Create internal/errorpage package

parent 14cdbd60
package main package main
import ( import (
"./internal/helper"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"path/filepath" "path/filepath"
...@@ -15,7 +16,7 @@ func handleDeployPage(documentRoot *string, handler http.Handler) http.HandlerFu ...@@ -15,7 +16,7 @@ func handleDeployPage(documentRoot *string, handler http.Handler) http.HandlerFu
return return
} }
setNoCacheHeaders(w.Header()) helper.SetNoCacheHeaders(w.Header())
w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write(data) w.Write(data)
......
package main package main
import ( import (
"./internal/errorpage"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
...@@ -25,7 +26,7 @@ func TestIfErrorPageIsPresented(t *testing.T) { ...@@ -25,7 +26,7 @@ func TestIfErrorPageIsPresented(t *testing.T) {
w.WriteHeader(404) w.WriteHeader(404)
fmt.Fprint(w, "Not Found") fmt.Fprint(w, "Not Found")
}) })
handleRailsError(&dir, h)(w, nil) errorpage.Inject(dir, h)(w, nil)
w.Flush() w.Flush()
assertResponseCode(t, w, 404) assertResponseCode(t, w, 404)
...@@ -45,7 +46,7 @@ func TestIfErrorPassedIfNoErrorPageIsFound(t *testing.T) { ...@@ -45,7 +46,7 @@ func TestIfErrorPassedIfNoErrorPageIsFound(t *testing.T) {
w.WriteHeader(404) w.WriteHeader(404)
fmt.Fprint(w, errorResponse) fmt.Fprint(w, errorResponse)
}) })
handleRailsError(&dir, h)(w, nil) errorpage.Inject(dir, h)(w, nil)
w.Flush() w.Flush()
assertResponseCode(t, w, 404) assertResponseCode(t, w, 404)
......
...@@ -54,12 +54,6 @@ func cleanUpProcessGroup(cmd *exec.Cmd) { ...@@ -54,12 +54,6 @@ func cleanUpProcessGroup(cmd *exec.Cmd) {
cmd.Wait() cmd.Wait()
} }
func setNoCacheHeaders(header http.Header) {
header.Set("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate")
header.Set("Pragma", "no-cache")
header.Set("Expires", "Fri, 01 Jan 1990 00:00:00 GMT")
}
// Borrowed from: net/http/server.go // Borrowed from: net/http/server.go
// Return the canonical path for p, eliminating . and .. elements. // Return the canonical path for p, eliminating . and .. elements.
func cleanURIPath(p string) string { func cleanURIPath(p string) string {
......
package main package errorpage
import ( import (
"../helper"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
...@@ -12,7 +13,7 @@ type errorPageResponseWriter struct { ...@@ -12,7 +13,7 @@ type errorPageResponseWriter struct {
rw http.ResponseWriter rw http.ResponseWriter
status int status int
hijacked bool hijacked bool
path *string path string
} }
func (s *errorPageResponseWriter) Header() http.Header { func (s *errorPageResponseWriter) Header() http.Header {
...@@ -37,14 +38,14 @@ func (s *errorPageResponseWriter) WriteHeader(status int) { ...@@ -37,14 +38,14 @@ func (s *errorPageResponseWriter) WriteHeader(status int) {
s.status = status s.status = status
if 400 <= s.status && s.status <= 599 { if 400 <= s.status && s.status <= 599 {
errorPageFile := filepath.Join(*s.path, fmt.Sprintf("%d.html", s.status)) errorPageFile := filepath.Join(s.path, fmt.Sprintf("%d.html", s.status))
// check if custom error page exists, serve this page instead // check if custom error page exists, serve this page instead
if data, err := ioutil.ReadFile(errorPageFile); err == nil { if data, err := ioutil.ReadFile(errorPageFile); err == nil {
s.hijacked = true s.hijacked = true
log.Printf("ErrorPage: serving predefined error page: %d", s.status) log.Printf("ErrorPage: serving predefined error page: %d", s.status)
setNoCacheHeaders(s.rw.Header()) helper.SetNoCacheHeaders(s.rw.Header())
s.rw.Header().Set("Content-Type", "text/html; charset=utf-8") s.rw.Header().Set("Content-Type", "text/html; charset=utf-8")
s.rw.WriteHeader(s.status) s.rw.WriteHeader(s.status)
s.rw.Write(data) s.rw.Write(data)
...@@ -59,7 +60,7 @@ func (s *errorPageResponseWriter) Flush() { ...@@ -59,7 +60,7 @@ func (s *errorPageResponseWriter) Flush() {
s.WriteHeader(http.StatusOK) s.WriteHeader(http.StatusOK)
} }
func handleRailsError(documentRoot *string, handler http.Handler) http.HandlerFunc { func Inject(documentRoot string, handler http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
rw := errorPageResponseWriter{ rw := errorPageResponseWriter{
rw: w, rw: w,
......
...@@ -16,6 +16,12 @@ func LogError(err error) { ...@@ -16,6 +16,12 @@ func LogError(err error) {
log.Printf("error: %v", err) log.Printf("error: %v", err)
} }
func SetNoCacheHeaders(header http.Header) {
header.Set("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate")
header.Set("Pragma", "no-cache")
header.Set("Expires", "Fri, 01 Jan 1990 00:00:00 GMT")
}
func OpenFile(path string) (file *os.File, fi os.FileInfo, err error) { func OpenFile(path string) (file *os.File, fi os.FileInfo, err error) {
file, err = os.Open(path) file, err = os.Open(path)
if err != nil { if err != nil {
......
...@@ -14,6 +14,7 @@ In this file we start the web server and hand off to the upstream type. ...@@ -14,6 +14,7 @@ In this file we start the web server and hand off to the upstream type.
package main package main
import ( import (
"./internal/errorpage"
"flag" "flag"
"fmt" "fmt"
"log" "log"
...@@ -95,7 +96,7 @@ func compileRoutes(u *upstream) { ...@@ -95,7 +96,7 @@ func compileRoutes(u *upstream) {
u.handleServeFile(documentRoot, CacheExpireMax, u.handleServeFile(documentRoot, CacheExpireMax,
handleDevelopmentMode(developmentMode, handleDevelopmentMode(developmentMode,
handleDeployPage(documentRoot, handleDeployPage(documentRoot,
handleRailsError(documentRoot, errorpage.Inject(*documentRoot,
proxy, proxy,
), ),
), ),
...@@ -107,7 +108,7 @@ func compileRoutes(u *upstream) { ...@@ -107,7 +108,7 @@ func compileRoutes(u *upstream) {
httpRoute{"", nil, httpRoute{"", nil,
u.handleServeFile(documentRoot, CacheDisabled, u.handleServeFile(documentRoot, CacheDisabled,
handleDeployPage(documentRoot, handleDeployPage(documentRoot,
handleRailsError(documentRoot, errorpage.Inject(*documentRoot,
proxy, proxy,
), ),
), ),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment