MCPcopy
hub / github.com/cherrypy/cherrypy / read

Method read

cherrypy/_cpreqbody.py:759–848  ·  view source on GitHub ↗

Read bytes from the request body and return or write them to a file. A number of bytes less than or equal to the 'size' argument are read off the socket. The actual number of bytes read are tracked in self.bytes_read. The number may be smaller than 'size' when 1) the

(self, size=None, fp_out=None)

Source from the content-addressed store, hash-verified

757 self.has_trailers = has_trailers
758
759 def read(self, size=None, fp_out=None):
760 """Read bytes from the request body and return or write them to a file.
761
762 A number of bytes less than or equal to the 'size' argument are
763 read off the socket. The actual number of bytes read are tracked
764 in self.bytes_read. The number may be smaller than 'size' when
765 1) the client sends fewer bytes, 2) the 'Content-Length' request
766 header specifies fewer bytes than requested, or 3) the number of
767 bytes read exceeds self.maxbytes (in which case, 413 is raised).
768
769 If the 'fp_out' argument is None (the default), all bytes read
770 are returned in a single byte string.
771
772 If the 'fp_out' argument is not None, it must be a file-like
773 object that supports the 'write' method; all bytes read will be
774 written to the fp, and None is returned.
775 """
776
777 if self.length is None:
778 if size is None:
779 remaining = inf
780 else:
781 remaining = size
782 else:
783 remaining = self.length - self.bytes_read
784 if size and size < remaining:
785 remaining = size
786 if remaining == 0:
787 self.finish()
788 if fp_out is None:
789 return b''
790 else:
791 return None
792
793 chunks = []
794
795 # Read bytes from the buffer.
796 if self.buffer:
797 if remaining is inf:
798 data = self.buffer
799 self.buffer = b''
800 else:
801 data = self.buffer[:remaining]
802 self.buffer = self.buffer[remaining:]
803 datalen = len(data)
804 remaining -= datalen
805
806 # Check lengths.
807 self.bytes_read += datalen
808 if self.maxbytes and self.bytes_read > self.maxbytes:
809 raise cherrypy.HTTPError(413)
810
811 # Store the data.
812 if fp_out is None:
813 chunks.append(data)
814 else:
815 fp_out.write(data)
816

Callers 1

readlineMethod · 0.95

Calls 4

finishMethod · 0.95
writeMethod · 0.80
joinMethod · 0.80
readMethod · 0.45

Tested by

no test coverage detected