FAQ
I had the same issue, and had been doing a workaround by checking the URL
before passing to FileServer, but of course that isn't great - too many
static files, too few 404's. Here's an implementation overriding the
WriteHeader of http.ResponseWriter:


     type hijack404 struct {
         http.ResponseWriter
         R *http.Request
         Handle404 func (w http.ResponseWriter, r *http.Request) bool
     }

     func (h *hijack404) WriteHeader(code int) {
         if 404==code && h.Handle404(h.ResponseWriter, h.R) {
             panic(h)
         }
         h.ResponseWriter.WriteHeader(code)
     }

     // Handle404 will pass any 404's from the handler to the handle404
     // function. If handle404 returns true, the response is considered
complete,
     // and the processing by handler is aborted.
     func Handle404(handler http.Handler, handle404 func (w
http.ResponseWriter, r *http.Request) bool) http.Handler {
         return http.HandlerFunc(func (w http.ResponseWriter, r
*http.Request) {
             hijack := &hijack404{ ResponseWriter:w, R: r, Handle404:
handle404 }
             defer func() {
                 if p:=recover(); p!=nil {
                     if p==hijack {
                         return
                     }
                     panic(p)
                 }
             }()
             handler.ServeHTTP(hijack, r)
         })
     }

It works with the FileServer, and I suppose should work with other handlers
as well, as long as they call WriteHeader before any other output methods.
Any comments and feedback welcome.

All the best,
Craig

On Friday, April 20, 2012 5:21:29 PM UTC+2, Vasiliy Tolstov wrote:

Hello golang!

I'm try to use http.FileServer for static files like ico, jpg and
other. And i need to set my own 404 error page for absent files.
Please, add like in http.NotFoundHandler ability to set own function for
error.
(Now i need to copy/paste some parts from
golang.org/src/pkg/net/http/fs.go to create own handler...
Thanks!

--
Vasiliy Tolstov,
Clodo.ru
e-mail: v.to...@selfip.ru <javascript:>
jabber: va...@selfip.ru <javascript:>
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Dan Nuzzo at Jan 3, 2014 at 4:09 am
    I'm using gorillia pat with FileServer and cannot for the life of me figure
    out why I'm getting a 404,

      r.Add("GET","/media/",http.StripPrefix("/media/",
    http.FileServer(http.Dir(DownloadDir))))

    still kind of new at the golang and http, how would i use what you have
    done here to debug my issue.
    On Thursday, September 12, 2013 12:42:51 AM UTC-4, Craig Mason-Jones wrote:

    I had the same issue, and had been doing a workaround by checking the URL
    before passing to FileServer, but of course that isn't great - too many
    static files, too few 404's. Here's an implementation overriding the
    WriteHeader of http.ResponseWriter:


    type hijack404 struct {
    http.ResponseWriter
    R *http.Request
    Handle404 func (w http.ResponseWriter, r *http.Request) bool
    }

    func (h *hijack404) WriteHeader(code int) {
    if 404==code && h.Handle404(h.ResponseWriter, h.R) {
    panic(h)
    }
    h.ResponseWriter.WriteHeader(code)
    }

    // Handle404 will pass any 404's from the handler to the handle404
    // function. If handle404 returns true, the response is considered
    complete,
    // and the processing by handler is aborted.
    func Handle404(handler http.Handler, handle404 func (w
    http.ResponseWriter, r *http.Request) bool) http.Handler {
    return http.HandlerFunc(func (w http.ResponseWriter, r
    *http.Request) {
    hijack := &hijack404{ ResponseWriter:w, R: r, Handle404:
    handle404 }
    defer func() {
    if p:=recover(); p!=nil {
    if p==hijack {
    return
    }
    panic(p)
    }
    }()
    handler.ServeHTTP(hijack, r)
    })
    }

    It works with the FileServer, and I suppose should work with other
    handlers as well, as long as they call WriteHeader before any other output
    methods. Any comments and feedback welcome.

    All the best,
    Craig

    On Friday, April 20, 2012 5:21:29 PM UTC+2, Vasiliy Tolstov wrote:

    Hello golang!

    I'm try to use http.FileServer for static files like ico, jpg and
    other. And i need to set my own 404 error page for absent files.
    Please, add like in http.NotFoundHandler ability to set own function for
    error.
    (Now i need to copy/paste some parts from
    golang.org/src/pkg/net/http/fs.go to create own handler...
    Thanks!

    --
    Vasiliy Tolstov,
    Clodo.ru
    e-mail: v.to...@selfip.ru
    jabber: va...@selfip.ru
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • DisposaBoy at Jan 3, 2014 at 7:42 am
    A wild guess: you shouldn't be stripping the trailing slash

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dan Nuzzo at Jan 3, 2014 at 12:06 pm
    No,

    I after looking through the FileServer code I found my problem, I'm on
    windows, and I was trying to use a the windows \ in the path, not the unix
    / in the path,
    but there was a comment in the FileServer code that stats the path should
    always be / not matter what OS.
    On Friday, January 3, 2014 2:42:43 AM UTC-5, DisposaBoy wrote:

    A wild guess: you shouldn't be stripping the trailing slash
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 12, '13 at 4:42a
activeJan 3, '14 at 12:06p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase