getRequestFormat() public static method

Here is the process to determine the format: * format defined by the user (with setRequestFormat()) * _format request attribute * $default
public static getRequestFormat ( string $default = 'html' ) : string
$default string The default format
return string The request format
 public function prepare(Request $request)
 {
     $headers = $this->headers;
     if ($this->isInformational() || in_array($this->statusCode, array(204, 304))) {
         $this->setContent(null);
     }
     // Content-type based on the Request
     if (!$headers->has('Content-Type')) {
         $format = $request->getRequestFormat();
         if (null !== $format && ($mimeType = $request->getMimeType($format))) {
             $headers->set('Content-Type', $mimeType);
         }
     }
     // Fix Content-Type
     $charset = $this->charset ?: 'UTF-8';
     if (!$headers->has('Content-Type')) {
         $headers->set('Content-Type', 'text/html; charset=' . $charset);
     } elseif (0 === strpos($headers->get('Content-Type'), 'text/') && false === strpos($headers->get('Content-Type'), 'charset')) {
         // add the charset
         $headers->set('Content-Type', $headers->get('Content-Type') . '; charset=' . $charset);
     }
     // Fix Content-Length
     if ($headers->has('Transfer-Encoding')) {
         $headers->remove('Content-Length');
     }
     if ('HEAD' === $request->getMethod()) {
         // cf. RFC2616 14.13
         $length = $headers->get('Content-Length');
         $this->setContent(null);
         if ($length) {
             $headers->set('Content-Length', $length);
         }
     }
     return $this;
 }
Beispiel #2
0
 /**
  * Prepares the Response before it is sent to the client.
  *
  * This method tweaks the Response to ensure that it is
  * compliant with RFC 2616. Most of the changes are based on
  * the Request that is "associated" with this Response.
  *
  * @param Request $request A Request instance
  *
  * @return Response The current response.
  */
 public function prepare(Request $request)
 {
     $headers = $this->headers;
     if ($this->isInformational() || in_array($this->statusCode, array(204, 304))) {
         $this->setContent(null);
         $headers->remove('Content-Type');
         $headers->remove('Content-Length');
     } else {
         // Content-type based on the Request
         if (!$headers->has('Content-Type')) {
             $format = $request->getRequestFormat();
             if (null !== $format && ($mimeType = $request->getMimeType($format))) {
                 $headers->set('Content-Type', $mimeType);
             }
         }
         // Fix Content-Type
         $charset = $this->charset ?: 'UTF-8';
         if (!$headers->has('Content-Type')) {
             $headers->set('Content-Type', 'text/html; charset=' . $charset);
         } elseif (0 === stripos($headers->get('Content-Type'), 'text/') && false === stripos($headers->get('Content-Type'), 'charset')) {
             // add the charset
             $headers->set('Content-Type', $headers->get('Content-Type') . '; charset=' . $charset);
         }
         // Fix Content-Length
         if ($headers->has('Transfer-Encoding')) {
             $headers->remove('Content-Length');
         }
         if ($request->isMethod('HEAD')) {
             // cf. RFC2616 14.13
             $length = $headers->get('Content-Length');
             $this->setContent(null);
             if ($length) {
                 $headers->set('Content-Length', $length);
             }
         }
     }
     // Fix protocol
     if ('HTTP/1.0' != $request->server->get('SERVER_PROTOCOL')) {
         $this->setProtocolVersion('1.1');
     }
     // Check if we need to send extra expire info headers
     if ('1.0' == $this->getProtocolVersion() && 'no-cache' == $this->headers->get('Cache-Control')) {
         $this->headers->set('pragma', 'no-cache');
         $this->headers->set('expires', -1);
     }
     $this->ensureIEOverSSLCompatibility($request);
     return $this;
 }
    /**
     * Prepares the Response before it is sent to the client.
     *
     * This method tweaks the Response to ensure that it is
     * compliant with RFC 2616. Most of the changes are based on
     * the Request that is "associated" with this Response.
     *
     * @param Request $request A Request instance
     *
     * @return Response The current response.
     */
    public function prepare(Request $request)
    {
        $headers = $this->headers;

        if ($this->isInformational() || in_array($this->statusCode, array(204, 304))) {
            $this->setContent(null);
        }

        // Content-type based on the Request
        if (!$headers->has('Content-Type')) {
            $format = $request->getRequestFormat();
            if (null !== $format && $mimeType = $request->getMimeType($format)) {
                $headers->set('Content-Type', $mimeType);
            }
        }

        // Fix Content-Type
        $charset = $this->charset ?: 'UTF-8';
        if (!$headers->has('Content-Type')) {
            $headers->set('Content-Type', 'text/html; charset='.$charset);
        } elseif (0 === strpos($headers->get('Content-Type'), 'text/') && false === strpos($headers->get('Content-Type'), 'charset')) {
            // add the charset
            $headers->set('Content-Type', $headers->get('Content-Type').'; charset='.$charset);
        }

        // Fix Content-Length
        if ($headers->has('Transfer-Encoding')) {
            $headers->remove('Content-Length');
        }

        if ($request->isMethod('HEAD')) {
            // cf. RFC2616 14.13
            $length = $headers->get('Content-Length');
            $this->setContent(null);
            if ($length) {
                $headers->set('Content-Length', $length);
            }
        }

        // Fix protocol
        if ('HTTP/1.0' != $request->server->get('SERVER_PROTOCOL')) {
            $this->setProtocolVersion('1.1');
        }

        // Check if we need to send extra expire info headers
        if ('1.0' == $this->getProtocolVersion() && 'no-cache' == $this->headers->get('Cache-Control')) {
            $this->headers->set('pragma', 'no-cache');
            $this->headers->set('expires', -1);
        }

        /**
         * Check if we need to remove Cache-Control for ssl encrypted downloads when using IE < 9
         * @link http://support.microsoft.com/kb/323308
         */
        if (false !== stripos($this->headers->get('Content-Disposition'), 'attachment') && preg_match('/MSIE (.*?);/i', $request->server->get('HTTP_USER_AGENT'), $match) == 1 && true === $request->isSecure()) {
            if(intval(preg_replace("/(MSIE )(.*?);/", "$2", $match[0])) < 9) {
                $this->headers->remove('Cache-Control');
            }
        }

        return $this;
    }
Beispiel #4
0
 function testGetRequestFormat()
 {
     $this->assertEqual(Request::getRequestFormat("/prova/"), "php", "Il formato della richiesta non e' 'php' !!");
     $this->assertEqual(Request::getRequestFormat("/"), "php", "Il formato della richiesta non e' 'php' !!");
     $this->assertEqual(Request::getRequestFormat("/prova/ugh/ciao.xml"), "xml", "Il formato della richiesta non e' 'xml' !!");
 }