-
Notifications
You must be signed in to change notification settings - Fork 259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve error handling #13
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
import socket | ||
import subprocess | ||
import sys | ||
import traceback | ||
import urllib.parse | ||
import urllib3 | ||
|
||
|
@@ -29,7 +30,7 @@ def printf(fmt, *args, file=sys.stdout): | |
|
||
def is_html(response): | ||
''' Return True if the response is a HTML webpage ''' | ||
return '<html>' in response.text | ||
return "html" in response.headers["Content-Type"] | ||
|
||
|
||
def get_indexed_files(response): | ||
|
@@ -84,6 +85,16 @@ def get_referenced_sha1(obj_file): | |
return objs | ||
|
||
|
||
def verify_response(response): | ||
if response.status_code != 200: | ||
return False, f"[-] %s/%s responded with status code {response.status_code}\n" | ||
elif "Content-Length" in response.headers and response.headers["Content-Length"] == 0: | ||
return False, "[-] %s/%s responded with a zero-length body\n" | ||
elif "Content-Type" in response.headers and "text/html" in response.headers["Content-Type"]: | ||
return False, "[-] %s/%s responded with HTML\n" | ||
else: | ||
return True, True | ||
|
||
class Worker(multiprocessing.Process): | ||
''' Worker for process_tasks ''' | ||
|
||
|
@@ -105,7 +116,12 @@ def run(self): | |
if task is None: # end signal | ||
return | ||
|
||
result = self.do_task(task, *self.args) | ||
try: | ||
result = self.do_task(task, *self.args) | ||
except Exception as e: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, you're right, good catch |
||
print("Task %s raised exception:" % task) | ||
traceback.print_exc() | ||
result = [] | ||
|
||
assert isinstance(result, list), 'do_task() should return a list of tasks' | ||
|
||
|
@@ -180,10 +196,13 @@ def do_task(self, filepath, url, directory, retry, timeout): | |
allow_redirects=False, | ||
stream=True, | ||
timeout=timeout)) as response: | ||
verification = verify_response(response) | ||
if not verification[0]: | ||
printf(verification[1], url, filepath) | ||
return [] | ||
Comment on lines
+199
to
+202
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here and below: Instead of using verification[0] and verification[1] we could use unpacking:
|
||
printf('[-] Fetching %s/%s [%d]\n', url, filepath, response.status_code) | ||
|
||
if response.status_code != 200: | ||
return [] | ||
|
||
|
||
abspath = os.path.abspath(os.path.join(directory, filepath)) | ||
create_intermediate_dirs(abspath) | ||
|
@@ -205,20 +224,19 @@ def do_task(self, filepath, url, directory, retry, timeout): | |
stream=True, | ||
timeout=timeout)) as response: | ||
printf('[-] Fetching %s/%s [%d]\n', url, filepath, response.status_code) | ||
|
||
if (response.status_code in (301, 302) and | ||
'Location' in response.headers and | ||
response.headers['Location'].endswith(filepath + '/')): | ||
return [filepath + '/'] | ||
|
||
if response.status_code != 200: | ||
return [] | ||
|
||
if filepath.endswith('/'): # directory index | ||
assert is_html(response) | ||
|
||
return [filepath + filename for filename in get_indexed_files(response)] | ||
else: # file | ||
verification = verify_response(response) | ||
if not verification[0]: | ||
printf(verification[1], url, filepath) | ||
return [] | ||
abspath = os.path.abspath(os.path.join(directory, filepath)) | ||
create_intermediate_dirs(abspath) | ||
|
||
|
@@ -237,10 +255,12 @@ def do_task(self, filepath, url, directory, retry, timeout): | |
response = self.session.get('%s/%s' % (url, filepath), | ||
allow_redirects=False, | ||
timeout=timeout) | ||
printf('[-] Fetching %s/%s [%d]\n', url, filepath, response.status_code) | ||
|
||
if response.status_code != 200: | ||
verification = verify_response(response) | ||
if not verification[0]: | ||
printf(verification[1], url, filepath) | ||
return [] | ||
printf('[-] Fetching %s/%s [%d]\n', url, filepath, response.status_code) | ||
|
||
abspath = os.path.abspath(os.path.join(directory, filepath)) | ||
create_intermediate_dirs(abspath) | ||
|
@@ -269,11 +289,14 @@ def do_task(self, obj, url, directory, retry, timeout): | |
response = self.session.get('%s/%s' % (url, filepath), | ||
allow_redirects=False, | ||
timeout=timeout) | ||
printf('[-] Fetching %s/%s [%d]\n', url, filepath, response.status_code) | ||
|
||
if response.status_code != 200: | ||
verification = verify_response(response) | ||
if not verification[0]: | ||
printf(verification[1], url, filepath) | ||
return [] | ||
|
||
printf('[-] Fetching %s/%s [%d]\n', url, filepath, response.status_code) | ||
|
||
abspath = os.path.abspath(os.path.join(directory, filepath)) | ||
create_intermediate_dirs(abspath) | ||
|
||
|
@@ -309,13 +332,12 @@ def fetch_git(url, directory, jobs, retry, timeout): | |
response = requests.get('%s/.git/HEAD' % url, verify=False, allow_redirects=False) | ||
printf('[%d]\n', response.status_code) | ||
|
||
if response.status_code != 200: | ||
printf('error: %s/.git/HEAD does not exist\n', url, file=sys.stderr) | ||
return 1 | ||
elif not response.text.startswith('ref:'): | ||
printf('error: %s/.git/HEAD is not a git HEAD file\n', url, file=sys.stderr) | ||
Comment on lines
-315
to
-316
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we keep this check? |
||
verification = verify_response(response) | ||
if not verification[0]: | ||
printf(verification[1], url, "/.git/HEAD") | ||
return 1 | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: remove the empty newline |
||
# check for directory listing | ||
printf('[-] Testing %s/.git/ ', url) | ||
response = requests.get('%s/.git/' % url, verify=False, allow_redirects=False) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it be
text/html
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, good catch.