def got_end_of_body(self): if self.request: utils.safe_seek(self.request.body, 0) prettyprintbody(self.request, "<") self.parent.got_request(self, self.request) self.request = None else: self.close()
def got_end_of_body(self): ''' Invoked at the end of the body ''' if self.request: utils.safe_seek(self.request.body, 0) self.request.prettyprintbody("<") self.parent.got_request(self, self.request) self.request = None else: self.close()
def got_end_of_body(self): if self.requests: request = self.requests.popleft() utils.safe_seek(request.response.body, 0) prettyprintbody(request.response, "<") self.parent.got_response(self, request, request.response) if (request["connection"] == "close" or request.response["connection"] == "close"): self.close() else: self.close()
def got_end_of_body(self): ''' Invoked at the end of the body ''' if self.requests: request = self.requests.popleft() utils.safe_seek(request.response.body, 0) request.response.prettyprintbody("<") self.parent.got_response(self, request, request.response) if (request["connection"] == "close" or request.response["connection"] == "close"): self.close() else: self.close()
def process_request(self, stream, request): ''' Process a request and generate the response ''' response = Message() if not request.uri.startswith("/"): response.compose(code="403", reason="Forbidden", body="403 Forbidden") stream.send_response(request, response) return for prefix, child in self.childs.items(): if request.uri.startswith(prefix): child.process_request(stream, request) return rootdir = self.conf.get("http.server.rootdir", "") if not rootdir: response.compose(code="403", reason="Forbidden", body="403 Forbidden") stream.send_response(request, response) return if request.uri == "/": response.compose_redirect(stream, "/api/index") stream.send_response(request, response) return if '?' in request.uri: request_uri = request.uri.split('?')[0] else: request_uri = request.uri fullpath = utils_path.append(rootdir, request_uri, True) if not fullpath: response.compose(code="403", reason="Forbidden", body="403 Forbidden") stream.send_response(request, response) return try: filep = open(fullpath, "rb") except (IOError, OSError): logging.error("HTTP: Not Found: %s (WWWDIR: %s)", fullpath, rootdir) response.compose(code="404", reason="Not Found", body="404 Not Found") stream.send_response(request, response) return if self.conf.get("http.server.mime", True): mimetype, encoding = mimetypes.guess_type(fullpath) # Do not attempt SSI if the resource is, say, gzipped if not encoding: if mimetype == "text/html": ssi = self.conf.get("http.server.ssi", False) if ssi: body = ssi_replace(rootdir, filep) filep = StringIO.StringIO(body) #XXX Do we need to enforce the charset? if mimetype in ("text/html", "application/x-javascript"): mimetype += "; charset=UTF-8" else: response["content-encoding"] = encoding else: mimetype = "text/plain" response.compose(code="200", reason="Ok", body=filep, mimetype=mimetype) if request.method == "HEAD": utils.safe_seek(filep, 0, os.SEEK_END) stream.send_response(request, response)
def compose(self, **kwargs): self.method = kwargs.get("method", "") if kwargs.get("uri", ""): self.uri = kwargs.get("uri", "") (self.scheme, self.address, self.port, self.pathquery) = urlsplit(self.uri) self["host"] = self.address + ":" + self.port else: self.scheme = kwargs.get("scheme", "") self.address = kwargs.get("address", "") self.port = kwargs.get("port", "") self.pathquery = kwargs.get("pathquery", "") if self.method: # # "A client MUST include a Host header field in all HTTP/1.1 # request messages. If the requested URI does not include # an Internet host name for the service being requested, then # the Host header field MUST be given with an empty value." # -- RFC 2616 # self["host"] = kwargs.get("host", "") if not self["host"]: LOG.oops("Missing host header") self.code = kwargs.get("code", "") self.reason = kwargs.get("reason", "") self.protocol = kwargs.get("protocol", "HTTP/1.1") if kwargs.get("nocache", True): if self.method: self["pragma"] = "no-cache" self["cache-control"] = "no-cache" if kwargs.get("date", True): self["date"] = date() if not kwargs.get("keepalive", True): self["connection"] = "close" if kwargs.get("body", None): self.body = kwargs.get("body", None) if isinstance(self.body, basestring): self.length = len(self.body) else: utils.safe_seek(self.body, 0, os.SEEK_END) self.length = self.body.tell() utils.safe_seek(self.body, 0, os.SEEK_SET) self["content-length"] = str(self.length) if kwargs.get("mimetype", ""): self["content-type"] = kwargs.get("mimetype", "") elif kwargs.get("chunked", None): self.body = kwargs.get("chunked", None) self.length = -1 self["transfer-encoding"] = "chunked" if kwargs.get("mimetype", ""): self["content-type"] = kwargs.get("mimetype", "") else: self["content-length"] = "0" self.family = kwargs.get("family", socket.AF_INET)