6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HttpSocketResponseのHTTP Statusが0になる

Posted at

環境

PHP: 5.3.3(RHEL)
CakePHP: 2.4.10

HTTP Statusが0になる

HTTPステータスが200のときは問題ないが、reasonPhraseが二個以上の単語で返ってくると、codeが設定されず0になる現象があります。

例えば、以下のようなレスポンスがあったとします。

HTTP/1.1 503 Service Temporarily Unavailable\r\n

HttpSockeResponseは以下のようになってしまいます。
Versoinが正しく設定されているのは、デフォルト文字列のようです。

[httpVersion] => HTTP/1.1
[code] => 0
[reasonPhrase] => 

どうも、ここのpreg_matchがまずいのではないだろうか。

lib/cake/Network/Http/HttpSocketResponse.php:L155

HttpSocketResponse.php
if (preg_match("/(.+) ([0-9]{3})\s*([^ ]*)\r\n/DU", $statusLine, $match)) {
  $response->httpVersion = $match[1];
  $response->code = $match[2];
  $response->reasonPhrase = $match[3];
}

以下のように修正してやると、正しく設定されます。

HttpSocketResponse.php
if (preg_match("/(.+) ([0-9]{3})\s*([^ ]+.*)\r\n/DU", $statusLine, $match) {
  $response->httpVersion = $match[1];
  $response->code = $match[2];
  $response->reasonPhrase = $match[3];
}
6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?