You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is a subtle bug in the above function. It disregards any content-length info passed to the make_raw function and adds its own content-length header that "hides" the one given by the caller. This is a problem if I write the call like so:
Since I give an empty body and encode the size of the resource in the defined header, I expect the response to contain "10" for the content-length header, however in the current implementation is will be "0". This is because make_raw is implemented as:
. It unconditionally adds another duplicated content-length header with the wrong value. This is bad because the implementation of Header.set prepends the new wrong header value at the head of the list like so:
An unsuspecting user like myself will run the following code on the response header expecting to get the correct content-length:
matchList.assoc_opt "content-length" headers with|Somel -> int_of_string l
|None -> ...
In the above snippet I expect l to be "10" but its "0".
I think it's best to first check if ?headers parameter contains a content-length value before adding one inside the make_raw function to avoid this subtle bug. This is especially important when writing a HEAD method handler like so:
where I don't have access to the resource contents and only have its size via VFS.file_size. To workaround this i'm forced to basically return a dummy body of length l just to make this implementation of make_raw return a sensible result.
Let me know what you think is the best way to move forward.
The text was updated successfully, but these errors were encountered:
I don't think it's a bug, because this function also defines the body to "". The content length *must* match the body's actual length.
If your use case requires more control over the body, I'd suggest one of the streaming body functions? You also wouldn't set the content length but it'd use chunked encoding to allow you to write the body as you produce it.
There is a subtle bug in the above function. It disregards any content-length info passed to the
make_raw
function and adds its own content-length header that "hides" the one given by the caller. This is a problem if I write the call like so:Since I give an empty body and encode the size of the resource in the defined header, I expect the response to contain "10" for the content-length header, however in the current implementation is will be "0". This is because
make_raw
is implemented as:tiny_httpd/src/core/response.ml
Lines 15 to 20 in b80c5f9
. It unconditionally adds another duplicated content-length header with the wrong value. This is bad because the implementation of
Header.set
prepends the new wrong header value at the head of the list like so:tiny_httpd/src/core/headers.ml
Lines 22 to 24 in b80c5f9
An unsuspecting user like myself will run the following code on the response header expecting to get the correct content-length:
In the above snippet I expect
l
to be"10"
but its"0"
.I think it's best to first check if
?headers
parameter contains a content-length value before adding one inside themake_raw
function to avoid this subtle bug. This is especially important when writing aHEAD
method handler like so:where I don't have access to the resource contents and only have its size via
VFS.file_size
. To workaround this i'm forced to basically return a dummy body of lengthl
just to make this implementation ofmake_raw
return a sensible result.Let me know what you think is the best way to move forward.
The text was updated successfully, but these errors were encountered: