contentLength default to -1 if not present (#19835)

* contentLength default to -1 if not present

* `httpclient.contentLength` changelog
This commit is contained in:
Bung 2022-09-22 06:01:22 +08:00 • committed by GitHub
commit de089d7fdb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 2 deletions

View file

@ -2,6 +2,7 @@
## Changes affecting backward compatibility ## Changes affecting backward compatibility
- `httpclient.contentLength` default to `-1` if the Content-Length header is not set in the response, it followed Apache HttpClient(Java), http(go) and .Net HttpWebResponse(C#) behavior. Previously raise `ValueError`.
- `addr` is now available for all addressable locations, - `addr` is now available for all addressable locations,
`unsafeAddr` is now deprecated and an alias for `addr`. `unsafeAddr` is now deprecated and an alias for `addr`.

View file

@ -274,9 +274,9 @@ proc contentLength*(response: Response | AsyncResponse): int =
## This is effectively the value of the "Content-Length" header. ## This is effectively the value of the "Content-Length" header.
## ##
## A `ValueError` exception will be raised if the value is not an integer. ## A `ValueError` exception will be raised if the value is not an integer.
var contentLengthHeader = response.headers.getOrDefault("Content-Length") ## If the Content-Length header is not set in the response, ContentLength is set to the value -1.
var contentLengthHeader = response.headers.getOrDefault("Content-Length", @["-1"])
result = contentLengthHeader.parseInt() result = contentLengthHeader.parseInt()
doAssert(result >= 0 and result <= high(int32))
proc lastModified*(response: Response | AsyncResponse): DateTime = proc lastModified*(response: Response | AsyncResponse): DateTime =
## Retrieves the specified response's last modified time. ## Retrieves the specified response's last modified time.