Skip to content

Commit 222a80b

Browse files
committed
SimpleChatTC:SimpleProxy:Switch web flow to use file helpers
This also indirectly adds support for local file system access through the web / fetch (ie urlraw and urltext) service request paths.
1 parent 27913f9 commit 222a80b

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

tools/server/public_simplechat/local.tools/filemagic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Response:
2020

2121

2222

23-
def get_from_web(url: str, tag: str, inContentType: str, inHeaders: dict[str, str]):
23+
def get_from_web(url: str, tag: str, inContentType: str, inHeaders: dict[str, str|None]):
2424
"""
2525
Get the url specified from web.
2626
@@ -67,7 +67,7 @@ def get_from_local(urlParts: urllib.parse.ParseResult, tag: str, inContentType:
6767
return Response(False, 502, f"WARN:{tag}:Failed:{exc}")
6868

6969

70-
def get_file(url: str, tag: str, inContentType: str, inHeaders: dict[str, str]={}):
70+
def get_file(url: str, tag: str, inContentType: str, inHeaders: dict[str, str|None]={}):
7171
"""
7272
Based on the scheme specified in the passed url,
7373
either get from local file system or from the web.

tools/server/public_simplechat/local.tools/webmagic.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from dataclasses import dataclass
88
import html.parser
99
import debug
10+
import filemagic as mFile
1011
from typing import TYPE_CHECKING
1112

1213
if TYPE_CHECKING:
@@ -48,23 +49,17 @@ def handle_urlreq(ph: 'ProxyHandler', pr: urllib.parse.ParseResult, tag: str):
4849
if not gotVU.callOk:
4950
return UrlReqResp(gotVU.callOk, gotVU.statusCode, gotVU.statusMsg)
5051
try:
51-
hUA = ph.headers.get('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0')
52-
hAL = ph.headers.get('Accept-Language', "en-US,en;q=0.9")
53-
hA = ph.headers.get('Accept', "text/html,*/*")
52+
hUA = ph.headers.get('User-Agent', None)
53+
hAL = ph.headers.get('Accept-Language', None)
54+
hA = ph.headers.get('Accept', None)
5455
headers = {
5556
'User-Agent': hUA,
5657
'Accept': hA,
5758
'Accept-Language': hAL
5859
}
59-
req = urllib.request.Request(url, headers=headers)
6060
# Get requested url
61-
print(f"DBUG:{tag}:Req:{req.full_url}:{req.headers}")
62-
with urllib.request.urlopen(req, timeout=10) as response:
63-
contentData = response.read().decode('utf-8')
64-
statusCode = response.status or 200
65-
contentType = response.getheader('Content-Type') or 'text/html'
66-
debug.dump({ 'url': req.full_url, 'headers': req.headers, 'ctype': contentType }, { 'cdata': contentData })
67-
return UrlReqResp(True, statusCode, "", contentType, contentData)
61+
gotFile = mFile.get_file(url, tag, "text/html", headers)
62+
return UrlReqResp(gotFile.callOk, gotFile.statusCode, gotFile.statusMsg, gotFile.contentType, gotFile.contentData.decode('utf-8'))
6863
except Exception as exc:
6964
return UrlReqResp(False, 502, f"WARN:{tag}:Failed:{exc}")
7065

0 commit comments

Comments
 (0)