Magento duplicate header ‘Content-Type’ FastCGI: comm with server “xxxxxxx” aborted – error parsing headers

I am recently experiencing an HTTP 500 Internal Server Error while browsing Magento Enterprise Edition v1.12.0.2 (latest by the time of writing this post). a “duplicate header” error message was found in Apache error log.

I am using Apache 2.2 + mod_fastcgi + php-fpm 5.3.14, the apache error log shows following:

[Sun Jul 15 21:39:11 2012] [error] [client 192.168.2.1] FastCGI: comm with server “/var/www/fcgi-bin/php5.fcgi” aborted: error parsing headers: duplicate header ‘Content-Type’

And it only happens if I enabled Magento EE’s Full Page Cache. This is really frustrating. It seems it’s common previous while accessing SOAP APIs, none of the case I could find over internet could address this problem.

Luckily, I found this post: http://www.magentocommerce.com/boards/viewreply/383462/

It’s working like charm after replacing the sendHeaders() method in app/code/core/Mage/Core/Controller/Response/Http.php

public function sendHeaders()
    {
        if (!$this->canSendHeaders()) {
            Mage::log('HEADERS ALREADY SENT: '.mageDebugBacktrace(true, true, true));
            return $this;
        }
        if (in_array(substr(php_sapi_name(), 0, 3), array('cgi', 'fpm')))
        {
            // remove duplicate headers
            $remove = array('status', 'content-type');

            // already sent headers
            $sent = array();
            foreach (headers_list() as $header)
            {
                // parse name
                if (!$pos = strpos($header, ':'))
                        continue;
                $sent[strtolower(substr($header, 0, $pos))] = true;
            }

            // raw headers
            $headersRaw = array();
            foreach ($this->_headersRaw as $i=>$header)
            {
                // parse name
                if (!$pos = strpos($header, ':'))
                        continue;
                $name = strtolower(substr($header, 0, $pos));

                if (in_array($name, $remove))
                {
                        // check sent headers
                        if ($sent[$name])
                        {
                            unset($this->_headersRaw[$i]);
                            continue;
                        }

                        // check header
                        if (!is_null($existing = $headers[$name]))
                        {
                            $this->_headersRaw[$existing] = $header;
                            unset($this->_headersRaw[$i]);
                        }
                        else
                            $headersRaw[$name] = $i;
                }
            }

            // object headers
            $headers = array();
            foreach ($this->_headers as $i=>$header)
            {
                $name = strtolower($header['name']);
                if (in_array($name, $remove))
                {
                        // check sent headers
                        if ($sent[$name])
                        {
                            unset($this->_headers[$i]);
                            continue;
                        }

                        // check header
                        if (!is_null($existing = $headers[$name]))
                        {
                            $this->_headers[$existing] = $header;
                            unset($this->_headers[$i]);
                        }
                        else
                            $headers[$name] = $i;

                        // check raw headers
                        if (!is_null($existing = $headersRaw[$name]))
                            unset($this->_headersRaw[$existing]);
                }
            }
        }

        parent::sendHeaders();
    }
Posted in PHP
2 comments on “Magento duplicate header ‘Content-Type’ FastCGI: comm with server “xxxxxxx” aborted – error parsing headers
  1. Simon says:

    Danke, hat mir sehr geholfen!

    Aber ich denke, im “raw headers” Teil sollte es
    if (!is_null($existing = $headersRaw[$name]))
    statt
    if (!is_null($existing = $headers[$name]))
    heißen. $headers ist an der Stelle gar nicht definiert.

  2. Stephanie says:

    Just FYI… this did not resolve the issue for me. However, I implemented this module and is corrected the issue in my case. https://github.com/MageGyver/ApiImprovements

Leave a Reply

Your email address will not be published. Required fields are marked *

*