fix: don't return 404 if the prefix doesn't exist
License: MIT Signed-off-by: Henrique Dias <hacdias@gmail.com>
This commit is contained in:
		
							parent
							
								
									df5fc427ef
								
							
						
					
					
						commit
						72e74d421c
					
				| 
						 | 
				
			
			@ -60,5 +60,5 @@ func NewHandler(storage *storage.Storage, server *settings.Server) (http.Handler
 | 
			
		|||
	public.PathPrefix("/dl").Handler(monkey(publicDlHandler, "/api/public/dl/")).Methods("GET")
 | 
			
		||||
	public.PathPrefix("/share").Handler(monkey(publicShareHandler, "/api/public/share/")).Methods("GET")
 | 
			
		||||
 | 
			
		||||
	return http.StripPrefix(server.BaseURL, r), nil
 | 
			
		||||
	return stripPrefix(server.BaseURL, r), nil
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,7 +3,9 @@ package http
 | 
			
		|||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"net/url"
 | 
			
		||||
	"os"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/filebrowser/filebrowser/v2/errors"
 | 
			
		||||
)
 | 
			
		||||
| 
						 | 
				
			
			@ -37,3 +39,21 @@ func errToStatus(err error) int {
 | 
			
		|||
		return http.StatusInternalServerError
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// This is an addaptation if http.StripPrefix in which we don't
 | 
			
		||||
// return 404 if the page doesn't have the needed prefix.
 | 
			
		||||
func stripPrefix(prefix string, h http.Handler) http.Handler {
 | 
			
		||||
	if prefix == "" {
 | 
			
		||||
		return h
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		p := strings.TrimPrefix(r.URL.Path, prefix)
 | 
			
		||||
		r2 := new(http.Request)
 | 
			
		||||
		*r2 = *r
 | 
			
		||||
		r2.URL = new(url.URL)
 | 
			
		||||
		*r2.URL = *r.URL
 | 
			
		||||
		r2.URL.Path = p
 | 
			
		||||
		h.ServeHTTP(w, r2)
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue