-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Added first version of normalize_path_middleware #1118
Added first version of normalize_path_middleware #1118
Conversation
I'm adding the changes to look for feedback. Right now the test for the append_slash is working. I'll start now with the double slashes one. |
No, |
aiohttp/middlewares.py
Outdated
def middleware(request): | ||
|
||
router = request.app.router | ||
match_info = yield from router.resolve(request) |
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.
request
already has match_info
, no need to resolve it twice
Codecov Report@@ Coverage Diff @@
## master #1118 +/- ##
==========================================
- Coverage 98.59% 98.51% -0.09%
==========================================
Files 30 32 +2
Lines 7055 7250 +195
Branches 1176 1203 +27
==========================================
+ Hits 6956 7142 +186
- Misses 58 62 +4
- Partials 41 46 +5
Continue to review full report at Codecov.
|
setup.cfg
Outdated
@@ -1,8 +1,12 @@ | |||
[pep8] | |||
max-line-length=80 |
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.
Please keep 79: https://www.python.org/dev/peps/pep-0008/#id19
Looks pretty good but please convert I have a guts feeling: sometimes we'll need to add some parameters to the middleware, e.g.:
All params should be keyword-only arguments. Right now it's not an issue but please respect my guts :) Also |
Yeah! In my current branch I have it as a factory but haven't commited it yet until I have it working with both args ( Ok with keyword only and Ok with renaming :). |
In this version both append_slash and merge_slash features are implemented. The middleware returns as soon as it finds a path that resolves.
With this last commit functionality is covered. Anyway, in case it is ready to merge in your opinion, I would like to keep it until tomorrow night to see if I can find a more readable way to express it. A bit off topic but that came while working with this merge request:
|
Finished. We still have a caveat that I don't know how (or if) you want to address it. Suppose the user has the following route defined: If the user tries to access to |
7c94a7a
to
764781e
Compare
I think this would also solve #682 |
@@ -1,8 +1,12 @@ | |||
[pep8] |
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.
Why did you add this option? 79 lines is default value.
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.
People can have configurations at user home level.
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.
Hmm. Ok, make sense.
Please drop autodoc markup from docstrings but explicitly document Also middleware should be described in |
Rename the module to
|
aiohttp/middlewares.py
Outdated
return False, request | ||
|
||
|
||
def normalize_path( |
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.
Rename to normalize_path_middleware
. The name is long but it will be used only in one place of program.
Adding middleware
suffix explicitly describes the function job.
Is the clone function really necessary? As it may impose a performance issue if the server is under a huge amount of load. In my opinion a better approach would be to provide an overload function for Also I am wondering if it's actually better to directly integrate this feature in aiohttp's core (One can enable this feature by a flag), so we can actually drop returning For example the expected behavior for this would be: A request something like this Again, I think we we should do the normalization as early as possible to elimination possible performance (More like we are being repetitive). |
I was working on an temporary approach (More like a hack) where I utilize a middleware which does the normalization then crafts a temporary object where |
@cynecx I have this MR a bit abandoned because Im spending my time with other stuff right now. About what you comment, I remember my first approach trying what you said about not using the On the other hand, I totally agree that an alternative |
|
Wow looks interresting be nice if this PR could be updated so it can be Merged. |
This is a useful middleware. And if this gets integrated, I could get rid of my currently-broken implementation in aiohttp_utils. 👍 |
I've upgraded the branch to resolve conflicts and apply some comments from @asvetlov. I'm having some problems from 5 test cases that didn't have before: self = <aiohttp.test_utils.TestServer object at 0x7f1108c474a8>, path = '//resource2//a//b/'
def make_url(self, path):
url = URL(path)
> assert not url.is_absolute()
E assert not True
E + where True = <bound method URL.is_absolute of URL('//resource2//a//b/')>()
E + where <bound method URL.is_absolute of URL('//resource2//a//b/')> = URL('//resource2//a//b/').is_absolute I can change the test cases and instead of Also, I'm missing the documentation in web_reference.rst. Do you want me to copy paste the docstring there or include it or? |
paths_to_check.append(request.path + '/') | ||
if merge_slashes and append_slash: | ||
paths_to_check.append( | ||
re.sub('//+', '/', request.path + '/')) |
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.
i think re.sub
should be compiled.
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.
In terms of optimization is not a big thing: http://stackoverflow.com/questions/452104/is-it-worth-using-pythons-re-compile
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.
ok
Create new section in web_reference "Middlewares", and add you doctoring. |
why |
Because of the following: tests/test_web_middleware.py:184:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
aiohttp/client.py:530: in __iter__
resp = yield from self._coro
aiohttp/test_utils.py:249: in request
method, self.make_url(path), *args, **kwargs
aiohttp/test_utils.py:237: in make_url
return self._server.make_url(path)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <aiohttp.test_utils.TestServer object at 0x7fdf41c7a630>, path = '//resource2//a//b/'
def make_url(self, path):
url = URL(path)
> assert not url.is_absolute()
E assert not True
E + where True = <bound method URL.is_absolute of URL('//resource2//a//b/')>()
E + where <bound method URL.is_absolute of URL('//resource2//a//b/')> = URL('//resource2//a//b/').is_absolute
aiohttp/test_utils.py:82: AssertionError If the url is absolute its not a valid url to query I don't know why. This is a behavior that was introduced with yarl because I don't remember this happening before. |
i see. is it possible to skip |
I have no idea, the code for the
which doesn't accept extra parameter. I believe it could be a |
ok, i will fix that |
@argaen i added tests that you deleted. some of them actually failing. |
@argaen i need you to review failing tests |
What do these changes do?
Adds a
normalize_path_middleware
middleware that deals with trailing slashes and double slashes in path.As a side effect, also
Request.clone()
was implemented.Are there changes in behavior for the user?
Users will now be able to do:
Related issue number
#355
Checklist
Extra checklist
Request.clone