Yank direct support for URLPath from common.py

This commit is contained in:
Jean-Paul Calderone 2020-10-19 12:33:15 -04:00
parent 72e60f8301
commit b68c08cff9
2 changed files with 34 additions and 16 deletions

View File

@ -37,9 +37,6 @@ from twisted.web.util import (
from twisted.python.reflect import (
fullyQualifiedName,
)
from twisted.python.urlpath import (
URLPath,
)
from twisted.python import log
from twisted.python.failure import (
Failure,
@ -583,15 +580,6 @@ def _finish(result, render, request):
)
request.write(result)
request.finish()
elif isinstance(result, URLPath):
Message.log(
message_type=u"allmydata:web:common-render:URLPath",
)
if result.netloc == b"":
root = URLPath.fromRequest(request)
result.scheme = root.scheme
result.netloc = root.netloc
_finish(redirectTo(str(result), request), render, request)
elif isinstance(result, DecodedURL):
Message.log(
message_type=u"allmydata:web:common-render:DecodedURL",
@ -684,3 +672,34 @@ def handle_when_done(req, d):
if when_done:
d.addCallback(lambda res: DecodedURL.from_text(when_done.decode("utf-8")))
return d
def url_for_string(req, url_string):
"""
Construct a universal URL using the given URL string.
:param IRequest req: The request being served. If ``redir_to`` is not
absolute then this is used to determine the net location of this
server and the resulting URL is made to point at it.
:param bytes url_string: A byte string giving a universal or absolute URL.
:return DecodedURL: An absolute URL based on this server's net location
and the given URL string.
"""
url = DecodedURL.from_text(url_string.decode("utf-8"))
if url.host == b"":
root = req.URLPath()
netloc = root.netloc.split(b":", 1)
if len(netloc) == 1:
host = netloc
port = None
else:
host = netloc[0]
port = int(netloc[1])
url = url.replace(
scheme=root.scheme.decode("ascii"),
host=host.decode("ascii"),
port=port,
)
return url

View File

@ -1,10 +1,8 @@
import urllib
from twisted.web import http
from twisted.internet import defer
from twisted.python.urlpath import (
URLPath,
)
from twisted.python.filepath import FilePath
from twisted.web.resource import Resource
from twisted.web.template import (
@ -23,6 +21,7 @@ from allmydata.web.common import (
get_format,
get_mutable_type,
render_exception,
url_for_string,
)
from allmydata.web import status
@ -68,7 +67,7 @@ def POSTUnlinkedCHK(req, client):
def _done(upload_results, redir_to):
if "%(uri)s" in redir_to:
redir_to = redir_to.replace("%(uri)s", urllib.quote(upload_results.get_uri()))
return URLPath.fromString(redir_to)
return url_for_string(req, redir_to)
d.addCallback(_done, when_done)
else:
# return the Upload Results page, which includes the URI