Skip to content

Commit

Permalink
Properly return urlshortener response
Browse files Browse the repository at this point in the history
  • Loading branch information
nothingface0 committed Jun 24, 2024
1 parent 9cddfa0 commit e11ef54
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions src/python/Core/GUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
# such as file and socket operations. On "straight" python code the
# interpreter yields the lock only every N byte code instructions;
# this server configures a large N (1'000'000).

import ssl
from importlib import import_module
from imp import get_suffixes
from copy import deepcopy
from html import escape
from socket import gethostname, getaddrinfo
from socket import gethostname
from threading import Thread, Lock
from cherrypy import expose, HTTPError, request, response, engine, log, tools
from cherrypy.lib.static import serve_file
Expand Down Expand Up @@ -824,7 +824,7 @@ def urlshortener(self, *args, **kwargs):
if not "url" in kwargs.keys():
return "{}"

longUrl = base64.b64decode(kwargs["url"])
longUrl = base64.b64decode(kwargs["url"]).decode("utf-8")
shortUrl = longUrl

try:
Expand All @@ -833,30 +833,24 @@ def urlshortener(self, *args, **kwargs):
# Timeout is ignored in the part of URL to IP resolution,
# so do it seperately.
# See: https://stackoverflow.com/a/28674109/6562491
tinyurl_ip, tinyurl_port = getaddrinfo("tinyurl.com", 443)[0][-1]
connection = client.HTTPSConnection(
host=tinyurl_ip, port=tinyurl_port, timeout=3
)

connection = client.HTTPSConnection(host="tinyurl.com", port=443, timeout=3)
connection.request("GET", f"/api-create.php?url={longUrl}")
response = connection.getresponse()

if response.status == 200:
shortUrl = response.read()
shortUrl = response.read().decode("utf-8")
else:
log(
f"WARNING: urlshortener returned status: {response.status} for url: {longUrl}",
f"WARNING: urlshortener returned status: {response.status} and response: {response.read().decode()} for url: {longUrl}",
severity=logging.WARNING,
)

connection.close()
except Exception as e:
log(f"WARNING: unable to shorten URL: {longUrl}. Reason: {repr(e)}")

return (
'{"id": "%s"}' % shortUrl.decode("utf-8")
if isinstance(shortUrl, bytes)
else shortUrl
)
return '{"id": "%s"}' % shortUrl

def sessionIndex(self, session, *args, **kwargs):
"""Generate top level session index. This produces the main GUI web
Expand Down

0 comments on commit e11ef54

Please sign in to comment.