Skip to content

Commit

Permalink
HTML header keys.lower() for HTTP2
Browse files Browse the repository at this point in the history
  • Loading branch information
cclauss committed Nov 9, 2021
1 parent 8a62727 commit 559bd14
Show file tree
Hide file tree
Showing 51 changed files with 132 additions and 110 deletions.
2 changes: 1 addition & 1 deletion openlibrary/accounts/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ def s3auth(cls, access_key, secret_key):
url = lending.config_ia_s3_auth_url
try:
response = requests.get(url, headers={
'Content-Type': 'application/json',
'content-type': 'application/json',
'authorization': 'LOW %s:%s' % (access_key, secret_key)
})
response.raise_for_status()
Expand Down
14 changes: 7 additions & 7 deletions openlibrary/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _request(self, path, method='GET', data=None, headers=None, params=None):
headers = headers or {}
params = params or {}
if self.cookie:
headers['Cookie'] = self.cookie
headers['cookie'] = self.cookie

try:
response = requests.request(method, url, data=data, headers=headers,
Expand Down Expand Up @@ -95,15 +95,15 @@ def autologin(self, section=None):
def login(self, username, password):
"""Login to Open Library with given credentials.
"""
headers = {'Content-Type': 'application/json'}
headers = {'content-type': 'application/json'}
try:
data = json.dumps(dict(username=username, password=password))
response = self._request('/account/login', method='POST', data=data, headers=headers)
except OLError as e:
response = e

if 'Set-Cookie' in response.headers:
cookies = response.headers['Set-Cookie'].split(',')
cookies = response.headers['set-cookie'].split(',')
self.cookie = ';'.join([c.split(';')[0] for c in cookies])

def get(self, key, v=None):
Expand All @@ -127,21 +127,21 @@ def _get_many(self, keys):
return response.json()['result']

def save(self, key, data, comment=None):
headers = {'Content-Type': 'application/json'}
headers = {'content-type': 'application/json'}
data = marshal(data)
if comment:
headers['Opt'] = '"%s/dev/docs/api"; ns=42' % self.base_url
headers['opt'] = '"%s/dev/docs/api"; ns=42' % self.base_url
headers['42-comment'] = comment
data = json.dumps(data)
return self._request(key, method="PUT", data=data, headers=headers).content

def _call_write(self, name, query, comment, action):
headers = {'Content-Type': 'application/json'}
headers = {'content-type': 'application/json'}
query = marshal(query)

# use HTTP Extension Framework to add custom headers. see RFC 2774 for more details.
if comment or action:
headers['Opt'] = '"%s/dev/docs/api"; ns=42' % self.base_url
headers['opt'] = '"%s/dev/docs/api"; ns=42' % self.base_url
if comment:
headers['42-comment'] = comment
if action:
Expand Down
4 changes: 2 additions & 2 deletions openlibrary/catalog/amazon/crawl.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ def now():

max_size = 1024 * 1024 * 1024 * 10 # 10 GB
ip = '207.241.229.141'
content_type_hdr = 'Content-Type: '
content_type_hdr = 'content-type: '
re_charset_header = re.compile('; charset=(.+)\r\n')
version_block = '1 0 Open Library\nURL IP-address Archive-date Content-type Archive-length\n'
version_block = '1 0 Open Library\nURL IP-address Archive-date Content-Type Archive-length\n'

class Scraper:
def __init__(self, recording=True):
Expand Down
4 changes: 2 additions & 2 deletions openlibrary/catalog/get_ia.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def get_from_archive_bulk(locator):
"""
if locator.startswith('marc:'):
locator = locator[5:]
filename, offset, length = locator.split (":")
filename, offset, length = locator.split(":")
offset = int(offset)
length = int(length)

Expand All @@ -142,7 +142,7 @@ def get_from_archive_bulk(locator):

assert 0 < length < MAX_MARC_LENGTH

response = urlopen_keep_trying(url, headers={'Range': 'bytes=%d-%d' % (r0, r1)})
response = urlopen_keep_trying(url, headers={'range': 'bytes=%d-%d' % (r0, r1)})
data = None
if response:
# this truncates the data to MAX_MARC_LENGTH, but is probably not necessary here?
Expand Down
2 changes: 1 addition & 1 deletion openlibrary/catalog/utils/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def urlopen(url, data=None):
version = "%s.%s.%s" % sys.version_info[:3]
user_agent = 'Mozilla/5.0 (openlibrary; %s) Python/%s' % (__name__, version)
headers = {
'User-Agent': user_agent
'user-agent': user_agent
}

return requests.get(url, data=data, headers=headers)
Expand Down
4 changes: 2 additions & 2 deletions openlibrary/components/ObservationForm/ObservationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function deleteObservation(type, value, workKey, username) {
fetch(`${workKey}/observations`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
'content-type': 'application/json'
},
body: JSON.stringify(data)
})
Expand All @@ -31,7 +31,7 @@ export function addObservation(type, value, workKey, username) {
fetch(`${workKey}/observations`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
'content-type': 'application/json'
},
body: JSON.stringify(data)
})
Expand Down
4 changes: 2 additions & 2 deletions openlibrary/core/civicrm.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_contact(username=None, contact_id=None):
params=data,
timeout=3,
headers=dict(
Authorization=f"Basic {lending.config_ia_civicrm_api.get('auth', '')}"
Authorization=f"basic {lending.config_ia_civicrm_api.get('auth', '')}"
))
contacts = r.status_code == 200 and r.json().get("values", None)
return contacts and contacts[0]
Expand Down Expand Up @@ -83,7 +83,7 @@ def get_sponsorships_by_contact_id(contact_id=None, isbn=None):
timeout=3,
params=data,
headers=dict(
Authorization=f"Basic {lending.config_ia_civicrm_api.get('auth', '')}"
Authorization=f"basic {lending.config_ia_civicrm_api.get('auth', '')}"
)).json().get("values")
return [{
"isbn": t.pop(CIVI_ISBN),
Expand Down
8 changes: 5 additions & 3 deletions openlibrary/core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ def new_start_response(status, headers):
response.status = status
response.headers = headers

if status.startswith("200") and get_response_header("Content-Type", "").startswith("text/"):
headers.append(("Content-Encoding", "gzip"))
headers.append(("Vary", "Accept-Encoding"))
if status.startswith("200") and (
get_response_header("content-type", "").startswith("text/")
):
headers.append(("content-encoding", "gzip"))
headers.append(("vary", "Accept-Encoding"))
response.compress = True
return start_response(status, headers)

Expand Down
2 changes: 1 addition & 1 deletion openlibrary/core/sponsorships.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def email_sponsor(recipient, book, bcc="mek@archive.org"):
'<p>The <a href="https://openlibrary.org">Open Library</a> Team</p>'
),
bcc=bcc,
headers={'Content-Type':'text/html;charset=utf-8'}
headers={'content-type': 'text/html;charset=utf-8'}
)
return resp

Expand Down
10 changes: 5 additions & 5 deletions openlibrary/coverstore/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,13 @@ def redirect(id):
if not web.modified(trim_microsecond(d.created), etag=etag):
raise web.notmodified()

web.header('Cache-Control', 'public')
web.header('cache-control', 'public')
web.expires(100 * 365 * 24 * 3600) # this image is not going to expire in next 100 years.
else:
web.header('Cache-Control', 'public')
web.header('cache-control', 'public')
web.expires(10*60) # Allow the client to cache the image for 10 mins to avoid further requests

web.header('Content-Type', 'image/jpeg')
web.header('content-type', 'image/jpeg')
try:
return read_image(d, size)
except IOError:
Expand Down Expand Up @@ -362,7 +362,7 @@ def GET(self, category, key, value):
d = _query(category, key, value)

if key == 'id':
web.header('Content-Type', 'application/json')
web.header('content-type', 'application/json')
d = db.details(value)
if d:
if isinstance(d['created'], datetime.datetime):
Expand Down Expand Up @@ -410,7 +410,7 @@ def process(r):
result = [process(r) for r in result]

json_data = json.dumps(result)
web.header('Content-Type', 'text/javascript')
web.header('content-type', 'text/javascript')
if i.callback:
return "%s(%s);" % (i.callback, json_data)
else:
Expand Down
8 changes: 4 additions & 4 deletions openlibrary/coverstore/tests/test_webapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def upload(self, olid, path):

path = join(static_dir, path)
content_type, data = utils.urlencode({'olid': olid, 'data': open(path).read()})
b.open('/b/upload2', data, {'Content-Type': content_type})
b.open('/b/upload2', data, {'content-type': content_type})
return json.loads(b.data)['id']

def delete(self, id, redirect_url=None):
Expand Down Expand Up @@ -122,7 +122,7 @@ def test_upload(self):
path = join(static_dir, 'logos/logo-en.png')
filedata = open(path).read()
content_type, data = utils.urlencode({'olid': 'OL1234M', 'data': filedata})
b.open('/b/upload2', data, {'Content-Type': content_type})
b.open('/b/upload2', data, {'content-type': content_type})
assert b.status == 200
id = json.loads(b.data)['id']

Expand All @@ -138,7 +138,7 @@ def test_upload_with_url(self, monkeypatch):
monkeypatch.setattr(code, 'download', mock)

content_type, data = utils.urlencode({'olid': 'OL1234M', 'source_url': source_url})
b.open('/b/upload2', data, {'Content-Type': content_type})
b.open('/b/upload2', data, {'content-type': content_type})
assert b.status == 200
id = json.loads(b.data)['id']

Expand All @@ -154,7 +154,7 @@ def verify_upload(self, id, data, expected_info=None):

response = b.open('/b/id/%d.jpg' % id)
assert b.status == 200
assert response.info().getheader('Content-Type') == 'image/jpeg'
assert response.info().getheader('content-type') == 'image/jpeg'
assert b.data == data

b.open('/b/id/%d-S.jpg' % id)
Expand Down
4 changes: 2 additions & 2 deletions openlibrary/coverstore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def ol_get(olkey):


def download(url):
return requests.get(url, headers={'User-Agent': USER_AGENT}).content
return requests.get(url, headers={'user-agent': USER_AGENT}).content


def urldecode(url):
Expand Down Expand Up @@ -156,7 +156,7 @@ def encode(key, value, out):
if isinstance(value, file):
out.append('--' + BOUNDARY)
out.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, value.name))
out.append('Content-Type: %s' % get_content_type(value.name))
out.append('content-type: %s' % get_content_type(value.name))
out.append('')
out.append(value.read())
elif isinstance(value, list):
Expand Down
2 changes: 1 addition & 1 deletion openlibrary/data/sitemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="robots" content="noindex,follow" />
<link href="/css/all.css" rel="stylesheet" type="text/css" />
<title>$page.title</title>
Expand Down
2 changes: 1 addition & 1 deletion openlibrary/i18n/cs/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ msgstr ""
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: cs <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"content-type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.5.3\n"
"Language: cs\n"
Expand Down
2 changes: 1 addition & 1 deletion openlibrary/i18n/de/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ msgstr ""
"Last-Translator: justcomplaining <justcomplaining@mailbox.org>\n"
"Language-Team: German\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"content-type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.5.3\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
Expand Down
2 changes: 1 addition & 1 deletion openlibrary/i18n/es/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ msgstr ""
"Language-Team: \n"
"Language: es\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"content-type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"Generated-By: Babel 2.7.0\n"
Expand Down
2 changes: 1 addition & 1 deletion openlibrary/i18n/fr/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ msgstr ""
"Language-Team: fr <traduc@traduc.org>\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"content-type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.7.0\n"

Expand Down
2 changes: 1 addition & 1 deletion openlibrary/i18n/hr/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ msgstr ""
"Language-Team: \n"
"Language: hr\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"content-type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.7.0\n"
"X-Generator: Poedit 3.0\n"
Expand Down
2 changes: 1 addition & 1 deletion openlibrary/i18n/ja/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ msgstr ""
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"content-type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.7.0\n"

Expand Down
2 changes: 1 addition & 1 deletion openlibrary/i18n/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ msgstr ""
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"content-type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.9.1\n"

Expand Down
2 changes: 1 addition & 1 deletion openlibrary/i18n/pl/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ msgstr ""
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"content-type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.7.0\n"

Expand Down
2 changes: 1 addition & 1 deletion openlibrary/i18n/te/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ msgstr ""
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: Telugu\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"content-type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.5.3\n"
"Language: te\n"
Expand Down
2 changes: 1 addition & 1 deletion openlibrary/plugins/admin/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ def get_stats(self, today):

class ipstats:
def GET(self):
web.header('Content-Type', 'application/json')
web.header('content-type', 'application/json')
text = requests.get("http://www.archive.org/download/stats/numUniqueIPsOL.json").text
return delegate.RawText(text)

Expand Down
8 changes: 4 additions & 4 deletions openlibrary/plugins/akismet/akismet.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def verify_key(self):
url = 'http://%sverify-key' % self.baseurl
# we *don't* trap the error here
# so if akismet is down it will raise an HTTPError or URLError
headers = {'User-Agent' : self.user_agent}
headers = {'user-agent': self.user_agent}
resp = self._safeRequest(url, urlencode(data), headers)
if resp.lower() == 'valid':
return True
Expand Down Expand Up @@ -282,7 +282,7 @@ def comment_check(self, comment, data=None, build_data=True, DEBUG=False):
url = '%scomment-check' % self._getURL()
# we *don't* trap the error here
# so if akismet is down it will raise an HTTPError or URLError
headers = {'User-Agent' : self.user_agent}
headers = {'user-agent': self.user_agent}
resp = self._safeRequest(url, urlencode(data), headers)
if DEBUG:
return resp
Expand Down Expand Up @@ -313,7 +313,7 @@ def submit_spam(self, comment, data=None, build_data=True):
url = '%ssubmit-spam' % self._getURL()
# we *don't* trap the error here
# so if akismet is down it will raise an HTTPError or URLError
headers = {'User-Agent' : self.user_agent}
headers = {'user-agent': self.user_agent}
self._safeRequest(url, urlencode(data), headers)


Expand All @@ -334,7 +334,7 @@ def submit_ham(self, comment, data=None, build_data=True):
url = '%ssubmit-ham' % self._getURL()
# we *don't* trap the error here
# so if akismet is down it will raise an HTTPError or URLError
headers = {'User-Agent' : self.user_agent}
headers = {'user-agent': self.user_agent}
self._safeRequest(url, urlencode(data), headers)

"""
Expand Down
Loading

0 comments on commit 559bd14

Please sign in to comment.