Exemple #1
0
    def __call__(self, environ, start_response):

        selector_matches = (environ.get('wsgiorg.routing_args') or ([],{}))[1]
        if 'working_path' in selector_matches:
            path_info = selector_matches['working_path'].decode('utf8')
        else:
            path_info = environ.get('PATH_INFO', '').decode('utf8')

        full_path = os.path.abspath(os.path.join(self.content_path, path_info.strip('/')))
        repo = get_repo_parent(full_path)
        item_path = full_path.split(str(repo))[-1][1:]

        # look for the item in the repo
        items = repo.items(path=item_path)

        # return file-like object
        if items:
            file_like = items[0].file()
        else:
            default = os.path.join(BOX_STATIC_DIR, os.path.basename(item_path))
            file_like = open(default, 'rb')

        return self.package_response(file_like, environ, start_response)
Exemple #2
0
    def __call__(self, environ, start_response):

        selector_matches = (environ.get('wsgiorg.routing_args') or ([],{}))[1]
        if 'working_path' in selector_matches:
            path_info = selector_matches['working_path'].decode('utf8')
        else:
            path_info = environ.get('PATH_INFO', '').decode('utf8')

        scheme = environ.get('wsgi.url_scheme', 'http')
        host = environ.get('HTTP_HOST', 'localhost').decode('utf8')
        self.url = '%s://%s/%s' %(scheme, host, path_info)

        full_path = os.path.abspath(os.path.join(self.content_path, path_info.strip('/')))
        _pp = os.path.abspath(self.content_path)

        cmd, kwargs = self.get_params(environ)

        if not full_path.startswith(_pp):
            log.error('forbidden: %s' % full_path)
            return self.canned_handlers(environ, start_response, 'forbidden')
        
        if os.path.exists(full_path):
            mtime = os.stat(full_path).st_mtime
            etag, last_modified =  str(mtime), email.utils.formatdate(mtime)
        else:
            mtime, etag, last_modified = (None, None, None)

        headers = [
            ('Content-type', 'text/plain'),
            ('Date', email.utils.formatdate(time.time())),
            ('Last-Modified', last_modified),
            ('ETag', etag)
        ]

        fmap = {
            'read': handle_read,
            'new': handle_branch,
            'branch': handle_branch,
            'repos': handle_repos,
            'items': handle_items,
            'versions': handle_versions,
            'submodules': handle_submodules,
            'addSubmodule': handle_addSubmodule,
            'parent': handle_parent,
            'upload': handle_upload,
        }

        repo = get_repo_parent(full_path)
        if repo is None:
            repo = full_path
        item_path = full_path.split(str(repo))[-1][1:]

        #FIXME: hack, get the item, swap with repo
        if item_path and cmd != 'submodules':
            log.debug('full_path: %s, item_path: %s' % (full_path, item_path))
            items = repo.items(path=item_path)
            if items:
                repo = item = items[0]

        if cmd == 'data':
            data = repo.file()
            return self.package_response(data, environ, start_response)

        else:
            func = fmap.get(cmd, None)
            if func:
                response = func(repo, **kwargs)
            else:
                response = getattr(repo, cmd)(**kwargs)
        return self.json_response(response, environ, start_response)