def serve_cdx(self, environ, coll='$root'): """Make the upstream CDX query for a collection and response with the results of the query :param dict environ: The WSGI environment dictionary for the request :param str coll: The name of the collection this CDX query is for :return: The WbResponse containing the results of the CDX query :rtype: WbResponse """ base_url = self.rewriterapp.paths['cdx-server'] # if coll == self.all_coll: # coll = '*' cdx_url = base_url.format(coll=coll) if environ.get('QUERY_STRING'): cdx_url += '&' if '?' in cdx_url else '?' cdx_url += environ.get('QUERY_STRING') if self.query_limit: cdx_url += '&' if '?' in cdx_url else '?' cdx_url += 'limit=' + str(self.query_limit) try: res = requests.get(cdx_url, stream=True) content_type = res.headers.get('Content-Type') return WbResponse.bin_stream(StreamIter(res.raw), content_type=content_type) except Exception as e: return WbResponse.text_response('Error: ' + str(e), status='400 Bad Request')
def serve_cdx(self, environ, coll='$root'): """Make the upstream CDX query for a collection and response with the results of the query :param dict environ: The WSGI environment dictionary for the request :param str coll: The name of the collection this CDX query is for :return: The WbResponse containing the results of the CDX query :rtype: WbResponse """ base_url = self.rewriterapp.paths['cdx-server'] #if coll == self.all_coll: # coll = '*' cdx_url = base_url.format(coll=coll) if environ.get('QUERY_STRING'): cdx_url += '&' if '?' in cdx_url else '?' cdx_url += environ.get('QUERY_STRING') try: res = requests.get(cdx_url, stream=True) content_type = res.headers.get('Content-Type') return WbResponse.bin_stream(StreamIter(res.raw), content_type=content_type) except Exception as e: return WbResponse.text_response('Error: ' + str(e), status='400 Bad Request')
def test_resp_2(): resp = vars( WbResponse.bin_stream([b'Test', b'Another'], content_type='text/plain; charset=utf-8', status='404')) expected = { 'body': [b'Test', b'Another'], 'status_headers': StatusAndHeaders(protocol='', statusline='404', headers=[('Content-Type', 'text/plain; charset=utf-8') ]) } assert (resp == expected)
def __call__(self, environ, url_str): url = url_str.split('?')[0] if url.endswith('/'): url += 'index.html' full_path = environ.get('pywb.static_dir') if full_path: full_path = os.path.join(full_path, url) if not os.path.isfile(full_path): full_path = None if not full_path: full_path = os.path.join(self.static_path, url) try: data = self.block_loader.load(full_path) data.seek(0, 2) size = data.tell() data.seek(0) headers = [('Content-Length', str(size))] reader = None if 'wsgi.file_wrapper' in environ: try: reader = environ['wsgi.file_wrapper'](data) except: pass if not reader: reader = iter(lambda: data.read(), b'') content_type = 'application/octet-stream' guessed = mimetypes.guess_type(full_path) if guessed[0]: content_type = guessed[0] return WbResponse.bin_stream(reader, content_type=content_type, headers=headers) except IOError: raise NotFoundException('Static File Not Found: ' + url_str)
def __call__(self, environ, url_str): url = url_str.split('?')[0] full_path = environ.get('pywb.static_dir') if full_path: full_path = os.path.join(full_path, url) if not os.path.isfile(full_path): full_path = None if not full_path: full_path = os.path.join(self.static_path, url) try: data = self.block_loader.load(full_path) data.seek(0, 2) size = data.tell() data.seek(0) headers = [('Content-Length', str(size))] reader = None if 'wsgi.file_wrapper' in environ: try: reader = environ['wsgi.file_wrapper'](data) except: pass if not reader: reader = iter(lambda: data.read(), b'') content_type = 'application/octet-stream' guessed = mimetypes.guess_type(full_path) if guessed[0]: content_type = guessed[0] return WbResponse.bin_stream(reader, content_type=content_type, headers=headers) except IOError: raise NotFoundException('Static File Not Found: ' + url_str)
def serve_cdx(self, environ, coll='$root'): base_url = self.rewriterapp.paths['cdx-server'] #if coll == self.all_coll: # coll = '*' cdx_url = base_url.format(coll=coll) if environ.get('QUERY_STRING'): cdx_url += '&' if '?' in cdx_url else '?' cdx_url += environ.get('QUERY_STRING') try: res = requests.get(cdx_url, stream=True) content_type = res.headers.get('Content-Type') return WbResponse.bin_stream(StreamIter(res.raw), content_type=content_type) except Exception as e: return WbResponse.text_response('Error: ' + str(e), status='400 Bad Request')