Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
interfect
funkwhale
Commits
46d40c7f
Verified
Commit
46d40c7f
authored
Mar 31, 2018
by
Eliot Berriot
Browse files
Util function to convert django meta to proper headers
parent
043153a5
Changes
2
Hide whitespace changes
Inline
Side-by-side
api/funkwhale_api/federation/utils.py
View file @
46d40c7f
...
...
@@ -12,3 +12,24 @@ def full_url(path):
return
root
+
'/'
+
path
else
:
return
root
+
path
def
clean_wsgi_headers
(
raw_headers
):
"""
Convert WSGI headers from CONTENT_TYPE to Content-Type notation
"""
cleaned
=
{}
non_prefixed
=
[
'content_type'
,
'content_length'
,
]
for
raw_header
,
value
in
raw_headers
.
items
():
h
=
raw_header
.
lower
()
if
not
h
.
startswith
(
'http_'
)
and
h
not
in
non_prefixed
:
continue
words
=
h
.
replace
(
'http_'
,
''
,
1
).
split
(
'_'
)
cleaned_header
=
'-'
.
join
([
w
.
capitalize
()
for
w
in
words
])
cleaned
[
cleaned_header
]
=
value
return
cleaned
api/tests/federation/test_utils.py
View file @
46d40c7f
...
...
@@ -12,3 +12,37 @@ from funkwhale_api.federation import utils
def
test_full_url
(
settings
,
url
,
path
,
expected
):
settings
.
FUNKWHALE_URL
=
url
assert
utils
.
full_url
(
path
)
==
expected
def
test_extract_headers_from_meta
():
wsgi_headers
=
{
'HTTP_HOST'
:
'nginx'
,
'HTTP_X_REAL_IP'
:
'172.20.0.4'
,
'HTTP_X_FORWARDED_FOR'
:
'188.165.228.227, 172.20.0.4'
,
'HTTP_X_FORWARDED_PROTO'
:
'http'
,
'HTTP_X_FORWARDED_HOST'
:
'localhost:80'
,
'HTTP_X_FORWARDED_PORT'
:
'80'
,
'HTTP_CONNECTION'
:
'close'
,
'CONTENT_LENGTH'
:
'1155'
,
'CONTENT_TYPE'
:
'txt/application'
,
'HTTP_SIGNATURE'
:
'Hello'
,
'HTTP_DATE'
:
'Sat, 31 Mar 2018 13:53:55 GMT'
,
'HTTP_USER_AGENT'
:
'http.rb/3.0.0 (Mastodon/2.2.0; +https://mastodon.eliotberriot.com/)'
}
cleaned_headers
=
utils
.
clean_wsgi_headers
(
wsgi_headers
)
expected
=
{
'Host'
:
'nginx'
,
'X-Real-Ip'
:
'172.20.0.4'
,
'X-Forwarded-For'
:
'188.165.228.227, 172.20.0.4'
,
'X-Forwarded-Proto'
:
'http'
,
'X-Forwarded-Host'
:
'localhost:80'
,
'X-Forwarded-Port'
:
'80'
,
'Connection'
:
'close'
,
'Content-Length'
:
'1155'
,
'Content-Type'
:
'txt/application'
,
'Signature'
:
'Hello'
,
'Date'
:
'Sat, 31 Mar 2018 13:53:55 GMT'
,
'User-Agent'
:
'http.rb/3.0.0 (Mastodon/2.2.0; +https://mastodon.eliotberriot.com/)'
}
assert
cleaned_headers
==
expected
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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