Handle edge case of when url is not escapable

This commit is contained in:
Ben Sarmiento
2024-01-25 01:18:18 +01:00
parent 0ce5c36276
commit b2bd188736

View File

@@ -135,7 +135,10 @@ func (hs *Handlers) handleInfuseRoot(resp http.ResponseWriter, req *http.Request
// handle torrent list request
func (hs *Handlers) innerTorrentsListHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(string, *torrent.TorrentManager) ([]byte, error), contentType string) {
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
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)
@@ -156,7 +159,10 @@ func (hs *Handlers) innerTorrentsListHandler(resp http.ResponseWriter, req *http
}
func (hs *Handlers) handleHttpTorrentsList(resp http.ResponseWriter, req *http.Request) {
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
handlerFunc := intHttp.ServeTorrentsList
if directory == config.DOWNLOADS {
handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
@@ -167,7 +173,10 @@ func (hs *Handlers) handleHttpTorrentsList(resp http.ResponseWriter, req *http.R
}
func (hs *Handlers) handleDavTorrentsList(resp http.ResponseWriter, req *http.Request) {
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
handlerFunc := dav.ServeTorrentsList
if directory == config.DOWNLOADS {
handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
@@ -178,7 +187,10 @@ func (hs *Handlers) handleDavTorrentsList(resp http.ResponseWriter, req *http.Re
}
func (hs *Handlers) handleInfuseTorrentsList(resp http.ResponseWriter, req *http.Request) {
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
handlerFunc := dav.ServeTorrentsListForInfuse
if directory == config.DOWNLOADS {
handlerFunc = func(_ string, torMgr *torrent.TorrentManager) ([]byte, error) {
@@ -191,8 +203,14 @@ func (hs *Handlers) handleInfuseTorrentsList(resp http.ResponseWriter, req *http
// handle files list request
func (hs *Handlers) innerFilesListHandler(resp http.ResponseWriter, req *http.Request, handleFunc func(string, string, *torrent.TorrentManager) ([]byte, error), contentType string) {
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
torrentName, err := url.PathUnescape(chi.URLParam(req, "torrent"))
if err != nil {
torrentName = chi.URLParam(req, "torrent")
}
out, err := handleFunc(directory, torrentName, hs.torMgr)
if err != nil && strings.Contains(contentType, "xml") {
hs.log.Debugf("Not implemented: %s %s", req.Method, req.URL)
@@ -250,9 +268,18 @@ func (hs *Handlers) handleInfuseDownloadsList(resp http.ResponseWriter, req *htt
// handle delete request
func (hs *Handlers) deleteFileHandler(resp http.ResponseWriter, req *http.Request) {
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
fileName, _ := url.PathUnescape(chi.URLParam(req, "filename"))
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
torrentName, err := url.PathUnescape(chi.URLParam(req, "torrent"))
if err != nil {
torrentName = chi.URLParam(req, "torrent")
}
fileName, err := url.PathUnescape(chi.URLParam(req, "filename"))
if err != nil {
fileName = chi.URLParam(req, "filename")
}
if dav.HandleDeleteFile(directory, torrentName, fileName, hs.torMgr) != nil {
http.NotFound(resp, req)
return
@@ -261,8 +288,14 @@ func (hs *Handlers) deleteFileHandler(resp http.ResponseWriter, req *http.Reques
}
func (hs *Handlers) deleteTorrentHandler(resp http.ResponseWriter, req *http.Request) {
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
torrentName, err := url.PathUnescape(chi.URLParam(req, "torrent"))
if err != nil {
torrentName = chi.URLParam(req, "torrent")
}
if dav.HandleDeleteTorrent(directory, torrentName, hs.torMgr) != nil {
http.NotFound(resp, req)
return
@@ -273,9 +306,18 @@ func (hs *Handlers) deleteTorrentHandler(resp http.ResponseWriter, req *http.Req
// other handlers
func (hs *Handlers) davCheckSingleFileHandler(resp http.ResponseWriter, req *http.Request) {
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
fileName, _ := url.PathUnescape(chi.URLParam(req, "filename"))
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
torrentName, err := url.PathUnescape(chi.URLParam(req, "torrent"))
if err != nil {
torrentName = chi.URLParam(req, "torrent")
}
fileName, err := url.PathUnescape(chi.URLParam(req, "filename"))
if err != nil {
fileName = chi.URLParam(req, "filename")
}
out, err := dav.HandleSingleFile(directory, torrentName, fileName, hs.torMgr)
if err != nil {
http.NotFound(resp, req)
@@ -291,9 +333,18 @@ func (hs *Handlers) mkcolTorrentHandler(resp http.ResponseWriter, req *http.Requ
}
func (hs *Handlers) moveFileHandler(resp http.ResponseWriter, req *http.Request) {
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
fileName, _ := url.PathUnescape(chi.URLParam(req, "filename"))
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
torrentName, err := url.PathUnescape(chi.URLParam(req, "torrent"))
if err != nil {
torrentName = chi.URLParam(req, "torrent")
}
fileName, err := url.PathUnescape(chi.URLParam(req, "filename"))
if err != nil {
fileName = chi.URLParam(req, "filename")
}
newName := req.Header.Get("Destination")
newName = filepath.Base(newName)
if dav.HandleRenameFile(directory, torrentName, fileName, newName, hs.torMgr) != nil {
@@ -304,8 +355,14 @@ func (hs *Handlers) moveFileHandler(resp http.ResponseWriter, req *http.Request)
}
func (hs *Handlers) moveTorrentHandler(resp http.ResponseWriter, req *http.Request) {
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
torrentName, err := url.PathUnescape(chi.URLParam(req, "torrent"))
if err != nil {
torrentName = chi.URLParam(req, "torrent")
}
newName := req.Header.Get("Destination")
newName = filepath.Base(newName)
if dav.HandleRenameTorrent(directory, torrentName, newName, hs.torMgr) != nil {
@@ -318,14 +375,26 @@ func (hs *Handlers) moveTorrentHandler(resp http.ResponseWriter, req *http.Reque
// universal handlers
func (hs *Handlers) handleDownloadFile(resp http.ResponseWriter, req *http.Request) {
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
fileName, _ := url.PathUnescape(chi.URLParam(req, "filename"))
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
torrentName, err := url.PathUnescape(chi.URLParam(req, "torrent"))
if err != nil {
torrentName = chi.URLParam(req, "torrent")
}
fileName, err := url.PathUnescape(chi.URLParam(req, "filename"))
if err != nil {
fileName = chi.URLParam(req, "filename")
}
hs.downloader.DownloadFile(directory, torrentName, fileName, resp, req, hs.torMgr, hs.cfg, hs.log)
}
func (hs *Handlers) handleDownloadLink(resp http.ResponseWriter, req *http.Request) {
filename, _ := url.PathUnescape(chi.URLParam(req, "filename"))
filename, err := url.PathUnescape(chi.URLParam(req, "filename"))
if err != nil {
filename = chi.URLParam(req, "filename")
}
if download, ok := hs.torMgr.DownloadMap.Get(filename); ok {
hs.downloader.DownloadLink(download.Filename, download.Download, resp, req, hs.torMgr, hs.cfg, hs.log)
} else {
@@ -334,14 +403,26 @@ func (hs *Handlers) handleDownloadLink(resp http.ResponseWriter, req *http.Reque
}
func (hs *Handlers) handleCheckFile(resp http.ResponseWriter, req *http.Request) {
directory, _ := url.PathUnescape(chi.URLParam(req, "directory"))
torrentName, _ := url.PathUnescape(chi.URLParam(req, "torrent"))
fileName, _ := url.PathUnescape(chi.URLParam(req, "filename"))
directory, err := url.PathUnescape(chi.URLParam(req, "directory"))
if err != nil {
directory = chi.URLParam(req, "directory")
}
torrentName, err := url.PathUnescape(chi.URLParam(req, "torrent"))
if err != nil {
torrentName = chi.URLParam(req, "torrent")
}
fileName, err := url.PathUnescape(chi.URLParam(req, "filename"))
if err != nil {
fileName = chi.URLParam(req, "filename")
}
universal.CheckFile(directory, torrentName, fileName, resp, req, hs.torMgr, hs.log)
}
func (hs *Handlers) handleCheckDownloadLink(resp http.ResponseWriter, req *http.Request) {
filename, _ := url.PathUnescape(chi.URLParam(req, "filename"))
filename, err := url.PathUnescape(chi.URLParam(req, "filename"))
if err != nil {
filename = chi.URLParam(req, "filename")
}
if download, ok := hs.torMgr.DownloadMap.Get(filename); ok {
universal.CheckDownloadLink(download, resp, req, hs.torMgr, hs.log)
} else {