Index: src/OFHTTPClient.m ================================================================== --- src/OFHTTPClient.m +++ src/OFHTTPClient.m @@ -118,10 +118,11 @@ OFURL *URL = request.URL; OFString *path; OFString *user = URL.user, *password = URL.password; OFMutableString *requestString; OFMutableDictionary OF_GENERIC(OFString *, OFString *) *headers; + bool hasContentLength, chunked; OFEnumerator OF_GENERIC(OFString *) *keyEnumerator, *objectEnumerator; OFString *key, *object; if (URL.path != nil) path = URL.URLEncodedPath; @@ -185,11 +186,15 @@ request.protocolVersion.minor == 0 && [headers objectForKey: @"Connection"] == nil) [headers setObject: @"keep-alive" forKey: @"Connection"]; - if ([headers objectForKey: @"Content-Length"] != nil && + hasContentLength = ([headers objectForKey: @"Content-Length"] != nil); + chunked = [[headers objectForKey: @"Transfer-Encoding"] + isEqual: @"chunked"]; + + if ((hasContentLength || chunked) && [headers objectForKey: @"Content-Type"] == nil) [headers setObject: @"application/x-www-form-" @"urlencoded; charset=UTF-8" forKey: @"Content-Type"]; @@ -551,11 +556,11 @@ encoding: (of_string_encoding_t)encoding bytesWritten: (size_t)bytesWritten exception: (id)exception { OFDictionary OF_GENERIC(OFString *, OFString *) *headers; - OFString *transferEncoding; + bool chunked; if (exception != nil) { if ([exception isKindOfClass: [OFWriteFailedException class]] && ([exception errNo] == ECONNRESET || [exception errNo] == EPIPE)) { @@ -569,14 +574,14 @@ } _firstLine = true; headers = _request.headers; - transferEncoding = [headers objectForKey: @"Transfer-Encoding"]; + chunked = [[headers objectForKey: @"Transfer-Encoding"] + isEqual: @"chunked"]; - if ([transferEncoding isEqual: @"chunked"] || - [headers objectForKey: @"Content-Length"] != nil) { + if (chunked || [headers objectForKey: @"Content-Length"] != nil) { stream.delegate = nil; OFStream *requestBody = [[[OFHTTPClientRequestBodyStream alloc] initWithHandler: self socket: (OFTCPSocket *)stream] autorelease]; @@ -723,11 +728,11 @@ transferEncoding = [headers objectForKey: @"Transfer-Encoding"]; _chunked = [transferEncoding isEqual: @"chunked"]; contentLengthString = [headers objectForKey: @"Content-Length"]; if (contentLengthString != nil) { - if (_chunked) + if (_chunked || contentLengthString.length == 0) @throw [OFInvalidArgumentException exception]; contentLength = contentLengthString.decimalValue; if (contentLength < 0) @@ -867,11 +872,11 @@ _chunked = [[headers objectForKey: @"Transfer-Encoding"] isEqual: @"chunked"]; contentLength = [headers objectForKey: @"Content-Length"]; if (contentLength != nil) { - if (_chunked) + if (_chunked || contentLength.length == 0) @throw [OFInvalidServerReplyException exception]; _hasContentLength = true; @try { @@ -1001,12 +1006,11 @@ @throw [OFInvalidServerReplyException exception]; } @try { - _toRead = line.hexadecimalValue; - if (_toRead < 0) + if ((_toRead = line.hexadecimalValue) < 0) @throw [OFOutOfRangeException exception]; } @catch (OFInvalidFormatException *e) { @throw [OFInvalidServerReplyException exception]; } Index: src/OFHTTPServer.m ================================================================== --- src/OFHTTPServer.m +++ src/OFHTTPServer.m @@ -413,11 +413,11 @@ OFString *contentLengthString = [_headers objectForKey: @"Content-Length"]; intmax_t contentLength = 0; if (contentLengthString != nil) { - if (chunked) + if (chunked || contentLengthString.length == 0) return [self sendErrorAndClose: 400]; @try { contentLength = contentLengthString.decimalValue; @@ -717,12 +717,11 @@ @throw [OFTruncatedDataException exception]; else @throw [OFInvalidFormatException exception]; } - _toRead = line.hexadecimalValue; - if (_toRead < 0) + if ((_toRead = line.hexadecimalValue) < 0) @throw [OFOutOfRangeException exception]; if (_toRead == 0) { _setAtEndOfStream = true; _toRead = -2;