leafsphp / http

📡 Leaf Http module
https://leafphp.dev/modules/http/
7 stars 9 forks source link

Request header null #9

Closed swimitup closed 1 year ago

swimitup commented 1 year ago

The method input in http\Request class is dealing with null for Headers::get('Content-Type'): To fix this, I've applied a random default value other than null, because there's a straps comparison which will give a fatal error:

  public static function input($safeData = true)
  {
    $handler = fopen('php://input', 'r');
    $data = stream_get_contents($handler);

   // ------ my change
   // ----- give a default value for Headers::get('Content-Type')
    $content_type = Headers::get('Content-Type') ?? '';
   // ----- end change, substitute variables in this method
    if ($content_type === 'application/x-www-form-urlencoded') {
      $d = $data;
      $data = [];

      foreach (explode('&', $d) as $chunk) {
        $param = explode('=', $chunk);
        $data[$param[0]] = $param[1];
      }
    } else if (strpos($content_type ?? '', 'application/json') !== 0 && strpos($content_type, 'multipart/form-data') !== 0) {
      $safeData = false;
      $data = [$data];
    } else {
      if (!$data) {
        $data = json_encode([]);
      }

      $parsedData = json_decode($data, true);
      $data = is_array($parsedData) ? $parsedData : [$parsedData];
    }

    return $safeData ? \Leaf\Anchor::sanitize($data) : $data;
  }
mychidarko commented 1 year ago

Hey @swimitup thanks, I'll check this out

mychidarko commented 1 year ago

Fixed, will be published in next release