-
Notifications
You must be signed in to change notification settings - Fork 6k
join() threads for a cleaner stop procedure #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c103c05
4945d99
fd7baa2
d415a60
7d39e1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import logging | ||
|
|
||
| from telegram import Update, NullHandler | ||
| from future.utils import bytes_to_native_str as n | ||
| from future.utils import bytes_to_native_str | ||
| from threading import Lock | ||
| import json | ||
| try: | ||
|
|
@@ -14,6 +14,13 @@ | |
| logging.getLogger(__name__).addHandler(H) | ||
|
|
||
|
|
||
| class _InvalidPost(Exception): | ||
|
|
||
| def __init__(self, http_code): | ||
| self.http_code = http_code | ||
| super(_InvalidPost, self).__init__() | ||
|
|
||
|
|
||
| class WebhookServer(BaseHTTPServer.HTTPServer, object): | ||
| def __init__(self, server_address, RequestHandlerClass, update_queue, | ||
| webhook_path): | ||
|
|
@@ -63,12 +70,15 @@ def do_GET(self): | |
|
|
||
| def do_POST(self): | ||
| self.logger.debug("Webhook triggered") | ||
| if self.path == self.server.webhook_path and \ | ||
| 'content-type' in self.headers and \ | ||
| 'content-length' in self.headers and \ | ||
| self.headers['content-type'] == 'application/json': | ||
| json_string = \ | ||
| n(self.rfile.read(int(self.headers['content-length']))) | ||
| try: | ||
| self._validate_post() | ||
| clen = self._get_content_len() | ||
| except _InvalidPost as e: | ||
| self.send_error(e.http_code) | ||
| self.end_headers() | ||
| else: | ||
| buf = self.rfile.read(clen) | ||
| json_string = bytes_to_native_str(buf) | ||
|
|
||
| self.send_response(200) | ||
| self.end_headers() | ||
|
|
@@ -80,6 +90,20 @@ def do_POST(self): | |
| update.update_id) | ||
| self.server.update_queue.put(update) | ||
|
|
||
| else: | ||
| self.send_error(403) | ||
| self.end_headers() | ||
| def _validate_post(self): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I may be missing something, but this is not called, is it?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. your eyes are working properly :) |
||
| if not (self.path == self.server.webhook_path and | ||
| 'content-type' in self.headers and | ||
| self.headers['content-type'] == 'application/json'): | ||
| raise _InvalidPost(403) | ||
|
|
||
| def _get_content_len(self): | ||
| clen = self.headers.get('content-length') | ||
| if clen is None: | ||
| raise _InvalidPost(411) | ||
| try: | ||
| clen = int(clen) | ||
| except ValueError: | ||
| raise _InvalidPost(403) | ||
| if clen < 0: | ||
| raise _InvalidPost(403) | ||
| return clen | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does pycharm recognize
"""asyc_threads (set[Thread]): """? This format would be preferrable, since it's a google style docstring.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in order not to leave this "in the air"
it was agreed that we can use the Sphinx docstring comment style for this kind of case (i.e. "standalone" variable)
this was the most reliable form we found and the most friendly to pycharm