Browse Source

Fix not sending Content-Type in POST requests

tags/v0.4
Ben Kurtovic 5 years ago
parent
commit
2df64ede65
2 changed files with 11 additions and 19 deletions
  1. +1
    -0
      CHANGELOG
  2. +10
    -19
      earwigbot/wiki/site.py

+ 1
- 0
CHANGELOG View File

@@ -1,6 +1,7 @@
v0.4 (unreleased):

- Copyvio detector: improved parsing of excluded URL lists.
- Wiki: fixed not sending Content-Type header in POST requests.

v0.3 (released March 24, 2019):



+ 10
- 19
earwigbot/wiki/site.py View File

@@ -21,11 +21,12 @@
# SOFTWARE.

from cookielib import CookieJar
from json import dumps
from logging import getLogger, NullHandler
from os.path import expanduser
from threading import RLock
from time import sleep, time
from urllib import quote_plus, unquote_plus
from urllib import unquote_plus
from urlparse import urlparse

import requests
@@ -211,16 +212,6 @@ class Site(object):
return value
return unicode(value, encoding)

def _urlencode_utf8(self, params):
"""Implement urllib.urlencode() with support for unicode input."""
enc = lambda s: s.encode("utf8") if isinstance(s, unicode) else str(s)
args = []
for key, val in params.iteritems():
key = quote_plus(enc(key))
val = quote_plus(enc(val))
args.append(key + "=" + val)
return "&".join(args)

def _api_query(self, params, tries=0, wait=5, ignore_maxlag=False,
no_assert=False, ae_retry=True):
"""Do an API query with *params* as a dict of parameters.
@@ -237,16 +228,18 @@ class Site(object):
sleep(wait_time)
self._last_query_time = time()

url, data = self._build_api_query(params, ignore_maxlag, no_assert)
url, params = self._build_api_query(params, ignore_maxlag, no_assert)
if "lgpassword" in params:
self._logger.debug("{0} -> <hidden>".format(url))
elif len(data) > 1000:
self._logger.debug("{0} -> {1}...".format(url, data[:997]))
else:
self._logger.debug("{0} -> {1}".format(url, data))
data = dumps(params)
if len(data) > 1000:
self._logger.debug("{0} -> {1}...".format(url, data[:997]))
else:
self._logger.debug("{0} -> {1}".format(url, data))

try:
response = self._session.post(url, data=data)
response = self._session.post(url, data=params)
response.raise_for_status()
except requests.RequestException as exc:
raise exceptions.APIError("API query failed: {0}".format(exc))
@@ -282,9 +275,7 @@ class Site(object):
if "csrf" not in self._tokens:
# If we don't have a CSRF token, try to fetch one:
self._request_csrf_token(params)

data = self._urlencode_utf8(params)
return url, data
return url, params

def _handle_api_result(self, response, params, tries, wait, ae_retry):
"""Given an API query response, attempt to return useful data."""


Loading…
Cancel
Save