Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rfc6266
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
rfc6266
Commits
b98dcd40
Commit
b98dcd40
authored
Jan 30, 2012
by
Gabriel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify the API.
parent
fefcfd32
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
21 deletions
+30
-21
rfc6266.py
+30
-21
No files found.
rfc6266.py
View file @
b98dcd40
...
...
@@ -2,8 +2,11 @@
"""Implements RFC 6266, the Content-Disposition HTTP header.
ContentDisposition handles the receiver side,
header_for_filename handles the sender side.
parse_headers (and variant parse_httplib2_response) handles the receiver side.
It returns a ContentDisposition object with attributes like is_inline,
filename_unsafe, filename_sanitized.
build_header handles the sender side.
"""
from
lepl
import
*
...
...
@@ -15,7 +18,12 @@ import os.path
import
re
__all__
=
(
'ContentDisposition'
,
'header_for_filename'
,
)
__all__
=
(
'ContentDisposition'
,
'parse_headers'
,
'parse_httplib2_response'
,
'build_header'
,
)
LangTagged
=
namedtuple
(
'LangTagged'
,
'string langtag'
)
...
...
@@ -33,8 +41,8 @@ class ContentDisposition(object):
def
__init__
(
self
,
disposition
=
'inline'
,
assocs
=
None
,
location
=
None
):
"""This constructor is used internally after parsing the header.
Instances should generally be created from a factory
class
method, such as from_headers and from
_httplib2_response.
Instances should generally be created from a factory
function, such as parse_headers and parse
_httplib2_response.
"""
self
.
disposition
=
disposition
...
...
@@ -122,13 +130,13 @@ class ContentDisposition(object):
return
'ContentDisposition(
%
r,
%
r,
%
r)'
%
(
self
.
disposition
,
self
.
assocs
,
self
.
location
)
@classmethod
def
from_headers
(
cls
,
content_disposition
,
location
=
None
):
def
parse_headers
(
content_disposition
,
location
=
None
):
"""Build a ContentDisposition from header values.
"""
if
content_disposition
is
None
:
return
cls
(
location
=
location
)
return
ContentDisposition
(
location
=
location
)
# Both alternatives seem valid.
if
False
:
...
...
@@ -144,6 +152,7 @@ class ContentDisposition(object):
# in the filename parameter. But it does mean we occasionally
# give less-than-certain values for some legacy senders.
content_disposition
=
content_disposition
.
encode
(
'iso-8859-1'
)
# Check the caller already did LWS-folding (normally done
# when separating header names and values; RFC 2616 section 2.2
# says it should be done before interpretation at any rate).
...
...
@@ -155,16 +164,17 @@ class ContentDisposition(object):
# However http doesn't allow isolated CR and LF in headers outside
# of LWS.
assert
is_lws_safe
(
content_disposition
)
parsed
=
content_disposition_value
.
parse
(
content_disposition
)
return
ContentDisposition
(
disposition
=
parsed
[
0
],
assocs
=
parsed
[
1
:],
location
=
location
)
@classmethod
def
from_httplib2_response
(
cls
,
response
):
def
parse_httplib2_response
(
response
):
"""Build a ContentDisposition from an httplib2 response.
"""
return
cls
.
from
_headers
(
return
parse
_headers
(
response
[
'content-disposition'
],
response
[
'content-location'
])
...
...
@@ -298,7 +308,7 @@ def qd_quote(text):
return
text
.
replace
(
'
\\
'
,
'
\\\\
'
)
.
replace
(
'"'
,
'
\\
"'
)
def
header_for_filename
(
def
build_header
(
filename
,
disposition
=
'attachment'
,
filename_compat
=
None
):
"""Generate a Content-Disposition header for a given filename.
...
...
@@ -357,17 +367,17 @@ def header_for_filename(
def
test_parsing
():
cdfh
=
ContentDisposition
.
from_headers
assert
ContentDisposition
()
.
disposition
==
'inline
'
assert
cdfh
(
'attachment'
)
.
disposition
==
'attachment
'
assert
cdfh
(
'attachment; key=val'
)
.
assocs
[
'key'
]
==
'val'
assert
cdfh
(
'attachment; filename=simple'
)
.
filename_unsafe
==
'simple'
assert
parse_headers
(
None
)
.
disposition
==
'inline'
assert
parse_headers
(
'attachment'
)
.
disposition
==
'attachment
'
assert
parse_headers
(
'attachment; key=val'
)
.
assocs
[
'key'
]
==
'val
'
assert
parse_headers
(
'attachment; filename=simple'
)
.
filename_unsafe
==
'simple'
# test ISO-8859-1
fname
=
cdfh
(
u'attachment; filename="oyé"'
)
.
filename_unsafe
fname
=
parse_headers
(
u'attachment; filename="oyé"'
)
.
filename_unsafe
assert
fname
==
u'oyé'
,
repr
(
fname
)
cd
=
cdfh
(
cd
=
parse_headers
(
'attachment; filename="EURO rates";'
' filename*=utf-8
\'\'
%
e2
%82%
ac
%20
rates'
)
assert
cd
.
filename_unsafe
==
u'€ rates'
...
...
@@ -375,8 +385,7 @@ def test_parsing():
def
test_roundtrip
():
def
roundtrip
(
filename
):
return
ContentDisposition
.
from_headers
(
header_for_filename
(
filename
))
.
filename_unsafe
return
parse_headers
(
build_header
(
filename
))
.
filename_unsafe
def
assert_roundtrip
(
filename
):
assert
roundtrip
(
filename
)
==
filename
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment