From 5a1f42cfccd66ccad20c7ca7a2432a926ec13d6c Mon Sep 17 00:00:00 2001 From: Dhruv Agarwal Date: Sat, 12 Mar 2016 16:24:04 +0530 Subject: [PATCH 1/3] Add custom template option --- bin/http_request_translator | 5 ++++- http_request_translator/base.py | 23 ++++++++++++++--------- http_request_translator/plugin_manager.py | 4 ++-- http_request_translator/translator.py | 2 +- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/bin/http_request_translator b/bin/http_request_translator index 0c683f0..bcd2888 100755 --- a/bin/http_request_translator +++ b/bin/http_request_translator @@ -38,7 +38,10 @@ def take_arguments(): help="Interactive mode: read raw HTTP request from keyboard.") parser.add_argument( '--data', '-d', - help='Add the data that you want to send along with the header') + help='Add the data that you want to send along with the header.') + parser.add_argument( + '--template', '-t', + help='Add the path of the custom template you want to provide.') request_group.add_argument( '--request', '-r', help='Input the HTTP request') diff --git a/http_request_translator/base.py b/http_request_translator/base.py index 3fdd306..bc39b1f 100644 --- a/http_request_translator/base.py +++ b/http_request_translator/base.py @@ -27,16 +27,17 @@ class AbstractScript(object): code_search = '' code_nosearch = '' - def __init__(self, headers=None, details=None, search=None): + def __init__(self, headers=None, details=None, search=None, template=None): """Initialize the script generation. :param list headers: Headers list containing fields like 'Host', 'User-Agent', etc. :param dict details: Request specific details dictionary like body and method of the request. :param str search: String to search for in the response to the request. + :param str template: Custom template path in dotted module format. :raises ValueError: When url is invalid. """ - self.load_attributes(self.__class__) + self.load_attributes(self.__class__, template) self._script = '' self.headers = headers self.details = details @@ -190,21 +191,25 @@ def encode_url(self, url): return encoded_url @staticmethod - def load_attributes(cls): + def load_attributes(cls, template=None): """Loads attributes to Script class from a given script's template Imports the template file/module, assigns all the attributes defined in the template file to the given class. :param class cls: Script class to which template is to be loaded. + :param str template: Custom template path in dotted module format. :raises AttributeError: When __language__ attribute is not present. """ - templates_path = "{}.templates".format(__name__.split('.', 1)[0]) - if not hasattr(cls, '__language__'): - raise AttributeError("__language__ not found in class: {}, attributes cannot be loaded".format(cls.__name__)) - template = import_module("{templates_path}.{class_template}".format( - templates_path=templates_path, - class_template=cls.__language__)) + if template: + template = import_module(template) + else: + templates_path = "{}.templates".format(__name__.split('.', 1)[0]) + if not hasattr(cls, '__language__'): + raise AttributeError("__language__ not found in class: {}, attributes cannot be loaded".format(cls.__name__)) + template = import_module("{templates_path}.{class_template}".format( + templates_path=templates_path, + class_template=cls.__language__)) attributes = (var for var in vars(template) if var.startswith('code_')) for attr in attributes: setattr(cls, attr, getattr(template, attr)) diff --git a/http_request_translator/plugin_manager.py b/http_request_translator/plugin_manager.py index 234496d..c346e8d 100644 --- a/http_request_translator/plugin_manager.py +++ b/http_request_translator/plugin_manager.py @@ -22,7 +22,7 @@ def get_script_class(script_name): raise ValueError("The {} language is not supported.".format(script_name)) -def generate_script(script, headers, details, search_string=None): +def generate_script(script, headers, details, search_string=None, template=None): """Returns the script code for the HTTP request passed in script language :param str script: Name of the language for which script is to be generated @@ -34,4 +34,4 @@ def generate_script(script, headers, details, search_string=None): :rtype: `str` """ class_script = get_script_class(script.strip().lower()) - return class_script(headers=headers, details=details, search=search_string).generate_script() + return class_script(headers=headers, details=details, search=search_string, template=template).generate_script() diff --git a/http_request_translator/translator.py b/http_request_translator/translator.py index 30f07c9..10e1ff4 100644 --- a/http_request_translator/translator.py +++ b/http_request_translator/translator.py @@ -72,7 +72,7 @@ def process_arguments(args): elif args.search_regex: arg_option = args.search_regex if len(script_list) == 0: - generated_code = generate_script('bash', headers, details, arg_option) + generated_code = generate_script('bash', headers, details, arg_option, args.template) print(generated_code) else: for script in script_list: From 0af5da9191f02211880d19085c8c1c94e71fc606 Mon Sep 17 00:00:00 2001 From: Dhruv Agarwal Date: Sat, 12 Mar 2016 22:49:36 +0530 Subject: [PATCH 2/3] change --template to --custom-template arg --- bin/http_request_translator | 2 +- http_request_translator/translator.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/http_request_translator b/bin/http_request_translator index bcd2888..61fa08f 100755 --- a/bin/http_request_translator +++ b/bin/http_request_translator @@ -40,7 +40,7 @@ def take_arguments(): '--data', '-d', help='Add the data that you want to send along with the header.') parser.add_argument( - '--template', '-t', + '--custom-template', '-t', help='Add the path of the custom template you want to provide.') request_group.add_argument( '--request', '-r', diff --git a/http_request_translator/translator.py b/http_request_translator/translator.py index 10e1ff4..f903300 100644 --- a/http_request_translator/translator.py +++ b/http_request_translator/translator.py @@ -72,7 +72,7 @@ def process_arguments(args): elif args.search_regex: arg_option = args.search_regex if len(script_list) == 0: - generated_code = generate_script('bash', headers, details, arg_option, args.template) + generated_code = generate_script('bash', headers, details, arg_option, args.custom_template) print(generated_code) else: for script in script_list: From 1bf9269e130f4eb02ff4d4a83a1efcd046e4e8ca Mon Sep 17 00:00:00 2001 From: Dhruv Agarwal Date: Mon, 14 Mar 2016 18:24:35 +0530 Subject: [PATCH 3/3] adding global custom template --- http_request_translator/base.py | 3 +++ http_request_translator/util.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/http_request_translator/base.py b/http_request_translator/base.py index bc39b1f..827ddfd 100644 --- a/http_request_translator/base.py +++ b/http_request_translator/base.py @@ -11,6 +11,7 @@ from importlib import import_module from .url import get_url, check_valid_url +from .util import exists_global_custom_template, get_global_custom_template class AbstractScript(object): @@ -203,6 +204,8 @@ def load_attributes(cls, template=None): """ if template: template = import_module(template) + elif exists_global_custom_template(cls.__language__): + template = get_global_custom_template(cls.__language__) else: templates_path = "{}.templates".format(__name__.split('.', 1)[0]) if not hasattr(cls, '__language__'): diff --git a/http_request_translator/util.py b/http_request_translator/util.py index 12d3096..1a66e13 100644 --- a/http_request_translator/util.py +++ b/http_request_translator/util.py @@ -1,4 +1,7 @@ +from importlib import import_module +import os import re +import sys # Blindy copied from: https://gist.github.com/mnordhoff/2213179 @@ -7,3 +10,15 @@ re_ipv6_address = re.compile('^(?:(?:[0-9A-Fa-f]{1,4}:){6}(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?: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-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,4}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}|(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))|(?:(?:[0-9A-Fa-f]{1,4}:){,5}[0-9A-Fa-f]{1,4})?::[0-9A-Fa-f]{1,4}|(?:(?:[0-9A-Fa-f]{1,4}:){,6}[0-9A-Fa-f]{1,4})?::)$') # Homebrew re_domain = re.compile(r'^(?:(?:[A-Z](?:[A-Z-]{0,61}[A-Z])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|)$', re.IGNORECASE) + +DEFAULT_CUSTOM_TEMPLATE_PATH = os.environ.get('DEFAULT_CUSTOM_TEMPLATE_PATH', '~/.config/translator/templates') +DEFAULT_CUSTOM_TEMPLATE_PATH = DEFAULT_CUSTOM_TEMPLATE_PATH.replace('~', os.environ.get('HOME', '')) + + +def exists_global_custom_template(language): + return os.path.exists(os.path.join(DEFAULT_CUSTOM_TEMPLATE_PATH, "{}.py".format(language))) + + +def get_global_custom_template(language): + sys.path.insert(0, DEFAULT_CUSTOM_TEMPLATE_PATH) + return import_module(language) \ No newline at end of file