Content-Length maximum allowed length

2019-08-15 03:36发布

问题:

I'm programming in ObjectiveC. I guess that all oC programmers are using the rather standard code for posting:

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

Currently I'm getting warning that NSUInteger should not be used as format argument. The warning suggest a type cast (unsigned long). And also replacing %d with %lu. That sounds logical.

In the HTTP definition, I can not find how long the Content-Length may actually be. Just the rather vague Octet. Is that 8 bits? I can not find the answer!

回答1:

There is no specific limit on the maximum value for the Content-Length.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

14.13 Content-Length
Any Content-Length greater than or equal to zero is a valid value.

Also the Apple documentation for setValue:forHTTPHeaderField: in the NSMutableURLRequest class reference states

Additionally, if the length of your upload body data can be determined automatically (for example, if you provide the body content with an NSData object), then the value of Content-Length is set for you.



回答2:

This rfc sucks because it says that content-length must be a "decimal number" but it also says that it's a number of octets (so it can't be a decimal).

It depends on your implementation. For example in Java, max content-length is Integer.MAX_VALUE but, you can bypass it by reading the header directly (HTTP headers are strings).

There is no real HTTP limitation there.