Skip to content
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

Adding lambda support #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

andymillersj
Copy link

I wanted to perform a string substitution that could be more dynamic than what was already supported, so I added support for using lambda functions as replacement arguments.

While implementing my changes, I also made a slight modification to encode_content to better handle content passed to it and removed the mitmproxy net import since it is not necessary since this commit.

moxy.py Outdated
if index != -1:
replace_lambda_value = lambda_with_args(replace_lambda_value, index)
try:
code_object = compile(replace_lambda_value, "<lambda>", "eval")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if these compiled lambdas should be cached like compiled regexes are? Also, several lines of duplicated code between replace_lambda_in_dict and replace_lambda_in_content, maybe move to a function called by both?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great feedback, I will refactor accordingly.

moxy.py Outdated
- An object (such as a dictionary) that can be dumped as JSON, in which case
it is converted to JSON.
"""
content_type = "application/json"
if isinstance(content, str):
try:
with open(content, "rb") as content_file:
if content.endswith("html"):
if content.endswith(".html"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lack of . was actually intentional to cover xhtml, shtml and such variants, but TBH probably not much of an issue in practice, and xhtml might need its own type anyway

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the context; I'll go ahead and revert this.

@andymillersj andymillersj requested a review from arkku February 2, 2021 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants