# This file is part of rhizi, a collaborative knowledge graph editor.
# Copyright (C) 2014-2015 Rhizi
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see
' tag wrapped HTTP response
"""
resp_arr = ['',
'',
html_str,
''
]
return (''.join(resp_arr), 200)
def make_response__json__html(status=200, html_str=''):
"""
Construct a json response with HTML payload
"""
return make_response__json(status=status, data={'response__html': html_str })
def make_response__json__redirect(redirect_url, status=303, html_str=''):
"""
Construct a json response with redirect payload
"""
assert status >= 300 and status < 400
return make_response__json(status=status,
data={'response__html': html_str,
'redirect_url': redirect_url })
def sock_addr_from_env_HTTP_headers(req_env, key_name__addr):
"""
Extract remote socket address based on header data
[!] if header value contains more than a single address only the first one is used.
@param key_name__addr: header name to probe for address value
@return: (remote_addr, remote_port) where remote_port may be None
@raise Exception: if header is missing from env
"""
header_key = 'HTTP_' + key_name__addr.upper().replace('-', '_')
addr_set_str = req_env.get(header_key)
if not addr_set_str:
raise Exception('\'%s\' header missing' % (key_name__addr))
addr_set = addr_set_str.split(',')
if len(addr_set) > 1:
log.warning('%s header contains multiple addresses, using first: header value: \'%s\'' % (key_name__addr, addr_set_str))
addr_val = addr_set.pop()
# deduce addr:port - port might not be present in header value
addr_val_arr = addr_val.split(':')
rmt_addr = addr_val_arr[0]
rmt_port = addr_val_arr[1] if 2 == len(addr_val_arr) else None
return rmt_addr, rmt_port
def sock_addr_from_REMOTE_X_keys(req_env):
rmt_addr = req_env['REMOTE_ADDR']
rmt_port = req_env['REMOTE_PORT']
return rmt_addr, rmt_port