2424import urllib .request
2525from dataclasses import dataclass
2626import html .parser
27- import re
2827import time
2928import urlvalidator as uv
29+ from typing import Callable
3030
3131
3232gMe = {
@@ -109,6 +109,19 @@ def auth_check(self):
109109 return { 'AllOk' : False , 'Msg' : "Invalid auth" }
110110 return { 'AllOk' : True , 'Msg' : "Auth Ok" }
111111
112+ def auth_and_run (self , pr :urllib .parse .ParseResult , handler :Callable [['ProxyHandler' , urllib .parse .ParseResult ], None ]):
113+ """
114+ If authorisation is ok for the request, run the specified handler.
115+ """
116+ acGot = self .auth_check ()
117+ if not acGot ['AllOk' ]:
118+ self .send_error (400 , f"WARN:{ acGot ['Msg' ]} " )
119+ else :
120+ try :
121+ handler (self , pr )
122+ except Exception as e :
123+ self .send_error (400 , f"ERRR:ProxyHandler:{ e } " )
124+
112125 def do_GET (self ):
113126 """
114127 Handle GET requests
@@ -119,23 +132,11 @@ def do_GET(self):
119132 print (f"DBUG:ProxyHandler:GET:{ pr } " )
120133 match pr .path :
121134 case '/urlraw' :
122- acGot = self .auth_check ()
123- if not acGot ['AllOk' ]:
124- self .send_error (400 , f"WARN:{ acGot ['Msg' ]} " )
125- else :
126- handle_urlraw (self , pr )
135+ self .auth_and_run (pr , handle_urlraw )
127136 case '/urltext' :
128- acGot = self .auth_check ()
129- if not acGot ['AllOk' ]:
130- self .send_error (400 , f"WARN:{ acGot ['Msg' ]} " )
131- else :
132- handle_urltext (self , pr )
137+ self .auth_and_run (pr , handle_urltext )
133138 case '/pdf2text' :
134- acGot = self .auth_check ()
135- if not acGot ['AllOk' ]:
136- self .send_error (400 , f"WARN:{ acGot ['Msg' ]} " )
137- else :
138- handle_pdf2text (self , pr )
139+ self .auth_and_run (pr , handle_pdf2text )
139140 case '/aum' :
140141 handle_aum (self , pr )
141142 case _:
0 commit comments