Commit 7d175c02 authored by iv's avatar iv Committed by iv

Add mimetypes + clean some code.

parent e6d856b1
...@@ -6,6 +6,7 @@ import urlparse ...@@ -6,6 +6,7 @@ import urlparse
import shutil import shutil
import utils import utils
import os import os
import mimetypes
app = Flask(__name__.split('.')[0]) app = Flask(__name__.split('.')[0])
app.config.from_object(__name__) app.config.from_object(__name__)
...@@ -99,7 +100,6 @@ def before_request(): ...@@ -99,7 +100,6 @@ def before_request():
headers = {} headers = {}
headers['Access-Control-Max-Age'] = '3600' headers['Access-Control-Max-Age'] = '3600'
headers['Access-Control-Allow-Credentials'] = 'true' headers['Access-Control-Allow-Credentials'] = 'true'
content = ''
headers['Access-Control-Allow-Headers'] = \ headers['Access-Control-Allow-Headers'] = \
'Origin, Accept, Accept-Encoding, Content-Length, ' + \ 'Origin, Accept, Accept-Encoding, Content-Length, ' + \
'Content-Type, Authorization, Depth, If-Modified-Since, '+ \ 'Content-Type, Authorization, Depth, If-Modified-Since, '+ \
...@@ -112,16 +112,14 @@ def before_request(): ...@@ -112,16 +112,14 @@ def before_request():
specific_header = request.headers.get('Access-Control-Request-Headers') specific_header = request.headers.get('Access-Control-Request-Headers')
if is_authorized(): if is_authorized():
response = make_response(content, 200) status_code = 200
response.headers = headers
elif request.method == 'OPTIONS' and specific_header: elif request.method == 'OPTIONS' and specific_header:
# tells the world we do CORS when authorized # tells the world we do CORS when authorized
debug('OPTIONS request special header: ' + specific_header) debug('OPTIONS request special header: ' + specific_header)
headers['Access-Control-Request-Headers'] = specific_header headers['Access-Control-Request-Headers'] = specific_header
headers['Access-Control-Allow-Methods'] = ', '.join(ALLOWED_METHODS) headers['Access-Control-Allow-Methods'] = ', '.join(ALLOWED_METHODS)
response = make_response(content, 200) response = make_response('', 200, headers)
response.headers = headers
return response return response
else: else:
...@@ -130,12 +128,12 @@ def before_request(): ...@@ -130,12 +128,12 @@ def before_request():
urlparse.urljoin(request.url_root, urlparse.urljoin(request.url_root,
URI_BEGINNING_PATH['authorization']) + '?sig=' + \ URI_BEGINNING_PATH['authorization']) + '?sig=' + \
s.get_signature(origin) + '{&back_url,origin}' s.get_signature(origin) + '{&back_url,origin}'
response = make_response(content, 401) response = make_response('', 401, headers)
response.headers = headers
# do not handle the request if not authorized # do not handle the request if not authorized
return response return response
g.response = response g.status = status_code
g.headers = headers
class WebDAV(MethodView): class WebDAV(MethodView):
""" WebDAV server that handles request when destinated to it """ """ WebDAV server that handles request when destinated to it """
...@@ -168,32 +166,36 @@ class WebDAV(MethodView): ...@@ -168,32 +166,36 @@ class WebDAV(MethodView):
GET: GET:
return headers + body (resource content or list of resources) return headers + body (resource content or list of resources)
""" """
response = g.response status = g.status
localpath = app.fs_handler.uri2local(request.path) headers = g.headers
localpath = app.fs_handler.uri2local(URI_BEGINNING_PATH['webdav'] + pathname)
data = '' data = ''
if os.path.isdir(localpath): if os.path.isdir(localpath):
data = render_template('get_collection.html', link_list=app.fs_handler.get_children(request.path)) data = render_template('get_collection.html', link_list=app.fs_handler.get_children(URI_BEGINNING_PATH['webdav'] + pathname))
elif os.path.isfile(localpath): elif os.path.isfile(localpath):
try: try:
data_resource = app.fs_handler.get_data(request.path) headers["Content-type"] = mimetypes.guess_type(localpath)[0]
def generate(): data_resource = app.fs_handler.get_data(URI_BEGINNING_PATH['webdav'] + pathname)
data = data_resource.read(BUFFER_SIZE) if len(data_resource) > BUFFER_SIZE:
while data: def generate():
debug('get a chunk: ' + data)
yield data
data = data_resource.read(BUFFER_SIZE) data = data_resource.read(BUFFER_SIZE)
return Response(response=generate(), status=response.status, while data:
headers=response.headers) debug('get a chunk: ' + data)
yield data
data = data_resource.read(BUFFER_SIZE)
return Response(response=generate(), status=status,
headers=headers)
return Response(response=data_resource.read(BUFFER_SIZE),
status=status, headers=headers)
except Exception, e: except Exception, e:
debug(e) debug(e)
response.status = '500' status = 500
else: else:
response.status = '404' status = 404
response.data = data
return response return make_response(data, status, headers)
def put(self, pathname): def put(self, pathname):
""" """
...@@ -202,19 +204,20 @@ class WebDAV(MethodView): ...@@ -202,19 +204,20 @@ class WebDAV(MethodView):
on ressource: create if not exists, else change content on ressource: create if not exists, else change content
""" """
response = g.response status = g.status
headers = g.headers
localpath = app.fs_handler.uri2local(request.path) localpath = app.fs_handler.uri2local(URI_BEGINNING_PATH['webdav'] + pathname)
# TODO: get large request chunk by chunk... # TODO: get large request chunk by chunk...
request_body = self.get_body() request_body = self.get_body()
if request_body is None: if request_body is None:
response.status = '500' status = 500
elif os.path.isdir(localpath): elif os.path.isdir(localpath):
response.status = '405' status = 405
else: else:
response.status = str(app.fs_handler.put(request.path, request_body)) status = app.fs_handler.put(URI_BEGINNING_PATH['webdav'] + pathname, request_body)
return response return make_response('', status, headers)
def propfind(self, pathname): def propfind(self, pathname):
""" """
...@@ -222,17 +225,18 @@ class WebDAV(MethodView): ...@@ -222,17 +225,18 @@ class WebDAV(MethodView):
return informations about the properties of a resource/collection return informations about the properties of a resource/collection
into a XML body response into a XML body response
""" """
response = g.response status = g.status
headers = g.headers
pf = utils.PropfindProcessor( pf = utils.PropfindProcessor(
request.path, URI_BEGINNING_PATH['webdav'] + pathname,
app.fs_handler, app.fs_handler,
request.headers.get('Depth', 'infinity'), request.headers.get('Depth', 'infinity'),
self.get_body()) self.get_body())
try: try:
response.data = pf.create_response() + '\n' response = make_response(pf.create_response() + '\n', status, headers)
except IOError: except IOError:
response.status = '404' response = make_response('', 404, headers)
return response return response
...@@ -242,11 +246,12 @@ class WebDAV(MethodView): ...@@ -242,11 +246,12 @@ class WebDAV(MethodView):
allow changes of the properties allow changes of the properties
""" """
response = g.response headers = g.headers
# currently unsupported # currently unsupported
response.status = '501' status = 501
return response
return make_response('', status, headers)
def mkcol(self, pathname): def mkcol(self, pathname):
""" """
...@@ -255,10 +260,10 @@ class WebDAV(MethodView): ...@@ -255,10 +260,10 @@ class WebDAV(MethodView):
system) system)
""" """
response = g.response headers = g.headers
response.status = str(app.fs_handler.mkcol(request.path)) status = app.fs_handler.mkcol(URI_BEGINNING_PATH['webdav'] + pathname)
return response return make_response('', status, headers)
def delete(self, pathname): def delete(self, pathname):
""" """
...@@ -266,24 +271,25 @@ class WebDAV(MethodView): ...@@ -266,24 +271,25 @@ class WebDAV(MethodView):
delete a resource or collection delete a resource or collection
""" """
response = g.response status = g.status
headers = g.headers
localpath = app.fs_handler.uri2local(request.path) localpath = app.fs_handler.uri2local(URI_BEGINNING_PATH['webdav'] + pathname)
if not os.path.exists(localpath): if not os.path.exists(localpath):
response.status = '404' status = 404
if os.path.isdir(localpath): if os.path.isdir(localpath):
try: try:
shutil.rmtree(localpath) shutil.rmtree(localpath)
response.status = '204' status = 204
except OSError: except OSError:
response.status = '403' status = 403
elif os.path.isfile(localpath): elif os.path.isfile(localpath):
try: try:
os.remove(localpath) os.remove(localpath)
response.status = '204' status = 204
except OSError: except OSError:
response.status = '403' status = 403
return response return make_response('', status, headers)
def copy(self, pathname): def copy(self, pathname):
""" """
...@@ -291,9 +297,10 @@ class WebDAV(MethodView): ...@@ -291,9 +297,10 @@ class WebDAV(MethodView):
copy a resource or collection copy a resource or collection
""" """
response = g.response status = g.status
headers = g.headers
localpath = app.fs_handler.uri2local(request.path) localpath = app.fs_handler.uri2local(URI_BEGINNING_PATH['webdav'] + pathname)
host = request.headers['Host'] host = request.headers['Host']
destination = request.headers['Destination'].split( destination = request.headers['Destination'].split(
host + URI_BEGINNING_PATH['webdav'], host + URI_BEGINNING_PATH['webdav'],
...@@ -302,17 +309,17 @@ class WebDAV(MethodView): ...@@ -302,17 +309,17 @@ class WebDAV(MethodView):
debug('COPY: %s -> %s' % (localpath, destination_path)) debug('COPY: %s -> %s' % (localpath, destination_path))
if not os.path.exists(localpath): if not os.path.exists(localpath):
response.status = '404' status = 404
elif not destination_path: elif not destination_path:
response.status = '400' status = 400
elif 'Overwrite' in request.headers and \ elif 'Overwrite' in request.headers and \
request.headers['Overwrite'] == 'F' \ request.headers['Overwrite'] == 'F' \
and os.path.exists(destination_path): and os.path.exists(destination_path):
response.status = '412' status = 412
else: else:
response.status = '201' status = 201
if os.path.exists(destination_path): if os.path.exists(destination_path):
response.status = self.delete(destination) status = self.delete(destination)
if os.path.isfile(localpath): if os.path.isfile(localpath):
try: try:
...@@ -324,7 +331,7 @@ class WebDAV(MethodView): ...@@ -324,7 +331,7 @@ class WebDAV(MethodView):
shutil.copytree(localpath, destination_path) shutil.copytree(localpath, destination_path)
except Exception: except Exception:
debug('problem with copytree') debug('problem with copytree')
return response return make_response('', status, headers)
def move(self, pathname): def move(self, pathname):
""" """
...@@ -332,14 +339,14 @@ class WebDAV(MethodView): ...@@ -332,14 +339,14 @@ class WebDAV(MethodView):
move a resource or collection move a resource or collection
""" """
response = g.response headers = g.headers
copy_response = self.copy(request.path) copy_response = self.copy(URI_BEGINNING_PATH['webdav'] + pathname)
response.status = copy_response.status status = copy_response.status
if copy_response.status == '201' or copy_response.status == '204': if copy_response.status == '201' or copy_response.status == '204':
delete_response = self.delete(request.path) delete_response = self.delete(URI_BEGINNING_PATH['webdav'] + pathname)
if delete_response.status != '204': if delete_response.status != '204':
response.status = '424' status = '424'
return response return response
def options(self, pathname): def options(self, pathname):
...@@ -349,7 +356,7 @@ class WebDAV(MethodView): ...@@ -349,7 +356,7 @@ class WebDAV(MethodView):
sent in the before_request in that case... sent in the before_request in that case...
""" """
return g.response return make_response('', g.status, g.headers)
app.add_url_rule(URI_BEGINNING_PATH['webdav'] + '<path:pathname>', app.add_url_rule(URI_BEGINNING_PATH['webdav'] + '<path:pathname>',
...@@ -386,9 +393,7 @@ def authorize(): ...@@ -386,9 +393,7 @@ def authorize():
return 'Something went wrong...' return 'Something went wrong...'
response.status = '301' # moved permanently response.status = '301' # moved permanently
response.headers['Location'] = '/' response.headers['Location'] = '/' if not back else back
if back:
response.headers['Location'] = back
else: else:
debug(request.args) debug(request.args)
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<body> <body>
<ul> <ul>
{% for link in link_list %} {% for link in link_list %}
<li> <a href={{ link }}>{{ link }}</a> </li> <li> <a href="{{ link }}">{{ link }}</a> </li>
{% endfor %} {% endfor %}
</ul> </ul>
</body> </body>
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<body> <body>
<ul> <ul>
{% for link in link_correspondance %} {% for link in link_correspondance %}
<li> <a href={{ link[1] }}>{{ link[0] }}</a> </li> <li> <a href="{{ link[1] }}">{{ link[0] }}</a> </li>
{% endfor %} {% endfor %}
</ul> </ul>
</body> </body>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment