Expected put message. Got: ERROR (2072211)

https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=2072211

 

Copying the vmdk file from the datastore browser fails with the error: Expected put message. Got: ERROR (2072211)

Symptoms

  • Cannot copy the .vmdk file using the datastore browser

  • Copying the .vmdk file using the datastore browser fails

  • You see the error:

    Expected put message. Got: ERROR

  • In the hostd.log file, you see the error:

    Read error from the nfcLib: NFC_FILE_ERROR

Resolution

This is an expected behavior when the virtual machine is powered on.

To be able to copy the .vmdk file using the datastore browser, ensure that the virtual machine is powered off or shut down.


Alternatively, you can use a third party software, such as FileZilla or WinSCP, to copy the .vmdk file.

本文转自学海无涯博客51CTO博客,原文链接http://blog.51cto.com/549687/1785051如需转载请自行联系原作者


520feng2007

r"""HTTP/1.1 client library <intro stuff goes here> <other stuff, too> HTTPConnection goes through a number of "states", which define when a client may legally make another request or fetch the response for a particular request. This diagram details these state transitions: (null) | | HTTPConnection() v Idle | | putrequest() v Request-started | | ( putheader() )* endheaders() v Request-sent |\_____________________________ | | getresponse() raises | response = getresponse() | ConnectionError v v Unread-response Idle [Response-headers-read] |\____________________ | | | response.read() | putrequest() v v Idle Req-started-unread-response ______/| / | response.read() | | ( putheader() )* endheaders() v v Request-started Req-sent-unread-response | | response.read() v Request-sent This diagram presents the following rules: -- a second request may not be started until {response-headers-read} -- a response [object] cannot be retrieved until {request-sent} -- there is no differentiation between an unread response body and a partially read response body Note: this enforcement is applied by the HTTPConnection class. The HTTPResponse class does not enforce this state machine, which implies sophisticated clients may accelerate the request/response pipeline. Caution should be taken, though: accelerating the states beyond the above pattern may imply knowledge of the server's connection-close behavior for certain requests. For example, it is impossible to tell whether the server will close the connection UNTIL the response headers have been read; this means that further requests cannot be placed into the pipeline until it is known that the server will NOT be closing the connection. Logical State __state __response ------------- ------- ---------- Idle _CS_IDLE None Request-started _CS_REQ_STARTED None Request-sent _CS_REQ_SENT None Unread-response _CS_IDLE <response_class> Req-started-unread-response _CS_REQ_STARTED <response_class> Req-sent-unread-response _CS_REQ_SENT <response_class> """ import email.parser import email.message import errno import http import io import re import socket import sys import collections.abc from urllib.parse import urlsplit # HTTPMessage, parse_headers(), and the HTTP status code constants are # intentionally omitted for simplicity __all__ = ["HTTPResponse", "HTTPConnection", "HTTPException", "NotConnected", "UnknownProtocol", "UnknownTransferEncoding", "UnimplementedFileMode", "IncompleteRead", "InvalidURL", "ImproperConnectionState", "CannotSendRequest", "CannotSendHeader", "ResponseNotReady", "BadStatusLine", "LineTooLong", "RemoteDisconnected", "error", "responses"] HTTP_PORT = 80 HTTPS_PORT = 443 _UNKNOWN = 'UNKNOWN' # connection states _CS_IDLE = 'Idle' _CS_REQ_STARTED = 'Request-started' _CS_REQ_SENT = 'Request-sent' # hack to maintain backwards compatibility globals().update(http.HTTPStatus.__members__) # another hack to maintain backwards compatibility # Mapping status codes to official W3C names responses = {v: v.phrase for v in http.HTTPStatus.__members__.values()} # maximal line length when calling readline(). _MAXLINE = 65536 _MAXHEADERS = 100 # Header name/value ABNF (http://tools.ietf.org/html/rfc7230#section-3.2) # # VCHAR = %x21-7E # obs-text = %x80-FF # header-field = field-name ":" OWS field-value OWS # field-name = token # field-value = *( field-content / obs-fold ) # field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ] # field-vchar = VCHAR / obs-text # # obs-fold = CRLF 1*( SP / HTAB ) # ; obsolete line folding # ; see Section 3.2.4 # token = 1*tchar # # tchar = "!" / "#" / "$" / "%" / "&" / "'" / "*" # / "+" / "-" / "." / "^" / "_" / "`" / "|" / "~" # / DIGIT / ALPHA # ; any VCHAR, except delimiters # # VCHAR defined in http://tools.ietf.org/html/rfc5234#appendix-B.1 # the patterns for both name and value are more lenient than RFC # definitions to allow for backwards compatibility _is_legal_header_name = re.compile(rb'[^:\s][^:\r\n]*').fullmatch _is_illegal_header_value = re.compile(rb'\n(?![ \t])|\r(?![ \t\n])').search # These characters are not allowed within HTTP URL paths. # See https://tools.ietf.org/html/rfc3986#section-3.3 and the # https://tools.ietf.org/html/rfc3986#appendix-A pchar definition. # Prevents CVE-2019-9740. Includes control characters such as \r\n. # We don't restrict chars above \x7f as putrequest() limits us to ASCII. _contains_disallowed_url_pchar_re = re.compile('[\x00-\x20\x7f]') # Arguably only these _should_ allowed: # _is_allowed_url_pchars_re = re.compile(r"^[/!$&'()*+,;=:@%a-zA-Z0-9._~-]+$") # We are more lenient for assumed real world compatibility purposes. # These characters are not allowed within HTTP method names # to prevent http header injection. _contains_disallowed_method_pchar_re = re.compile('[\x00-\x1f]') # We always set the Content-Length header for these methods because some # servers will otherwise respond with a 411 _METHODS_EXPECTING_BODY = {'PATCH', 'POST', 'PUT'} def _encode(data, name='data'): """Call data.encode("latin-1") but show a better error message.""" try: return data.encode("latin-1") except UnicodeEncodeError as err: raise UnicodeEncodeError( err.encoding, err.object, err.start, err.end, "%s (%.20r) is not valid Latin-1. Use %s.encode('utf-8') " "if you want to send it encoded in UTF-8." % (name.title(), data[err.start:err.end], name)) from None class HTTPMessage(email.message.Message): # XXX The only usage of this method is in # http.server.CGIHTTPRequestHandler. Maybe move the code there so # that it doesn't need to be part of the public API. The API has # never been defined so this could cause backwards compatibility # issues. def getallmatchingheaders(self, name): """Find all header lines matching a given header name. Look through the list of headers and find all lines matching a given header name (and their continuation lines). A list of the lines is returned, without interpretation. If the header does not occur, an empty list is returned. If the header occurs multiple times, all occurrences are returned. Case is not important in the header name. """ name = name.lower() + ':' n = len(name) lst = [] hit = 0 for line in self.keys(): if line[:n].lower() == name: hit = 1 elif not line[:1].isspace(): hit = 0 if hit: lst.append(line) return lst def _read_headers(fp): """Reads potential header lines into a list from a file pointer. Length of line is limited by _MAXLINE, and number of headers is limited by _MAXHEADERS. """ headers = [] while True: line = fp.readline(_MAXLINE + 1) if len(line) > _MAXLINE: raise LineTooLong("header line") headers.append(line) if len(headers) > _MAXHEADERS: raise HTTPException("got more than %d headers" % _MAXHEADERS) if line in (b'\r\n', b'\n', b''): break return headers def parse_headers(fp, _class=HTTPMessage): """Parses only RFC2822 headers from a file pointer. email Parser wants to see strings rather than bytes. But a TextIOWrapper around self.rfile would buffer too many bytes from the stream, bytes which we later need to read as bytes. So we read the correct bytes here, as bytes, for email Parser to parse. """ headers = _read_headers(fp) hstring = b''.join(headers).decode('iso-8859-1') return email.parser.Parser(_class=_class).parsestr(hstring) class HTTPResponse(io.BufferedIOBase): # See RFC 2616 sec 19.6 and RFC 1945 sec 6 for details. # The bytes from the socket object are iso-8859-1 strings. # See RFC 2616 sec 2.2 which notes an exception for MIME-encoded # text following RFC 2047. The basic status line parsing only # accepts iso-8859-1. def __init__(self, sock, debuglevel=0, method=None, url=None): # If the response includes a content-length header, we need to # make sure that the client doesn't read more than the # specified number of bytes. If it does, it will block until # the server times out and closes the connection. This will # happen if a self.fp.read() is done (without a size) whether # self.fp is buffered or not. So, no self.fp.read() by # clients unless they know what they are doing. self.fp = sock.makefile("rb") self.debuglevel = debuglevel self._method = method # The HTTPResponse object is returned via urllib. The clients # of http and urllib expect different attributes for the # headers. headers is used here and supports urllib. msg is # provided as a backwards compatibility layer for http # clients. self.headers = self.msg = None # from the Status-Line of the response self.version = _UNKNOWN # HTTP-Version self.status = _UNKNOWN # Status-Code self.reason = _UNKNOWN # Reason-Phrase self.chunked = _UNKNOWN # is "chunked" being used? self.chunk_left = _UNKNOWN # bytes left to read in current chunk self.length = _UNKNOWN # number of bytes left in response self.will_close = _UNKNOWN # conn will close at end of response def _read_status(self): line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") if len(line) > _MAXLINE: raise LineTooLong("status line") if self.debuglevel > 0: print("reply:", repr(line)) if not line: # Presumably, the server closed the connection before # sending a valid response. raise RemoteDisconnected("Remote end closed connection without" " response") try: version, status, reason = line.split(None, 2) except ValueError: try: version, status = line.split(None, 1) reason = "" except ValueError: # empty version will cause next test to fail. version = "" if not version.startswith("HTTP/"): self._close_conn() raise BadStatusLine(line) # The status code is a three-digit number try: status = int(status) if status < 100 or status > 999: raise BadStatusLine(line) except ValueError: raise BadStatusLine(line) return version, status, reason def begin(self): if self.headers is not None: # we've already started reading the response return # read until we get a non-100 response while True: version, status, reason = self._read_status() if status != CONTINUE: break # skip the header from the 100 response skipped_headers = _read_headers(self.fp) if self.debuglevel > 0: print("headers:", skipped_headers) del skipped_headers self.code = self.status = status self.reason = reason.strip() if version in ("HTTP/1.0", "HTTP/0.9"): # Some servers might still return "0.9", treat it as 1.0 anyway self.version = 10 elif version.startswith("HTTP/1."): self.version = 11 # use HTTP/1.1 code for HTTP/1.x where x>=1 else: raise UnknownProtocol(version) self.headers = self.msg = parse_headers(self.fp) if self.debuglevel > 0: for hdr, val in self.headers.items(): print("header:", hdr + ":", val) # are we using the chunked-style of transfer encoding? tr_enc = self.headers.get("transfer-encoding") if tr_enc and tr_enc.lower() == "chunked": self.chunked = True self.chunk_left = None else: self.chunked = False # will the connection close at the end of the response? self.will_close = self._check_close() # do we have a Content-Length? # NOTE: RFC 2616, S4.4, #3 says we ignore this if tr_enc is "chunked" self.length = None length = self.headers.get("content-length") if length and not self.chunked: try: self.length = int(length) except ValueError: self.length = None else: if self.length < 0: # ignore nonsensical negative lengths self.length = None else: self.length = None # does the body have a fixed length? (of zero) if (status == NO_CONTENT or status == NOT_MODIFIED or 100 <= status < 200 or # 1xx codes self._method == "HEAD"): self.length = 0 # if the connection remains open, and we aren't using chunked, and # a content-length was not provided, then assume that the connection # WILL close. if (not self.will_close and not self.chunked and self.length is None): self.will_close = True def _check_close(self): conn = self.headers.get("connection") if self.version == 11: # An HTTP/1.1 proxy is assumed to stay open unless # explicitly closed. if conn and "close" in conn.lower(): return True return False # Some HTTP/1.0 implementations have support for persistent # connections, using rules different than HTTP/1.1. # For older HTTP, Keep-Alive indicates persistent connection. if self.headers.get("keep-alive"): return False # At least Akamai returns a "Connection: Keep-Alive" header, # which was supposed to be sent by the client. if conn and "keep-alive" in conn.lower(): return False # Proxy-Connection is a netscape hack. pconn = self.headers.get("proxy-connection") if pconn and "keep-alive" in pconn.lower(): return False # otherwise, assume it will close return True def _close_conn(self): fp = self.fp self.fp = None fp.close() def close(self): try: super().close() # set "closed" flag finally: if self.fp: self._close_conn() # These implementations are for the benefit of io.BufferedReader. # XXX This class should probably be revised to act more like # the "raw stream" that BufferedReader expects. def flush(self): super().flush() if self.fp: self.fp.flush() def readable(self): """Always returns True""" return True # End of "raw stream" methods def isclosed(self): """True if the connection is closed.""" # NOTE: it is possible that we will not ever call self.close(). This # case occurs when will_close is TRUE, length is None, and we # read up to the last byte, but NOT past it. # # IMPLIES: if will_close is FALSE, then self.close() will ALWAYS be # called, meaning self.isclosed() is meaningful. return self.fp is None def read(self, amt=None): if self.fp is None: return b"" if self._method == "HEAD": self._close_conn() return b"" if self.chunked: return self._read_chunked(amt) if amt is not None: if self.length is not None and amt > self.length: # clip the read to the "end of response" amt = self.length s = self.fp.read(amt) if not s and amt: # Ideally, we would raise IncompleteRead if the content-length # wasn't satisfied, but it might break compatibility. self._close_conn() elif self.length is not None: self.length -= len(s) if not self.length: self._close_conn() return s else: # Amount is not given (unbounded read) so we must check self.length if self.length is None: s = self.fp.read() else: try: s = self._safe_read(self.length) except IncompleteRead: self._close_conn() raise self.length = 0 self._close_conn() # we read everything return s def readinto(self, b): """Read up to len(b) bytes into bytearray b and return the number of bytes read. """ if self.fp is None: return 0 if self._method == "HEAD": self._close_conn() return 0 if self.chunked: return self._readinto_chunked(b) if self.length is not None: if len(b) > self.length: # clip the read to the "end of response" b = memoryview(b)[0:self.length] # we do not use _safe_read() here because this may be a .will_close # connection, and the user is reading more bytes than will be provided # (for example, reading in 1k chunks) n = self.fp.readinto(b) if not n and b: # Ideally, we would raise IncompleteRead if the content-length # wasn't satisfied, but it might break compatibility. self._close_conn() elif self.length is not None: self.length -= n if not self.length: self._close_conn() return n def _read_next_chunk_size(self): # Read the next chunk size from the file line = self.fp.readline(_MAXLINE + 1) if len(line) > _MAXLINE: raise LineTooLong("chunk size") i = line.find(b";") if i >= 0: line = line[:i] # strip chunk-extensions try: return int(line, 16) except ValueError: # close the connection as protocol synchronisation is # probably lost self._close_conn() raise def _read_and_discard_trailer(self): # read and discard trailer up to the CRLF terminator ### note: we shouldn't have any trailers! while True: line = self.fp.readline(_MAXLINE + 1) if len(line) > _MAXLINE: raise LineTooLong("trailer line") if not line: # a vanishingly small number of sites EOF without # sending the trailer break if line in (b'\r\n', b'\n', b''): break def _get_chunk_left(self): # return self.chunk_left, reading a new chunk if necessary. # chunk_left == 0: at the end of the current chunk, need to close it # chunk_left == None: No current chunk, should read next. # This function returns non-zero or None if the last chunk has # been read. chunk_left = self.chunk_left if not chunk_left: # Can be 0 or None if chunk_left is not None: # We are at the end of chunk, discard chunk end self._safe_read(2) # toss the CRLF at the end of the chunk try: chunk_left = self._read_next_chunk_size() except ValueError: raise IncompleteRead(b'') if chunk_left == 0: # last chunk: 1*("0") [ chunk-extension ] CRLF self._read_and_discard_trailer() # we read everything; close the "file" self._close_conn() chunk_left = None self.chunk_left = chunk_left return chunk_left def _read_chunked(self, amt=None): assert self.chunked != _UNKNOWN value = [] try: while True: chunk_left = self._get_chunk_left() if chunk_left is None: break if amt is not None and amt <= chunk_left: value.append(self._safe_read(amt)) self.chunk_left = chunk_left - amt break value.append(self._safe_read(chunk_left)) if amt is not None: amt -= chunk_left self.chunk_left = 0 return b''.join(value) except IncompleteRead as exc: raise IncompleteRead(b''.join(value)) from exc def _readinto_chunked(self, b): assert self.chunked != _UNKNOWN total_bytes = 0 mvb = memoryview(b) try: while True: chunk_left = self._get_chunk_left() if chunk_left is None: return total_bytes if len(mvb) <= chunk_left: n = self._safe_readinto(mvb) self.chunk_left = chunk_left - n return total_bytes + n temp_mvb = mvb[:chunk_left] n = self._safe_readinto(temp_mvb) mvb = mvb[n:] total_bytes += n self.chunk_left = 0 except IncompleteRead: raise IncompleteRead(bytes(b[0:total_bytes])) def _safe_read(self, amt): """Read the number of bytes requested. This function should be used when <amt> bytes "should" be present for reading. If the bytes are truly not available (due to EOF), then the IncompleteRead exception can be used to detect the problem. """ data = self.fp.read(amt) if len(data) < amt: raise IncompleteRead(data, amt-len(data)) return data def _safe_readinto(self, b): """Same as _safe_read, but for reading into a buffer.""" amt = len(b) n = self.fp.readinto(b) if n < amt: raise IncompleteRead(bytes(b[:n]), amt-n) return n def read1(self, n=-1): """Read with at most one underlying system call. If at least one byte is buffered, return that instead. """ if self.fp is None or self._method == "HEAD": return b"" if self.chunked: return self._read1_chunked(n) if self.length is not None and (n < 0 or n > self.length): n = self.length result = self.fp.read1(n) if not result and n: self._close_conn() elif self.length is not None: self.length -= len(result) return result def peek(self, n=-1): # Having this enables IOBase.readline() to read more than one # byte at a time if self.fp is None or self._method == "HEAD": return b"" if self.chunked: return self._peek_chunked(n) return self.fp.peek(n) def readline(self, limit=-1): if self.fp is None or self._method == "HEAD": return b"" if self.chunked: # Fallback to IOBase readline which uses peek() and read() return super().readline(limit) if self.length is not None and (limit < 0 or limit > self.length): limit = self.length result = self.fp.readline(limit) if not result and limit: self._close_conn() elif self.length is not None: self.length -= len(result) return result def _read1_chunked(self, n): # Strictly speaking, _get_chunk_left() may cause more than one read, # but that is ok, since that is to satisfy the chunked protocol. chunk_left = self._get_chunk_left() if chunk_left is None or n == 0: return b'' if not (0 <= n <= chunk_left): n = chunk_left # if n is negative or larger than chunk_left read = self.fp.read1(n) self.chunk_left -= len(read) if not read: raise IncompleteRead(b"") return read def _peek_chunked(self, n): # Strictly speaking, _get_chunk_left() may cause more than one read, # but that is ok, since that is to satisfy the chunked protocol. try: chunk_left = self._get_chunk_left() except IncompleteRead: return b'' # peek doesn't worry about protocol if chunk_left is None: return b'' # eof # peek is allowed to return more than requested. Just request the # entire chunk, and truncate what we get. return self.fp.peek(chunk_left)[:chunk_left] def fileno(self): return self.fp.fileno() def getheader(self, name, default=None): '''Returns the value of the header matching *name*. If there are multiple matching headers, the values are combined into a single string separated by commas and spaces. If no matching header is found, returns *default* or None if the *default* is not specified. If the headers are unknown, raises http.client.ResponseNotReady. ''' if self.headers is None: raise ResponseNotReady() headers = self.headers.get_all(name) or default if isinstance(headers, str) or not hasattr(headers, '__iter__'): return headers else: return ', '.join(headers) def getheaders(self): """Return list of (header, value) tuples.""" if self.headers is None: raise ResponseNotReady() return list(self.headers.items()) # We override IOBase.__iter__ so that it doesn't check for closed-ness def __iter__(self): return self # For compatibility with old-style urllib responses. def info(self): '''Returns an instance of the class mimetools.Message containing meta-information associated with the URL. When the method is HTTP, these headers are those returned by the server at the head of the retrieved HTML page (including Content-Length and Content-Type). When the method is FTP, a Content-Length header will be present if (as is now usual) the server passed back a file length in response to the FTP retrieval request. A Content-Type header will be present if the MIME type can be guessed. When the method is local-file, returned headers will include a Date representing the file's last-modified time, a Content-Length giving file size, and a Content-Type containing a guess at the file's type. See also the description of the mimetools module. ''' return self.headers def geturl(self): '''Return the real URL of the page. In some cases, the HTTP server redirects a client to another URL. The urlopen() function handles this transparently, but in some cases the caller needs to know which URL the client was redirected to. The geturl() method can be used to get at this redirected URL. ''' return self.url def getcode(self): '''Return the HTTP status code that was sent with the response, or None if the URL is not an HTTP URL. ''' return self.status class HTTPConnection: _http_vsn = 11 _http_vsn_str = 'HTTP/1.1' response_class = HTTPResponse default_port = HTTP_PORT auto_open = 1 debuglevel = 0 @staticmethod def _is_textIO(stream): """Test whether a file-like object is a text or a binary stream. """ return isinstance(stream, io.TextIOBase) @staticmethod def _get_content_length(body, method): """Get the content-length based on the body. If the body is None, we set Content-Length: 0 for methods that expect a body (RFC 7230, Section 3.3.2). We also set the Content-Length for any method if the body is a str or bytes-like object and not a file. """ if body is None: # do an explicit check for not None here to distinguish # between unset and set but empty if method.upper() in _METHODS_EXPECTING_BODY: return 0 else: return None if hasattr(body, 'read'): # file-like object. return None try: # does it implement the buffer protocol (bytes, bytearray, array)? mv = memoryview(body) return mv.nbytes except TypeError: pass if isinstance(body, str): return len(body) return None def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source_address=None, blocksize=8192): self.timeout = timeout self.source_address = source_address self.blocksize = blocksize self.sock = None self._buffer = [] self.__response = None self.__state = _CS_IDLE self._method = None self._tunnel_host = None self._tunnel_port = None self._tunnel_headers = {} (self.host, self.port) = self._get_hostport(host, port) self._validate_host(self.host) # This is stored as an instance variable to allow unit # tests to replace it with a suitable mockup self._create_connection = socket.create_connection def set_tunnel(self, host, port=None, headers=None): """Set up host and port for HTTP CONNECT tunnelling. In a connection that uses HTTP CONNECT tunneling, the host passed to the constructor is used as a proxy server that relays all communication to the endpoint passed to `set_tunnel`. This done by sending an HTTP CONNECT request to the proxy server when the connection is established. This method must be called before the HTTP connection has been established. The headers argument should be a mapping of extra HTTP headers to send with the CONNECT request. """ if self.sock: raise RuntimeError("Can't set up tunnel for established connection") self._tunnel_host, self._tunnel_port = self._get_hostport(host, port) if headers: self._tunnel_headers = headers else: self._tunnel_headers.clear() def _get_hostport(self, host, port): if port is None: i = host.rfind(':') j = host.rfind(']') # ipv6 addresses have [...] if i > j: try: port = int(host[i+1:]) except ValueError: if host[i+1:] == "": # http://foo.com:/ == http://foo.com/ port = self.default_port else: raise InvalidURL("nonnumeric port: '%s'" % host[i+1:]) host = host[:i] else: port = self.default_port if host and host[0] == '[' and host[-1] == ']': host = host[1:-1] return (host, port) def set_debuglevel(self, level): self.debuglevel = level def _tunnel(self): connect = b"CONNECT %s:%d HTTP/1.0\r\n" % ( self._tunnel_host.encode("ascii"), self._tunnel_port) headers = [connect] for header, value in self._tunnel_headers.items(): headers.append(f"{header}: {value}\r\n".encode("latin-1")) headers.append(b"\r\n") # Making a single send() call instead of one per line encourages # the host OS to use a more optimal packet size instead of # potentially emitting a series of small packets. self.send(b"".join(headers)) del headers response = self.response_class(self.sock, method=self._method) (version, code, message) = response._read_status() if code != http.HTTPStatus.OK: self.close() raise OSError(f"Tunnel connection failed: {code} {message.strip()}") while True: line = response.fp.readline(_MAXLINE + 1) if len(line) > _MAXLINE: raise LineTooLong("header line") if not line: # for sites which EOF without sending a trailer break if line in (b'\r\n', b'\n', b''): break if self.debuglevel > 0: print('header:', line.decode()) def connect(self): """Connect to the host and port specified in __init__.""" sys.audit("http.client.connect", self, self.host, self.port) self.sock = self._create_connection( (self.host,self.port), self.timeout, self.source_address) # Might fail in OSs that don't implement TCP_NODELAY try: self.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) except OSError as e: if e.errno != errno.ENOPROTOOPT: raise if self._tunnel_host: self._tunnel() def close(self): """Close the connection to the HTTP server.""" self.__state = _CS_IDLE try: sock = self.sock if sock: self.sock = None sock.close() # close it manually... there may be other refs finally: response = self.__response if response: self.__response = None response.close() def send(self, data): """Send `data' to the server. ``data`` can be a string object, a bytes object, an array object, a file-like object that supports a .read() method, or an iterable object. """ if self.sock is None: if self.auto_open: self.connect() else: raise NotConnected() if self.debuglevel > 0: print("send:", repr(data)) if hasattr(data, "read") : if self.debuglevel > 0: print("sendIng a read()able") encode = self._is_textIO(data) if encode and self.debuglevel > 0: print("encoding file using iso-8859-1") while 1: datablock = data.read(self.blocksize) if not datablock: break if encode: datablock = datablock.encode("iso-8859-1") sys.audit("http.client.send", self, datablock) self.sock.sendall(datablock) return sys.audit("http.client.send", self, data) try: self.sock.sendall(data) except TypeError: if isinstance(data, collections.abc.Iterable): for d in data: self.sock.sendall(d) else: raise TypeError("data should be a bytes-like object " "or an iterable, got %r" % type(data)) def _output(self, s): """Add a line of output to the current request buffer. Assumes that the line does *not* end with \\r\\n. """ self._buffer.append(s) def _read_readable(self, readable): if self.debuglevel > 0: print("sendIng a read()able") encode = self._is_textIO(readable) if encode and self.debuglevel > 0: print("encoding file using iso-8859-1") while True: datablock = readable.read(self.blocksize) if not datablock: break if encode: datablock = datablock.encode("iso-8859-1") yield datablock def _send_output(self, message_body=None, encode_chunked=False): """Send the currently buffered request and clear the buffer. Appends an extra \\r\\n to the buffer. A message_body may be specified, to be appended to the request. """ self._buffer.extend((b"", b"")) msg = b"\r\n".join(self._buffer) del self._buffer[:] self.send(msg) if message_body is not None: # create a consistent interface to message_body if hasattr(message_body, 'read'): # Let file-like take precedence over byte-like. This # is needed to allow the current position of mmap'ed # files to be taken into account. chunks = self._read_readable(message_body) else: try: # this is solely to check to see if message_body # implements the buffer API. it /would/ be easier # to capture if PyObject_CheckBuffer was exposed # to Python. memoryview(message_body) except TypeError: try: chunks = iter(message_body) except TypeError: raise TypeError("message_body should be a bytes-like " "object or an iterable, got %r" % type(message_body)) else: # the object implements the buffer interface and # can be passed directly into socket methods chunks = (message_body,) for chunk in chunks: if not chunk: if self.debuglevel > 0: print('Zero length chunk ignored') continue if encode_chunked and self._http_vsn == 11: # chunked encoding chunk = f'{len(chunk):X}\r\n'.encode('ascii') + chunk \ + b'\r\n' self.send(chunk) if encode_chunked and self._http_vsn == 11: # end chunked transfer self.send(b'0\r\n\r\n') def putrequest(self, method, url, skip_host=False, skip_accept_encoding=False): """Send a request to the server. `method' specifies an HTTP request method, e.g. 'GET'. `url' specifies the object being requested, e.g. '/index.html'. `skip_host' if True does not add automatically a 'Host:' header `skip_accept_encoding' if True does not add automatically an 'Accept-Encoding:' header """ # if a prior response has been completed, then forget about it. if self.__response and self.__response.isclosed(): self.__response = None # in certain cases, we cannot issue another request on this connection. # this occurs when: # 1) we are in the process of sending a request. (_CS_REQ_STARTED) # 2) a response to a previous request has signalled that it is going # to close the connection upon completion. # 3) the headers for the previous response have not been read, thus # we cannot determine whether point (2) is true. (_CS_REQ_SENT) # # if there is no prior response, then we can request at will. # # if point (2) is true, then we will have passed the socket to the # response (effectively meaning, "there is no prior response"), and # will open a new one when a new request is made. # # Note: if a prior response exists, then we *can* start a new request. # We are not allowed to begin fetching the response to this new # request, however, until that prior response is complete. # if self.__state == _CS_IDLE: self.__state = _CS_REQ_STARTED else: raise CannotSendRequest(self.__state) self._validate_method(method) # Save the method for use later in the response phase self._method = method url = url or '/' self._validate_path(url) request = '%s %s %s' % (method, url, self._http_vsn_str) self._output(self._encode_request(request)) if self._http_vsn == 11: # Issue some standard headers for better HTTP/1.1 compliance if not skip_host: # this header is issued *only* for HTTP/1.1 # connections. more specifically, this means it is # only issued when the client uses the new # HTTPConnection() class. backwards-compat clients # will be using HTTP/1.0 and those clients may be # issuing this header themselves. we should NOT issue # it twice; some web servers (such as Apache) barf # when they see two Host: headers # If we need a non-standard port,include it in the # header. If the request is going through a proxy, # but the host of the actual URL, not the host of the # proxy. netloc = '' if url.startswith('http'): nil, netloc, nil, nil, nil = urlsplit(url) if netloc: try: netloc_enc = netloc.encode("ascii") except UnicodeEncodeError: netloc_enc = netloc.encode("idna") self.putheader('Host', netloc_enc) else: if self._tunnel_host: host = self._tunnel_host port = self._tunnel_port else: host = self.host port = self.port try: host_enc = host.encode("ascii") except UnicodeEncodeError: host_enc = host.encode("idna") # As per RFC 273, IPv6 address should be wrapped with [] # when used as Host header if host.find(':') >= 0: host_enc = b'[' + host_enc + b']' if port == self.default_port: self.putheader('Host', host_enc) else: host_enc = host_enc.decode("ascii") self.putheader('Host', "%s:%s" % (host_enc, port)) # note: we are assuming that clients will not attempt to set these # headers since *this* library must deal with the # consequences. this also means that when the supporting # libraries are updated to recognize other forms, then this # code should be changed (removed or updated). # we only want a Content-Encoding of "identity" since we don't # support encodings such as x-gzip or x-deflate. if not skip_accept_encoding: self.putheader('Accept-Encoding', 'identity') # we can accept "chunked" Transfer-Encodings, but no others # NOTE: no TE header implies *only* "chunked" #self.putheader('TE', 'chunked') # if TE is supplied in the header, then it must appear in a # Connection header. #self.putheader('Connection', 'TE') else: # For HTTP/1.0, the server will assume "not chunked" pass def _encode_request(self, request): # ASCII also helps prevent CVE-2019-9740. return request.encode('ascii') def _validate_method(self, method): """Validate a method name for putrequest.""" # prevent http header injection match = _contains_disallowed_method_pchar_re.search(method) if match: raise ValueError( f"method can't contain control characters. {method!r} " f"(found at least {match.group()!r})") def _validate_path(self, url): """Validate a url for putrequest.""" # Prevent CVE-2019-9740. match = _contains_disallowed_url_pchar_re.search(url) if match: raise InvalidURL(f"URL can't contain control characters. {url!r} " f"(found at least {match.group()!r})") def _validate_host(self, host): """Validate a host so it doesn't contain control characters.""" # Prevent CVE-2019-18348. match = _contains_disallowed_url_pchar_re.search(host) if match: raise InvalidURL(f"URL can't contain control characters. {host!r} " f"(found at least {match.group()!r})") def putheader(self, header, *values): """Send a request header line to the server. For example: h.putheader('Accept', 'text/html') """ if self.__state != _CS_REQ_STARTED: raise CannotSendHeader() if hasattr(header, 'encode'): header = header.encode('ascii') if not _is_legal_header_name(header): raise ValueError('Invalid header name %r' % (header,)) values = list(values) for i, one_value in enumerate(values): if hasattr(one_value, 'encode'): values[i] = one_value.encode('latin-1') elif isinstance(one_value, int): values[i] = str(one_value).encode('ascii') if _is_illegal_header_value(values[i]): raise ValueError('Invalid header value %r' % (values[i],)) value = b'\r\n\t'.join(values) header = header + b': ' + value self._output(header) def endheaders(self, message_body=None, *, encode_chunked=False): """Indicate that the last header line has been sent to the server. This method sends the request to the server. The optional message_body argument can be used to pass a message body associated with the request. """ if self.__state == _CS_REQ_STARTED: self.__state = _CS_REQ_SENT else: raise CannotSendHeader() self._send_output(message_body, encode_chunked=encode_chunked) def request(self, method, url, body=None, headers={}, *, encode_chunked=False): """Send a complete request to the server.""" self._send_request(method, url, body, headers, encode_chunked) def _send_request(self, method, url, body, headers, encode_chunked): # Honor explicitly requested Host: and Accept-Encoding: headers. header_names = frozenset(k.lower() for k in headers) skips = {} if 'host' in header_names: skips['skip_host'] = 1 if 'accept-encoding' in header_names: skips['skip_accept_encoding'] = 1 self.putrequest(method, url, **skips) # chunked encoding will happen if HTTP/1.1 is used and either # the caller passes encode_chunked=True or the following # conditions hold: # 1. content-length has not been explicitly set # 2. the body is a file or iterable, but not a str or bytes-like # 3. Transfer-Encoding has NOT been explicitly set by the caller if 'content-length' not in header_names: # only chunk body if not explicitly set for backwards # compatibility, assuming the client code is already handling the # chunking if 'transfer-encoding' not in header_names: # if content-length cannot be automatically determined, fall # back to chunked encoding encode_chunked = False content_length = self._get_content_length(body, method) if content_length is None: if body is not None: if self.debuglevel > 0: print('Unable to determine size of %r' % body) encode_chunked = True self.putheader('Transfer-Encoding', 'chunked') else: self.putheader('Content-Length', str(content_length)) else: encode_chunked = False for hdr, value in headers.items(): self.putheader(hdr, value) if isinstance(body, str): # RFC 2616 Section 3.7.1 says that text default has a # default charset of iso-8859-1. body = _encode(body, 'body') self.endheaders(body, encode_chunked=encode_chunked) def getresponse(self): """Get the response from the server. If the HTTPConnection is in the correct state, returns an instance of HTTPResponse or of whatever object is returned by the response_class variable. If a request has not been sent or if a previous response has not be handled, ResponseNotReady is raised. If the HTTP response indicates that the connection should be closed, then it will be closed before the response is returned. When the connection is closed, the underlying socket is closed. """ # if a prior response has been completed, then forget about it. if self.__response and self.__response.isclosed(): self.__response = None # if a prior response exists, then it must be completed (otherwise, we # cannot read this response's header to determine the connection-close # behavior) # # note: if a prior response existed, but was connection-close, then the # socket and response were made independent of this HTTPConnection # object since a new request requires that we open a whole new # connection # # this means the prior response had one of two states: # 1) will_close: this connection was reset and the prior socket and # response operate independently # 2) persistent: the response was retained and we await its # isclosed() status to become true. # if self.__state != _CS_REQ_SENT or self.__response: raise ResponseNotReady(self.__state) if self.debuglevel > 0: response = self.response_class(self.sock, self.debuglevel, method=self._method) else: response = self.response_class(self.sock, method=self._method) try: try: response.begin() except ConnectionError: self.close() raise assert response.will_close != _UNKNOWN self.__state = _CS_IDLE if response.will_close: # this effectively passes the connection to the response self.close() else: # remember this, so we can tell when it is complete self.__response = response return response except: response.close() raise try: import ssl except ImportError: pass else: class HTTPSConnection(HTTPConnection): "This class allows communication via SSL." default_port = HTTPS_PORT # XXX Should key_file and cert_file be deprecated in favour of context? def __init__(self, host, port=None, key_file=None, cert_file=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT, source_address=None, *, context=None, check_hostname=None, blocksize=8192): super(HTTPSConnection, self).__init__(host, port, timeout, source_address, blocksize=blocksize) if (key_file is not None or cert_file is not None or check_hostname is not None): import warnings warnings.warn("key_file, cert_file and check_hostname are " "deprecated, use a custom context instead.", DeprecationWarning, 2) self.key_file = key_file self.cert_file = cert_file if context is None: context = ssl._create_default_https_context() # send ALPN extension to indicate HTTP/1.1 protocol if self._http_vsn == 11: context.set_alpn_protocols(['http/1.1']) # enable PHA for TLS 1.3 connections if available if context.post_handshake_auth is not None: context.post_handshake_auth = True will_verify = context.verify_mode != ssl.CERT_NONE if check_hostname is None: check_hostname = context.check_hostname if check_hostname and not will_verify: raise ValueError("check_hostname needs a SSL context with " "either CERT_OPTIONAL or CERT_REQUIRED") if key_file or cert_file: context.load_cert_chain(cert_file, key_file) # cert and key file means the user wants to authenticate. # enable TLS 1.3 PHA implicitly even for custom contexts. if context.post_handshake_auth is not None: context.post_handshake_auth = True self._context = context if check_hostname is not None: self._context.check_hostname = check_hostname def connect(self): "Connect to a host on a given (SSL) port." super().connect() if self._tunnel_host: server_hostname = self._tunnel_host else: server_hostname = self.host self.sock = self._context.wrap_socket(self.sock, server_hostname=server_hostname) __all__.append("HTTPSConnection") class HTTPException(Exception): # Subclasses that define an __init__ must call Exception.__init__ # or define self.args. Otherwise, str() will fail. pass class NotConnected(HTTPException): pass class InvalidURL(HTTPException): pass class UnknownProtocol(HTTPException): def __init__(self, version): self.args = version, self.version = version class UnknownTransferEncoding(HTTPException): pass class UnimplementedFileMode(HTTPException): pass class IncompleteRead(HTTPException): def __init__(self, partial, expected=None): self.args = partial, self.partial = partial self.expected = expected def __repr__(self): if self.expected is not None: e = ', %i more expected' % self.expected else: e = '' return '%s(%i bytes read%s)' % (self.__class__.__name__, len(self.partial), e) __str__ = object.__str__ class ImproperConnectionState(HTTPException): pass class CannotSendRequest(ImproperConnectionState): pass class CannotSendHeader(ImproperConnectionState): pass class ResponseNotReady(ImproperConnectionState): pass class BadStatusLine(HTTPException): def __init__(self, line): if not line: line = repr(line) self.args = line, self.line = line class LineTooLong(HTTPException): def __init__(self, line_type): HTTPException.__init__(self, "got more than %d bytes when reading %s" % (_MAXLINE, line_type)) class RemoteDisconnected(ConnectionResetError, BadStatusLine): def __init__(self, *pos, **kw): BadStatusLine.__init__(self, "") ConnectionResetError.__init__(self, *pos, **kw) # for backwards compatibility error = HTTPException 解析这些代码 怎么联接这个服务器
06-19
<think>我们遇到了一个错误:`ERROR: Empty request body (expected 41 bytes)`。这意味着在解析POST请求时,服务器期望有41字节的请求体,但实际上接收到的请求体为空。 可能的原因及解决方案: 1. **请求解析问题**:在解析请求时,可能没有正确读取请求体。我们之前已经改进了`parse_request`函数,但可能还存在边界情况。 2. **网络问题**:客户端可能没有发送完整的请求体,或者连接被意外关闭。 3. **缓冲区问题**:在解析请求头时,可能没有正确找到请求体的起始位置,或者请求体被截断。 为了彻底解决这个问题,我们需要: a. 确保在解析请求时,能够正确读取完整的请求体(特别是当请求体大于初始缓冲区时)。 b. 添加更详细的错误日志,以便于定位问题。 下面我们重新检查并增强`parse_request`函数中处理POST请求体的部分: 步骤: 1. 检查Content-Length头部是否存在,并获取其值。 2. 在接收数据时,确保读取到完整的请求体(包括可能的分多次读取)。 3. 处理请求体被部分包含在初始缓冲区中的情况。 我们已经在之前的代码中实现了这些逻辑,但为了确保可靠性,我们将再次优化: 修改后的`parse_request`函数中处理POST请求体的部分: ```c // 解析Body (POST/PUT) if (strcmp(req->method, "POST") == 0 || strcmp(req->method, "PUT") == 0) { // 检查是否有内容长度 if (req->content_length == 0) { fprintf(stderr, "POST request with no Content-Length\n"); return; } // 分配内存用于存储请求体 req->body = malloc(req->content_length + 1); if (!req->body) { perror("malloc failed"); return; } // 查找请求头结束位置(空行) char *body_start = strstr(buffer, "\r\n\r\n"); if (body_start) { body_start += 4; // 跳过空行 size_t header_len = body_start - buffer; size_t body_in_buffer = bytes_read - header_len; // 复制缓冲区中已有的请求体部分 if (body_in_buffer > 0) { memcpy(req->body, body_start, body_in_buffer); req->body_len = body_in_buffer; } } else { // 如果没有找到空行,则请求体未包含在初始缓冲区中 req->body_len = 0; } // 如果请求体未完整读取,则读取剩余部分 if (req->body_len < req->content_length) { ssize_t remaining = req->content_length - req->body_len; ssize_t n; while (remaining > 0 && (n = recv(sockfd, req->body + req->body_len, remaining, 0)) > 0) { req->body_len += n; remaining -= n; } if (n < 0) { perror("recv failed for body"); } } // 确保字符串结束 req->body[req->body_len] = '\0'; } ``` 但是,上述代码在找不到空行(即body_start为NULL)时,会跳过复制初始缓冲区中的请求体部分,然后尝试从套接字中读取整个请求体。这可能导致问题,因为请求头可能被分多次读取?实际上,我们的初始recv调用已经读取了BUFFER_SIZE-1字节,通常一个请求的头部不会超过这个大小,所以头部和部分请求体会在第一次recv中读取到。如果请求头超过了BUFFER_SIZE,那么我们的解析就会出错。 因此,我们需要考虑两种情况: - 情况1:请求头+请求体全部在第一次recv中读取到(最常见)。 - 情况2:请求头太大,分多次读取。这种情况我们暂时不处理,因为通常请求头不会超过4KB。 我们假设请求头不会超过BUFFER_SIZE。如果超过,我们可能无法正确解析。为了安全,我们可以循环读取直到读取完请求头(即直到遇到空行),然后再读取请求体。 但是,为了简单起见,我们假设请求头不会超过BUFFER_SIZE。因此,我们只处理请求体可能分多次读取的情况。 然而,上面的代码在找不到空行时,会认为整个请求都是请求体?这显然不对。所以,我们要求请求头必须包含空行,否则视为无效请求。 因此,我们修改:如果找不到空行,则返回错误。 修改如下: ```c // 查找请求头结束位置(空行) char *body_start = strstr(buffer, "\r\n\r\n"); if (!body_start) { fprintf(stderr, "Invalid request: no empty line after headers\n"); free(req->body); req->body = NULL; req->content_length = 0; return; } ``` 但是,我们也要考虑另一种可能:请求头没有完全被一次recv读取。这种情况我们暂时不处理,因为我们的BUFFER_SIZE是4096,通常请求头不会这么大。 所以,我们假设请求头一定在一次recv中被完整读取。 完整修改后的`parse_request`函数中处理POST请求体的部分: ```c // 解析Body (POST/PUT) if (strcmp(req->method, "POST") == 0 || strcmp(req->method, "PUT") == 0) { // 检查是否有内容长度 if (req->content_length == 0) { fprintf(stderr, "POST request with no Content-Length\n"); return; } // 分配内存用于存储请求体 req->body = malloc(req->content_length + 1); if (!req->body) { perror("malloc failed"); return; } // 查找请求头结束位置(空行) char *body_start = strstr(buffer, "\r\n\r\n"); if (!body_start) { fprintf(stderr, "Invalid request: no empty line after headers\n"); free(req->body); req->body = NULL; req->content_length = 0; return; } body_start += 4; // 跳过空行 size_t header_len = body_start - buffer; size_t body_in_buffer = bytes_read - header_len; // 复制缓冲区中已有的请求体部分 if (body_in_buffer > 0) { memcpy(req->body, body_start, body_in_buffer); req->body_len = body_in_buffer; } else { req->body_len = 0; } // 如果请求体未完整读取,则读取剩余部分 if (req->body_len < req->content_length) { size_t remaining = req->content_length - req->body_len; ssize_t n; while (remaining > 0) { n = recv(sockfd, req->body + req->body_len, remaining, 0); if (n <= 0) { // 读取错误或连接关闭 if (n < 0) { perror("recv failed for body"); } break; } req->body_len += n; remaining -= n; } } // 确保字符串结束 req->body[req->body_len] = '\0'; } ``` 这样,我们确保: - 必须找到空行,否则视为无效请求。 - 将初始缓冲区中的请求体部分复制到body中。 - 如果请求体不完整,则继续读取直到达到Content-Length指定的长度。 另外,在`handle_post`函数中,我们也要注意,如果请求体解析失败(req->body_len为0,但content_length不为0),则返回400错误。 在`handle_post`函数开头添加: ```c void handle_post(int sockfd, HttpRequest *req) { // 检查请求体是否完整 if (req->content_length > 0 && (req->body == NULL || req->body_len != req->content_length)) { printf("ERROR: Incomplete request body (expected %zu, got %zu)\n", req->content_length, req->body_len); send_response(sockfd, 400, "Bad Request", "text/plain", "Incomplete request body", 23); return; } // ... 其他代码 ... } ``` 这样,当请求体不完整时,我们能够返回错误信息。 最后,我们还需要注意,在解析请求头时,我们只解析了Content-Length,没有处理Transfer-Encoding: chunked。目前我们只支持Content-Length。 因此,如果客户端使用分块传输,我们的服务器将无法正确解析。所以,我们暂时要求客户端必须提供Content-Length。 通过以上修改,应该可以解决“Empty request body”的问题,因为现在我们会等待并读取完整的请求体。 但是,如果网络延迟导致请求体到达慢,我们的recv可能会阻塞。为了解决这个问题,我们可以设置套接字为非阻塞模式,并使用select/poll/epoll来管理。但为了简单,我们先使用阻塞模式,并设置一个超时。 在创建套接字后,我们可以设置接收超时: ```c struct timeval tv; tv.tv_sec = 5; // 5秒超时 tv.tv_usec = 0; setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof tv); ``` 这样,如果5秒内没有收到数据,recv会返回错误。 在`parse_request`函数中,我们处理recv错误时,要注意超时错误(EAGAIN或EWOULDBLOCK,在Linux下是EAGAIN,在Windows下是WSAETIMEDOUT)。 修改初始recv部分: ```c ssize_t bytes_read = recv(sockfd, buffer, BUFFER_SIZE - 1, 0); if (bytes_read <= 0) { if (bytes_read == 0) { fprintf(stderr, "Connection closed by client\n"); } else { if (errno == EAGAIN || errno == EWOULDBLOCK) { fprintf(stderr, "recv timeout\n"); } else { perror("recv failed"); } } return; } ``` 这样,我们就避免了无限等待请求体。 综上所述,我们通过以下方式解决“Empty request body”问题: 1. 确保正确解析请求头,找到空行。 2. 正确读取请求体(包括初始缓冲区和后续读取)。 3. 在请求体不完整时返回错误。 4. 设置接收超时避免无限等待。 最后,我们给出完整的`parse_request`函数(仅POST请求体部分)和`handle_post`函数开头部分的修改。 注意:这些修改需要结合之前的代码。 如果问题仍然存在,请添加详细的日志,打印出接收到的原始请求数据,以便分析。 例如,在`parse_request`函数中,在recv之后可以打印: ```c printf("Raw request data:\n%.*s\n", (int)bytes_read, buffer); ``` 但要注意,如果请求体包含二进制数据,打印可能会乱码。 希望这些修改能解决问题。</think>这个错误表明在 POST 请求中,服务器期望接收 41 字节的请求体,但实际接收到的请求体为空。让我们深入分析这个问题并提供完整的解决方案。 ### 问题分析与解决方案 #### 根本原因分析 1. **请求解析问题**:`parse_request` 函数未能正确读取请求体 2. **网络问题**:客户端可能未发送完整的请求体 3. **缓冲区处理**:请求头与请求体之间的分隔处理不当 4. **超时问题**:服务器在读取完整请求体前关闭了连接 #### 完整修复方案 ```c // 增强的请求解析函数 ParseResult parse_request(int sockfd, HttpRequest *req) { char buffer[BUFFER_SIZE]; ssize_t total_read = 0; ssize_t bytes_read; // 初始化请求结构 memset(req, 0, sizeof(HttpRequest)); req->content_length = 0; // 第一阶段:读取请求行和头部 while (1) { bytes_read = recv(sockfd, buffer + total_read, BUFFER_SIZE - total_read - 1, 0); if (bytes_read <= 0) { return (bytes_read == 0) ? PARSE_ERROR_CONNECTION_CLOSED : PARSE_ERROR_RECV_FAILED; } total_read += bytes_read; buffer[total_read] = '\0'; // 检查是否已读取完整的头部(以\r\n\r\n结尾) char *header_end = strstr(buffer, "\r\n\r\n"); if (header_end) { *header_end = '\0'; // 标记头部结束位置 break; } // 防止缓冲区溢出 if (total_read >= BUFFER_SIZE - 1) { return PARSE_ERROR_TOO_MANY_HEADERS; } } // 解析请求行 char *line = strtok(buffer, "\r\n"); if (!line || sscanf(line, "%15s %255s %15s", req->method, req->path, req->protocol) != 3) { return PARSE_ERROR_INVALID_REQUEST_LINE; } // 解析头部 req->header_count = 0; while ((line = strtok(NULL, "\r\n")) && *line != '\0') { if (req->header_count >= MAX_HEADERS) { return PARSE_ERROR_TOO_MANY_HEADERS; } char *sep = strchr(line, ':'); if (!sep) continue; *sep = '\0'; char *value_start = sep + 1; while (*value_start == ' ') value_start++; // 存储头部键值对 req->headers[req->header_count].key = strdup(line); req->headers[req->header_count].value = strdup(value_start); req->header_count++; // 处理Content-Length if (strcasecmp(line, "Content-Length") == 0) { req->content_length = strtoul(value_start, NULL, 10); if (req->content_length > MAX_BODY_SIZE) { return PARSE_ERROR_BODY_TOO_LARGE; } } } // 第二阶段:读取请求体(仅适用于POST/PUT) if ((strcmp(req->method, "POST") == 0 || strcmp(req->method, "PUT") == 0) && req->content_length > 0) { // 计算已读取的请求体部分 char *body_start = header_end + 4; // 跳过\r\n\r\n size_t body_in_buffer = total_read - (body_start - buffer); // 分配内存存储请求体 req->body = malloc(req->content_length + 1); if (!req->body) { return PARSE_ERROR_MEMORY_ALLOCATION; } // 复制缓冲区中的请求体部分 size_t to_copy = (body_in_buffer > req->content_length) ? req->content_length : body_in_buffer; memcpy(req->body, body_start, to_copy); req->body_len = to_copy; // 读取剩余的请求体 if (req->body_len < req->content_length) { size_t remaining = req->content_length - req->body_len; ssize_t n; // 设置读取超时(5秒) struct timeval tv; tv.tv_sec = 5; tv.tv_usec = 0; setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv)); // 读取剩余数据 while (remaining > 0) { n = recv(sockfd, req->body + req->body_len, remaining, 0); if (n <= 0) { if (n == 0) { fprintf(stderr, "Connection closed by client while reading body\n"); } else { perror("recv failed for body"); } break; } req->body_len += n; remaining -= n; } } // 确保字符串终止 req->body[req->body_len] = '\0'; } return PARSE_SUCCESS; } // 增强的POST请求处理 void handle_post(int sockfd, HttpRequest *req) { // 验证请求体 if (req->content_length > 0 && (!req->body || req->body_len == 0)) { char error_msg[128]; snprintf(error_msg, sizeof(error_msg), "Empty request body (expected %zu bytes, got %zu bytes)", req->content_length, req->body_len); fprintf(stderr, "ERROR: %s\n", error_msg); send_response(sockfd, 400, "Bad Request", "text/plain", error_msg, strlen(error_msg)); return; } // 处理请求体 char *body_copy = strndup(req->body, req->body_len); if (!body_copy) { send_response(sockfd, 500, "Internal Error", "text/plain", "Memory error", 12); return; } // 解析表单数据 char name[256] = {0}; char email[256] = {0}; char message[1024] = {0}; char *saveptr = NULL; char *pair = strtok_r(body_copy, "&", &saveptr); while (pair) { char *eq = strchr(pair, '='); if (eq) { *eq = '\0'; char *key = pair; char *value = eq + 1; if (strcasecmp(key, "name") == 0) { url_decode(name, value, sizeof(name)); } else if (strcasecmp(key, "email") == 0) { url_decode(email, value, sizeof(email)); } else if (strcasecmp(key, "message") == 0) { url_decode(message, value, sizeof(message)); } } pair = strtok_r(NULL, "&", &saveptr); } // 构建响应 char response[2048]; int len = snprintf(response, sizeof(response), "Form submission received:\nName: %s\nEmail: %s\nMessage: %s", name, email, message); send_response(sockfd, 200, "OK", "text/plain", response, len); free(body_copy); } ``` ### 关键修复点解释 #### 1. 两阶段请求解析 ```c // 第一阶段:读取请求行和头部 while (1) { bytes_read = recv(sockfd, buffer + total_read, BUFFER_SIZE - total_read - 1, 0); // ... char *header_end = strstr(buffer, "\r\n\r\n"); if (header_end) break; } ``` 这种方法确保: - 完整读取所有请求头 - 正确处理大请求头 - 准确找到请求体起始位置 #### 2. 可靠的请求体读取 ```c // 设置读取超时(5秒) struct timeval tv; tv.tv_sec = 5; tv.tv_usec = 0; setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv)); // 读取剩余数据 while (remaining > 0) { n = recv(sockfd, req->body + req->body_len, remaining, 0); // ... } ``` 这段代码: - 防止无限等待请求体 - 处理网络中断情况 - 确保完整读取Content-Length指定的字节数 #### 3. 详细的错误处理 ```c if (req->content_length > 0 && (!req->body || req->body_len == 0)) { char error_msg[128]; snprintf(error_msg, sizeof(error_msg), "Empty request body (expected %zu bytes, got %zu bytes)", req->content_length, req->body_len); // ... } ``` 这种错误报告: - 明确显示期望和实际字节数 - 帮助诊断网络问题 - 提供客户端调试信息 #### 4. 增强的URL解码 ```c void url_decode(char *dst, const char *src, size_t max_len) { size_t i = 0; while (*src && i < max_len - 1) { if (*src == '%' && isxdigit(src[1]) && isxdigit(src[2])) { char hex[3] = {src[1], src[2], '\0'}; *dst++ = (char)strtol(hex, NULL, 16); src += 3; } else if (*src == '+') { *dst++ = ' '; src++; } else { *dst++ = *src++; } i++; } *dst = '\0'; } ``` 这个解码函数: - 正确处理所有百分比编码 - 转换+号为空格 - 防止缓冲区溢出 ### 使用示例 ```c // 主循环中处理连接 void handle_connection(int client_fd) { HttpRequest req; ParseResult result = parse_request(client_fd, &req); switch (result) { case PARSE_SUCCESS: if (strcmp(req.method, "POST") == 0) { handle_post(client_fd, &req); } else { handle_get(client_fd, &req); } break; case PARSE_ERROR_BODY_TOO_LARGE: send_response(client_fd, 413, "Payload Too Large", "text/plain", "Request body too large", 22); break; case PARSE_ERROR_CONNECTION_CLOSED: fprintf(stderr, "Client closed connection prematurely\n"); break; default: send_response(client_fd, 400, "Bad Request", "text/plain", "Invalid request", 15); } free_request(&req); close(client_fd); } ``` ### 预防性措施 1. **添加请求日志**: ```c void log_request(const char *ip, HttpRequest *req) { printf("[%s] %s %s %s\n", ip, req->method, req->path, req->protocol); printf("Content-Length: %zu, Body-Length: %zu\n", req->content_length, req->body_len); if (req->body_len > 0) { printf("Body: %.*s\n", (int)(req->body_len > 100 ? 100 : req->body_len), req->body); } } ``` 2. **客户端模拟测试**: ```python # Python测试脚本 import requests data = { "name": "John Doe", "email": "john@example.com", "message": "This is a test message" } # 正常请求 response = requests.post("http://localhost:8080/submit", data=data) print("Normal response:", response.text) # 模拟中断连接 try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("localhost", 8080)) s.sendall(b"POST /submit HTTP/1.1\r\n") s.sendall(b"Host: localhost\r\n") s.sendall(b"Content-Length: 41\r\n") s.sendall(b"Content-Type: application/x-www-form-urlencoded\r\n") s.sendall(b"\r\n") s.sendall(b"name=John") # 只发送部分数据 s.close() # 故意中断连接 except Exception as e: print("Simulated error:", e) ``` 3. **压力测试工具**: ```bash # 使用ab进行压力测试 ab -n 1000 -c 50 -p postdata.txt -T "application/x-www-form-urlencoded" http://localhost:8080/submit ``` ### 完整解决方案优势 1. **健壮性**:处理各种网络异常和客户端行为 2. **安全性**:防止缓冲区溢出和拒绝服务攻击 3. **可诊断性**:详细的错误日志和响应 4. **性能优化**:高效的内存管理和网络I/O 5. **兼容性**:符合HTTP/1.1标准 这个解决方案彻底解决了"Empty request body"错误,同时提高了服务器的整体健壮性和可靠性。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值