def birth(self, address, port, resources): log.debug(self, "SWAN Server starting...") self.resources = resources # resource dictionary pool self.handlers = [] self.add_handler(100) self.sock = open_socket(port) self.accept()
def options(self, specifier, request, response): log.debug(self,"OPTIONS for " + request.path) pattern = "%s" if not specifier else "%s_" + specifier methods = ['get', 'put', 'delete', 'post', 'head'] options = [method for method in methods if hasattr(self, pattern % methods)]#[] content = "<methods>" + reduce(lambda a,b: a+b, map(lambda x: "<method>%s</method>" % x, options)) + "</methods>" log.debug(self, "OPTIONS for %s are %s" % (request.path, content)) response.with_status(200).with_content_type("text/xml").with_content("%s" % content).send()
def send_get_response(self, content, request, response, content_type): path = request.params.get('path','/') if content: log.debug(self, "responding with content of %s for %s" % (path, request.socket)) response.with_status(200).with_content_type(content_type).with_content("%s" % content).send() log.debug(self, "finished handling %s for %s" % (path, request.socket)) else: log.error(self, "couldn't find %s for %s" % (path, request.socket)) response.send_error(404, "Resource at %s could not be found" % path).send()
def get(self, path=None): filepath = self.root+path if path else self.root filename = filepath.split('/')[-1] content_type = mt.guess_type(filename)[0] if not content_type and filename.split('.'): filepath += 'index.html' if filepath[-1]=='/' else '/index.html' content_type = 'text/html' log.debug(self, "request for %s on %s" % (filepath, request.socket)) callback('send_get_response', one(self.workers).read(filepath), request, response, content_type)
def handle_request(self, sock, rfile, wfile=None): # log.debug(self, "Handling connection from %s:%d" % sock.getpeername()) try: sock.settimeout(2.0) raw_requestline = rfile.readline() sock.settimeout(None) # disable timeouts if not raw_requestline: log.debug(self, "No Request. Closing connection to %s:%d" % sock.getpeername()) sock.close() log.debug(self, "\n****************\n\n****************") return request = raw_requestline.strip() log.debug(self, "Request: " + request) # get command, path, and request_version and headers command, request_path, http = raw_requestline.strip().split(" ") scheme, netloc, path, path_params, query, frag = urlparse(request_path) headers = {} headerline = rfile.readline() while not headerline.strip() == "": key, value = headerline.split(":", 1) # print key," : ",value headers[key.strip()] = value.strip() headerline = rfile.readline() (responder_pool, specifier, params) = one(self.registries).lookup(path) # log.debug(self,"Responders: %s,\n Specifier: %s,\n Params: %s\n" % (responder_pool, specifier, params)) request = Request(self, sock, rfile, wfile, command, path, headers, params, path_params, query, frag) handler = one(responder_pool) log.debug(self, "%s to %s" % (str(request), handler)) handler.respond(request, specifier) except Exception as exc: log.warn(self, str(exc))
def respond(self, request, specifier): log.debug(self, "responding to %s" % request) response = request.start_response() method = request.method if method == 'OPTIONS': self.options(specifier, request, response) handle_name = "%s_%s" % (method.lower(), specifier) if specifier else method.lower() handle_method = thread_local.actor.state.get_method(handle_name) handle_method.func_globals.update(request=request, response=response) params = dict([(k,v) for (k,v) in request.params.iteritems() if not v == None]) if method in ['PUT', 'POST']: params.update({'body':request.get_body()}) handle_method(**params)
def do404(self): path = request.path log.debug(self, "Resource for %s not found" % path) response.send_error(404, "Resource at %s could not be found" % path).send()
def handle(self, sock): log.debug(self, "Conection from %s:%d" % sock.getpeername()) rfile = sock.makefile("rb", -1) self.handle_request(sock, rfile)
def accept_connection(self): log.debug(self, "Accepting connection") self.handler_cycle.next().handle(self.sock.accept())