def write(self, arg): self.send_headers() if isinstance(arg, text_type): arg = arg.encode('utf-8') assert isinstance(arg, binary_type), "%r is not a byte." % arg arglen = len(arg) tosend = arglen if self.response_length is not None: if self.sent >= self.response_length: # Never write more than self.response_length bytes return tosend = min(self.response_length - self.sent, tosend) if tosend < arglen: arg = arg[:tosend] # Sending an empty chunk signals the end of the # response and prematurely closes the response if self.chunked and tosend == 0: return self.sent += tosend util.write(self.sock, arg, self.chunked)
def send_headers(self): if self.headers_sent: return tosend = self.default_headers() tosend.extend(["%s: %s\r\n" % (n, v) for n, v in self.headers]) util.write(self.sock, "%s\r\n" % "".join(tosend)) self.headers_sent = True
def send(self): # send headers resp_head = [] resp_head.append("HTTP/1.1 %s\r\n" % (self.status)) resp_head.append("Server: %s\r\n" % self.SERVER_VERSION) resp_head.append("Date: %s\r\n" % http_date()) # broken clients resp_head.append("Status: %s\r\n" % str(self.status)) # always close the conenction resp_head.append("Connection: close\r\n") for name, value in self.headers.items(): resp_head.append("%s: %s\r\n" % (name, value)) write(self.sock, "%s\r\n" % "".join(resp_head)) for chunk in list(self.data): write(self.sock, chunk) close(self.sock) if hasattr(self.data, "close"): self.data.close()
def send_headers(self): if self.headers_sent: return tosend = self.default_headers() tosend.extend(["%s: %s\r\n" % (k, v) for k, v in self.headers]) header_str = "%s\r\n" % "".join(tosend) util.write(self.sock, util.to_bytestring(header_str, "ascii")) self.headers_sent = True
def send_headers(self): if self.headers_sent: return tosend = self.default_headers() tosend.extend(["%s: %s\r\n" % (k, v) for k, v in self.headers]) header_str = "%s\r\n" % "".join(tosend) util.write(self.sock, util.to_bytestring(header_str)) self.headers_sent = True
def sendfile_use_send(self, fileno, fo_offset, nbytes): # send file in blocks of 8182 bytes BLKSIZE = 8192 sent = 0 while sent != nbytes: data = os.read(fileno, BLKSIZE) if not data: break sent += len(data) if sent > nbytes: data = data[:nbytes - sent] util.write(self.sock, data, self.chunked)
def write(self, arg): self.send_headers() assert isinstance(arg, basestring), "%r is not a string." % arg arglen = len(arg) tosend = arglen if self.clength is not None: if self.sent >= self.clength: # Never write more than self.clength bytes return tosend = min(self.clength - self.sent, tosend) if tosend < arglen: arg = arg[:tosend] self.sent += tosend util.write(self.sock, arg, self.chunked)
def write(self, arg): self.send_headers() if not isinstance(arg, bytes): raise TypeError('%r is not a byte' % arg) arglen = len(arg) tosend = arglen if self.response_length is not None: if self.sent >= self.response_length: # Never write more than self.response_length bytes return tosend = min(self.response_length - self.sent, tosend) if tosend < arglen: arg = arg[:tosend] # Sending an empty chunk signals the end of the # response and prematurely closes the response if self.chunked and tosend == 0: return self.sent += tosend util.write(self.sock, arg, self.chunked)
def send(self): # send headers resp_head = [ "HTTP/1.1 %s\r\n" % self.status, "Server: %s\r\n" % self.SERVER_VERSION, "Date: %s\r\n" % http_date(), "Connection: close\r\n" ] resp_head.extend(["%s: %s\r\n" % (n, v) for n, v in self.headers]) write(self._sock, "%s\r\n" % "".join(resp_head)) for chunk in list(self.data): if chunk == "": break write(self._sock, chunk, self.chunked) if self.chunked: # send last chunk write_chunk(self._sock, "") close(self._sock) if hasattr(self.data, "close"): self.data.close()
def write(self, arg): self.send_headers() if not isinstance(arg, bytes): raise TypeError('%r is not a byte' % arg) total = len(arg) remain = total if self.response_length is not None: if self.sent >= self.response_length: # Never write more than self.response_length bytes return remain = min(self.response_length - self.sent, remain) if remain < total: arg = arg[:remain] # Sending an empty chunk signals the end of the # response and prematurely closes the response if self.chunked and remain == 0: return self.sent += remain util.write(self.sock, arg, self.chunked)
def send(self): # send headers resp_head = [] resp_head.append("HTTP/1.1 %s\r\n" % (self.status)) resp_head.append("Server: %s\r\n" % self.SERVER_VERSION) resp_head.append("Date: %s\r\n" % http_date()) # broken clients resp_head.append("Status: %s\r\n" % str(self.status)) # always close the connection resp_head.append("Connection: close\r\n") for name, value in self.headers.items(): resp_head.append("%s: %s\r\n" % (name, value)) write(self.sock, "%s\r\n" % "".join(resp_head)) for chunk in list(self.data): write(self.sock, chunk) close(self.sock) if hasattr(self.data, "close"): self.data.close()
def write(self, arg): self.send_headers() assert isinstance(arg, basestring), "%r is not a string." % arg util.write(self.sock, arg, self.chunked)
def write(self, arg): if arg == ACK and not self.chunked: util.write(self.sock, arg, self.chunked) else: orginal_write(self, arg)