swoole / swoole-src

🚀 Coroutine-based concurrency library for PHP
https://www.swoole.com
Apache License 2.0
18.47k stars 3.16k forks source link

when using http2 and SwooleResponse sendfile, Content-Type header is not respected #4492

Closed abnud1 closed 3 years ago

abnud1 commented 3 years ago
  1. What did you do? If possible, provide a simple script for reproducing the error. I have a javascript file already compressed using brotli at build time, I need to send it as a response with Content-Type: application/javascript, Content-Encoding: br.

This is the script I'm using:

#!/usr/bin/env php
<?php

use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Http\Server;

$server = new Server('0.0.0.0', 9503, SWOOLE_BASE);
$server->set(
    [
        'open_http2_protocol' => true,
    ]
);
$server->on(
    'request',
    function (Request $request, Response $response) {
        $response->header("content-type","application/javascript");
        $response->header("content-encoding","br");
        $response->sendfile("./test.js.br");
    }
);
$server->start(); 

consider test.js.br.zip which contains test.js.br inside just as an example.

  1. What did you expect to see? using curl -i --http2-prior-knowledge http://server:950 I would expect to see this output:
    
    HTTP/2 200 
    content-type: application/javascript
    content-encoding: br
    server: swoole-http-server
    date: Mon, 15 Nov 2021 20:41:13 GMT
    content-length: 53685

Warning: Binary output can mess up your terminal. Use "--output -" to tell Warning: curl to output it to your terminal anyway, or consider "--output Warning: " to save to a file.

3. What did you see instead?

HTTP/2 200 content-type: application/octet-stream content-encoding: br server: swoole-http-server date: Mon, 15 Nov 2021 20:41:13 GMT content-length: 53685

Warning: Binary output can mess up your terminal. Use "--output -" to tell Warning: curl to output it to your terminal anyway, or consider "--output Warning: " to save to a file.


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

swoole

Swoole => enabled Author => Swoole Team team@swoole.com Version => 4.8.1 Built => Nov 14 2021 09:07:25 coroutine => enabled with boost asm context epoll => enabled eventfd => enabled signalfd => enabled cpu_affinity => enabled spinlock => enabled rwlock => enabled openssl => OpenSSL 1.1.1l 24 Aug 2021 dtls => enabled http2 => enabled pcre => enabled zlib => 1.2.11 brotli => E16777225/D16777225 mutex_timedlock => enabled pthread_barrier => enabled futex => enabled async_redis => enabled

Directive => Local Value => Master Value swoole.enable_coroutine => On => On swoole.enable_library => On => On swoole.enable_preemptive_scheduler => Off => Off swoole.display_errors => On => On swoole.use_shortname => On => On swoole.unixsock_buffer_size => 8388608 => 8388608



5. What is your machine environment used (show your `uname -a` & `php -v` & `gcc -v`) ?
uname -a: `Linux 7607e75ef1a9 5.14.16-arch1-1 #1 SMP PREEMPT Tue, 02 Nov 2021 22:22:59 +0000 x86_64 GNU/Linux`(Arch Linux x64)
php -v: 8.0.12
gcc -v: 11.1.0
bitslip6 commented 10 months ago

This bug has regressed in 22.1.2 at least. Exact same issue, sendfile forces the content-type to the autodetected mime type

leocavalcante commented 10 months ago

@bitslip6, please note that v22.1.2 corresponds to the OpenSwoole fork https://github.com/openswoole/ext-openswoole/releases not this official Swoole repository.