def index(self, req): """ Handle GET and HEAD requests for static files. Directory requests are not allowed""" static_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), '../static/')) # filter out .. try: static_path = req.urlvars['path'].replace('/..', '') except: return HTTPForbidden() path = os.path.join(static_dir, static_path) if os.path.isdir(path): return HTTPForbidden() if req.method == 'GET' or req.method == 'HEAD': if os.path.isfile(path): etag, modified, mime_type, size = self._get_stat(path) res = Response() res.headers['content-type'] = mime_type res.date = rfc822.formatdate(time.time()) res.last_modified = modified res.etag = etag if_modified_since = req.headers.get('HTTP_IF_MODIFIED_SINCE') if if_modified_since: if rfc822.parsedate(if_modified_since) >= rfc822.parsedate(last_modified): return HTTPNotModified() if_none_match = req.headers.get('HTTP_IF_NONE_MATCH') if if_none_match: if if_none_match == '*' or etag in if_none_match: return HTTPNotModified() # set the response body if req.method == 'GET': fd = open(path, 'rb') if 'wsgi.file_wrapper' in req.environ: res.app_iter = req.environ['wsgi.file_wrapper'](fd) res.content_length = size else: res.app_iter = iter(lambda: fd.read(8192), '') res.content_length = size else: res.body = '' return res else: return None else: return None
def index(self, req): """ Handle GET and HEAD requests for static files. Directory requests are not allowed""" static_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), '../static/')) # filter out .. try: static_path = req.urlvars['path'].replace('/..', '') except: return HTTPForbidden() path = os.path.join(static_dir, static_path) if os.path.isdir(path): return HTTPForbidden() if req.method == 'GET' or req.method == 'HEAD': if os.path.isfile(path): etag, modified, mime_type, size = self._get_stat(path) res = Response() res.headers['content-type'] = mime_type res.date = rfc822.formatdate(time.time()) res.last_modified = modified res.etag = etag if_modified_since = req.headers.get('HTTP_IF_MODIFIED_SINCE') if if_modified_since: if rfc822.parsedate(if_modified_since) >= rfc822.parsedate(last_modified): return HTTPNotModified() if_none_match = req.headers.get('HTTP_IF_NONE_MATCH') if if_none_match: if if_none_match == '*' or etag in if_none_match: return HTTPNotModified() # set the response body if req.method == 'GET': fd = open(path, 'rb') if 'wsgi.file_wrapper' in req.environ: res.app_iter = req.environ['wsgi.file_wrapper'](fd) res.content_length = size else: res.app_iter = iter(lambda: fd.read(8192), '') res.content_length = size else: res.body = '' return res else: return None else: return None
def make_response(uri, environ): res = Response(conditional_response=True) # check if the host is online. If not raise an http error if not pingSMB( parseSMBuri(uri)["host"] ): return HTTPGatewayTimeout("Host is currently offline. You may try again at a later time.") try: f = c.open(uri) except smbc.NoEntryError: return HTTPNotFound("The file you requested is no longer available!") fs = f.fstat() filesize = fs[6] last_modified = fs[8] # mtime filename = uri.split("/")[-1] req = Request(environ) log.debug("Incoming request: \n" + str(req)) if req.range: log.debug("begin ranged transfer") res.status_int = 206 res.content_range = req.range.content_range(filesize) (start, stop) = req.range.range_for_length(filesize) log.debug("filesize: " + str(filesize)) log.debug("start: " + str(start) + " stop: " + str(stop)) log.debug("Content-Range: " + str(res.content_range)) res.app_iter = FileIterable(uri, start=start, stop=stop) res.content_length = stop - start else: log.debug("begin normal transfer") res.app_iter = FileIterable(uri) res.content_length = filesize log.debug("Content-Length: " + str(res.content_length)) res.server_protocol = "HTTP/1.1" # Make sure the file gets downloaded and not played live res.content_type = "application/octet-stream" res.last_modified = last_modified res.etag = '%s-%s-%s' % (fs[8], fs[6], hash(f)) res.headers.add("Content-Disposition", 'attachment; filename="%s"' % str(filename) ) res.headers.add("Accept-Ranges", "bytes") return res
def __call__(self, environ, start_response): req = Request(environ) if req.path_info.endswith('.txt'): s = 'Hello %s' % req.path_info resp = Response(s) resp.content_type = 'text/plain' elif req.path_info.endswith('.iter'): resp = Response() s = 'Hello %s' % req.path_info.encode('ascii') def app_iter(sample): for piece in ('<html><body>', sample, '</body>', '</html>'): yield piece self.consumed_iter = True yield ' ' self.consumed_iter = False resp.content_type = 'text/html' resp.app_iter = app_iter(s) else: s = '<html><body><h1>Hello %s</h1></body></html>' % req.path_info resp = Response(s) resp.content_type = 'text/html' return resp(environ, start_response)
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 do_download(self, collection_id, post_data, is_new=False): if is_new: return self.error_response('POST argument required: collection_id') writer = post_data.get('writer', self.default_writer) try: log.info('download %s %s' % (collection_id, writer)) output_path = self.get_path(collection_id, self.output_filename, writer) os.utime(output_path, None) status = self.read_status_file(collection_id, writer) response = Response() response.app_iter = FileIterable(output_path) response.content_length = os.path.getsize(output_path) if 'content_type' in status: response.content_type = status['content_type'].encode('utf-8', 'ignore') else: log.warn('no content type in status file') if 'file_extension' in status: response.headers['Content-Disposition'] = 'inline; filename=collection.%s' % ( status['file_extension'].encode('utf-8', 'ignore'), ) else: log.warn('no file extension in status file') return response except Exception, exc: log.ERROR('exception in do_download(): %r' % exc) return Response(status=500)
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 inforefs(self, request, environ): """WSGI Response producer for HTTP GET Git Smart HTTP /info/refs request.""" git_command = request.GET['service'] if git_command not in self.commands: return exc.HTTPMethodNotAllowed() # note to self: # please, resist the urge to add '\n' to git capture and increment line count by 1. # The code in Git client not only does NOT need '\n', but actually blows up # if you sprinkle "flush" (0000) as "0001\n". # It reads binary, per number of bytes specified. # if you do add '\n' as part of data, count it. smart_server_advert = '# service=%s' % git_command try: out = subprocessio.SubprocessIOChunker( r'git %s --stateless-rpc --advertise-refs "%s"' % (git_command[4:], self.content_path), starting_values = [ str(hex(len(smart_server_advert)+4)[2:].rjust(4,'0') + smart_server_advert + '0000') ] ) except EnvironmentError as e: log.exception(e) raise exc.HTTPExpectationFailed() resp = Response() resp.content_type = 'application/x-%s-advertisement' % str(git_command) resp.app_iter = out return resp
def backend(self, request, environ): """ WSGI Response producer for HTTP POST Git Smart HTTP requests. Reads commands and data from HTTP POST's body. returns an iterator obj with contents of git command's response to stdout """ git_command = request.path_info.strip('/') if git_command not in self.commands: return exc.HTTPMethodNotAllowed() if 'CONTENT_LENGTH' in environ: inputstream = FileWrapper(environ['wsgi.input'], request.content_length) else: inputstream = environ['wsgi.input'] try: out = subprocessio.SubprocessIOChunker( r'git %s --stateless-rpc "%s"' % (git_command[4:], self.content_path), inputstream = inputstream ) except EnvironmentError as e: log.exception(e) raise exc.HTTPExpectationFailed() if git_command in ['git-receive-pack']: # updating refs manually after each push. Needed for pre-1.7.0.4 git clients using regular HTTP mode. subprocess.call('git --git-dir "%s" update-server-info' % self.content_path, shell=True) resp = Response() resp.content_type = 'application/x-%s-result' % git_command.encode('utf8') resp.app_iter = out return resp
def __call__(self, environ, start_response): req = Request(environ) if req.path_info.endswith('.txt'): s = 'Hello %s' % req.path_info resp = Response(s) resp.content_type = 'text/plain' elif req.path_info.endswith('.iter'): resp = Response() s = 'Hello %s' % req.path_info.encode('ascii') def app_iter(sample): for piece in ('<html><body>', sample, '</body>', '</html>'): yield piece self.consumed_iter = True yield ' ' self.consumed_iter = False resp.content_type = 'text/html' resp.app_iter = app_iter(s) else: s = '<html><body><h1>Hello %s</h1></body></html>' % req.path_info resp = Response(s) resp.content_type = 'text/html' return resp(environ, start_response)
def app(environ, start_response): """Multipart AJAX request example. See: http://test.getify.com/mpAjax/description.html """ response = Response() parts = [] for i in range(12): for j in range(12): parts.append(executor.submit(mul, i, j)) def stream(parts, timeout=None): try: for future in as_completed(parts, timeout): mime, result = future.result() result = result.encode('utf8') yield "!!!!!!=_NextPart_{num}\nContent-Type: {mime}\nContent-Length: {length}\n\n".format( num=randint(100000000, 999999999), mime=mime, length=len(result)).encode('utf8') + result except TimeoutError: for future in parts: future.cancel() response.content_length = None response.app_iter = stream(parts, 0.2) return response(environ, start_response)
def app(environ, start_response): """Multipart AJAX request example. See: http://test.getify.com/mpAjax/description.html """ response = Response() parts = [] for i in range(12): for j in range(12): parts.append(executor.submit(mul, i, j)) def stream(parts, timeout=None): try: for future in as_completed(parts, timeout): mime, result = future.result() result = result.encode('utf8') yield "!!!!!!=_NextPart_{num}\nContent-Type: {mime}\nContent-Length: {length}\n\n".format( num = randint(100000000, 999999999), mime = mime, length = len(result) ).encode('utf8') + result except TimeoutError: for future in parts: future.cancel() response.content_length = None response.app_iter = stream(parts, 0.2) return response(environ, start_response)
def __call__(self, environ, start_response): res = Response(conditional_response=True) def x(): for i in xrange(10): yield str(i) res.app_iter = x() res.content_length = 10 return res(environ, start_response)
def test_encode_content_gzip_notyet_gzipped_lazy(): res = Response() res.app_iter = StringIO('foo') result = res.encode_content('gzip', lazy=True) eq_(result, None) eq_(res.content_length, None) eq_(list(res.app_iter), ['\x1f\x8b\x08\x00\x00\x00\x00\x00\x02\xff', '', 'K\xcb\xcf\x07\x00', '!es\x8c\x03\x00\x00\x00'])
def make_response(file, content_type): response = Response(content_type=content_type) response.app_iter = FileIterable(file) stat = os.fstat(file.fileno()) response.content_length = stat.st_size response.last_modified = stat.st_mtime response.etag = '%s-%s' % (stat.st_mtime, stat.st_size) return response
def make_response(filename): res = Response(content_type=get_mimetype(filename)) res.app_iter = FileIterable(filename) res.content_length = os.path.getsize(filename) res.last_modified = os.path.getmtime(filename) res.etag = '%s-%s-%s' % (os.path.getmtime(filename), os.path.getsize(filename), hash(filename)) return res
def make_response(filename): res = Response(content_type=get_mimetype(filename), conditional_response=True) res.app_iter = FileIterable(filename) res.content_length = os.path.getsize(filename) res.last_modified = os.path.getmtime(filename) res.etag = '%s-%s-%s' % (os.path.getmtime(filename), os.path.getsize(filename), hash(filename)) return res
def create(self, request): app, commit, text = self._assert_request_params( request, 'app', 'commit', 'text') self.log.info(dict(request.headers)) #request.is_body_readable = True process = self.builder(app, commit, text, request.body_file) response = Response(status=200) response.app_iter = process return response
def file_response(filename): """return a webob response object appropriate to a file name""" res = Response(content_type=get_mimetype(filename), conditional_response=True) res.app_iter = FileIterable(filename) res.content_length = os.path.getsize(filename) res.last_modified = os.path.getmtime(filename) res.etag = '%s-%s-%s' % (os.path.getmtime(filename), os.path.getsize(filename), hash(filename)) return res
def make_response(self, result, status=200): if is_file(result): response = Response(content_type='application/octet-stream') response.app_iter = FileIter(result) else: response = Response(content_type='application/json', charset='utf-8') response.text = json_encode(result) response.status = status return response
def __call__(self, environ, start_response): res = Response(conditional_response=True) def x(): for i in xrange(10): yield str(i) res.app_iter = x() res.content_length = 10 return res(environ, start_response)
def do_thumb(self, req, image): """ Serve a thumbnail of an image from the library """ if not image in self.library: self.not_found(req) resp = Response() resp.content_type = 'image/jpeg' resp.content_length = self.library.stat_thumbnail(image).st_size resp.app_iter = FileWrapper(self.library.open_thumbnail(image)) return resp
def do_thumb(self, req, image): """ Serve a thumbnail of an image from the library """ if not image in self.library: self.not_found(req) resp = Response() resp.content_type = 'image/jpeg' resp.content_length = self.library.stat_thumbnail(image).st_size resp.app_iter = FileWrapper(self.library.open_thumbnail(image)) return resp
def app(environ, start_response): req = Request(environ) res = Response() logger.debug(req) try: res.status = 200 inner_app(req, res) except exceptions.CouldNotParse as e: res.status = 404 res.content_type = 'text/html; charset=UTF-8' res.app_iter = [usage(program, e)] except Exception: if debug: res.status = 500 res.content_type = 'text/plain; charset=UTF-8' res.app_iter = [format_exc().encode('utf-8')] else: raise return res(environ, start_response)
def _serve(filebuf, filename, filesize): """ request builder for serving files """ response = Response(conditional_response=True, content_type=mimetypes.guess_type(filename)[0] \ or "binary/octet-stream") response.headers.add("Content-Disposition", "attachment; filename=%s; size=%d" % ( filename, filesize)) response.app_iter = filebuf return response
def do_image(self, req, image): """ Serve an image from the library """ if not image in self.library: self.not_found(req) resp = Response() resp.content_type, resp.content_encoding = mimetypes.guess_type( image, strict=False) resp.content_length = self.library.stat_image(image).st_size resp.app_iter = FileWrapper(self.library.open_image(image)) return resp
def do_image(self, req, image): """ Serve an image from the library """ if not image in self.library: self.not_found(req) resp = Response() resp.content_type, resp.content_encoding = mimetypes.guess_type( image, strict=False) resp.content_length = self.library.stat_image(image).st_size resp.app_iter = FileWrapper(self.library.open_image(image)) return resp
def make_file_response(storage, filename, range=None): try: res = Response(content_type=get_mimetype(filename), conditional_response=True) res.app_iter = FileIterable(storage, filename) res.content_length = storage.size(filename) res.last_modified = storage.modified_time(filename) res.etag = '%s-%s-%s' % (storage.modified_time(filename), storage.size(filename), hash(filename)) return res except OSError: return HTTPNotFound("Not found")
def push_image(self, request, url): """Handle request for pushing an image to a registry. The request specifies `repository`, `tag` and optionally `auth`. """ data = self._assert_request_data(request, 'image') auth = data.get('auth') or {} iter = self.docker.push(data['image'], auth) response = Response(status=200) response.app_iter = _add_separators(iter) return response
def push_image(self, request, url): """Handle request for pushing an image to a registry. The request specifies `repository`, `tag` and optionally `auth`. """ data = self._assert_request_data(request, 'image') auth = data.get('auth') or {} iter = self.docker.push(data['image'], auth) response = Response(status=200) response.app_iter = _add_separators(iter) return response
def backend(self, req, environ): """ WSGI Response producer for HTTP POST Git Smart HTTP requests. Reads commands and data from HTTP POST's body. returns an iterator obj with contents of git command's response to stdout """ _git_path = kallithea.CONFIG.get('git_path', 'git') git_command = self._get_fixedpath(req.path_info) if git_command not in self.commands: log.debug('command %s not allowed', git_command) return exc.HTTPMethodNotAllowed() if 'CONTENT_LENGTH' in environ: inputstream = FileWrapper(environ['wsgi.input'], req.content_length) else: inputstream = environ['wsgi.input'] gitenv = dict(os.environ) # forget all configs gitenv['GIT_CONFIG_NOGLOBAL'] = '1' cmd = [ _git_path, git_command[4:], '--stateless-rpc', self.content_path ] log.debug('handling cmd %s', cmd) try: out = subprocessio.SubprocessIOChunker( cmd, inputstream=inputstream, env=gitenv, cwd=self.content_path, ) except EnvironmentError as e: log.error(traceback.format_exc()) raise exc.HTTPExpectationFailed() if git_command in ['git-receive-pack']: # updating refs manually after each push. # Needed for pre-1.7.0.4 git clients using regular HTTP mode. from kallithea.lib.vcs import get_repo from dulwich.server import update_server_info repo = get_repo(self.content_path) if repo: update_server_info(repo._repo) resp = Response() resp.content_type = 'application/x-%s-result' % git_command.encode( 'utf-8') resp.charset = None resp.app_iter = out return resp
def __call__(self, environ, start_response): req = Request(environ) sess = req.environ['gimlet.session'] def do_stuff(): for ii in range(5): yield "%d\n" % ii sess.set('foo', 'bar', clientside='clientside' in req.params) resp = Response() resp.app_iter = do_stuff() resp.content_type = 'text/plain' return resp(environ, start_response)
def do_download(self, req): """ Send the library as a .zip archive """ archive = self.library.archive() size = archive.seek(0, io.SEEK_END) archive.seek(0) resp = Response() resp.content_type = 'application/zip' resp.content_length = size resp.content_disposition = 'attachment; filename=images.zip' resp.app_iter = FileWrapper(archive) return resp
def _response(operation, result): returns = operation.returns response = Response() if returns and result is not None: response.content_type = returns.content_type if isinstance(returns, s.reader): response.app_iter = _App_Iter(result) else: response.body = returns.bin_encode(result) else: response.status_code = exc.HTTPNoContent.code response.content_type = None return response
def send_file(self, path, mimetype): """Send the file located at 'path' back to the user """ response = Response(content_type=mimetype, conditional_response=True) response.last_modified = os.path.getmtime(path) response.app_iter = FileIterable(path) with open(path) as f: response.body = f.read() response.content_length = os.path.getsize(path) # do not accept ranges, since this does not work reliable # with acrobat IE plugin response.headers['Accept-Ranges'] = 'none' return response
def do_download(self, req): """ Send the library as a .zip archive """ archive = self.library.archive() size = archive.seek(0, io.SEEK_END) archive.seek(0) resp = Response() resp.content_type = 'application/zip' resp.content_length = size resp.content_disposition = 'attachment; filename=images.zip' resp.app_iter = FileWrapper(archive) return resp
def make_response(filename, if_iter = True, if_ranger = False): if not if_iter: res = Response(content_type = get_mimetype(filename)) res.body = open(filename, 'rb').read() else: res = Response(content_type = get_mimetype(filename), conditional_response = True) res.app_iter = FileIterable(filename) res.content_length = os.path.getsize(filename) res.last_modified = os.path.getmtime(filename) res.etag = '%s-%s-%s' % (os.path.getmtime(filename), os.path.getsize(filename), hash(filename)) return res
def send_file(self, path, mimetype): """Send the file located at 'path' back to the user """ response = Response(content_type=mimetype, conditional_response=True) response.last_modified = os.path.getmtime(path) response.app_iter = FileIterable(path) with open(path) as f: response.body = f.read() response.content_length = os.path.getsize(path) # do not accept ranges, since this does not work reliable # with acrobat IE plugin response.headers['Accept-Ranges'] = 'none' return response
def __call__(self, environ, start_response): req = Request(environ) sess = req.environ['gimlet.session'] def do_stuff(): for ii in range(5): yield str(ii).encode('utf8') # yield b"%d\n" % ii sess.set('foo', 'bar', clientside='clientside' in req.params) resp = Response() resp.app_iter = do_stuff() resp.content_type = 'text/plain' return resp(environ, start_response)
def handle_request(self, req): if (req.method == 'GET'): #resp = Response(request=req) #data = open('./save.jpg', 'r').read() data = open('./save.jpg', 'r') #resp = Response(app_iter = data, request=req) resp = Response(request=req) resp.app_iter=data #resp = Response(request=req) pprint(req.environ) print req.body return resp if (req.method == 'POST'): return self.downloadData(req)
def __call__(self, environ, start_response): req = Request(environ) resp = Response() filename = req.path_info.strip('/') lfilename = filename.lower() if not req.path_info.strip('/') or os.path.isdir(filename): if filename: filename = path(filename, 'index.html') else: filename = 'index.html' body = open(filename, 'rb').read() resp.body = body elif os.path.isfile(filename): if req.method.lower() == 'delete': sh.rm(filename + '*', shell=True) resp = exc.HTTPNoContent() return resp(environ, start_response) if req.path_info.endswith('.metadata'): cfg = ConfigObject(filename=filename) if req.method.lower() == 'get': resp.content_type = 'application/json' elif req.method.lower() == 'put': data = json.loads(req.body) cfg.metadata.update(data) cfg.write() metadata = dict(cfg.metadata.items()) metadata.update(tags=cfg.metadata.tags.as_list()) resp.body = json.dumps(metadata) elif req.path_info.endswith('.js'): resp.content_type = 'text/javascript' elif req.path_info.endswith('.json'): resp.content_type = 'application/json' elif req.path_info.endswith('.css'): resp.content_type = 'text/css' elif lfilename.endswith('.jpg'): resp.charset = None resp.content_type = 'image/jpeg' print(filename) if not resp.content_length: resp.app_iter = fd(filename) elif req.path_info.startswith('/delete/'): filename = req.path_info[8:-1] self.delete(filename) resp.status_int = 302 resp.location = '/' + path.dirname(filename) else: resp.body = str(req.path_info) resp.last_modified = datetime.now() return resp(environ, start_response)
def backend(self, request, environ): """ WSGI Response producer for HTTP POST Git Smart HTTP requests. Reads commands and data from HTTP POST's body. returns an iterator obj with contents of git command's response to stdout """ _git_path = kallithea.CONFIG.get('git_path', 'git') git_command = self._get_fixedpath(request.path_info) if git_command not in self.commands: log.debug('command %s not allowed', git_command) return exc.HTTPMethodNotAllowed() if 'CONTENT_LENGTH' in environ: inputstream = FileWrapper(environ['wsgi.input'], request.content_length) else: inputstream = environ['wsgi.input'] gitenv = dict(os.environ) # forget all configs gitenv['GIT_CONFIG_NOGLOBAL'] = '1' cmd = [_git_path, git_command[4:], '--stateless-rpc', self.content_path] log.debug('handling cmd %s', cmd) try: out = subprocessio.SubprocessIOChunker( cmd, inputstream=inputstream, env=gitenv, cwd=self.content_path, ) except EnvironmentError as e: log.error(traceback.format_exc()) raise exc.HTTPExpectationFailed() if git_command in [u'git-receive-pack']: # updating refs manually after each push. # Needed for pre-1.7.0.4 git clients using regular HTTP mode. from kallithea.lib.vcs import get_repo from dulwich.server import update_server_info repo = get_repo(self.content_path) if repo: update_server_info(repo._repo) resp = Response() resp.content_type = 'application/x-%s-result' % git_command.encode('utf8') resp.charset = None resp.app_iter = out return resp
def do_static(self, req, path): """ Serve static files from disk """ path = os.path.normpath(os.path.join(self.static_dir, path)) if not path.startswith(self.static_dir): self.not_found(req) resp = Response() resp.content_type, resp.content_encoding = mimetypes.guess_type( path, strict=False) if resp.content_type is None: resp.content_type = 'application/octet-stream' resp.content_length = os.stat(path).st_size resp.app_iter = FileWrapper(io.open(path, 'rb')) return resp
def do_static(self, req, path): """ Serve static files from disk """ path = os.path.normpath(os.path.join(self.static_dir, path)) if not path.startswith(self.static_dir): self.not_found(req) resp = Response() resp.content_type, resp.content_encoding = mimetypes.guess_type( path, strict=False) if resp.content_type is None: resp.content_type = 'application/octet-stream' resp.content_length = os.stat(path).st_size resp.app_iter = FileWrapper(io.open(path, 'rb')) return resp
def sendRawResponse(status, filename, lastmod): """Send data. Assume status is a number and filename is the name of a file containing the body of the response.""" resp = Response(status=status, content_type='text/html') resp.headers['Access-Control-Allow-Origin'] = '*' if lastmod and status != 304: resp.last_modified = lastmod fp = open(filename) fp.seek(0, 2) size = fp.tell() fp.seek(0) resp.content_length = size resp.app_iter = Chunked(fp) return resp
def sendRawResponse(status, filename, lastmod): """Send data. Assume status is a number and filename is the name of a file containing the body of the response.""" resp = Response(status=status, content_type='text/html') resp.headers['Access-Control-Allow-Origin'] = '*' if lastmod and status != 304: resp.last_modified = lastmod fp = open(filename) fp.seek(0, 2) size = fp.tell() fp.seek(0) resp.content_length = size resp.app_iter = Chunked(fp) return resp
def handle_request(self, req): if (req.method == 'GET'): #resp = Response(request=req) #data = open('./save.jpg', 'r').read() #data = open('./save.jpg', 'r') #resp = Response(app_iter = data, request=req) resp = Response(request=req) chunksize=4096000 resp.app_iter = fileiter.FileIterable('./random.txt',chunksize) #resp.app_iter = fileiter.FileIterable('./random.txt') #resp.app_iter=data #resp = Response(request=req) pprint(req.environ) print req.body return resp if (req.method == 'POST'): return self.downloadData(req)
def serve_file(filename): if os.path.exists(filename): basename = urlutils.basename(filename) content_type = mimetypes.guess_type(basename)[0] res = Response(content_type=content_type, conditional_response=True) res.app_iter = FileIterable(filename) res.content_length = os.path.getsize(filename) res.last_modified = os.path.getmtime(filename) # Todo: is this the best value for the etag? # perhaps md5 would be a better alternative res.etag = '%s-%s-%s' % (os.path.getmtime(filename), os.path.getsize(filename), hash(filename)) return res else: return HTTPNotFound()
def inforefs(self, request, unused_environ): """ WSGI Response producer for HTTP GET Git Smart HTTP /info/refs request. """ git_command = request.GET.get('service') if git_command not in self.commands: log.debug('command %s not allowed', git_command) return exc.HTTPForbidden() # please, resist the urge to add '\n' to git capture and increment # line count by 1. # by git docs: Documentation/technical/http-protocol.txt#L214 \n is # a part of protocol. # The code in Git client not only does NOT need '\n', but actually # blows up if you sprinkle "flush" (0000) as "0001\n". # It reads binary, per number of bytes specified. # if you do add '\n' as part of data, count it. server_advert = '# service=%s\n' % git_command packet_len = str(hex(len(server_advert) + 4)[2:].rjust(4, '0')).lower() try: gitenv = dict(os.environ) # forget all configs gitenv['RC_SCM_DATA'] = json.dumps(self.extras) command = [ self.git_path, git_command[4:], '--stateless-rpc', '--advertise-refs', self.content_path ] out = subprocessio.SubprocessIOChunker( command, env=gitenv, starting_values=[packet_len + server_advert + '0000'], shell=False) except EnvironmentError: log.exception('Error processing command') raise exc.HTTPExpectationFailed() resp = Response() resp.content_type = 'application/x-%s-advertisement' % str(git_command) resp.charset = None resp.app_iter = out return resp
def inforefs(self, req, environ): """ WSGI Response producer for HTTP GET Git Smart HTTP /info/refs request. """ git_command = req.GET.get('service') if git_command not in self.commands: log.debug('command %s not allowed', git_command) return exc.HTTPMethodNotAllowed() # From Documentation/technical/http-protocol.txt shipped with Git: # # Clients MUST verify the first pkt-line is `# service=$servicename`. # Servers MUST set $servicename to be the request parameter value. # Servers SHOULD include an LF at the end of this line. # Clients MUST ignore an LF at the end of the line. # # smart_reply = PKT-LINE("# service=$servicename" LF) # ref_list # "0000" server_advert = '# service=%s\n' % git_command packet_len = hex(len(server_advert) + 4)[2:].rjust(4, '0').lower() _git_path = kallithea.CONFIG.get('git_path', 'git') cmd = [ _git_path, git_command[4:], '--stateless-rpc', '--advertise-refs', self.content_path ] log.debug('handling cmd %s', cmd) try: out = subprocessio.SubprocessIOChunker( cmd, starting_values=[ ascii_bytes(packet_len + server_advert + '0000') ]) except EnvironmentError as e: log.error(traceback.format_exc()) raise exc.HTTPExpectationFailed() resp = Response() resp.content_type = 'application/x-%s-advertisement' % str(git_command) resp.charset = None resp.app_iter = out return resp
def inforefs(self, request, unused_environ): """ WSGI Response producer for HTTP GET Git Smart HTTP /info/refs request. """ git_command = request.GET.get('service') if git_command not in self.commands: log.debug('command %s not allowed', git_command) return exc.HTTPForbidden() # please, resist the urge to add '\n' to git capture and increment # line count by 1. # by git docs: Documentation/technical/http-protocol.txt#L214 \n is # a part of protocol. # The code in Git client not only does NOT need '\n', but actually # blows up if you sprinkle "flush" (0000) as "0001\n". # It reads binary, per number of bytes specified. # if you do add '\n' as part of data, count it. server_advert = '# service=%s\n' % git_command packet_len = str(hex(len(server_advert) + 4)[2:].rjust(4, '0')).lower() try: gitenv = dict(os.environ) # forget all configs gitenv['RC_SCM_DATA'] = json.dumps(self.extras) command = [self.git_path, git_command[4:], '--stateless-rpc', '--advertise-refs', self.content_path] out = subprocessio.SubprocessIOChunker( command, env=gitenv, starting_values=[packet_len + server_advert + '0000'], shell=False ) except EnvironmentError: log.exception('Error processing command') raise exc.HTTPExpectationFailed() resp = Response() resp.content_type = 'application/x-%s-advertisement' % str(git_command) resp.charset = None resp.app_iter = out return resp
def handle_request(self, req): if (req.method == 'GET'): #resp = Response(request=req) #data = open('./save.jpg', 'r').read() #data = open('./save.jpg', 'r') #resp = Response(app_iter = data, request=req) resp = Response(request=req) chunksize=1024000 uid='aaa' chunkList = self.conn.readMeta(uid) print ' --------- snow chunk list --------' print chunkList resp.app_iter = fileiter.FileIterable(chunkList) #resp.app_iter = fileiter.FileIterable('./random.txt') #resp.app_iter=data #resp = Response(request=req) #pprint(req.environ) #print req.body return resp
def make_file_response(filename): res = Response(content_type=get_mimetype(filename), conditional_response=True) res.headers.add('Accept-Ranges','bytes') res.headers.add('Server', render('__server_info__')) res.headers.add('Content-Disposition',str('attachment; filename=%s'%(filename.split(os.path.sep)[-1]))) res.app_iter = FileIterable(filename) #try: res.content_length = os.path.getsize(filename) res.last_modified = os.path.getmtime(filename) res.etag = '%s-%s-%s' % (os.path.getmtime(filename), os.path.getsize(filename), hash(filename)) #=========================================================================== # except WindowsError, e: # if e.errno == 2: # del res # res = Response(status='404') # res.headers.add('Server', render('__server_info__')) #=========================================================================== return res
def __call__(self, context, request): subpath = '/'.join(request.subpath) caught = [] def catch_start_response(status, headers, exc_info=None): caught[:] = (status, headers, exc_info) ecopy = request.environ.copy() # Fix up PATH_INFO to get rid of everything but the "subpath" # (the actual path to the file relative to the root dir). # Zero out SCRIPT_NAME for good measure. ecopy['PATH_INFO'] = '/' + subpath ecopy['SCRIPT_NAME'] = '' body = self.app(ecopy, catch_start_response) if caught: status, headers, exc_info = caught response = Response() response.app_iter = body response.status = status response.headerlist = headers return response else: raise RuntimeError('WSGI start_response not called')
def _handle_request(self, request): try: outgoing = self.router.route(request).prepare() except NoRouteError: raise HTTPNotFound() except Exception: raise raise HTTPBadGateway() else: upstream = self.requests.send(outgoing, stream=True, proxies=self.proxies) response = Response(status=upstream.status_code, headers={}) print upstream.headers for header, value in upstream.headers.items(): print "ADD HEADER", header if is_hop_by_hop(header): continue response.headers.add(capitalize_header(header), value) clen = response.headers.get('Content-Length') response.app_iter = upstream.iter_content(4096) response.content_length = clen return response
status_int = httplib.OK # 2012-09-06 dougfort Ticket #44 (temporary Connection: close) response.headers["Connection"] = "close" response.last_modified = last_modified response.content_length = content_length if content_type is None: response.content_type = "application/octet-stream" else: response.content_type = content_type if content_encoding is not None: response.content_encoding = content_encoding response.status_int = status_int response.app_iter = retrieve_generator return response def _retrieve_meta(self, req, match_object, user_request_id): collection_name = match_object.group("collection_name") key = match_object.group("key") self._log.debug("request {0}: _retrieve_meta".format(user_request_id)) try: collection_row = \ self._authenticator.authenticate(collection_name, None, req) except AccessForbidden, instance: self._log.error("request {0}: forbidden {1}".format(
def relay_req(self, req, req_url, path_str_ls, relay_servers, webcaches): """ """ # util def get_relay_netloc(relay_server): parsed = urlparse(relay_server) svr = parsed.netloc.split(':') if len(svr) == 1: relay_addr = svr[0] relay_port = '443' if parsed.scheme == 'https' else '80' else: relay_addr, relay_port = svr return relay_addr, relay_port relay_id = str(uuid4()) parsed_req_url = urlparse(req_url) relay_servers_count = len(relay_servers) connect_path = '/' + '/'.join(path_str_ls) if parsed_req_url.path.endswith('/'): connect_path = connect_path + '/' for relay_server in relay_servers: relay_addr, relay_port = get_relay_netloc(relay_server) connect_url = urlunparse( (parsed_req_url.scheme, relay_addr + ':' + relay_port, connect_path, parsed_req_url.params, parsed_req_url.query, parsed_req_url.fragment)) if webcaches[relay_server]: proxy = webcaches[relay_server] else: proxy = None if req.headers.has_key('x-object-manifest'): object_manifest = req.headers['x-object-manifest'] cont = object_manifest.split('/')[0] obj = object_manifest.split('/')[1:] cont_parts = cont.split(':') if len(cont_parts) >= 2: real_cont = ':'.join(cont_parts[1:]) object_manifest = real_cont + '/' + '/'.join(obj) req.headers['x-object-manifest'] = object_manifest original_url = req.url self.logger.info( 'Request[%s]: %s %s with headers = %s, Connect to %s (via %s)' % (str(relay_id), req.method, req.url, req.headers, connect_url, proxy)) result = RelayRequest(self.conf, req, connect_url, proxy=proxy, conn_timeout=self.conn_timeout, node_timeout=self.node_timeout, chunk_size=self.client_chunk_size)() if isinstance(result, HTTPException): if relay_servers_count > 1: relay_servers_count -= 1 self.logger.info( 'Retry Req[%s]: %s %s with headers = %s, Connect to %s (via %s)' % (str(relay_id), req.method, req.url, req.headers, connect_url, proxy)) continue else: return result if result.getheader('location'): location = result.getheader('location') parsed_location = urlparse(location) parsed_connect_url = urlparse(connect_url) if parsed_location.netloc.startswith( parsed_connect_url.netloc): parsed_orig_url = urlparse(original_url) loc_prefix = parsed_orig_url.path.split('/')[1] if parsed_orig_url.path.split( '/')[1] != self.req_version_str: rewrited_path = '/' + loc_prefix + parsed_location.path else: rewrited_path = parsed_location.path rewrited_location = (parsed_orig_url.scheme, parsed_orig_url.netloc, rewrited_path, parsed_location.params, parsed_location.query, parsed_location.fragment) response = Response(status='%s %s' % (result.status, result.reason)) response.bytes_transferred = 0 def response_iter(): try: while True: with ChunkReadTimeout(self.client_timeout): chunk = result.read(self.client_chunk_size) if not chunk: break yield chunk response.bytes_transferred += len(chunk) except GeneratorExit: pass except (Exception, TimeoutError): raise response.headerlist = result.getheaders() response.content_length = result.getheader('Content-Length') if response.content_length < 4096: response.body = result.read() else: response.app_iter = response_iter() update_headers(response, {'accept-ranges': 'bytes'}) response.content_length = result.getheader('Content-Length') update_headers(response, result.getheaders()) if req.method == 'HEAD': update_headers( response, {'Content-Length': result.getheader('Content-Length')}) if result.getheader('location'): update_headers(response, {'Location': urlunparse(rewrited_location)}) response.status = result.status self.logger.info('Response[%s]: %s by %s %s %s' % (str(relay_id), response.status, req.method, req.url, response.headers)) return response
def GETorHEAD_base(self, req, server_type, partition, nodes, path, attempts): """ Base handler for HTTP GET or HEAD requests. :param req: webob.Request object :param server_type: server type :param partition: partition :param nodes: nodes :param path: path for the request :param attempts: number of attempts to try :returns: webob.Response object """ statuses = [] reasons = [] bodies = [] source = None sources = [] newest = req.headers.get('x-newest', 'f').lower() in TRUE_VALUES nodes = iter(nodes) while len(statuses) < attempts: try: node = nodes.next() except StopIteration: break if self.error_limited(node): continue try: with ConnectionTimeout(self.app.conn_timeout): headers = dict(req.headers) headers['Connection'] = 'close' conn = http_connect(node['ip'], node['port'], node['device'], partition, req.method, path, headers=headers, query_string=req.query_string) with Timeout(self.app.node_timeout): possible_source = conn.getresponse() # See NOTE: swift_conn at top of file about this. possible_source.swift_conn = conn except (Exception, Timeout): self.exception_occurred( node, server_type, _('Trying to %(method)s %(path)s') % { 'method': req.method, 'path': req.path }) continue if possible_source.status == HTTP_INSUFFICIENT_STORAGE: self.error_limit(node) continue if is_success(possible_source.status) or \ is_redirection(possible_source.status): # 404 if we know we don't have a synced copy if not float(possible_source.getheader('X-PUT-Timestamp', 1)): statuses.append(HTTP_NOT_FOUND) reasons.append('') bodies.append('') possible_source.read() continue if newest: if sources: ts = float( source.getheader('x-put-timestamp') or source.getheader('x-timestamp') or 0) pts = float( possible_source.getheader('x-put-timestamp') or possible_source.getheader('x-timestamp') or 0) if pts > ts: sources.insert(0, possible_source) else: sources.append(possible_source) else: sources.insert(0, possible_source) source = sources[0] statuses.append(source.status) reasons.append(source.reason) bodies.append('') continue else: source = possible_source break statuses.append(possible_source.status) reasons.append(possible_source.reason) bodies.append(possible_source.read()) if is_server_error(possible_source.status): self.error_occurred(node, _('ERROR %(status)d %(body)s ' \ 'From %(type)s Server') % {'status': possible_source.status, 'body': bodies[-1][:1024], 'type': server_type}) if source: if req.method == 'GET' and \ source.status in (HTTP_OK, HTTP_PARTIAL_CONTENT): if newest: # we need to close all hanging swift_conns sources.pop(0) for src in sources: self.close_swift_conn(src) res = Response(request=req, conditional_response=True) res.app_iter = self._make_app_iter(node, source) # See NOTE: swift_conn at top of file about this. res.swift_conn = source.swift_conn update_headers(res, source.getheaders()) # Used by container sync feature if res.environ is None: res.environ = dict() res.environ['swift_x_timestamp'] = \ source.getheader('x-timestamp') update_headers(res, {'accept-ranges': 'bytes'}) res.status = source.status res.content_length = source.getheader('Content-Length') if source.getheader('Content-Type'): res.charset = None res.content_type = source.getheader('Content-Type') return res elif is_success(source.status) or is_redirection(source.status): res = status_map[source.status](request=req) update_headers(res, source.getheaders()) # Used by container sync feature if res.environ is None: res.environ = dict() res.environ['swift_x_timestamp'] = \ source.getheader('x-timestamp') update_headers(res, {'accept-ranges': 'bytes'}) res.content_length = source.getheader('Content-Length') if source.getheader('Content-Type'): res.charset = None res.content_type = source.getheader('Content-Type') return res return self.best_response(req, statuses, reasons, bodies, '%s %s' % (server_type, req.method))