Esempio n. 1
0
 def getChild(self, path, request):
     """
     Move 'zig'. For great justice.
     """
     if path == '..':
         return error.NoResource()
     if path == '.idx':
         if os.path.exists(os.path.join(self.path, self.indexName)):
             return error.NoResource()
         else:
             return DirectoryListing(self.path)
     if path == '':
         path = self.indexName
         # This next step is so urls like
         #     /foo/bar/baz/
         # will be represented (internally) as
         #     ['foo','bar','baz','index.qux']
         # So that request.childLink() will work correctly.
         request.prepath[-1] = self.indexName
     newpath = os.path.join(self.path, path)
     # forgive me, oh lord, for I know not what I do
     p, ext = os.path.splitext(newpath)
     processor = self.processors.get(ext)
     if processor:
         return processor(newpath)
     f = File(newpath)
     f.processors = self.processors
     f.indexName = self.indexName
     return f
Esempio n. 2
0
    def getChild(self, name, request):
        if name == '':
            return self

        td = '.twistd'

        if name[-len(td):] == td:
            username = name[:-len(td)]
            sub = 1
        else:
            username = name
            sub = 0
        try:
            pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell \
                     = pwd.getpwnam(username)
        except KeyError:
            return error.NoResource()
        if sub:
            twistdsock = os.path.join(pw_dir, self.userSocketName)
            rs = ResourceSubscription('unix', twistdsock)
            self.putChild(name, rs)
            return rs
        else:
            path = os.path.join(pw_dir, self.userDirName)
            if not os.path.exists(path):
                return error.NoResource()
            return static.File(path)
Esempio n. 3
0
 def _getResourceForRequest(self, request):
     """(Internal) Get the appropriate resource for the given host.
     """
     hostHeader = request.getHeader('host')
     if hostHeader == None:
         return self.default or error.NoResource()
     else:
         host = string.split(string.lower(hostHeader), ':')[0]
     return (self.hosts.get(host, self.default)
             or error.NoResource("host %s not in vhost map" % repr(host)))
Esempio n. 4
0
 def getChild(self, path, request):
     if path == '':
         # ZOOP!
         if isinstance(self, Widget):
             return self.page(self)
     widget = self.getWidget(path, request)
     if widget:
         if isinstance(widget, resource.Resource):
             return widget
         else:
             p = self.page(widget)
             p.isLeaf = getattr(widget, 'isLeaf', 0)
             return p
     elif path in self.files:
         prefix = getattr(sys.modules[self.__module__], '__file__', '')
         if prefix:
             prefix = os.path.abspath(os.path.dirname(prefix))
         return static.File(os.path.join(prefix, path))
     elif path == '__reload__':
         return self.page(
             Reloader(
                 map(reflect.namedModule,
                     [self.__module__] + self.modules)))
     else:
         return error.NoResource()
Esempio n. 5
0
    def getChild(self, path, request):
        fn = os.path.join(self.path, path)

        if os.path.isdir(fn):
            return ResourceScriptDirectory(fn, self.registry)
        if os.path.exists(fn):
            return ResourceScript(fn, self.registry)
        return error.NoResource()
Esempio n. 6
0
    def getChild(self, path, request):
        fn = os.path.join(self.path, path)

        if os.path.isdir(fn):
            return CGIDirectory(fn)
        if os.path.exists(fn):
            return CGIScript(fn)
        return error.NoResource()
Esempio n. 7
0
 def getChild(self, path, request):
     fnp = self.child(path)
     if not fnp.exists():
         return static.File.childNotFound
     elif fnp.isdir():
         return CGIDirectory(fnp.path)
     else:
         return CGIScript(fnp.path)
     return error.NoResource()
Esempio n. 8
0
    def render(self, request):
        """Render me to a web client.

        Load my file, execute it in a special namespace (with 'request' and
        '__file__' global vars) and finish the request.  Output to the web-page
        will NOT be handled with print - standard output goes to the log - but
        with request.write.
        """
        request.setHeader("x-powered-by","Twisted/%s" % copyright.version)
        namespace = {'request': request,
                     '__file__': self.filename}
        try:
            execfile(self.filename, namespace, namespace)
        except IOError, e:
            if e.errno == 2: #file not found
                request.setResponseCode(http.NOT_FOUND)
                request.write(error.NoResource().render(request))
Esempio n. 9
0
 def render(self, request):
     return error.NoResource().render(request)
Esempio n. 10
0
    def render(self, request):
        "You know what you doing."
        try:
            mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime =\
                  os.stat(self.path)
        except OSError:
            # This part is quite hacky, but it gets the job done.  What I'm
            # saying here is to redirect to .idx (the directory index) if this
            # is supposed to be an index.html but no index.html actually
            # exists.
            if os.path.basename(self.path) == self.indexName:
                request.setResponseCode(http.MOVED_PERMANENTLY)
                request.setHeader(
                    "location",
                    "http://%s%s.idx" % (request.getHeader("host"),
                                         (string.split(request.uri, '?')[0])))
                return ' '
            return error.NoResource().render(request)
        if stat.S_ISDIR(mode):
            # tack a '/' on to the response -- this isn't exactly correct, but
            # works for the avg. case now.  If you're looking for an index
            # which doesn't exist, tack on a "/.idx" instead, which will cause
            # the directory index to be loaded.
            if os.path.exists(os.path.join(self.path, self.indexName)):
                loc = ''
            else:
                loc = '.idx'
            request.setHeader(
                "location",
                "http://%s%s/%s" % (request.getHeader("host"),
                                    (string.split(request.uri, '?')[0]), loc))
            request.setResponseCode(http.MOVED_PERMANENTLY)
            return " "
        request.setHeader('accept-ranges', 'bytes')
        request.setHeader('last-modified', server.date_time_string(mtime))
        if self.type:
            request.setHeader('content-type', self.type)
        if self.encoding:
            request.setHeader('content-encoding', self.encoding)

        # caching headers support
        modified_since = request.getHeader('if-modified-since')
        if modified_since is not None:
            # check if file has been modified and if not don't return the file
            modified_since = server.string_date_time(modified_since)
            if modified_since >= mtime:
                request.setResponseCode(http.NOT_MODIFIED)
                return ''

        f = open(self.path, 'rb')
        try:
            range = request.getHeader('range')
            if range is not None:
                # This is a request for partial data...
                bytesrange = string.split(range, '=')
                assert bytesrange[0] == 'bytes',\
                       "Syntactically invalid http range header!"
                start, end = string.split(bytesrange[1], '-')
                if start:
                    f.seek(int(start))
                if end:
                    end = int(end)
                    size = end
                else:
                    end = size
                request.setResponseCode(http.PARTIAL_CONTENT)
                request.setHeader(
                    'content-range',
                    "bytes %s-%s/%s " % (str(start), str(end), str(size)))
            else:
                request.setHeader('content-length', size)
        except:
            traceback.print_exc(file=log.logfile)

        if request.method == 'HEAD':
            return ''

        # return data
        FileTransfer(f, size, request)
        # and make sure the connection doesn't get closed
        return server.NOT_DONE_YET
Esempio n. 11
0
 def render(self, request):
     return error.NoResource(
         "CGI directories do not support directory listing.").render(
             request)