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

[WIP] Add RESTful API for mitmweb option #2395

Merged
merged 8 commits into from Jun 24, 2017
Merged

[WIP] Add RESTful API for mitmweb option #2395

merged 8 commits into from Jun 24, 2017

Conversation

MatthewShao
Copy link
Contributor

@MatthewShao MatthewShao commented Jun 13, 2017

  • Implement GET /options
  • Implement POST /options
    The return json looks like this:
[
    {
        "default": false, 
        "help": "Add all certificates of the upstream server to the certificate chain that will be served to the proxy client, as extras.", 
        "name": "add_upstream_certs_to_client_chain", 
        "type": "bool", 
        "value": false
    }, 
    {
        "default": false, 
        "help": "Strip out request headers that might cause the server to return 304-not-modified.", 
        "name": "anticache", 
        "type": "bool", 
        "value": false
    }, 
...]

The tests passed on my local repo, but kept falling on Travis... Is that because of some compatibility problem?

Copy link
Member

@mhils mhils left a comment

Choose a reason for hiding this comment

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

Good stuff! All comments are really complaints on a high level. :)

I haven't seen travis fail here, but I guess this is due to @Kriechi's draconian fantastic coverage checks. We should add a test for the API call in https://github.com/mitmproxy/mitmproxy/blob/master/test/mitmproxy/tools/web/test_app.py. :)

"""
Dumps the options into a list of dict object.

Return: A list like: [ { name: "anticahce", type: "bool", default: false, value: true, help: "help text"}]
Copy link
Member

Choose a reason for hiding this comment

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

typo: anticache

"""
options_list = []
for k in sorted(opts.keys()):
o = opts._options[k]
Copy link
Member

Choose a reason for hiding this comment

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

You can simplify this a tiny bit:

for k, o in sorted(opts.items()):

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm afraid we could not do this, opts here is not a dict, but a OptManager, or do you think we should add a .items method for it?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see. Let's keep it as is then. :)

options_list = []
for k in sorted(opts.keys()):
o = opts._options[k]
option = {'name': k, 'type': o.typespec.__name__, 'default': o.default, 'value': o.current(), 'help': o.help.strip()}
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add some line breaks here to make this easier to read?

Copy link
Member

Choose a reason for hiding this comment

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

Also, the help.strip() here should be un-necessary - we dedent and format help text on initialisation. Let's keep the help formatting in one place.

Copy link
Member

Choose a reason for hiding this comment

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

Good catch. This also needs to support .choices.

options_list = []
for k in sorted(opts.keys()):
o = opts._options[k]
if o.typespec in (str, int, bool):
Copy link
Member

Choose a reason for hiding this comment

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

Move this typespec to string conversion into its own function in mitmproxy/utils/typecheck? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good advice!

Copy link
Member

@mhils mhils left a comment

Choose a reason for hiding this comment

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

Looks good so far! A few comments below, feel free to address those after your essays. :-)


def test_option_update(self):
assert self.put_json("/options", {"anticache": True}).code == 200
assert self.put_json("/options", {"wtf": True}).code == 400
Copy link
Member

Choose a reason for hiding this comment

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

We should also test type confusion here (e.g. {"anticache": "foo"})

@@ -98,3 +98,15 @@ def check_option_type(name: str, value: typing.Any, typeinfo: typing.Any) -> Non
return
elif not isinstance(value, typeinfo):
raise e


def typespec_to_str(typespec: typing.Any) -> str:
Copy link
Member

Choose a reason for hiding this comment

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

We should also use this in dump_defaults

if typespec in (str, int, bool):
t = typespec.__name__
elif typespec == typing.Optional[str]:
t = 'Union'
Copy link
Member

Choose a reason for hiding this comment

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

Make this "optional str" (and "sequence of str" below this) to match what is currently being done in dump_defaults

Copy link
Member

@mhils mhils left a comment

Choose a reason for hiding this comment

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

👍 🍰

@mhils mhils merged commit bde6474 into mitmproxy:master Jun 24, 2017
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.

None yet

3 participants