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

Set Content-Length for HEAD request manually #2690

Closed
yueyuzhao opened this issue Jul 15, 2019 · 5 comments
Closed

Set Content-Length for HEAD request manually #2690

yueyuzhao opened this issue Jul 15, 2019 · 5 comments

Comments

@yueyuzhao
Copy link

Hello, thanks for this amazing project.
But I'm making a HEAD request to check the Content-Length field in the headers, but swoole won't accept Content-Length field. I don't want to flush the full content to swoole or I don‘t have the content but I do have the content length. How can I do that?

Please answer these questions before submitting your issue. Thanks!

  1. What did you do? If possible, provide a simple script for reproducing the error.
<?php

$http = new swoole_http_server("127.0.0.1", 1337, SWOOLE_BASE);

$http->set([
    'worker_num' => 4,
]);

$data = json_encode([
  'code' => 'ok',
  'error' => false,
  'payload' => 'Hello World'
]);

$http->on('request', function ($req, swoole_http_response $resp) use ($data) {
    $resp->header('Content-Type', 'application/json');
    if ($req->server['request_method'] == 'HEAD') {
        $resp->header('Content-Length', strlen($data));
        $resp->end();
        return;
    }
    $resp->end($data);
});

$http->start();
$ curl -I http://127.0.0.1:3737/
  1. What did you expect to see?

Content-Length: 51

  1. What did you see instead?

Content-Length: 0

  1. What version of Swoole are you using (show your php --ri swoole)?

4.4.1-alpha

  1. What is your machine environment used (including version of kernel & php & gcc) ?

PHP 7.2.16

@yueyuzhao
Copy link
Author

From #958 , I know that swoole would set Content-Length automatically.

@twose
Copy link
Member

twose commented Jul 15, 2019

This does not conform to the HTTP RFC

@yueyuzhao
Copy link
Author

yueyuzhao commented Jul 15, 2019

From the manual, one way I can come up with:

$http->on('request', function ($req, swoole_http_response $resp) use ($http, $data) {
    $resp->header('Content-Type', 'application/json');
    if ($req->server['request_method'] == 'HEAD') {
        $resp->detach();
        $length = strlen($data);
        $http->send($resp->fd, "HTTP/1.1 200 OK\r\nContent-Length: {$length}\r\nServer: server\r\n\r\n");
        return;
    }
    $resp->end($data);
});

But is there a nicer way?

@twose
Copy link
Member

twose commented Jul 15, 2019

sorry, I didn't notice this was a HEAD request, we need to support it

@yueyuzhao
Copy link
Author

@twose Many thanks for your quick response!

springleng referenced this issue in hyperf/hyperf Jul 16, 2019
Fixed proxy of guzzle client does not work expected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants