def _respond_bad_request(self, request, message="Bad Request"): """Generate a "400 Bad Request" error response.""" resp = Response() resp.status = 400 resp.body = message request.environ["repoze.who.application"] = resp return None
def subscriptions_opml(request, mailbox_slug): """ handles managing feed subscriptions using an OPML document. """ ctx = request.context mb = ctx.get_mailbox(mailbox_slug) if mb is None: return HttpResponse(status=404) if request.method == 'GET': return HttpResponse(_get_opml(request, mb), status=200, content_type="text/x-opml") elif request.method == 'POST': return _post_opml(request, mb) elif request.method == 'PUT': return _put_opml(request, mb) else: res = HttpResponse(status=405) res.allow = ['GET', 'PUT', 'POST'] return res
def getRule(self, req): ''' Get rules from local.rules file :param req: http requests header ''' rule_dict = [] with open(self.file_address) as f: for rule in f: splitRule = rule.split('(') orderedPart = splitRule[0].strip().split() unorderedPart = splitRule[1] jsonRule = { "action": str(orderedPart[0]), "protocol": str(orderedPart[1]), "sourceip": str(orderedPart[2]), "sourceport": str(orderedPart[3]), "direction": str(orderedPart[4]), "destinationip": str(orderedPart[5]), "destinationport": str(orderedPart[6]), "msg": re.search(r'msg:(.*?);', unorderedPart, re.I).group(1), "priority": re.search(r'priority:(.*?);', unorderedPart, re.I).group(1), "sid": re.search(r'sid:(.*?);', unorderedPart, re.I).group(1), "rev": re.search(r'rev:(.*?);', unorderedPart, re.I).group(1) } rule_dict.append(jsonRule) response = Response(request=req, status=200, content_type='application/json') response.body = json.dumps(rule_dict) return response
def log_hit(self): # Make sure we get plain strings, not unicode #self.request.charset = None cur = self.request.str_GET.get('cur', None) ref = self.request.str_GET.get('ref', None) title = self.request.str_GET.get('title', None) if not cur: cur = self.request.headers.get('referer', None) if cur is not None: cur = get_unicode(unquote_plus(cur)) if ref is not None: ref = get_unicode(unquote_plus(ref)) if title is not None: title = get_unicode(unquote_plus(title)).strip() response = Response() response.headers['Content-Type'] = "image/gif" response.headers['Expires'] = "Sat, 26 Jul 1997 05:00:00 GMT" response.headers['Cache-Control'] = "no-cache, must-revalidate" response.body = self._image if not cur: return response visitor_ip = self.request.headers.get('X-Forwarded-For', None) if visitor_ip is None: visitor_ip = self.request.remote_addr hit = Hit(self._conf, cur, referrer=ref, title=title, visitor_ip=visitor_ip) if hit.is_whitelisted() and not hit.is_blacklisted(): self._storage.add_hit(hit) return response
def login(request): """ handles session login """ try: params = _get_params_by_ct(request) username = params['username'] password = params['password'] except: return HttpResponse(status=400) # attempt actual login... udb = request.context.get_users_database() user = User.get_by_username(udb, username) if user is None: return HttpResponse(status=401) if not user.check_password(password): return HttpResponse(status=401) # password was good, record in session request.context.set_session_user(user) if 'next' in params: req = HttpResponse(status=304) req.location = params['next'] return req else: return HttpResponse()
def downloadData(self, req): CHUNKSIZE = 4096000 filename = req.environ.get("QUERY_STRING") fname = filename.split("=")[1] print fname # time.sleep(5) f = open(fname, "w") chunk = req.environ["wsgi.input"].read(CHUNKSIZE) # ---------- write files ---------------- print "bbbb" # print chunk count = 1 while chunk: f.write(chunk) chunk = req.environ["wsgi.input"].read(CHUNKSIZE) # print chunk print "--------------------" count = count + 1 print count f.close() resp = Response(request=req) resp.body = "save over" pprint(req.environ) print req.body return resp
def dispatch_wsgi(self, environ, start_response): """ Dispatches a WSGI request. :param environ: WSGI request environment. :param start_response: WSGI response callback :returns: A valid WSGI response content. """ req = Request(environ) req.response = Response() try: resp = self.dispatch_request(req) except HTTPException as e: resp = e if inspect.isgenerator(resp): resp = Response(app_iter=resp) elif resp is None: resp = req.response if isinstance(resp, text_type): resp = bytes_(resp, req.charset) if isinstance(resp, bytes): body = resp resp = req.response resp.write(body) if resp is not req.response: resp = req.response.merge_cookies(resp) return resp(environ, start_response)
def on_error(self, state, e): run_hook.append('error') r = Response() r.text = u_('on_error') return r
def application(req): #doGzip = 0 #try: # if string.find(os.environ["HTTP_ACCEPT_ENCODING"], "gzip") != -1: # doGzip = 1 #except: # pass treeid = req.params.get("treeid") module = req.params.get("module") branch = req.params.get("branch") mindate = req.params.get("mindate") maxdate = req.params.get("maxdate") xml_nofiles = req.params.get("xml_nofiles") if not treeid or not module or not branch or not mindate or not maxdate: raise exc.HTTPBadRequest("ERROR") url = bonsai + "?" + "branchtype=match&sortby=Date&date=explicit&cvsroot=%2Fcvsroot&xml=1" url += "&treeid=%s&module=%s&branch=%s&mindate=%s&maxdate=%s" % (quote(treeid), quote(module), quote(branch), quote(mindate), quote(maxdate)) if xml_nofiles: url += "&xml_nofiles=1" urlstream = urllib.urlopen(url) resp = Response(content_type='text/xml') for s in urlstream: resp.write(s) urlstream.close() return resp
def generate_response(self, environ, start_response): """Returns a response to the given environment""" if self.content_length is not None: del self.content_length headerlist = list(self.headerlist) accept = environ.get('HTTP_ACCEPT', '') # Return JSON by default if accept and 'xml' in accept: content_type = 'application/xml' body = self.xml_body() else: content_type = 'application/json' body = self.json_body() extra_kw = {} if isinstance(body, unicode): extra_kw.update(charset='utf-8') resp = Response(body, status=self.status, headerlist=headerlist, content_type=content_type, **extra_kw) # Why is this repeated? resp.content_type = content_type return resp(environ, start_response)
def __call__(self, req): if req.headers.get("X-Auth-Token"): # already has auth token pass else: # We expire the token every 10 minutes if time.time() - self.token_timestamp >= 600000: self.token_timestamp = time.time() self.token = None self.access_resource(req) if req.path == self.auth_id: res = Response() res.status = 200 res.headers["Content-Type"] = "text/plain" res.body = str(self.tenant_id) + "\r\n" return res else: if self.sub_tenant_id: parts = req.environ.get("PATH_INFO").split("/") if len(parts) > 1 and self.tenant_id: parts[1] = self.tenant_id req.environ["PATH_INFO"] = "/".join(parts) return self.application
def get_err_response(code): """ Given an HTTP response code, create a properly formatted xml error response :param code: error code :returns: webob.response object """ error_table = { 'AccessDenied': (HTTP_FORBIDDEN, 'Access denied'), 'BucketAlreadyExists': (HTTP_CONFLICT, 'The requested bucket name is not available'), 'BucketNotEmpty': (HTTP_CONFLICT, 'The bucket you tried to delete is not empty'), 'InvalidArgument': (HTTP_BAD_REQUEST, 'Invalid Argument'), 'InvalidBucketName': (HTTP_BAD_REQUEST, 'The specified bucket is not valid'), 'InvalidURI': (HTTP_BAD_REQUEST, 'Could not parse the specified URI'), 'NoSuchBucket': (HTTP_NOT_FOUND, 'The specified bucket does not exist'), 'SignatureDoesNotMatch': (HTTP_FORBIDDEN, 'The calculated request signature does not '\ 'match your provided one'), 'NoSuchKey': (HTTP_NOT_FOUND, 'The resource you requested does not exist')} resp = Response(content_type='text/xml') resp.status = error_table[code][0] resp.body = error_table[code][1] resp.body = '<?xml version="1.0" encoding="UTF-8"?>\r\n<Error>\r\n ' \ '<Code>%s</Code>\r\n <Message>%s</Message>\r\n</Error>\r\n' \ % (code, error_table[code][1]) return resp
def __call__(self, environ, start_response): try: return self.app(environ, start_response) except Exception as exc: # get a formatted exception out = StringIO() print_exc(file=out) LOG.exception(exc) # get formatted WSGI environment formatted_environ = pformat(environ) # render our template result = debug_template.render( traceback=out.getvalue(), environment=formatted_environ ) # construct and return our response response = Response() if isinstance(exc, HTTPException): response.status_int = exc.status else: response.status_int = 500 response.unicode_body = result return response(environ, start_response)
def post(self): params = self.request.params api = BoruvkaAuthApi(self.dao) request_dict = {} for key, value in params.items(): request_dict[key] = value user_id, token_value = api.login( payload=request_dict, ) if user_id: self.session['user_id'] = user_id response = exc.HTTPMovedPermanently(location=self.request.application_url) response.set_cookie( name='Token', value=token_value, ) self.session.save() self.session.persist() return response else: # TODO: move translations handling to BaseController translation = list(self.request.accept_language)[0] view = BoruvkaAuthLoginView(translation) view.error = "Unauthorized" response = Response() response.body = view.render() #response = exc.HTTPUnauthorized() return response
def wrapped(environ, start_response): if auth and 'REMOTE_USER' not in environ: response = Response(status='401') _add_base_headers(response) return response(environ, start_response) config.c.stats.incr("requests_DATE") config.c.stats.incr("requests") request = BespinRequest(environ) response = BespinResponse(environ, start_response) skip_test = environ.get("BespinTestApp") if not skip_token_check and skip_test != "True": cookie_token = request.cookies.get("Domain-Token") header_token = environ.get("HTTP_X_DOMAIN_TOKEN") if cookie_token is None or header_token != cookie_token: log.error("request.url=%s" % request.url) log.error("cookies[Domain-Token]=%s" % cookie_token) log.error("headers[X-Domain-Token]=%s" % header_token) log.error("ERROR! The anti CSRF attack trip wire just went off. If you see this message and no-one is hacking you, please tell [email protected]") config.c.stats.incr("csrf_fail_DATE") user = request.user _add_base_headers(response) try: return func(request, response) except filesystem.NotAuthorized, e: response.error("401 Not Authorized", e)
def get_err_response(code): """ Given an HTTP response code, create a properly formatted error response :param code: error code :returns: webob.response object """ error_table = { 'AccessDenied': (403, 'Access denied'), 'BadRequest': (400, 'Bad request'), 'MalformedBody': (400, 'Request body can not be parsed, malformed request body'), 'NotFound': (404, 'Resource was not found'), 'NotImplemented': (501, 'Not implemented'), 'TestRequest': (200, 'Test request'), 'Conflict': (409, 'The requested name already exists as a different type')} resp = Response() resp.status = error_table[code][0] resp.body = error_table[code][1] return resp
def get(self): # TODO: move translations handling to BaseController translation = list(self.request.accept_language)[0] view = BoruvkaAuthLoginView(translation) response = Response() response.body = view.render() return response
def relay(self, req): """Relay a request to a remote machine for JS proxying""" host = req.GET['host'] conn = httplib.HTTPConnection(host) headers = req.headers # Re-assemble the query string query_str = {} for param, val in req.GET.iteritems(): if param in ['host', 'path']: continue query_str[param] = val query_str = urllib.urlencode(query_str) # Transport a GET or a POST if req.method == 'GET': conn.request("GET", '%s?%s' % (req.GET['path'], query_str), headers=headers) elif req.method == 'POST': conn.request("POST", req.GET['path'], req.body, headers=headers) # Handle the response and pull out the headers to proxy back resp = conn.getresponse() res = Response() for header, value in resp.getheaders(): if header.lower() in ['server', 'date']: continue res.headers[header] = value res.body = resp.read() return res
def source_code(self, req): location = req.params['location'] module_name, lineno = location.split(':', 1) module = sys.modules.get(module_name) if module is None: # Something weird indeed res = Response(content_type='text/html', charset='utf8') res.body = 'The module <code>%s</code> does not have an entry in sys.modules' % module_name return res filename = module.__file__ if filename[-4:] in ('.pyc', '.pyo'): filename = filename[:-1] elif filename.endswith('$py.class'): filename = '%s.py' % filename[:-9] f = open(filename, 'rb') source = f.read() f.close() html = ( ('<div>Module: <b>%s</b> file: %s</div>' '<style type="text/css">%s</style>' % (module_name, filename, formatter.pygments_css)) + formatter.highlight(filename, source, linenos=True)) source_lines = len(source.splitlines()) if source_lines < 60: html += '\n<br>'*(60-source_lines) res = Response(content_type='text/html', charset='utf8') res.unicode_body = html return res
def handle_request(self, req): if (req.method == 'GET'): resp = Response(request=req) resp.body = 'you send GET method' pprint(req.environ) print req.body return resp
def challenge(self, environ, status, app_headers, forget_headers): # redirect to login_form res = Response() res.status = 401 res.unicode_body = render_snippet('not_authorized.html') #res.location = '/data/not_authorized' #self.login_form_url+"?%s=%s" %(self.came_from_field, request.url) return res
def __init__(self, status=400, location='body', name='', description=''): body = {'status': status, 'errors': [{'location': location, 'name': name, 'description': description}] } Response.__init__(self, json.dumps(body)) self.status = status self.content_type = 'application/json'
def structured_text_view(context, request): """ Filesystem-based STX view """ result = stx2html(context.source) response = Response(result) response.content_type = 'text/html' return response
def make_response(obj, _content_type='application/json'): res = Response(content_type=_content_type) if _content_type=="application/json": res.body = dumps(obj) else: res.body = obj return res
def action(): if auth and 'REMOTE_USER' not in environ: response = Response(status='401') _add_base_headers(response) reply.append(response(environ, start_response)) return config.c.stats.incr("requests_DATE") config.c.stats.incr("requests") request = BespinRequest(environ) response = BespinResponse(environ, start_response) skip_test = environ.get("BespinTestApp") if not skip_token_check and skip_test != "True": cookie_token = request.cookies.get("Domain-Token") header_token = environ.get("HTTP_X_DOMAIN_TOKEN") if cookie_token is None or header_token != cookie_token: log.error("request.url=%s" % request.url) log.error("cookies[Domain-Token]=%s" % cookie_token) log.error("headers[X-Domain-Token]=%s" % header_token) log.error("WARNING: The anti CSRF attack trip wire just went off. This means an unprotected request has been made. This could be a hacking attempt, or incomplete protection. The request has NOT been halted") config.c.stats.incr("csrf_fail_DATE") # Do we need to do this? user = request.user _add_base_headers(response) try: reply.append(func(request, response)) return except filesystem.NotAuthorized, e: response.error("401 Not Authorized", e)
def file_response(path): resp = Response() if exists(path): resp.app_iter = FileIterator(path) else: raise exc.HTTPNotFound("No file found with path %s." % path) return resp
def svResume(self,req,id,sv_inst_id,body=None): res = Response(None,200) try: self.svinstanc_mgr.resume(sv_inst_id) except NUllResourceIDException,e: Logger.error(e.message) res.status=404
def __call__(self, env, start_response): """ """ req = Request(env) real_path = '/' + '/'.join(req.path.split('/')[2:]) #print real_path if not self.is_keystone_req(req): self.logger.info('pass through') return self.app(env, start_response) if self.is_keystone_auth_token_req(req): self.logger.info('return auth response that merged one and other') mbody, mheaders = self.relay_keystone_auth_req(req, real_path) #print 'mbody in keystone_merge: %s' % mbody if mbody: merged_resp = Response(request=req, body=mbody, headers=mheaders) start_response(merged_resp.status, merged_resp.headerlist) return json.dumps(merged_resp.body) else: return self.app(env, start_response) self.logger.info('normal keystone request to one') result = self.relay_keystone_ordinary_req(req) resp = Response(status='%s %s' % (result.status, result.reason)); resp.headerlist = result.getheaders() resp.body = result.read() start_response(resp.status, resp.headerlist) return resp.body
def application(environ, start_response): req = Request(environ) res = Response() res.status = 200 res.headerlist = [('Content-Type','text/html')] res.body = "<h1>Hello World!<h1>" return res(environ, start_response)
def export_notes(self, request, suffix=''): """ Return an pdf export of user and public notes Returns: response """ res = Response() student = self.__get_current_user() try: timecoded_data_set = self.__list_notes() timecoded_data_array = [] for timecoded_data in timecoded_data_set: timecoded_data_array.append([timecoded_data.seconds, Paragraph(timecoded_data.content.replace('\n','<br />'), ParagraphStyle("Page"))]) res.headerlist = [('Content-type', 'application/pdf'), ('Content-Disposition', 'attachment; filename=%s' % str(self.scope_ids.user_id)+".pdf")] p = canvas.Canvas(res) if (len(timecoded_data_array)>0): table = Table(timecoded_data_array, colWidths=[20, 500]) table.setStyle(TableStyle([('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black), ('BOX', (0, 0), (-1, -1), 0.25, colors.black)])) table.wrapOn(p, 700, 700) table.drawOn(p, 50, 700) p.showPage() p.save() return res except KNoteList.DoesNotExist: res.status = 404 return res
def action_edit_GET(self, req, page): text = self.edit_template.substitute(page=page, req=req) return Response(text)
def restricted_view(request): return Response('successful response')
def get_topology_node(self, req, **kwargs): proxy = self.proxy_spp body = json.dumps('Hola') return Response(content_type='application/json', body=body)
class TestTranscriptTranslationGetDispatch(TestVideo): """ Test video handler that provide translation transcripts. Tests for `translation` and `translation_bumper` dispatches. """ srt_file = _create_srt_file() DATA = """ <video show_captions="true" display_name="A Name" > <source src="example.mp4"/> <source src="example.webm"/> <transcript language="uk" src="{}"/> </video> """.format(os.path.split(srt_file.name)[1]) MODEL_DATA = {'data': DATA} def setUp(self): super(TestTranscriptTranslationGetDispatch, self).setUp() self.item_descriptor.render(STUDENT_VIEW) self.item = self.item_descriptor.xmodule_runtime.xmodule_instance self.item.video_bumper = {"transcripts": {"en": ""}} @ddt.data( # No language ('/translation', 'translation', '400 Bad Request'), # No videoId - HTML5 video with language that is not in available languages ('/translation/ru', 'translation/ru', '404 Not Found'), # Language is not in available languages ('/translation/ru?videoId=12345', 'translation/ru', '404 Not Found'), # Youtube_id is invalid or does not exist ('/translation/uk?videoId=9855256955511225', 'translation/uk', '404 Not Found'), ('/translation?is_bumper=1', 'translation', '400 Bad Request'), ('/translation/ru?is_bumper=1', 'translation/ru', '404 Not Found'), ('/translation/ru?videoId=12345&is_bumper=1', 'translation/ru', '404 Not Found'), ('/translation/uk?videoId=9855256955511225&is_bumper=1', 'translation/uk', '404 Not Found'), ) @ddt.unpack def test_translation_fails(self, url, dispatch, status_code): request = Request.blank(url) response = self.item.transcript(request=request, dispatch=dispatch) self.assertEqual(response.status, status_code) @ddt.data(('translation/en?videoId={}', 'translation/en', attach_sub), ('translation/en?videoId={}&is_bumper=1', 'translation/en', attach_bumper_transcript)) @ddt.unpack def test_translaton_en_youtube_success(self, url, dispatch, attach): subs = {"start": [10], "end": [100], "text": ["Hi, welcome to Edx."]} good_sjson = _create_file(json.dumps(subs)) _upload_sjson_file(good_sjson, self.item_descriptor.location) subs_id = _get_subs_id(good_sjson.name) attach(self.item, subs_id) request = Request.blank(url.format(subs_id)) response = self.item.transcript(request=request, dispatch=dispatch) self.assertDictEqual(json.loads(response.body), subs) def test_translation_non_en_youtube_success(self): subs = { u'end': [100], u'start': [12], u'text': [ u'\u041f\u0440\u0438\u0432\u0456\u0442, edX \u0432\u0456\u0442\u0430\u0454 \u0432\u0430\u0441.' ] } self.srt_file.seek(0) _upload_file(self.srt_file, self.item_descriptor.location, os.path.split(self.srt_file.name)[1]) subs_id = _get_subs_id(self.srt_file.name) # youtube 1_0 request, will generate for all speeds for existing ids self.item.youtube_id_1_0 = subs_id self.item.youtube_id_0_75 = '0_75' request = Request.blank('/translation/uk?videoId={}'.format(subs_id)) response = self.item.transcript(request=request, dispatch='translation/uk') self.assertDictEqual(json.loads(response.body), subs) # 0_75 subs are exist request = Request.blank('/translation/uk?videoId={}'.format('0_75')) response = self.item.transcript(request=request, dispatch='translation/uk') calculated_0_75 = { u'end': [75], u'start': [9], u'text': [ u'\u041f\u0440\u0438\u0432\u0456\u0442, edX \u0432\u0456\u0442\u0430\u0454 \u0432\u0430\u0441.' ] } self.assertDictEqual(json.loads(response.body), calculated_0_75) # 1_5 will be generated from 1_0 self.item.youtube_id_1_5 = '1_5' request = Request.blank('/translation/uk?videoId={}'.format('1_5')) response = self.item.transcript(request=request, dispatch='translation/uk') calculated_1_5 = { u'end': [150], u'start': [18], u'text': [ u'\u041f\u0440\u0438\u0432\u0456\u0442, edX \u0432\u0456\u0442\u0430\u0454 \u0432\u0430\u0441.' ] } self.assertDictEqual(json.loads(response.body), calculated_1_5) @ddt.data(('translation/en', 'translation/en', attach_sub), ('translation/en?is_bumper=1', 'translation/en', attach_bumper_transcript)) @ddt.unpack def test_translaton_en_html5_success(self, url, dispatch, attach): good_sjson = _create_file(json.dumps(TRANSCRIPT)) _upload_sjson_file(good_sjson, self.item_descriptor.location) subs_id = _get_subs_id(good_sjson.name) attach(self.item, subs_id) request = Request.blank(url) response = self.item.transcript(request=request, dispatch=dispatch) self.assertDictEqual(json.loads(response.body), TRANSCRIPT) def test_translaton_non_en_html5_success(self): subs = { u'end': [100], u'start': [12], u'text': [ u'\u041f\u0440\u0438\u0432\u0456\u0442, edX \u0432\u0456\u0442\u0430\u0454 \u0432\u0430\u0441.' ] } self.srt_file.seek(0) _upload_file(self.srt_file, self.item_descriptor.location, os.path.split(self.srt_file.name)[1]) # manually clean youtube_id_1_0, as it has default value self.item.youtube_id_1_0 = "" request = Request.blank('/translation/uk') response = self.item.transcript(request=request, dispatch='translation/uk') self.assertDictEqual(json.loads(response.body), subs) def test_translation_static_transcript_xml_with_data_dirc(self): """ Test id data_dir is set in XML course. Set course data_dir and ensure we get redirected to that path if it isn't found in the contentstore. """ # Simulate data_dir set in course. test_modulestore = MagicMock() attrs = { 'get_course.return_value': Mock(data_dir='dummy/static', static_asset_path='') } test_modulestore.configure_mock(**attrs) self.item_descriptor.runtime.modulestore = test_modulestore # Test youtube style en request = Request.blank('/translation/en?videoId=12345') response = self.item.transcript(request=request, dispatch='translation/en') self.assertEqual(response.status, '307 Temporary Redirect') self.assertIn( ('Location', '/static/dummy/static/subs_12345.srt.sjson'), response.headerlist) # Test HTML5 video style self.item.sub = 'OEoXaMPEzfM' request = Request.blank('/translation/en') response = self.item.transcript(request=request, dispatch='translation/en') self.assertEqual(response.status, '307 Temporary Redirect') self.assertIn( ('Location', '/static/dummy/static/subs_OEoXaMPEzfM.srt.sjson'), response.headerlist) # Test different language to ensure we are just ignoring it since we can't # translate with static fallback request = Request.blank('/translation/uk') response = self.item.transcript(request=request, dispatch='translation/uk') self.assertEqual(response.status, '404 Not Found') @ddt.data( # Test youtube style en ('/translation/en?videoId=12345', 'translation/en', '307 Temporary Redirect', '12345'), # Test html5 style en ('/translation/en', 'translation/en', '307 Temporary Redirect', 'OEoXaMPEzfM', attach_sub), # Test different language to ensure we are just ignoring it since we can't # translate with static fallback ('/translation/uk', 'translation/uk', '404 Not Found'), ('/translation/en?is_bumper=1', 'translation/en', '307 Temporary Redirect', 'OEoXaMPEzfM', attach_bumper_transcript), ('/translation/uk?is_bumper=1', 'translation/uk', '404 Not Found'), ) @ddt.unpack def test_translation_static_transcript(self, url, dispatch, status_code, sub=None, attach=None): """ Set course static_asset_path and ensure we get redirected to that path if it isn't found in the contentstore """ self._set_static_asset_path() if attach: attach(self.item, sub) request = Request.blank(url) response = self.item.transcript(request=request, dispatch=dispatch) self.assertEqual(response.status, status_code) if sub: self.assertIn( ('Location', '/static/dummy/static/subs_{}.srt.sjson'.format(sub)), response.headerlist) @patch('xmodule.video_module.VideoModule.course_id', return_value='not_a_course_locator') def test_translation_static_non_course(self, __): """ Test that get_static_transcript short-circuits in the case of a non-CourseLocator. This fixes a bug for videos inside of content libraries. """ self._set_static_asset_path() # When course_id is not mocked out, these values would result in 307, as tested above. request = Request.blank('/translation/en?videoId=12345') response = self.item.transcript(request=request, dispatch='translation/en') self.assertEqual(response.status, '404 Not Found') def _set_static_asset_path(self): """ Helper method for setting up the static_asset_path information """ self.course.static_asset_path = 'dummy/static' self.course.save() store = modulestore() with store.branch_setting(ModuleStoreEnum.Branch.draft_preferred, self.course.id): store.update_item(self.course, self.user.id) @patch( 'xmodule.video_module.transcripts_utils.edxval_api.get_video_transcript_data' ) @patch( 'openedx.core.djangoapps.video_config.models.VideoTranscriptEnabledFlag.feature_enabled', Mock(return_value=True)) @patch('xmodule.video_module.VideoModule.translation', Mock(side_effect=NotFoundError)) @patch('xmodule.video_module.VideoModule.get_static_transcript', Mock(return_value=Response(status=404))) def test_translation_fallback_transcript(self, mock_get_video_transcript_data): """ Verify that the val transcript is returned as a fallback, if it is not found in the content store. """ transcript = { 'content': json.dumps({ "start": [10], "end": [100], "text": ["Hi, welcome to Edx."], }), 'file_name': 'edx.sjson' } mock_get_video_transcript_data.return_value = transcript # Make request to XModule transcript handler response = self.item.transcript( request=Request.blank('/translation/en'), dispatch='translation/en') # Expected headers expected_headers = { 'Content-Language': 'en', 'Content-Type': 'application/json' } # Assert the actual response self.assertEqual(response.status_code, 200) self.assertEqual(response.text, transcript['content']) for attribute, value in expected_headers.iteritems(): self.assertEqual(response.headers[attribute], value) @patch( 'openedx.core.djangoapps.video_config.models.VideoTranscriptEnabledFlag.feature_enabled', Mock(return_value=False), ) @patch('xmodule.video_module.VideoModule.translation', Mock(side_effect=NotFoundError)) @patch('xmodule.video_module.VideoModule.get_static_transcript', Mock(return_value=Response(status=404))) def test_translation_fallback_transcript_feature_disabled(self): """ Verify that val transcript is not returned when its feature is disabled. """ # Make request to XModule transcript handler response = self.item.transcript( request=Request.blank('/translation/en'), dispatch='translation/en') # Assert the actual response self.assertEqual(response.status_code, 404) @ddt.data(True, False) @patch( 'xmodule.video_module.transcripts_utils.edxval_api.get_video_transcript_data' ) @patch( 'openedx.core.djangoapps.video_config.models.VideoTranscriptEnabledFlag.feature_enabled' ) def test_translations_bumper_transcript(self, feature_enabled, mock_val_video_transcript_feature, mock_get_video_transcript_data): """ Tests that the translations dispatch response remains the same with or without enabling video transcript feature. """ # Mock val api util and return the valid transcript file. transcript = { 'content': json.dumps({ "start": [10], "end": [100], "text": ["Hi, welcome to Edx."], }), 'file_name': 'edx.sjson' } mock_get_video_transcript_data.return_value = transcript mock_val_video_transcript_feature.return_value = feature_enabled self.item.video_bumper = {"transcripts": {"en": "unknown.srt.sjson"}} request = Request.blank('/translations/en?is_bumper=1') response = self.item.transcript(request=request, dispatch='translation/en') # Assert that despite the existence of val video transcripts, response remains 404. self.assertEqual(response.status_code, 404)
class ExportController(BaseController): def _validate_ip(self, ip): if not ip: # Use '' because None gets urlencoded to "None"... return '' try: netaddr.IPAddress(ip) return ip except: raise HTTPPreconditionFailed("Invalid ip: '%s'" % ip) def create(self, request): """ PUT /v1.0/{account_id}/volumes/{id}/export?ip=X&initiator=Y Create export """ try: volume = self.account_query(Volume).filter_by(id=self.id).one() except NoResultFound: raise HTTPNotFound( "Cannot create export for non-existent volume '%s'" % self.id) ip = self._validate_ip(request.params.get('ip')) resp = self.node_request(volume.node, 'PUT', '/volumes/%s/export' % volume.name, ip=ip) params = { 'status': 'ATTACHING', 'ip': ip, 'initiator': request.params.get('initiator', ''), 'target_name': resp['name'] } export, created = self.db.update_or_create(Export, params, id=volume.id) return Response(dict(export)) def delete(self, request): """ DELETE /v1.0/{account_id}/volumes/{id}/export Delete export """ try: volume = self.account_query(Volume).filter_by(id=self.id).one() except NoResultFound: raise HTTPNotFound( "Cannot delete export for non-existent volume '%s'" % self.id) if not volume.export: raise HTTPNotFound("No export found for volume: '%s'" % self.id) try: force = request.params.get('force', False) initiator = request.params.get('initiator', '') self.node_request(volume.node, 'DELETE', '/volumes/%s/export' % volume.name, force=force, initiator=initiator) except NodeError, e: if e.code != 404: raise volume.export.status = 'DELETED' return Response(dict(volume.export))
Update export info. Params: status, instance_id, and mountpoint. This also pulls the connected ip and initiator from the storage node. """ try: volume = self.account_query(Volume).filter_by(id=self.id).one() except NoResultFound: raise HTTPNotFound( "Cannot update export for non-existent volume '%s'" % self.id) update_params, meta_params = filter_update_params(request, Export) try: node_export = self.node_request(volume.node, 'GET', '/volumes/%s/export' % volume.name) except NodeError, e: logger.info('Node error fetching export: %s' % volume.id) node_export = {} sessions = node_export.get('sessions', []) if sessions: update_params['session_ip'] = sessions[0].get('ip', '') update_params['session_initiator'] = sessions[0].get( 'initiator', '') export, created = self.db.update_or_create(Export, update_params, id=volume.id) return Response(dict(export))
def grade_handler(self, request, suffix): # pylint: disable=unused-argument """ This is called by courseware.module_render, to handle an AJAX call. Used only for grading. Returns XML response. Example of request body from LTI provider:: <?xml version = "1.0" encoding = "UTF-8"?> <imsx_POXEnvelopeRequest xmlns = "some_link (may be not required)"> <imsx_POXHeader> <imsx_POXRequestHeaderInfo> <imsx_version>V1.0</imsx_version> <imsx_messageIdentifier>528243ba5241b</imsx_messageIdentifier> </imsx_POXRequestHeaderInfo> </imsx_POXHeader> <imsx_POXBody> <replaceResultRequest> <resultRecord> <sourcedGUID> <sourcedId>feb-123-456-2929::28883</sourcedId> </sourcedGUID> <result> <resultScore> <language>en-us</language> <textString>0.4</textString> </resultScore> </result> </resultRecord> </replaceResultRequest> </imsx_POXBody> </imsx_POXEnvelopeRequest> Example of correct/incorrect answer XML body:: see response_xml_template. """ response_xml_template = textwrap.dedent("""\ <?xml version="1.0" encoding="UTF-8"?> <imsx_POXEnvelopeResponse xmlns = "http://www.imsglobal.org/services/ltiv1p1/xsd/imsoms_v1p0"> <imsx_POXHeader> <imsx_POXResponseHeaderInfo> <imsx_version>V1.0</imsx_version> <imsx_messageIdentifier>{imsx_messageIdentifier}</imsx_messageIdentifier> <imsx_statusInfo> <imsx_codeMajor>{imsx_codeMajor}</imsx_codeMajor> <imsx_severity>status</imsx_severity> <imsx_description>{imsx_description}</imsx_description> <imsx_messageRefIdentifier> </imsx_messageRefIdentifier> </imsx_statusInfo> </imsx_POXResponseHeaderInfo> </imsx_POXHeader> <imsx_POXBody>{response}</imsx_POXBody> </imsx_POXEnvelopeResponse> """) # Returns when `action` is unsupported. # Supported actions: # - replaceResultRequest. unsupported_values = { 'imsx_codeMajor': 'unsupported', 'imsx_description': 'Target does not support the requested operation.', 'imsx_messageIdentifier': 'unknown', 'response': '' } # Returns if: # - past due grades are not accepted and grade is past due # - score is out of range # - can't parse response from TP; # - can't verify OAuth signing or OAuth signing is incorrect. failure_values = { 'imsx_codeMajor': 'failure', 'imsx_description': 'The request has failed.', 'imsx_messageIdentifier': 'unknown', 'response': '' } if not self.accept_grades_past_due and self.is_past_due(): failure_values['imsx_description'] = "Grade is past due" return Response(response_xml_template.format(**failure_values), content_type="application/xml") try: imsx_messageIdentifier, sourcedId, score, action = self.parse_grade_xml_body( request.body) except Exception as e: error_message = "Request body XML parsing error: " + escape( text_type(e)) log.debug("[LTI]: " + error_message) failure_values['imsx_description'] = error_message return Response(response_xml_template.format(**failure_values), content_type="application/xml") # Verify OAuth signing. try: self.verify_oauth_body_sign(request) except (ValueError, LTIError) as e: failure_values['imsx_messageIdentifier'] = escape( imsx_messageIdentifier) error_message = "OAuth verification error: " + escape(text_type(e)) failure_values['imsx_description'] = error_message log.debug("[LTI]: " + error_message) return Response(response_xml_template.format(**failure_values), content_type="application/xml") real_user = self.system.get_real_user( six.moves.urllib.parse.unquote(sourcedId.split(':')[-1])) if not real_user: # that means we can't save to database, as we do not have real user id. failure_values['imsx_messageIdentifier'] = escape( imsx_messageIdentifier) failure_values['imsx_description'] = "User not found." return Response(response_xml_template.format(**failure_values), content_type="application/xml") if action == 'replaceResultRequest': self.set_user_module_score(real_user, score, self.max_score()) values = { 'imsx_codeMajor': 'success', 'imsx_description': 'Score for {sourced_id} is now {score}'.format( sourced_id=sourcedId, score=score), 'imsx_messageIdentifier': escape(imsx_messageIdentifier), 'response': '<replaceResultResponse/>' } log.debug("[LTI]: Grade is saved.") return Response(response_xml_template.format(**values), content_type="application/xml") unsupported_values['imsx_messageIdentifier'] = escape( imsx_messageIdentifier) log.debug("[LTI]: Incorrect action.") return Response(response_xml_template.format(**unsupported_values), content_type='application/xml')
def http_error(self, status, error=None): response = Response() response.status = status response.text = json_encode({"error": error}) return response
def notfound(self, request, match): return Response(status=404)
def studio_submit(self, request, suffix=''): self.program_id = request.params['program_id'] return Response({'result': 'success'})
def default_delegate(self, request, match): return Response(status=200, body=dumps(request, ['/' + x for x in clusto.typelist.keys()]))
def get_response(self): response = Response() response.status = self.status_code response.text = self.body response.headers.update(self.headers) return response
def list_redirect(self, req, **kwargs): redirect_tcp = self.redirect_tcp_app body = json.dumps({ 'enable': redirect_tcp.redirects, }) return Response(content_type='application/json', body=body, charset='utf-8')
def types_delegate(self, request, match): objtype = match.groupdict()['objtype'] result = [] for obj in clusto.get_entities(clusto_types=(objtype, )): result.append(unclusto(obj)) return Response(status=200, body=dumps(request, result))
def __init__(self, msg='Not Implemented'): body = {'status': 501, 'message': msg} Response.__init__(self, json.dumps(body)) self.status = 501 self.content_type = 'application/json'
def showpython(request: Request): res = Response() res.body = "<h1>python</h1>".encode() res.content_type = 'text/html; charset=utf-8' res.server = 'eysdo/Server_v7' return res
def __init__(self, msg='Bad Request'): body = {'status': 400, 'message': msg} Response.__init__(self, json.dumps(body)) self.status = 400 self.content_type = 'application/json'
def __init__(self, msg='No Content'): body = {'status': 204, 'message': msg} Response.__init__(self, json.dumps(body)) self.status = 204 self.content_type = 'application/json'
def set_tcp_port(self, ip_src, ip_dst, tcp1, tcp2, vlan_src, vlan_dst, inport, outport, mode): flows = [] if mode == 'add': cmd = self.dp.ofproto.OFPFC_ADD elif mode == 'delete': cmd = self.dp.ofproto.OFPFC_DELETE_STRICT match = { "in_port": inport, "dl_vlan": int(vlan_src), "ip_proto": 6, "tcp_dst": int(tcp1), "ipv4_src": ip_src, "ipv4_dst": ip_dst, "eth_type": 2048 } actions = [{ "type": "POP_VLAN", "ethertype": 33024 }, { "type": "PUSH_VLAN", "ethertype": 33024 }, { "type": "SET_FIELD", "field": "vlan_vid", "value": 4096 + int(vlan_dst) }, { "type": "SET_FIELD", "field": "tcp_dst", "value": int(tcp2) }, { "type": "OUTPUT", "port": outport }] flow = self._to_of_flow(priority=1000, match=match, actions=actions) flows.append(flow) try: ofctl_v1_3.mod_flow_entry(self.dp, flow, cmd) except: raise ValueError('Invalid rule parameter.') match = { "in_port": outport, "dl_vlan": int(vlan_dst), "ipv4_src": ip_dst, "ip_proto": 6, "tcp_src": int(tcp2), "ipv4_dst": ip_src, "eth_type": 2048 } actions = [{ "type": "POP_VLAN", "ethertype": 33024 }, { "type": "PUSH_VLAN", "ethertype": 33024 }, { "type": "SET_FIELD", "field": "vlan_vid", "value": 4096 + int(vlan_src) }, { "type": "SET_FIELD", "field": "tcp_src", "value": int(tcp1) }, { "type": "OUTPUT", "port": inport }] flow = self._to_of_flow(priority=1000, match=match, actions=actions) flows.append(flow) try: ofctl_v1_3.mod_flow_entry(self.dp, flow, cmd) except: raise ValueError('Invalid rule parameter.') msg = json.dumps({'result': 'Rules added!'}) return Response(content_type='application/json', body=msg)
def __init__(self, msg='Unauthorized'): body = {'status': 401, 'message': msg} Response.__init__(self, json.dumps(body)) self.status = 401 self.content_type = 'application/json'
def get_dpids(self, req, **_kwargs): dps = list(self.dpset.dps.keys()) body = json.dumps(dps) return Response(content_type='application/json', body=body)
def get_conf_handler(self, req, **kwargs): body = json.dumps({'dst': self.dst, 'channels': self.channels}) return Response(content_type='application/json', body=body)
def test_app(req): return Response('whoa')
def get_ovs(self, req, **kwargs): body = json.dumps({'ovs': self.ovs}) return Response(content_type='application/json', body=body)
def test_app(req): return Response('%s: %s' % (req.POST['speaker'], req.POST['words']))
def test_app(req): return Response('Say wha!?')
def getFeed(self): """Get XML representing information in the middleware""" entries_xml = [] for entry in self.middleware.entries: request = entry['request'] response = entry.get('response') begin = time.localtime(request['begin']) entry_id = self._generateEntryTagURI(entry) entry_title = '%s %s ' % (request['method'], request['url']) short_url = request['url'] max_url_len = 40 if len(short_url) > max_url_len: prefix = short_url[:9] suffix = short_url[-max_url_len + 9:] short_url = prefix + '...' + suffix entry_title = '%s %s ' % (request['method'], short_url) # Make the <rz:cgi_variable> nodes into a string cgivars = "" for k, v in request['cgi_variables']: newv = cgi.escape(str(v)) s = cgi_variable_fmt % (k, newv) cgivars = cgivars + s # Make the <rz:cgi_variable> nodes into a string wsgivars = "" for k, v in request['wsgi_variables']: newv = cgi.escape(str(v)) s = wsgi_variable_fmt % (k, newv) wsgivars = wsgivars + s # Make the <rz:request> node rzrequest = rzrequest_fmt % { 'begin': request['begin'], 'cgi_variables': cgivars, 'wsgi_variables': wsgivars, 'method': request['method'], 'url': request['url'], } if response is not None: # Make the <rz:request> node headers = '' for k, v in response['headers']: newv = cgi.escape(str(v)) s = header_fmt % (k, newv) headers = headers + s rzresponse = rzresponse_fmt % { 'begin': response['begin'], 'end': response['end'], 'content-length': response['content-length'], 'headers': headers, 'status': response['status'], 'body': cgi.escape(response['body']), } else: rzresponse = '' # Make the atom:entry/atom:content node content = contentfmt % { 'logentry_id': entry_id, 'rzrequest': rzrequest, 'rzresponse': rzresponse, } entry_xml = entryfmt % { 'entry_id': entry_id, 'entry_title': cgi.escape(entry_title), 'updated': time.strftime('%Y-%m-%dT%H:%M:%SZ', begin), 'summary': cgi.escape(pprint.pformat(entry)), 'content': content, } entries_xml.append(entry_xml) now = time.time() body = feedfmt % { 'title': 'repoze.debug feed for pid %s' % self.middleware.pid, 'entries': '\n'.join(entries_xml), 'feed_id': self._generateFeedTagURI(now, self.middleware.pid), 'updated': time.strftime('%Y-%m-%dT%H:%M:%SZ', time.localtime(now)), } resp = Response(content_type='application/atom+xml', body=body) return resp
def test_app(req): self.assertEqual(req.method, 'PUT') return Response(req.body)
def list_Flow_Stat(self, req, **kwargs): return Response(content_type='application/json', body=self.simple_switch_app.flowStat)
def test_app(req): return Response(resp_str)