Skip to content

Bug: No default HTTP protocol version is set when creating a new Response #2383

@hcbd

Description

@hcbd

Describe the bug
No default HTTP protocol version is set when you create a new response object with
$request = new Response( \Config\App() );

CodeIgniter 4 version
4.0.0-rc3

Affected module(s)
HTTP\Response

Expected behavior
A default HTTP protocol version is set and outputted

Output:
HTTP/1.1 200 Found <rest of response>

Currently it outputs:
HTTP/ 200 Found <rest of response>

Reproduce

  1. Create your own filter
<?php namespace App\Filters;

use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\HTTP\Response;

class My_Filter_Name implements FilterInterface
{
  function before( RequestInterface $request )
  {
    // sends a response because it does not like the json provided for example
    $response = new Response( \Config\App() );
    $response->setHeader('X-Framework', 'CI4');
    return $response;  
  }
  function after( RequestInterface $request, ResponseInterface $response ) {return;}
}
  1. Create alias and add it to an route

  2. Invoke filter by opening the route in the browser

  3. You get the raw data string from the server; beginning with

HTTP/ 200 Found
Host: localhost:8080
Date: Sat, 02 Nov 2019 17:34:58 GMT
........

Workaround
set the protocol version in your filter:
$response->setProtocolVersion(1.1);

Fix/Patch
A fix is pretty simple, by editing the following line:

header(sprintf('HTTP/%s %s %s', $this->protocolVersion, $this->statusCode, $this->reason), true, $this->statusCode);

Replace
$this->protocolVersion
with
$this->getProtocolVersion()

header(sprintf('HTTP/%s %s %s', $this->getProtocolVersion(), $this->statusCode, $this->reason), true, , $this->statusCode);

The getProtocolVersion() function defaults to 1.1

public function getProtocolVersion(): string

Context

  • OS: Fedora
  • Web server: PHP buildt-in Webserver
  • PHP version 7.3.10

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions