Skip to content

Commit

Permalink
Add Referer for all requests via <form>
Browse files Browse the repository at this point in the history
This may fix #117
  • Loading branch information
kmyk committed Nov 25, 2020
1 parent aaaee4a commit 29b41c2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
9 changes: 6 additions & 3 deletions onlinejudge/_implementation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def parse_content(parent: Union[bs4.NavigableString, bs4.Tag, bs4.Comment]) -> b
return bs4.NavigableString(res)


# TODO: send referer by default
class FormSender:
def __init__(self, form: bs4.Tag, url: str):
assert isinstance(form, bs4.Tag)
Expand Down Expand Up @@ -100,13 +99,17 @@ def set_file(self, key: str, filename: str, content: bytes) -> None:
def unset(self, key: str) -> None:
del self.payload[key]

def request(self, session: requests.Session, method: str = None, action: Optional[str] = None, raise_for_status: bool = True, **kwargs) -> requests.Response:
def request(self, session: requests.Session, method: str = None, action: Optional[str] = None, raise_for_status: bool = True, headers: Optional[Dict[str, str]] = None, **kwargs) -> requests.Response:
if method is None:
method = self.form['method'].upper()
url = self.url
if action is not None:
url = urllib.parse.urljoin(self.url, action)
return request(method, url, session=session, raise_for_status=raise_for_status, data=self.payload, files=self.files, **kwargs)
if headers is None:
headers = {}
if 'Referer' not in headers:
headers['Referer'] = url
return request(method, url, session=session, raise_for_status=raise_for_status, data=self.payload, files=self.files, headers=headers, **kwargs)


def dos2unix(s: str) -> str:
Expand Down
2 changes: 1 addition & 1 deletion onlinejudge/service/yukicoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def submit_code(self, code: bytes, language_id: LanguageId, *, filename: Optiona
form.set('lang', language_id)
form.set_file('file', filename or 'code', code)
form.unset('custom_test')
resp = form.request(headers={'referer': url}, session=session)
resp = form.request(session=session)
resp.raise_for_status()
# result
if 'submissions' in resp.url:
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ disable =
redefined-builtin,
subprocess-popen-preexec-fn,
too-few-public-methods,
too-many-arguments,
too-many-boolean-expressions,
too-many-branches,
too-many-instance-attributes,
Expand Down

0 comments on commit 29b41c2

Please sign in to comment.