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

IsADirectoryError when accessing folder with a static route #921

Closed
SteffenDE opened this issue Jun 10, 2016 · 8 comments
Closed

IsADirectoryError when accessing folder with a static route #921

SteffenDE opened this issue Jun 10, 2016 · 8 comments

Comments

@SteffenDE
Copy link

When accessing a URL handled by a static route, which is a directory, the server responds with HTTP 200 AND HTTP 500 in the response body instead of just responding by e.g. Forbidden.

$ http http://localhost:5000/web/static/
HTTP/1.1 200 OK
CONTENT-LENGTH: 204
CONTENT-TYPE: application/octet-stream
DATE: Fri, 10 Jun 2016 17:27:48 GMT
LAST-MODIFIED: Wed, 08 Jun 2016 14:01:43 GMT
SERVER: Python/3.5 aiohttp/0.21.6

HTTP/1.1 500 Internal Server Error
CONTENT-TYPE: text/html; charset=utf-8
CONTENT-LENGTH: 170
CONNECTION: close
DATE: Fri, 10 Jun 2016 17:27:48 GMT
SERVER: Python/3.5 aiohttp/0.21.6


<html>
  <hea

The route is added by calling

app.router.add_static("/web/static", "web/static")
@asvetlov
Copy link
Member

Please make sure web/static both is a directory and web server process has appropriate rights.
As side note better to use absolute path to directory, it helps to avoid weird problems.

@SteffenDE
Copy link
Author

web/static is a directory and the web server has appropriate rights. Accessing files inside of the directory works fine.
I realized that I forgot to include the traceback:

======== Running on http://127.0.0.1:8888/ ========
(Press CTRL+C to quit)
Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/site-packages/aiohttp/server.py", line 266, in start
    yield from self.handle_request(message, payload)
  File "/usr/local/lib/python3.5/site-packages/aiohttp/web.py", line 90, in handle_request
    resp = yield from handler(request)
  File "/usr/local/lib/python3.5/site-packages/aiohttp/web_urldispatcher.py", line 595, in handle
    with filepath.open('rb') as f:
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/pathlib.py", line 1136, in open
    opener=self._opener)
IsADirectoryError: [Errno 21] Is a directory: '/Users/Steffen/Desktop/static'

Using an absolute path does not solve the issue.

import asyncio
from aiohttp import web

app = web.Application()
app.router.add_static("/static", "/Users/Steffen/Desktop/static")

web.run_app(app, port=8888)
$ http http://127.0.0.1:8888/static/
HTTP/1.1 200 OK
CONTENT-LENGTH: 68
CONTENT-TYPE: application/octet-stream
DATE: Sat, 11 Jun 2016 20:17:17 GMT
LAST-MODIFIED: Sat, 11 Jun 2016 20:16:51 GMT
SERVER: Python/3.5 aiohttp/0.21.6

HTTP/1.1 500 Internal Server Error
CONTENT-TYPE: text/html; charset

@asvetlov
Copy link
Member

Gotcha!
aiohttp doesn't support directory indexing yet.
Please point in a browser a path to concrete file like http://localhost/static/image.jpg

@SteffenDE
Copy link
Author

I think directory indexing is not needed but wouldn't it be better if the server would respond with a correct HTTP response? Sending 200 and then the headers and half a body of a 500 error does not seem right to me.

@asvetlov
Copy link
Member

Agreed.
Let's keep the issue open until I'll find a time to fix it.
Indexing may be optional (off by default).

@trimailov
Copy link

Tasks to close the issue (discussed with @asvetlov):

If folder is accessed:

  • By default throw HTTP 403 - Forbidden
  • If folder indexing is enabled - list the directory
    • Probably extra methods are needed in web_urldispatcher.py:StaticRoute class.

trimailov added a commit to trimailov/aiohttp that referenced this issue Aug 1, 2016
trimailov added a commit to trimailov/aiohttp that referenced this issue Aug 1, 2016
If we want to access a static file dir, it should return `Forbidden
(403)` by default.

Related: aio-libs#921
trimailov added a commit to trimailov/aiohttp that referenced this issue Aug 1, 2016
XXX: need for a response to return proper html

Related: aio-libs#921
trimailov added a commit to trimailov/aiohttp that referenced this issue Aug 1, 2016
Also I now return in place, instead of creating variable and returning
later, I am not a fan of returning somewehere inside a method, though if
we tried to return `ret` at the end as before, but I guess it's the most
clean pattern to do this.

This is because we have to conditional blocks, either of which can
return from the method. If first condtitonal creates `ret` variable,
later conditional may just raise `HTTPNotFound` which we do not want.
Though, I do not want to check that `ret` is populated either. Thus
return in place.

Related: aio-libs#921
trimailov added a commit to trimailov/aiohttp that referenced this issue Aug 1, 2016
tejksat pushed a commit to tejksat/aiohttp that referenced this issue Aug 15, 2016
tejksat pushed a commit to tejksat/aiohttp that referenced this issue Aug 15, 2016
tejksat pushed a commit to tejksat/aiohttp that referenced this issue Aug 18, 2016
tejksat pushed a commit to tejksat/aiohttp that referenced this issue Aug 18, 2016
tejksat pushed a commit to tejksat/aiohttp that referenced this issue Aug 18, 2016
tejksat pushed a commit to tejksat/aiohttp that referenced this issue Aug 18, 2016
tejksat pushed a commit to tejksat/aiohttp that referenced this issue Aug 18, 2016
tejksat pushed a commit to tejksat/aiohttp that referenced this issue Aug 19, 2016
asvetlov pushed a commit that referenced this issue Aug 22, 2016
* Add `show_index` flag to StaticRoute

Related: #921

* Accessing static dir, should return 403 by default

If we want to access a static file dir, it should return `Forbidden
(403)` by default.

Related: #921

* WIP: if allowed - return directory's index, otherwise - 403

XXX: need for a response to return proper html

Related: #921

* Return directory index list if we allow to show it

Also I now return in place, instead of creating variable and returning
later, I am not a fan of returning somewehere inside a method, though if
we tried to return `ret` at the end as before, but I guess it's the most
clean pattern to do this.

This is because we have to conditional blocks, either of which can
return from the method. If first condtitonal creates `ret` variable,
later conditional may just raise `HTTPNotFound` which we do not want.
Though, I do not want to check that `ret` is populated either. Thus
return in place.

Related: #921

* Test if correct statuses are returned if we allow acces index or not

Related: #921

* Prettier directory output

- Better method name
- More human readable output (newlines)
- Title is shown as relative path to static directory and not as posix
  path
- Show directories with slash at the end of the name

* Remove unnecessary comment

* Update docs

And fix a minor typo

* Check of html content is added for the response page with a directory index

Related: #921

* Test of accessing non-existing static resource added

Related: #921

* 403 Forbidden returned if the permission error occurred on listing requested folder

Related: #921

* show_index parameter usage of app.router.add_static method documented

Related: #921

* Import order fixed

Related: #921

* Make directory index sorted so stable for tests

Related: #921

* Improve tests coverage

Related: #921

* Improve tests coverage

Related: #921
@asvetlov
Copy link
Member

Fixed by #1094

@lock
Copy link

lock bot commented Oct 29, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 29, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants