Rename and only return 404 when not found

This commit is contained in:
Ben Adrian Sarmiento
2024-07-19 00:26:11 +02:00
parent f6b6ae113e
commit eaabd20a83
2 changed files with 86 additions and 89 deletions

View File

@@ -65,46 +65,53 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t
router.Post("/torrents/repair", hs.handleTriggerRepairAll)
router.Post("/torrents/reset-repair-state", hs.handleResetRepairState)
// version
router.Get(fmt.Sprintf("/{mountType}/%s", version.FILE), hs.handleVersionFile)
router.Head(fmt.Sprintf("/{mountType}/%s", version.FILE), hs.handleCheckVersionFile)
router.Get("/{mountType}/version.txt", hs.handleVersionFile)
router.Head("/{mountType}/version.txt", hs.handleCheckVersionFile)
// download
router.Get(fmt.Sprintf("/{mountType}/%s/{filename}", config.DOWNLOADS), hs.handleDownloadLink)
router.Head(fmt.Sprintf("/{mountType}/%s/{filename}", config.DOWNLOADS), hs.handleCheckDownloadLink)
router.Get("/{mountType}/__downloads__/{filename}", hs.handleDownloadLink)
router.Head("/{mountType}/__downloads__/{filename}", hs.handleCheckDownloadLink)
// file
router.Get("/{mountType}/{directory}/{torrent}/{filename}", hs.handleDownloadFile)
router.Head("/{mountType}/{directory}/{torrent}/{filename}", hs.handleCheckFile)
router.Get("/http/", hs.handleHttpRoot)
router.Get(fmt.Sprintf("/http/%s/", config.DOWNLOADS), hs.handleHttpDownloadsList)
router.Get("/http/{directory}/", hs.handleHttpTorrentsList)
router.Get("/http/{directory}/{torrent}/", hs.handleHttpFilesList)
// http
router.Get("/http/", hs.handleHttpRootDirectory)
router.Get("/http/__downloads__/", hs.handleHttpDownloads)
router.Get("/http/{directory}/", hs.handleHttpGroupDirectory)
router.Get("/http/{directory}/{torrent}/", hs.handleHttpTorrentFiles)
router.Get("/dav/", hs.handleDavRoot)
router.Get(fmt.Sprintf("/dav/%s/", config.DOWNLOADS), hs.handleDavDownloadsList)
router.Get("/dav/{directory}/", hs.handleDavTorrentsList)
router.Get("/dav/{directory}/{torrent}/", hs.handleDavFilesList)
router.MethodFunc("PROPFIND", "/dav/", hs.handleDavRoot)
router.MethodFunc("PROPFIND", fmt.Sprintf("/dav/%s/", config.DOWNLOADS), hs.handleDavDownloadsList)
router.MethodFunc("PROPFIND", "/dav/{directory}/", hs.handleDavTorrentsList)
router.MethodFunc("PROPFIND", "/dav/{directory}/{torrent}/", hs.handleDavFilesList)
// dav
router.Get("/dav/", hs.handleDavRootDirectory)
router.Get("/dav/__downloads__/", hs.handleDavDownloads)
router.Get("/dav/{directory}/", hs.handleDavGroupDirectory)
router.Get("/dav/{directory}/{torrent}/", hs.handleDavTorrentFiles)
router.Get("/infuse/", hs.handleInfuseRoot)
router.Get(fmt.Sprintf("/infuse/%s/", config.DOWNLOADS), hs.handleInfuseDownloadsList)
router.Get("/infuse/{directory}/", hs.handleInfuseTorrentsList)
router.Get("/infuse/{directory}/{torrent}/", hs.handleInfuseFilesList)
router.MethodFunc("PROPFIND", "/infuse/", hs.handleInfuseRoot)
router.MethodFunc("PROPFIND", fmt.Sprintf("/infuse/%s/", config.DOWNLOADS), hs.handleInfuseDownloadsList)
router.MethodFunc("PROPFIND", "/infuse/{directory}/", hs.handleInfuseTorrentsList)
router.MethodFunc("PROPFIND", "/infuse/{directory}/{torrent}/", hs.handleInfuseFilesList)
router.MethodFunc("PROPFIND", "/dav/", hs.handleDavRootDirectory)
router.MethodFunc("PROPFIND", "/dav/__downloads__/", hs.handleDavDownloads)
router.MethodFunc("PROPFIND", "/dav/{directory}/", hs.handleDavGroupDirectory)
router.MethodFunc("PROPFIND", "/dav/{directory}/{torrent}/", hs.handleDavTorrentFiles)
router.Get("/vidhub/", hs.handleVidHubRoot)
router.Get(fmt.Sprintf("/vidhub/%s/", config.DOWNLOADS), hs.handleVidHubDownloadsList)
router.Get("/vidhub/{directory}/", hs.handleVidHubTorrentsList)
router.Get("/vidhub/{directory}/{torrent}/", hs.handleVidHubFilesList)
router.MethodFunc("PROPFIND", "/vidhub/", hs.handleVidHubRoot)
router.MethodFunc("PROPFIND", fmt.Sprintf("/vidhub/%s", config.DOWNLOADS), hs.handleVidHubDownloadsList)
router.MethodFunc("PROPFIND", "/vidhub/{directory}", hs.handleVidHubTorrentsList)
router.MethodFunc("PROPFIND", "/vidhub/{directory}/{torrent}", hs.handleVidHubFilesList)
// infuse
router.Get("/infuse/", hs.handleInfuseRootDirectory)
router.Get("/infuse/__downloads__/", hs.handleInfuseDownloads)
router.Get("/infuse/{directory}/", hs.handleInfuseGroupDirectory)
router.Get("/infuse/{directory}/{torrent}/", hs.handleInfuseTorrentFiles)
router.MethodFunc("PROPFIND", "/infuse/", hs.handleInfuseRootDirectory)
router.MethodFunc("PROPFIND", "/infuse/__downloads__/", hs.handleInfuseDownloads)
router.MethodFunc("PROPFIND", "/infuse/{directory}/", hs.handleInfuseGroupDirectory)
router.MethodFunc("PROPFIND", "/infuse/{directory}/{torrent}/", hs.handleInfuseTorrentFiles)
// vidhub
router.Get("/vidhub/", hs.handleVidHubRootDirectory)
router.Get("/vidhub/__downloads__/", hs.handleVidHubDownloads)
router.Get("/vidhub/{directory}/", hs.handleVidHubGroupDirectory)
router.Get("/vidhub/{directory}/{torrent}/", hs.handleVidHubTorrentFiles)
router.MethodFunc("PROPFIND", "/vidhub/", hs.handleVidHubRootDirectory)
router.MethodFunc("PROPFIND", "/vidhub/__downloads__", hs.handleVidHubDownloads)
router.MethodFunc("PROPFIND", "/vidhub/{directory}", hs.handleVidHubGroupDirectory)
router.MethodFunc("PROPFIND", "/vidhub/{directory}/{torrent}", hs.handleVidHubTorrentFiles)
// note: reused handlers for dav and infuse
router.MethodFunc("PROPFIND", "/{mountType}/{directory}/{torrent}/{filename}", hs.checkSingleFileHandler)
@@ -121,21 +128,17 @@ func AttachHandlers(router *chi.Mux, downloader *universal.Downloader, torMgr *t
router.Get("/logs/upload/", hs.uploadLogsHandler)
router.MethodNotAllowed(func(resp http.ResponseWriter, req *http.Request) {
hs.log.Debugf("Method not allowed: %s %s %v", req.Method, req.URL, req.Header)
hs.log.Debugf("Method not allowed: %s %s (error=%v)", req.Method, req.URL, req.Header)
http.Error(resp, "Method not allowed", http.StatusMethodNotAllowed)
})
}
// handle root request
func (hs *Handlers) innerRootHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(*torrent.TorrentManager) ([]byte, error), contentType string) {
func (hs *Handlers) innerRootDirectoryHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(*torrent.TorrentManager) ([]byte, error), contentType string) {
out, err := handleFunc(hs.torMgr)
if err != nil && strings.Contains(contentType, "xml") {
hs.log.Debugf("Not implemented: %s %s", req.Method, req.URL)
resp.WriteHeader(http.StatusNotImplemented)
resp.Write([]byte("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:error xmlns:d=\"DAV:\" xmlns:s=\"DAV\"><s:exception>NotImplemented</s:exception><s:message>Not Implemented Method</s:message></d:error>"))
return
} else if err != nil {
if err != nil {
hs.log.Errorf("Not found: %s %s (error=%v)", req.Method, req.URL, err)
http.NotFound(resp, req)
return
}
@@ -148,36 +151,32 @@ func (hs *Handlers) innerRootHandler(resp http.ResponseWriter, req *http.Request
resp.Write(out)
}
func (hs *Handlers) handleHttpRoot(resp http.ResponseWriter, req *http.Request) {
hs.innerRootHandler(resp, req, intHttp.ServeRootDirectory, "text/html; charset=\"utf-8\"")
func (hs *Handlers) handleHttpRootDirectory(resp http.ResponseWriter, req *http.Request) {
hs.innerRootDirectoryHandler(resp, req, intHttp.ServeRootDirectory, "text/html; charset=\"utf-8\"")
}
func (hs *Handlers) handleDavRoot(resp http.ResponseWriter, req *http.Request) {
hs.innerRootHandler(resp, req, dav.ServeRootDirectoryForDav, "text/xml; charset=\"utf-8\"")
func (hs *Handlers) handleDavRootDirectory(resp http.ResponseWriter, req *http.Request) {
hs.innerRootDirectoryHandler(resp, req, dav.ServeRootDirectoryForDav, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleInfuseRoot(resp http.ResponseWriter, req *http.Request) {
hs.innerRootHandler(resp, req, dav.ServeRootDirectoryForInfuse, "text/xml; charset=\"utf-8\"")
func (hs *Handlers) handleInfuseRootDirectory(resp http.ResponseWriter, req *http.Request) {
hs.innerRootDirectoryHandler(resp, req, dav.ServeRootDirectoryForInfuse, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleVidHubRoot(resp http.ResponseWriter, req *http.Request) {
hs.innerRootHandler(resp, req, dav.ServeRootDirectoryForVidHub, "text/xml; charset=\"utf-8\"")
func (hs *Handlers) handleVidHubRootDirectory(resp http.ResponseWriter, req *http.Request) {
hs.innerRootDirectoryHandler(resp, req, dav.ServeRootDirectoryForVidHub, "text/xml; charset=\"utf-8\"")
}
// handle torrent list request
func (hs *Handlers) innerTorrentsListHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(string, *torrent.TorrentManager) ([]byte, error), contentType string) {
func (hs *Handlers) innerGroupDirectoryHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(string, *torrent.TorrentManager) ([]byte, error), contentType string) {
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
out, err := handleFunc(directory, hs.torMgr)
if err != nil && strings.Contains(contentType, "xml") {
hs.log.Debugf("Not implemented: %s %s", req.Method, req.URL)
resp.WriteHeader(http.StatusNotImplemented)
resp.Write([]byte("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:error xmlns:d=\"DAV:\" xmlns:s=\"DAV\"><s:exception>NotImplemented</s:exception><s:message>Not Implemented Method</s:message></d:error>"))
return
} else if err != nil {
if err != nil {
hs.log.Errorf("Not found: %s %s (error=%v)", req.Method, req.URL, err)
http.NotFound(resp, req)
return
}
@@ -190,25 +189,25 @@ func (hs *Handlers) innerTorrentsListHandler(resp http.ResponseWriter, req *http
resp.Write(out)
}
func (hs *Handlers) handleHttpTorrentsList(resp http.ResponseWriter, req *http.Request) {
hs.innerTorrentsListHandler(resp, req, intHttp.ServeGroupDirectory, "text/html; charset=\"utf-8\"")
func (hs *Handlers) handleHttpGroupDirectory(resp http.ResponseWriter, req *http.Request) {
hs.innerGroupDirectoryHandler(resp, req, intHttp.ServeGroupDirectory, "text/html; charset=\"utf-8\"")
}
func (hs *Handlers) handleDavTorrentsList(resp http.ResponseWriter, req *http.Request) {
hs.innerTorrentsListHandler(resp, req, dav.ServeGroupDirectoryForDav, "text/xml; charset=\"utf-8\"")
func (hs *Handlers) handleDavGroupDirectory(resp http.ResponseWriter, req *http.Request) {
hs.innerGroupDirectoryHandler(resp, req, dav.ServeGroupDirectoryForDav, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleInfuseTorrentsList(resp http.ResponseWriter, req *http.Request) {
hs.innerTorrentsListHandler(resp, req, dav.ServeGroupDirectoryForInfuse, "text/xml; charset=\"utf-8\"")
func (hs *Handlers) handleInfuseGroupDirectory(resp http.ResponseWriter, req *http.Request) {
hs.innerGroupDirectoryHandler(resp, req, dav.ServeGroupDirectoryForInfuse, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleVidHubTorrentsList(resp http.ResponseWriter, req *http.Request) {
hs.innerTorrentsListHandler(resp, req, dav.ServeGroupDirectoryForVidHub, "text/xml; charset=\"utf-8\"")
func (hs *Handlers) handleVidHubGroupDirectory(resp http.ResponseWriter, req *http.Request) {
hs.innerGroupDirectoryHandler(resp, req, dav.ServeGroupDirectoryForVidHub, "text/xml; charset=\"utf-8\"")
}
// handle files list request
func (hs *Handlers) innerFilesListHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(string, string, *torrent.TorrentManager, bool) ([]byte, error), contentType string) {
func (hs *Handlers) innerTorrentFilesHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(string, string, *torrent.TorrentManager, bool) ([]byte, error), contentType string) {
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
@@ -218,12 +217,8 @@ func (hs *Handlers) innerFilesListHandler(resp http.ResponseWriter, req *http.Re
torrentName = chi.URLParam(req, "torrent")
}
out, err := handleFunc(directory, torrentName, hs.torMgr, hs.cfg.ShouldHideBrokenTorrents())
if err != nil && strings.Contains(contentType, "xml") {
hs.log.Debugf("Not implemented: %s %s", req.Method, req.URL)
resp.WriteHeader(http.StatusNotImplemented)
resp.Write([]byte("<?xml version=\"1.0\" encoding=\"utf-8\"?><d:error xmlns:d=\"DAV:\" xmlns:s=\"DAV\"><s:exception>NotImplemented</s:exception><s:message>Not Implemented Method</s:message></d:error>"))
return
} else if err != nil {
if err != nil {
hs.log.Errorf("Not found: %s %s (error=%v)", req.Method, req.URL, err)
http.NotFound(resp, req)
return
}
@@ -236,38 +231,38 @@ func (hs *Handlers) innerFilesListHandler(resp http.ResponseWriter, req *http.Re
resp.Write(out)
}
func (hs *Handlers) handleHttpFilesList(resp http.ResponseWriter, req *http.Request) {
hs.innerFilesListHandler(resp, req, intHttp.ServeFilesList, "text/html; charset=\"utf-8\"")
func (hs *Handlers) handleHttpTorrentFiles(resp http.ResponseWriter, req *http.Request) {
hs.innerTorrentFilesHandler(resp, req, intHttp.ServeTorrentFiles, "text/html; charset=\"utf-8\"")
}
func (hs *Handlers) handleDavFilesList(resp http.ResponseWriter, req *http.Request) {
hs.innerFilesListHandler(resp, req, dav.ServeTorrentFilesForDav, "text/xml; charset=\"utf-8\"")
func (hs *Handlers) handleDavTorrentFiles(resp http.ResponseWriter, req *http.Request) {
hs.innerTorrentFilesHandler(resp, req, dav.ServeTorrentFilesForDav, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleInfuseFilesList(resp http.ResponseWriter, req *http.Request) {
hs.innerFilesListHandler(resp, req, dav.ServeTorrentFilesForInfuse, "text/xml; charset=\"utf-8\"")
func (hs *Handlers) handleInfuseTorrentFiles(resp http.ResponseWriter, req *http.Request) {
hs.innerTorrentFilesHandler(resp, req, dav.ServeTorrentFilesForInfuse, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleVidHubFilesList(resp http.ResponseWriter, req *http.Request) {
hs.innerFilesListHandler(resp, req, dav.ServeTorrentFilesForVidHub, "text/xml; charset=\"utf-8\"")
func (hs *Handlers) handleVidHubTorrentFiles(resp http.ResponseWriter, req *http.Request) {
hs.innerTorrentFilesHandler(resp, req, dav.ServeTorrentFilesForVidHub, "text/xml; charset=\"utf-8\"")
}
// handle downloads list request
func (hs *Handlers) handleHttpDownloadsList(resp http.ResponseWriter, req *http.Request) {
hs.innerTorrentsListHandler(resp, req, intHttp.ServeDownloadsList, "text/html; charset=\"utf-8\"")
func (hs *Handlers) handleHttpDownloads(resp http.ResponseWriter, req *http.Request) {
hs.innerGroupDirectoryHandler(resp, req, intHttp.ServeDownloads, "text/html; charset=\"utf-8\"")
}
func (hs *Handlers) handleDavDownloadsList(resp http.ResponseWriter, req *http.Request) {
hs.innerTorrentsListHandler(resp, req, dav.ServeDownloadsForDav, "text/xml; charset=\"utf-8\"")
func (hs *Handlers) handleDavDownloads(resp http.ResponseWriter, req *http.Request) {
hs.innerGroupDirectoryHandler(resp, req, dav.ServeDownloadsForDav, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleInfuseDownloadsList(resp http.ResponseWriter, req *http.Request) {
hs.innerTorrentsListHandler(resp, req, dav.ServeDownloadsForInfuse, "text/xml; charset=\"utf-8\"")
func (hs *Handlers) handleInfuseDownloads(resp http.ResponseWriter, req *http.Request) {
hs.innerGroupDirectoryHandler(resp, req, dav.ServeDownloadsForInfuse, "text/xml; charset=\"utf-8\"")
}
func (hs *Handlers) handleVidHubDownloadsList(resp http.ResponseWriter, req *http.Request) {
hs.innerTorrentsListHandler(resp, req, dav.ServeDownloadsForVidHub, "text/xml; charset=\"utf-8\"")
func (hs *Handlers) handleVidHubDownloads(resp http.ResponseWriter, req *http.Request) {
hs.innerGroupDirectoryHandler(resp, req, dav.ServeDownloadsForVidHub, "text/xml; charset=\"utf-8\"")
}
// handle delete request
@@ -469,6 +464,8 @@ func (hs *Handlers) uploadLogsHandler(resp http.ResponseWriter, req *http.Reques
}
func (hs *Handlers) handleNotFound(resp http.ResponseWriter, req *http.Request) {
hs.log.Debugf("Not found: %s %s", req.Method, req.URL)
if hs.cfg.ShouldLogRequests() {
hs.log.Debugf("Not found: %s %s", req.Method, req.URL)
}
http.NotFound(resp, req)
}

View File

@@ -51,7 +51,7 @@ func ServeGroupDirectory(directory string, torMgr *torrent.TorrentManager) ([]by
return buf.Bytes(), nil
}
func ServeFilesList(directory, torrentName string, torMgr *torrent.TorrentManager, shouldHideBrokenTorrents bool) ([]byte, error) {
func ServeTorrentFiles(directory, torrentName string, torMgr *torrent.TorrentManager, shouldHideBrokenTorrents bool) ([]byte, error) {
torrents, ok := torMgr.DirectoryMap.Get(directory)
if !ok {
return nil, fmt.Errorf("cannot find directory %s", directory)
@@ -94,7 +94,7 @@ func ServeFilesList(directory, torrentName string, torMgr *torrent.TorrentManage
return buf.Bytes(), nil
}
func ServeDownloadsList(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
func ServeDownloads(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
var buf bytes.Buffer
buf.WriteString("<ol>")
filenames := torMgr.DownloadMap.Keys()