キャッシュとCURLライブラリーの修正

守矢諏訪子 <suwako@076.moe>、2026-05-22 16:56
コミット b27063691f734f377bf37292a0bb3974e5d4667b ・親 83e251c ・ファイルを見る

3個のファイル、+39 -45

@@ -47,7 +47,7 @@
4747 protected int $dataDuration = 1800; // 30分
4848
4949 public function __construct(string $dataDir) {
50- $this->dataDir = ROOT.'/data/cache/openprovider/'.$dataDir.'/';
50+ $this->dataDir = ROOT.'/data/cache/'.$dataDir.'/';
5151 }
5252
5353 /**
@@ -409,33 +409,22 @@
409409 $request .= "{$accept}\r\n";
410410 $request .= "Connection: close\r\n";
411411
412+ // 認証ヘッダー
412413 if (!empty($authHeader)) {
413414 $request .= $authHeader;
414415 }
415416
416417 // ヘッダーを追加
417- foreach ($this->headers as $name => $value) {
418- $request .= "{$name}: {$value}\r\n";
418+ $ext = $this->buildHeaderString();
419+ if (!empty($ext)) {
420+ $request .= $ext;
419421 }
420422
421- // リファラーが設定されていれば追加
422- if (!empty($this->referer) && !isset($this->headers['Referer'])) {
423- $request .= "Referer: {$this->referer}\r\n";
424- }
425-
426- // クッキーヘッダーを追加
427- if (!empty($this->cookies) && !isset($this->headers['Cookie'])) {
428- $cookieStrings = [];
429- foreach ($this->cookies as $name => $value) {
430- $cookieStrings[] = $name.'='.urlencode($value);
431- }
432- $request .= 'Cookie: '.implode('; ', $cookieStrings)."\r\n";
433- }
434-
423+ // ヘッダー終了
435424 $request .= "\r\n";
436425
437426 // POSTデータを追加
438- if ($method === 'POST' || $method === 'PUT') {
427+ if ($httpData !== '') {
439428 $request .= $httpData;
440429 }
441430
@@ -507,6 +496,7 @@
507496 // ヘッダーを解析
508497 $this->responseHeaders = [];
509498 $this->responseCode = 0;
499+ $redirectUrl = '';
510500
511501 foreach ($headerLines as $index => $header) {
512502 if ($index === 0) {
@@ -520,6 +510,8 @@
520510 $name = trim($name);
521511 $value = trim($value);
522512 $this->responseHeaders[$name] = $value;
513+
514+ $redirectUrl = $this->checkReds($name, $value, $currentUrl);
523515 }
524516 }
525517
@@ -541,20 +533,23 @@
541533 }
542534
543535 // リダイレクトが必要な場合
544- if (!empty($redirectUrl) && $redirectCount < $this->maxRedirects) {
536+ if ($redirectUrl !== '' && $redirectCount < $this->maxRedirects) {
545537 $currentUrl = $redirectUrl;
546538 $redirectCount++;
547539 $this->info['redirect_count'] = $redirectCount;
540+ $this->info['redirect_url'] = $redirectUrl;
548541
549542 // 302や303リダイレクトはGETにメソッドを変更
550- if ($this->responseCode == 302 || $this->responseCode == 303) {
543+ if (in_array($this->responseCode, [302, 303], true)) {
551544 $this->method = 'GET';
552545 $this->postRaw = '';
553546 $this->postFields = [];
554547 }
555- } else {
556- break;
548+
549+ continue;
557550 }
551+
552+ break;
558553 } while (true);
559554
560555 // リクエスト完了後、元のメソッドに戻す
@@ -629,22 +624,20 @@
629624 * @return string リダイレクトURL、リダイレクトがない場合は空文字
630625 */
631626 private function checkReds(string $name, string $value, string $currentUrl): string {
632- $redirectUrl = '';
627+ if (!$this->followRedirects) return '';
628+ if (strtolower($name) !== 'location') return '';
629+ if ($this->responseCode < 300 || $this->responseCode >= 400) return '';
630+ $redirectUrl = $value;
633631
634- if ($this->followRedirects && (strtolower($name) === 'location'
635- && $this->responseCode >= 300 && $this->responseCode < 400)) {
636- $redirectUrl = $value;
637-
638- if (strpos($redirectUrl, 'http') !== 0) {
639- if ($redirectUrl[0] === '/') {
640- $uri = new Uri($currentUrl);
641- $redirectUrl = $uri->getScheme().'://'.$uri->getHost()
642- .(NULL !== $uri->getPort() ? ':'.$uri->getPort() : '')
643- .$redirectUrl;
644- } else {
645- $redirectUrl = dirname($currentUrl).'/'.$redirectUrl;
646- }
647- }
632+ if (strpos($redirectUrl, 'http') !== 0) {
633+ if ($redirectUrl[0] === '/') {
634+ $uri = new Uri($currentUrl);
635+ $redirectUrl = $uri->getScheme().'://'.$uri->getHost()
636+ .(NULL !== $uri->getPort() ? ':'.$uri->getPort() : '')
637+ .$redirectUrl;
638+ } else {
639+ $redirectUrl = dirname($currentUrl).'/'.$redirectUrl;
640+ }
648641 }
649642
650643 return $redirectUrl;
@@ -656,16 +649,16 @@
656649 * @return string 構築されたヘッダー文字列
657650 */
658651 private function buildHeaderString(): string {
659- $headers = [];
652+ $request = '';
660653
661654 // ユーザー指定のヘッダーを追加
662655 foreach ($this->headers as $name => $value) {
663- $headers[] = "{$name}: {$value}";
656+ $request .= "{$name}: {$value}\r\n";
664657 }
665658
666659 // リファラーが設定されていれば追加
667660 if (!empty($this->referer) && !isset($this->headers['Referer'])) {
668- $headers[] = "Referer: {$this->referer}";
661+ $request .= "Referer: {$this->referer}\r\n";
669662 }
670663
671664 // 必要に応じてクッキーヘッダーを追加
@@ -675,10 +668,10 @@
675668 $cookieStrings[] = $name.'='.urlencode($value);
676669 }
677670
678- $headers[] = 'Cookie: '.implode('; ', $cookieStrings);
671+ $request .= 'Cookie: '.implode('; ', $cookieStrings)."\r\n";
679672 }
680673
681- return implode("\r\n", $headers)."\r\n";
674+ return $request;
682675 }
683676
684677 /**
@@ -52,9 +52,9 @@
5252 private string $ip = '';
5353 private int $resellerId = 0;
5454 private int $lastAuth = 0;
55- private string $BASEURL = 'https://api.openprovider.eu/v1beta';
56- // private string $BASEURL = DEBUG_MODE ? 'http://api.sandbox.openprovider.nl:8480/v1beta'
57- // : 'https://api.openprovider.eu/v1beta';
55+ // private string $BASEURL = 'https://api.openprovider.eu/v1beta';
56+ private string $BASEURL = DEBUG_MODE ? 'http://api.sandbox.openprovider.nl:8480/v1beta'
57+ : 'https://api.openprovider.eu/v1beta';
5858
5959 public function __construct() {
6060 if (!OPENPROVIDER_ENABLED) return;
@@ -107,6 +107,7 @@
107107 if ($curl->getResponseCode() != 200) {
108108 $err = json_decode($body, true);
109109 assert_not_null($err, "エラーの受け取りに失敗。");
110+ // kys($body);
110111 assert($curl->getResponseCode() == 200, $err['desc']);
111112 return Result::Error();
112113 }