def Handle(self, client, req, server): if '.' not in str(req.path) and not str(req.path).endswith('/'): #And thus we can safely assume it's a directory, raise nss.HTTPRedirectError(str(req.path)+'/') reqpath=str(req.localpath) syspath=os.path.normpath(reqpath)[1:] print 'IN SIMPLEFILESYSTEM: syspath is', syspath fullpath=os.path.join(self.localpath, syspath) if not os.path.exists(fullpath): raise nss.HTTPError(404, longdesc='Entity does not exist.') if os.path.isdir(fullpath): #Check for directory overrides for oname in self.DIR_OVERRIDES: ofull=os.path.join(fullpath, oname) if os.path.exists(ofull) and os.path.isfile(ofull): fullpath=ofull break else: #No directory, do a listing return self.DIR_FORMATTER.ListDir(fullpath, reqpath) contenttype=mimetypes.guess_type(fullpath, False)[0] if contenttype is None: contenttype=self.DEFAULT_MIMETYPE try: f=open(fullpath, 'rb') except (EnvironmentError, IOError): raise nss.HTTPError(403, longdesc='The server lacks access permissions to read this file.') return nss.HTTPResponse(f, contenttype=contenttype) if self.USE_404: raise nss.HTTPError(404, longdesc='File or directory not found.') else: return None
def Handle(self, client, req, server): reqpath=str(req.localpath) syspath=os.path.normpath(reqpath)[1:] fullpath=os.path.join(self.localpath, syspath) if not os.path.exists(fullpath): raise nss.HTTPError(404, longdesc='Entity does not exist.') if os.path.isdir(fullpath): raise nss.HTTPError(403, longdesc='May not perform directory listings of CGI filesystems.') part, ext=os.path.splitext(fullpath) for extset, info in self.EXTENSIONS.iteritems(): if ext in extset: executable, args, scode=info break else: raise nss.HTTPError(403, longdesc='Not a CGI executable') #Set up the environment... env={} env['REQUEST_METHOD']=req.method env['QUERY_STRING']=req.urn.query env['REMOTE_ADDR']=client.peer[0] if 'Content-type' in req.headers: env['CONTENT_TYPE']=req.headers['Content-type'] else: env['CONTENT_TYPE']='' if 'Content-length' in req.headers: env['CONTENT_LENGTH']=req.headers['Content-length'] else: env['CONTENT_LENGTH']='' if 'Cookie' in req.headers: env['HTTP_COOKIE']=req.headers['Cookie'] else: env['HTTP_COOKIE']='' #Run the script return self.DoRun(fullpath, env, client, req, executable, args, scode)
def Handle(self, client, req, server): if not req.path.startswith(self.prefix): if USE_404: raise nss.HTTPError(404, longdesc='File or directory not found.') return reqpath=req.path[len(self.prefix):] syspath=os.path.normpath(reqpath) fullpath=os.path.join(self.localpath, syspath) if not os.path.exists(fullpath): raise nss.HTTPError(404, longdesc='Entity does not exist.') if os.path.isdir(fullpath): raise nss.HTTPError(403, longdesc='May not perform directory listings of CGI filesystems.') part, ext=os.path.splitext(fullpath) if ext not in self.EXTENSIONS: raise nss.HTTPError(403, longdesc='Not a CGI executable') #Set up the environment... env={} env['REQUEST_METHOD']=req.method env['QUERY_STRING']=req.urn.query env['REMOTE_ADDR']=client.peer[0] if 'Content-type' in req.headers: env['CONTENT_TYPE']=req.headers['Content-type'] else: env['CONTENT_TYPE']='' if 'Content-length' in req.headers: env['CONTENT_LENGTH']=req.headers['Content-length'] else: env['CONTENT_LENGTH']='' if 'Cookie' in req.headers: env['HTTP_COOKIE']=req.headers['Cookie'] else: env['HTTP_COOKIE']='' #Run the script return self.DoRun(fullpath, env, client, req)
def Handle(self, client, req, server): if self.param and self.param in req.path: #Stop the manager propagation chain with an error. raise nss.HTTPError( 403, longdesc='Permission to path explicitly denied.') else: return None #Allow the next manager to take over
def ListDir(self, fullpath, disppath): try: ents=os.listdir(fullpath) except (EnvironmentError, IOError): raise nss.HTTPError(403, longdesc='The server lacks access permissions to list this directory.') resp=cStringIO.StringIO() resp.write('<html><head><title>Directory listing: ') resp.write(disppath) resp.write('</title></head><body><h1>Directory contents of <a href="/cgi/main.py">/</a>') parts=nss.Path.Make(disppath).Components for idx, part in enumerate(parts): if idx!=len(parts)-1: resp.write('<a href="/') resp.write('/'.join([i for j, i in enumerate(parts) if j<=idx])) resp.write('">') resp.write(part) resp.write('</a>') resp.write('<hr/>Directores:<hr/>') for ent in ents: if os.path.isdir(os.path.join(fullpath, ent)): resp.write('<a href="'+disppath+'/'+ent+'">'+ent+'</a>') resp.write('<hr/>Files:<hr/>') for ent in ents: if os.path.isfile(os.path.join(fullpath, ent)): resp.write('<a href="'+disppath+'/'+ent+'">'+ent+'</a>')
def Handle(self, client, req, server): #print req.payload try: obj = json.loads(req.payload) except ValueError: raise nss.HTTPError(500, 'No JSON data present') rm.notification('<' + obj['item']['room']['name'] + '> ' + obj['item']['message']['message'], format='text') return nss.HTTPResponse('', 204)
def ListDir(self, fullpath, disppath): try: ents=os.listdir(fullpath) except (EnvironmentError, IOError): raise nss.HTTPError(403, longdesc='The server lacks access permissions to list this directory.') resp=cStringIO.StringIO() resp.write('<html><head><title>Directory listing: ') resp.write(disppath) resp.write('</title></head><body><ul>') resp.write('<li><a href="..">..</a></li>') for ent in ents: if os.path.isdir(os.path.join(fullpath, ent)): ent=ent+'/' resp.write('<li><a href="'+ent+'">'+ent+'</a></li>') resp.write('</ul></body></html>') return nss.HTTPResponse(resp)