Exemplo n.º 1
0
// onDirectoryListingForbiddenHandler replies to the request with an HTTP 403
// forbidden error. This function is triggered when a static path was
// requested, but the path is a directory without a suitable index file
func (s *Server) onDirectoryListingForbiddenHandler(w http.ResponseWriter,
	req *http.Request) {
	context := s.captureRequest(w, req, s.DirectoryListingForbiddenHandler)

	context.Output.Status = http.StatusForbidden

	s.logger.Context(logger.Fields{
		"method":      req.Method,
		"requestPath": req.URL.Path,
		"statusCode":  403,
	}).Debug("Directory listing forbidden")

	if seekOnDirectoryListingForbiddenHandler {
		template := Settings.SystemTemplates["onDirectoryListingForbiddenHandler"]
		err := context.HTMLTemplate(template, nil)
		if err != nil {
			s.logger.Context(logger.Fields{
				"template": template,
			}).Warn("Failed single attempt to load configured onDirectoryListingForbiddenHandler template--serving default response")
			seekOnDirectoryListingForbiddenHandler = false
		}
	}

	if !seekOnDirectoryListingForbiddenHandler {
		context.Output.Body([]byte(defaultResponseDirectoryListingForbidden))
	}
}
Exemplo n.º 2
0
// onMissingHandler replies to the request with an HTTP 404 not found error.
// This function is triggered when we are unable to match a route.
func (s *Server) onMissingHandler(w http.ResponseWriter, req *http.Request) {
	context := s.captureRequest(w, req, s.MissingHandler)

	context.Output.Status = http.StatusNotFound

	s.logger.Context(logger.Fields{"method": req.Method, "requestPath": req.URL.Path, "statusCode": 404}).Debug("Handler not found")

	if seekOnMissingHandler {
		template := Settings.SystemTemplates["onMissingHandler"]
		err := context.HTMLTemplate(template, nil)
		if err != nil {
			s.logger.Context(logger.Fields{"template": template}).Warn("Failed single attempt to load configured onMissingHandler template--serving default response")
			seekOnMissingHandler = false
		}
	}

	if !seekOnMissingHandler {
		context.Output.Body([]byte(defaultResponse404))
	}
}