Beispiel #1
0
    def __init__(self, path, cacheable=False):
        path = resolve_file_path(path)

        if p.isdir(path):
            path = p.join(path, 'index.html')

        self._path      = path
        self._content   = None
        self._cacheable = cacheable

        self._type = get_type(path)
        self._type = self.kind[0]
Beispiel #2
0
    def __init__(self, path, cacheable=False):
        accessible_path = resolve_file_path(path)

        self._is_originally_dir = p.isdir(accessible_path)

        if self._is_originally_dir:
            accessible_path = p.join(accessible_path, ResourceEntity.DEFAULT_INDEX_FILE)

        self._path      = accessible_path
        self._content   = None
        self._cacheable = cacheable

        self._type = get_type(accessible_path)
        self._type = self.kind[0]
Beispiel #3
0
def static_routes(paths: List[str], content: Optional[Union[bytes, str]] = None,
		file: Optional[str] = None, mime: Optional[Tuple[str, str]] = None):
	if content is None and file is None:
		raise TypeError("Expected content xor file to be present but neither were.")
	elif content is not None and file is not None:
		raise TypeError("Expected content xor file to be present but both were.")
	the_content: Optional[Union[str, bytes]] = open(file, "rb").read() \
		if file is not None else content
	the_mime = mime if mime is not None else get_type(file) \
		if file is not None else None

	def route(job: HTTPJob):
		assert the_content is not None
		content_length = len(the_content)
		job.write_head(200, {
				"Content-Type": the_mime[0],
				"Content-Length": str(content_length)
			})
		job.close_body(the_content)

	return [
		generate_endpoint("" if path == "/" else path, methods = {"GET": route}) \
			for path in paths
	]
Beispiel #4
0
def main():
    form = cgi.FormContentDict()

    archive = os.environ['PATH_TRANSLATED']
    try:
	path = form['file'][0]
    except KeyError:
	path = ''

    tmpdir = '/tmp/%s' % os.getpid()

    try:
	os.mkdir(tmpdir, 0755)
    except os.error:
	pass

    f = os.popen('/usr/contrib/bin/gzcat %s | tar tvf - %s' % (archive, path), 'r')


    listing = f.readlines()
    if listing:

	if listing[0][0] == 'd':
	    _write(sys.stdout,'Content-type: %s\r\n\r\n' % 'text/html')
	    _write(sys.stdout,'<html><head><title>%s</title></head><body><pre>\n' %
		   path)
	    # user asked for a directory - send back a listing
	    listing = listing[1:-1]
	    splpat = regex.symcomp('\(<f>.* \)\(<r>.*\)')
	    deeppat = regex.compile('%s/[^/]*/' % path)
	    pathinfo = os.environ['PATH_INFO']
	    super = string.join(string.split(path, '/')[:-1], '/')
	    if super:
		_write(sys.stdout,' '*32)
		_write(sys.stdout,'<a href="/cgi-bin/tgzextr%s?file=%s">&lt;Up One Level&gt;</a>\n' %
		      (pathinfo, super))
	    for line in map(string.strip, listing):
		if splpat.match(line) != -1:
		    first = splpat.group('f')
		    rest = splpat.group('r')
		    if deeppat.match(rest) != -1: continue
		    _write(sys.stdout,first[:11]+first[32:])
		    _write(sys.stdout,'<a href="/cgi-bin/tgzextr%s?file=%s">%s</a>\n' %
			  (pathinfo, rest, rest))
	else:
	    fmimetype = mimetypes.get_type(path)

	    _write(sys.stdout,'Content-type: %s\r\n\r\n' % fmimetype)
	    os.chdir(tmpdir)
	    os.system("/usr/contrib/bin/gzcat %s | tar xf - %s" % (archive, path))

	    _writelines(sys.stdout,open('%s/%s' % (tmpdir, path)).readlines())

	    os.unlink('%s/%s' % (tmpdir, path))
	    path = string.split(path)[:-1]
	    try:
		while path:
		    dirs = string.join(path)
		    os.rmdir('%s/%s', (tmpdir, dirs))
		    path = path[:-1]
		os.rmdir(tmpdir)
	    except os.error:
		pass
Beispiel #5
0
def main():
    form = cgi.FormContentDict()

    archive = os.environ['PATH_TRANSLATED']
    try:
        path = form['file'][0]
    except KeyError:
        path = ''

    tmpdir = '/tmp/%s' % os.getpid()

    try:
        os.mkdir(tmpdir, 0755)
    except os.error:
        pass

    f = os.popen('/usr/contrib/bin/gzcat %s | tar tvf - %s' % (archive, path),
                 'r')

    listing = f.readlines()
    if listing:

        if listing[0][0] == 'd':
            _write(sys.stdout, 'Content-type: %s\r\n\r\n' % 'text/html')
            _write(sys.stdout,
                   '<html><head><title>%s</title></head><body><pre>\n' % path)
            # user asked for a directory - send back a listing
            listing = listing[1:-1]
            splpat = regex.symcomp('\(<f>.* \)\(<r>.*\)')
            deeppat = regex.compile('%s/[^/]*/' % path)
            pathinfo = os.environ['PATH_INFO']
            super = string.join(string.split(path, '/')[:-1], '/')
            if super:
                _write(sys.stdout, ' ' * 32)
                _write(
                    sys.stdout,
                    '<a href="/cgi-bin/tgzextr%s?file=%s">&lt;Up One Level&gt;</a>\n'
                    % (pathinfo, super))
            for line in map(string.strip, listing):
                if splpat.match(line) != -1:
                    first = splpat.group('f')
                    rest = splpat.group('r')
                    if deeppat.match(rest) != -1: continue
                    _write(sys.stdout, first[:11] + first[32:])
                    _write(
                        sys.stdout,
                        '<a href="/cgi-bin/tgzextr%s?file=%s">%s</a>\n' %
                        (pathinfo, rest, rest))
        else:
            fmimetype = mimetypes.get_type(path)

            _write(sys.stdout, 'Content-type: %s\r\n\r\n' % fmimetype)
            os.chdir(tmpdir)
            os.system("/usr/contrib/bin/gzcat %s | tar xf - %s" %
                      (archive, path))

            _writelines(sys.stdout, open('%s/%s' % (tmpdir, path)).readlines())

            os.unlink('%s/%s' % (tmpdir, path))
            path = string.split(path)[:-1]
            try:
                while path:
                    dirs = string.join(path)
                    os.rmdir('%s/%s', (tmpdir, dirs))
                    path = path[:-1]
                os.rmdir(tmpdir)
            except os.error:
                pass