Skip to content
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

Merged
merged 4 commits into from
Sep 10, 2019

Conversation

xaviered
Copy link
Collaborator

@xaviered xaviered commented Sep 9, 2019

Sending an APPEND command is a two-step process. The first POST request is sent with Content-Length: -1 header, which returns in a Bad Request response. Setting it to 0 fixes this issue.

$length = 0;
if (isset($options[CURLOPT_POSTFIELDS])) {
if (function_exists('mb_strlen')) {
$length = mb_strlen($options[CURLOPT_POSTFIELDS]);
Copy link
Owner

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 sweet find!

$length = strlen($options[CURLOPT_POSTFIELDS]);
}
}
$options[CURLOPT_HTTPHEADER] = array_merge($options[CURLOPT_HTTPHEADER], ['Content-Length: '.$length]);
Copy link
Owner

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.

Copy link
Collaborator Author

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!

Copy link
Owner

@michaelbutler michaelbutler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

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]);
Copy link
Owner

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 @.

Copy link
Collaborator Author

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

Copy link
Owner

@michaelbutler michaelbutler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@xaviered xaviered merged commit 83f2e8a into master Sep 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants