@@ -81,6 +81,34 @@ def validate_url(self, url):
8181 sys .exit (1 )
8282
8383
84+ def head (self , curl ):
85+ """Take the curl object to head state, with redirect."""
86+ if isinstance (curl , pycurl .Curl ):
87+ buffer = BytesIO ()
88+ curl .setopt (pycurl .NOBODY , True )
89+ curl .setopt (pycurl .HEADERFUNCTION , buffer .write )
90+ curl .setopt (pycurl .FOLLOWLOCATION , True )
91+ temp_cert_path = None
92+
93+ if self .misconfigured_server :
94+ if not self .leaf_cert :
95+ sys .exit (1 )
96+ temp_cert_path = REQUEST_HANDLER .set_leaf (curl )
97+
98+ try :
99+ curl .perform ()
100+ except pycurl .error :
101+ print ("Error performing request:" , pycurl .error )
102+ finally :
103+ curl .close ()
104+
105+ if temp_cert_path and os .path .exists (temp_cert_path ):
106+ os .remove (temp_cert_path )
107+
108+ return buffer .getvalue ().decode ('utf-8' )
109+ return ""
110+
111+
84112 def validate_data_type (self , content_type ):
85113 """Limit to used data types."""
86114 valid_content_types = {
@@ -89,7 +117,8 @@ def validate_data_type(self, content_type):
89117 'application/zip' ,
90118 'image/jpeg' ,
91119 'image/png' ,
92- 'text/html'
120+ 'text/html' ,
121+ 'head' # this is not MIME
93122 }
94123
95124 if content_type not in valid_content_types :
@@ -127,15 +156,19 @@ def get_leaf(self, url):
127156 logging .error ("Failed to retrieve leaf certificate. Exiting." )
128157 sys .exit (1 )
129158
130-
131- def get_response (self , url , content_type ):
132- """Handle all https requests"""
159+ def setup_before_get_response (self , url , content_type ):
160+ """validate known url and content type"""
133161 self .validate_url (url )
134162 self .validate_data_type (content_type )
135163
136164 if url .startswith ("https://www.trle.net/" ) and not self .misconfigured_server :
137165 self .get_leaf (url )
138166
167+
168+ def get_response (self , url , content_type ):
169+ """Handle all https requests"""
170+ self .setup_before_get_response (url , content_type )
171+
139172 if content_type == 'application/zip' :
140173 return DOWNLOADER .download_file (url )
141174
@@ -152,8 +185,12 @@ def get_response(self, url, content_type):
152185 headers_buffer = BytesIO ()
153186 curl = pycurl .Curl () # pylint: disable=no-member
154187 curl .setopt (pycurl .URL , url )
155- curl .setopt (pycurl .WRITEDATA , response_buffer )
188+
189+ if content_type == 'application/zip' :
190+ return self .head (curl )
191+
156192 curl .setopt (pycurl .WRITEHEADER , headers_buffer )
193+ curl .setopt (pycurl .WRITEDATA , response_buffer )
157194
158195 if self .misconfigured_server :
159196 if not self .leaf_cert :
@@ -191,6 +228,11 @@ def get_response(self, url, content_type):
191228 if temp_cert_path and os .path .exists (temp_cert_path ):
192229 os .remove (temp_cert_path )
193230
231+ return self .close_response (curl , headers , response_buffer , content_type )
232+
233+
234+ def close_response (self , curl , headers , response_buffer , content_type ):
235+ """Pack response and close curl"""
194236 if curl is None :
195237 logging .error ("No curl instance" )
196238 sys .exit (1 )
@@ -244,6 +286,7 @@ def extract_content_type(self, headers):
244286 logging .error ("Could not extract content type from header: %s" , headers )
245287 return None
246288
289+
247290class Downloader :
248291 """Zip file downloader to be used in RequestHandler"""
249292 def __init__ (self ):
@@ -403,6 +446,7 @@ def get(url, content_type):
403446 'image/jpeg'
404447 'image/png'
405448 'text/html'
449+ 'head'
406450
407451 url must start with:
408452 "https://www.trle.net/"
0 commit comments