-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Auto-adding content-length heading to fix append-command bug #21
Conversation
src/org/apache/hadoop/tools/Curl.php
Outdated
$length = 0; | ||
if (isset($options[CURLOPT_POSTFIELDS])) { | ||
if (function_exists('mb_strlen')) { | ||
$length = mb_strlen($options[CURLOPT_POSTFIELDS]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Content-Length
HTTP header should be a pure binary character length of the body, aka raw bytes, not a human readable length. The former would be always calculated using strlen
, and the latter would be mb_strlen
. Let's use strlen
always here.
Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 sweet find!
src/org/apache/hadoop/tools/Curl.php
Outdated
$length = strlen($options[CURLOPT_POSTFIELDS]); | ||
} | ||
} | ||
$options[CURLOPT_HTTPHEADER] = array_merge($options[CURLOPT_HTTPHEADER], ['Content-Length: '.$length]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: spacing around .
char? Not sure what the code style of this repo is though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, I missed it. thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
src/org/apache/hadoop/tools/Curl.php
Outdated
if (!$has_content_length_header && !isset($options[CURLOPT_INFILE]) && !isset($options[CURLOPT_INFILESIZE])) { | ||
$length = 0; | ||
if (isset($options[CURLOPT_POSTFIELDS])) { | ||
$length = strlen($options[CURLOPT_POSTFIELDS]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually one more possible caveat; it looks like CURLOPT_POSTFIELDS
could be a @
prefixed string which implies a file name that is then read in (deprecated in PHP 5.5 though) or it could even be a key/value array, on which strlen
will fail. To be safe better also check if is_string
and that the string does not begin with @
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes, this library is >= php5.4.0 ... will make that change now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Sending an
APPEND
command is a two-step process. The first POST request is sent withContent-Length: -1
header, which returns in aBad Request
response. Setting it to0
fixes this issue.