commit 1535967e420fd2b1bd61fd9001188d4a71983684 Author: Arturo Filastò arturo@filasto.net Date: Thu Jan 28 18:27:48 2016 +0100
Add support for better handling start_time
Invert inlineCallbacks
Convert PUT to POST (PUT is supposed to be indempotent)
* Better exception handling for DNS --- oonib/oonibackend.py | 1 - oonib/report/handlers.py | 32 +++++++++++++++++--------------- oonib/testhelpers/http_helpers.py | 12 ++++++++++++ 3 files changed, 29 insertions(+), 16 deletions(-)
diff --git a/oonib/oonibackend.py b/oonib/oonibackend.py index 9c10dc8..a392b82 100644 --- a/oonib/oonibackend.py +++ b/oonib/oonibackend.py @@ -102,7 +102,6 @@ if config.helpers['http-return-json-headers'].port: multiService.addService(http_return_request_helper) http_return_request_helper.startService()
- # add the tor collector service here if config.main.tor_hidden_service: torconfig = TorConfig() diff --git a/oonib/report/handlers.py b/oonib/report/handlers.py index c3af0c6..6594805 100644 --- a/oonib/report/handlers.py +++ b/oonib/report/handlers.py @@ -99,7 +99,7 @@ class Report(object): del self.reports[self.report_id]
-def parseUpdateReportRequest(request): +def parseUpdateReportRequest(request, report_id=None): #db_report_id_regexp = re.compile("[a-zA-Z0-9]+$")
# this is the regexp for the reports that include the timestamp @@ -111,14 +111,15 @@ def parseUpdateReportRequest(request): # We are also keeping in memory multiple copies of the same object. A lot # of optimization can be done. parsed_request = json.loads(request) - try: - report_id = parsed_request['report_id'] - except KeyError: + + report_id = parsed_request.get('report_id', report_id) + if not report_id: raise e.MissingField('report_id')
if not re.match(report_id_regexp, report_id): raise e.InvalidRequestField('report_id')
+ parsed_request['report_id'] = report_id return parsed_request
@@ -173,7 +174,7 @@ def parseNewReportRequest(request): raise e.InvalidRequestField("start_time")
try: - parsed_request['start_time'] = float(header['start_time']) + parsed_request['start_time'] = float(parsed_request['start_time']) except ValueError as exc: log.exception(exc) raise e.InvalidRequestField("start_time") @@ -183,7 +184,7 @@ def parseNewReportRequest(request): header = yaml.safe_load(parsed_request['content']) parsed_request['probe_cc'] = header['probe_cc'] if not re.match(probe_cc, parsed_request['probe_cc']): - raise Exception("Does not match the regexp") + raise e.InvalidRequestField("probe_cc") except Exception as exc: log.exception(exc) raise e.InvalidRequestField("probe_cc") @@ -206,7 +207,7 @@ class ReportHandler(OONIBHandler): class UpdateReportMixin(object): def updateReport(self, report_id, parsed_request):
- log.debug("Got this request %s" % parsed_request) + log.msg("Got this request %s" % parsed_request) report_filename = os.path.join(self.report_dir, report_id) try: @@ -279,7 +280,11 @@ class NewReportHandlerFile(ReportHandler, UpdateReportMixin):
* Response
- {'backend_version': 'XXX', 'report_id': 'XXX'} + { + 'backend_version': 'XXX', + 'report_id': 'XXX', + 'supported_formats': ['yaml', 'json'] + }
""" # Note: the request is being validated inside of parseNewReportRequest. @@ -320,7 +325,8 @@ class NewReportHandlerFile(ReportHandler, UpdateReportMixin):
response = { 'backend_version': config.backend_version, - 'report_id': report_id + 'report_id': report_id, + 'supported_formats': ['yaml', 'json'] }
requested_helper = report_data.get('test_helper') @@ -363,17 +369,13 @@ class NewReportHandlerFile(ReportHandler, UpdateReportMixin): """ parsed_request = parseUpdateReportRequest(self.request.body) report_id = parsed_request['report_id'] - self.updateReport(report_id, parsed_request)
class UpdateReportHandlerFile(ReportHandler, UpdateReportMixin): - def post(self, report_id): - try: - parsed_request = json.loads(self.request.body) - except ValueError: - raise e.InvalidRequest + parsed_request = parseUpdateReportRequest(self.request.body, report_id) + report_id = parsed_request['report_id'] self.updateReport(report_id, parsed_request)
diff --git a/oonib/testhelpers/http_helpers.py b/oonib/testhelpers/http_helpers.py index a28cbad..603bdc5 100644 --- a/oonib/testhelpers/http_helpers.py +++ b/oonib/testhelpers/http_helpers.py @@ -2,6 +2,10 @@ import json import random import string
+from twisted.internet.protocol import Factory, Protocol + +from twisted.internet.endpoints import TCP4ClientEndpoint + from twisted.internet import protocol, defer
from cyclone.web import RequestHandler, Application @@ -168,6 +172,14 @@ class HTTPRandomPage(HTTPTrapAll): length = 100000 self.write(self.genRandomPage(length, keyword))
+class TCPConnectProtocol(Protocol): + def connectionMade(self): + self.transport.loseConnection() + +class TCPConnectFactory(Factory): + def buildProtocol(self, addr): + return TCPConnectProtocol() + HTTPRandomPageHelper = Application([ # XXX add regexps here (r"/(.*)/(.*)", HTTPRandomPage)