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

Add proper Content-Length based off of the request body's length #85

Merged
merged 1 commit into from
Jul 3, 2013
Merged

Add proper Content-Length based off of the request body's length #85

merged 1 commit into from
Jul 3, 2013

Conversation

hello-josh
Copy link
Contributor

Make sure to use serialized_payload length as Content-Length if the request has a body

Make sure to use serialized_payload length as Content-Length if the
request has a body
@hello-josh hello-josh mentioned this pull request Jul 3, 2013
if (isset($this->payload)) {
$this->serialized_payload = $this->_serializePayload($this->payload);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->serialized_payload);
$this->headers['Content-Length'] = strlen($this->serialized_payload);
Copy link
Owner

Choose a reason for hiding this comment

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

Haven't checked the HTTP spec yet, but should this be multi byte string length mb_strlen?

Copy link
Owner

Choose a reason for hiding this comment

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

Nope, I think you are correct. We want strlen even for multibyte characters.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct. It is the size in bytes not number of characters.
On Jul 3, 2013 12:00 PM, "Nate Good" [email protected] wrote:

In src/Httpful/Request.php:

@@ -784,6 +784,14 @@ public function _curlPrep()
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->strict_ssl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  •    // https://github.com/nategood/httpful/issues/84
    
  •    // set Content-Length to the size of the payload if present
    
  •    if (isset($this->payload)) {
    
  •        $this->serialized_payload = $this->_serializePayload($this->payload);
    
  •        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->serialized_payload);
    
  •        $this->headers['Content-Length'] = strlen($this->serialized_payload);
    

Nope, I think you are correct. We want strlen even for multibyte
characters.


Reply to this email directly or view it on GitHubhttps://github.com//pull/85/files#r5012797
.

Copy link
Owner

Choose a reason for hiding this comment

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

Yep. Silly question. Brain is moving slow this morning.

Choose a reason for hiding this comment

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

Result:
string 'test' (length=4)
int 4
int 4
int 4
string 'тест' (length=8)
int 4
int 4
int 8

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That fix is incorrect:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.13

More specifically: Applications SHOULD use this field to indicate the
transfer-length of the message-body, unless this is prohibited by the rules
in section 4.4.

On Fri, Sep 13, 2013 at 11:12 AM, wackyfrog [email protected]:

In src/Httpful/Request.php:

@@ -784,6 +784,14 @@ public function _curlPrep()
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->strict_ssl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  •    // https://github.com/nategood/httpful/issues/84
    
  •    // set Content-Length to the size of the payload if present
    
  •    if (isset($this->payload)) {
    
  •        $this->serialized_payload = $this->_serializePayload($this->payload);
    
  •        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->serialized_payload);
    
  •        $this->headers['Content-Length'] = strlen($this->serialized_payload);
    

Result:
string 'test' (length=4)
int 4
int 4
int 4
string 'тест' (length=8)
int 4
int 4
int 8

Reply to this email directly or view it on GitHubhttps://github.com//pull/85/files#r6349942
.

@racingDeveloper
Copy link
Contributor

It looks correct to me! It should contains the number of OCTETs (8bit), so the number of bytes, not chars!

nategood added a commit that referenced this pull request Jul 3, 2013
Add proper Content-Length based off of the request body's length
@nategood nategood merged commit fe72af0 into nategood:master Jul 3, 2013
@racingDeveloper
Copy link
Contributor

Hi!

Could you update it on packagist?
Composer still download the old version to me :-(

@nategood
Copy link
Owner

nategood commented Jul 5, 2013

Sorry about that. Forgot the version bump in composer.json. Try 0.2.6 via
composer/packagist.

On Thu, Jul 4, 2013 at 7:04 AM, Gabriele Tondi [email protected]:

Hi!

Could you update it on packagist?
Composer still download the old version to me :-(


Reply to this email directly or view it on GitHubhttps://github.com//pull/85#issuecomment-20471491
.


Nate Good
Director of Software Engineering
ShowClix, Inc.

Ph: 412.259.5869
Fax: 724.261.5332
http://www.showclix.com

@hello-josh hello-josh deleted the proper-content-length branch July 5, 2013 15:53
@hello-josh
Copy link
Contributor Author

https://packagist.org/packages/nategood/httpful is still showing dev-master, dev-dev, and 0.2.5 as the latest versions even though composer.json shows 0.2.6 as the version info. Do you have to force packagist to update?

@nategood
Copy link
Owner

nategood commented Jul 5, 2013

Okay figured it out. My git tag and composer.json update weren't aligned.
Packagist didn't like that. Fixed and confirmed.

On Fri, Jul 5, 2013 at 11:59 AM, Josh Johnston [email protected]:

https://packagist.org/packages/nategood/httpful is still showing
dev-master, dev-dev, and 0.2.5 as the latest versions even though
composer.json shows 0.2.6 as the version info. Do you have to force
packagist to update?


Reply to this email directly or view it on GitHubhttps://github.com//pull/85#issuecomment-20526069
.


Nate Good
Director of Software Engineering
ShowClix, Inc.

Ph: 412.259.5869
Fax: 724.261.5332
http://www.showclix.com

@wackyfrog
Copy link

Wrong length for mutlibyte body (utf-8). So, form submits incorrect.

@hello-josh
Copy link
Contributor Author

Are you sure? Content length should be the number of bytes not the number
of characters. Double check the receiving end.
On Sep 13, 2013 5:08 AM, "wackyfrog" [email protected] wrote:

Wrong length for mutlibyte body (utf-8). So, form submits incorrect.


Reply to this email directly or view it on GitHubhttps://github.com//pull/85#issuecomment-24382090
.

@wackyfrog
Copy link

I'm sure.
with ini param 'mbstring.func_overload' = 2 (overload strlen, http://php.net/manual/en/mbstring.overload.php)

Test example:

Result:
string 'test' (length=4)
int 4
int 4
int 4
string 'тест' (length=8)
int 4
int 4
int 8

So, header 'content-length' contains invalid length with strlen (shorter)

@nategood
Copy link
Owner

Ah. Yes. This makes sense if mbstring.func_overload = 2 is set. In that case it will be dependent your mb_internal_encoding default too. We may want to be explicit and use mb_strlen($var, '8bit') for users that are overloading strlen. Agreement?

@hello-josh
Copy link
Contributor Author

I misunderstood the problem. I thought you were suggesting that it should be sending the number of characters. Disregard my previous response(s).

I agree, the mb_strlen($var, '8bit') is the way to go. I completely overlooked the fact that you can overload strlen()

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.

4 participants