Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
added http_headers
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed Feb 22, 2024
1 parent c2c4adf commit a71dd35
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
15 changes: 14 additions & 1 deletion CloudFlare/cloudflare.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def __init__(self, config):
self.max_request_retries = int(self.max_request_retries)
except (TypeError, ValueError):
self.max_request_retries = DEFAULT_MAX_REQUEST_RETRIES
self.additional_http_headers = config['http_headers'] if 'http_headers' in config else None
self.profile = config['profile']
self.network = CFnetwork(
use_sessions=self.use_sessions,
Expand Down Expand Up @@ -135,6 +136,14 @@ def _add_headers(self, method, data, files, content_type=None):
self.headers['Content-Type'] = 'multipart/form-data'
# however something isn't right and this works ... look at again later!
del self.headers['Content-Type']
if self.additional_http_headers:
for h in self.additional_http_headers:
t, v = h.split(':', 1)
t = t.strip()
v = v.strip()
if len(v) > 0 and ((v[0] == '"' and v[-1] == '"') or (v[0] == "'" and v[-1] == "'")):
v = v[1:-1]
self.headers[t] = v
return data, files

def _add_auth_headers(self, method):
Expand Down Expand Up @@ -978,7 +987,7 @@ def api_from_openapi(self, url=None):

return self._base.api_from_openapi(url)

def __init__(self, email=None, key=None, token=None, certtoken=None, debug=False, raw=False, use_sessions=True, profile=None, base_url=None, global_request_timeout=None, max_request_retries=None):
def __init__(self, email=None, key=None, token=None, certtoken=None, debug=False, raw=False, use_sessions=True, profile=None, base_url=None, global_request_timeout=None, max_request_retries=None, http_headers=None):
""" Cloudflare v4 API"""

self._base = None
Expand Down Expand Up @@ -1020,6 +1029,10 @@ def __init__(self, email=None, key=None, token=None, certtoken=None, debug=False
config['global_request_timeout'] = global_request_timeout
if max_request_retries is not None:
config['max_request_retries'] = max_request_retries
if http_headers is not None:
if not isinstance(http_headers, list):
raise TypeError('http_headers is not a list')
config['http_headers'] = http_headers

# we do not need to handle item.call values - they pass straight thru

Expand Down
11 changes: 9 additions & 2 deletions CloudFlare/read_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def read_configs(profile=None):

config['global_request_timeout'] = os.getenv('CLOUDFLARE_GLOBAL_REQUEST_TIMEOUT')
config['max_request_retries'] = os.getenv('CLOUDFLARE_MAX_REQUEST_RETRIES')
config['http_headers'] = os.getenv('CLOUDFLARE_HTTP_HEADERS')

# grab values from config files
cp = configparser.ConfigParser()
Expand Down Expand Up @@ -64,11 +65,15 @@ def read_configs(profile=None):
if not cp.has_section(profile):
raise ReadConfigError("%s: configuration section missing - configuration file only has these sections: %s" % (profile, ','.join(cp.sections()))) from None

for option in ['email', 'key', 'token', 'certtoken', 'extras', 'base_url', 'openapi_url', 'global_request_timeout', 'max_request_retries']:
for option in ['email', 'key', 'token', 'certtoken', 'extras', 'base_url', 'openapi_url', 'global_request_timeout', 'max_request_retries', 'http_headers']:
try:
config_value = cp.get(profile, option)
if option == 'extras':
# we join all values together as one space seperated strings
config[option] = re.sub(r"\s+", ' ', config_value)
elif option == 'http_headers':
# we keep lines as is for now
config[option] = config_value
else:
config[option] = re.sub(r"\s+", '', config_value)
if config[option] is None or config[option] == '':
Expand All @@ -87,9 +92,11 @@ def read_configs(profile=None):
except (configparser.NoOptionError, configparser.NoSectionError):
pass

# do any final cleanup - only needed for extras (which are multiline)
# do any final cleanup - only needed for extras and http_headers (which are multiline)
if 'extras' in config and config['extras'] is not None:
config['extras'] = config['extras'].strip().split(' ')
if 'http_headers' in config and config['http_headers'] is not None:
config['http_headers'] = [h for h in config['http_headers'].split('\n') if len(h) > 0]

# remove blank entries
for x in sorted(config.keys()):
Expand Down

0 comments on commit a71dd35

Please sign in to comment.