]> git.mxchange.org Git - friendica.git/commitdiff
Move System::httpExit to BaseModule->httpExit
authorHypolite Petovan <hypolite@mrpetovan.com>
Thu, 21 Sep 2023 16:35:55 +0000 (12:35 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Sun, 24 Sep 2023 11:08:15 +0000 (07:08 -0400)
- This will ensure headers set in BaseModule->run will be carried in httpExit scenarios
- Deprecate httpExit() method in Core\System

24 files changed:
src/App/Page.php
src/BaseModule.php
src/Capabilities/ICanCreateResponses.php
src/Core/System.php
src/Module/Calendar/Event/API.php
src/Module/Calendar/Event/Show.php
src/Module/Contact/Hovercard.php
src/Module/DFRN/Poll.php
src/Module/Diaspora/Fetch.php
src/Module/Feed.php
src/Module/Item/Feed.php
src/Module/Media/Attachment/Browser.php
src/Module/Media/Photo/Browser.php
src/Module/Notifications/Ping.php
src/Module/OStatus/PubSub.php
src/Module/OpenSearch.php
src/Module/ParseUrl.php
src/Module/PermissionTooltip.php
src/Module/Post/Share.php
src/Module/PublicRSAKey.php
src/Module/ReallySimpleDiscovery.php
src/Module/Response.php
src/Module/WellKnown/HostMeta.php
src/Module/Xrd.php

index 364da32eb07bc35b2cd29a80228968586ce5b483..08efac627fe287cf2f9cdf359a1abc12c3a2422e 100644 (file)
@@ -496,7 +496,9 @@ class Page implements ArrayAccess
                        }
 
                        if ($_GET["mode"] == "raw") {
-                               System::httpExit(substr($target->saveHTML(), 6, -8), Response::TYPE_HTML);
+                               $response->withBody(Utils::streamFor($target->saveHTML()));
+                               System::echoResponse($response);
+                               System::exit();
                        }
                }
 
index 619095dfb6fbaf019c669c309cc07c6fece29ec9..d97cd60a9477c9fefb701cb70b6880ce879bdb82 100644 (file)
@@ -27,6 +27,7 @@ use Friendica\Capabilities\ICanCreateResponses;
 use Friendica\Core\Hook;
 use Friendica\Core\L10n;
 use Friendica\Core\Logger;
+use Friendica\Core\System;
 use Friendica\Model\User;
 use Friendica\Module\Response;
 use Friendica\Module\Special\HTTPException as ModuleHTTPException;
@@ -106,8 +107,7 @@ abstract class BaseModule implements ICanHandleRequests
         */
        protected function rawContent(array $request = [])
        {
-               // echo '';
-               // exit;
+               // $this->httpExit(...);
        }
 
        /**
@@ -234,7 +234,8 @@ abstract class BaseModule implements ICanHandleRequests
 
                $timestamp = microtime(true);
                // "rawContent" is especially meant for technical endpoints.
-               // This endpoint doesn't need any theme initialization or other comparable stuff.
+               // This endpoint doesn't need any theme initialization or
+               // templating and is expected to exit on its own if it is set.
                $this->rawContent($request);
 
                try {
@@ -456,4 +457,23 @@ abstract class BaseModule implements ICanHandleRequests
 
                return $tabs;
        }
+
+       /**
+        * This function adds the content and a content-type HTTP header to the output.
+        * After finishing the process is getting killed.
+        *
+        * @param string      $content
+        * @param string      $type
+        * @param string|null $content_type
+        * @return void
+        * @throws HTTPException\InternalServerErrorException
+        */
+       public function httpExit(string $content, string $type = Response::TYPE_HTML, ?string $content_type = null)
+       {
+               $this->response->setType($type, $content_type);
+               $this->response->addContent($content);
+               System::echoResponse($this->response->generate());
+
+               System::exit();
+       }
 }
index 300b1938aafd0e7e4f2afcad17475407bcdcf7dc..837e0cd8862baea8235803146faf1ef2e98f2306 100644 (file)
@@ -70,7 +70,7 @@ interface ICanCreateResponses
         *
         * @throws InternalServerErrorException
         */
-       public function setType(string $type, ?string $content_type = null): void;
+       public function setType(string $type = ICanCreateResponses::TYPE_HTML, ?string $content_type = null): void;
 
        /**
         * Sets the status and the reason for the response
index 5c2cd8dc12a952aa15e10877a5facd970cf71619..e1e84b775b4871ae27804ef4d8d6634c1083a227 100644 (file)
@@ -28,6 +28,7 @@ use Friendica\DI;
 use Friendica\Model\User;
 use Friendica\Module\Response;
 use Friendica\Network\HTTPException\FoundException;
+use Friendica\Network\HTTPException\InternalServerErrorException;
 use Friendica\Network\HTTPException\MovedPermanentlyException;
 use Friendica\Network\HTTPException\TemporaryRedirectException;
 use Friendica\Util\BasePath;
@@ -357,12 +358,15 @@ class System
         * This function adds the content and a content-type HTTP header to the output.
         * After finishing the process is getting killed.
         *
-        * @param string $content
-        * @param string $type
+        * @param string      $content
+        * @param string      $type
         * @param string|null $content_type
         * @return void
+        * @throws InternalServerErrorException
+        * @deprecated since 2023.09 Use BaseModule->httpExit() instead
         */
-       public static function httpExit(string $content, string $type = Response::TYPE_HTML, ?string $content_type = null) {
+       public static function httpExit(string $content, string $type = Response::TYPE_HTML, ?string $content_type = null)
+       {
                DI::apiResponse()->setType($type, $content_type);
                DI::apiResponse()->addContent($content);
                self::echoResponse(DI::apiResponse()->generate());
index e8fce8ed7e5b2abe3efb011d3cda1629e093fd81..af0c9fb8a65bdb41462ee9a6215978e199b38021 100644 (file)
@@ -183,7 +183,7 @@ class API extends BaseModule
 
                if (strcmp($finish, $start) < 0 && !$noFinish) {
                        if ($isPreview) {
-                               System::httpExit($this->t('Event can not end before it has started.'));
+                               $this->httpExit($this->t('Event can not end before it has started.'));
                        } else {
                                $this->sysMessages->addNotice($this->t('Event can not end before it has started.'));
                                $this->baseUrl->redirect($redirectOnError);
@@ -192,7 +192,7 @@ class API extends BaseModule
 
                if (empty($summary) || ($start === DBA::NULL_DATETIME)) {
                        if ($isPreview) {
-                               System::httpExit($this->t('Event title and start time are required.'));
+                               $this->httpExit($this->t('Event title and start time are required.'));
                        } else {
                                $this->sysMessages->addNotice($this->t('Event title and start time are required.'));
                                $this->baseUrl->redirect($redirectOnError);
@@ -251,7 +251,7 @@ class API extends BaseModule
                ];
 
                if (intval($request['preview'])) {
-                       System::httpExit(Event::getHTML($datarray));
+                       $this->httpExit(Event::getHTML($datarray));
                }
 
                $eventId = Event::store($datarray);
index b6a0bd56ccd2c872db7fe854c922d0f582670402..22c00d855fda64011e78efe45e433c38a84bd5d3 100644 (file)
@@ -81,6 +81,6 @@ class Show extends BaseModule
                        '$event' => $tplEvent,
                ]);
 
-               System::httpExit($o);
+               $this->httpExit($o);
        }
 }
index 4d58d994c283a4a473afea3c3fe5f408e920c4d6..9f2fb67bdbabec6917cc0e248d884e384800c740 100644 (file)
@@ -115,6 +115,6 @@ class Hovercard extends BaseModule
                        ],
                ]);
 
-               System::httpExit($o);
+               $this->httpExit($o);
        }
 }
index dbf91712fc07def07c36ee8c7ca6b7743f799049..3a8d370e9e6e56b2f86c7e8c9cbc2c63cacde5c5 100644 (file)
@@ -48,6 +48,6 @@ class Poll extends BaseModule
                }
 
                $last_update = $request['last_update'] ?? '';
-               System::httpExit(OStatus::feed($owner['nickname'], $last_update, 10) ?? '', Response::TYPE_ATOM);
+               $this->httpExit(OStatus::feed($owner['nickname'], $last_update, 10) ?? '', Response::TYPE_ATOM);
        }
 }
index 6e1259152f14b349415c5cb2b923e03121fbfdad..28b3d87ee307766b411f37cbad2cfc3ae4aadd03 100644 (file)
@@ -85,6 +85,6 @@ class Fetch extends BaseModule
                $xml = Diaspora::buildPostXml($status["type"], $status["message"]);
 
                // Send the envelope
-               System::httpExit(Diaspora::buildMagicEnvelope($xml, $user), Response::TYPE_XML, 'application/magic-envelope+xml');
+               $this->httpExit(Diaspora::buildMagicEnvelope($xml, $user), Response::TYPE_XML, 'application/magic-envelope+xml');
        }
 }
index 6d291198dcd09627e6fa60e984350c22ef38bc1b..b9d573979a721430eec0467b725635c5b0efb8b7 100644 (file)
@@ -71,6 +71,6 @@ class Feed extends BaseModule
 
                $feed = ProtocolFeed::atom($owner, $last_update, 10, $type);
 
-               System::httpExit($feed, Response::TYPE_ATOM);
+               $this->httpExit($feed, Response::TYPE_ATOM);
        }
 }
index 5cca0dfd63dce4fc4865ebcfa54bc2cabd6faa81..80a14e4bd4a85e3ee2377055b24af8a1cb685bed 100644 (file)
@@ -86,6 +86,6 @@ class Feed extends BaseModule
                        throw new HTTPException\InternalServerErrorException($this->t('The feed for this item is unavailable.', ['uri-id' => $uriId]));
                }
 
-               System::httpExit($xml, Response::TYPE_ATOM);
+               $this->httpExit($xml, Response::TYPE_ATOM);
        }
 }
index 80a87d5bde2bd34cf21896e655dc306486bd05e7..5ff070cf96774eb4bf8e04711818b513f25c276c 100644 (file)
@@ -80,7 +80,7 @@ class Browser extends BaseModule
                ]);
 
                if (empty($request['mode'])) {
-                       System::httpExit($output);
+                       $this->httpExit($output);
                }
 
                return $output;
index bf545d0db693c1b2bbd2d5be9e15dc34d4b4e48b..be0e931f907ed751a9cc5289946875457b38744c 100644 (file)
@@ -91,7 +91,7 @@ class Browser extends BaseModule
                ]);
 
                if (empty($request['mode'])) {
-                       System::httpExit($output);
+                       $this->httpExit($output);
                }
 
                return $output;
index 603d6408c7bde7c008f73772441ab63d15fd76e4..3b44bbe914354c3816a70f77ab77452965e43127 100644 (file)
@@ -303,7 +303,7 @@ class Ping extends BaseModule
 
                if (isset($_GET['callback'])) {
                        // JSONP support
-                       System::httpExit($_GET['callback'] . '(' . json_encode(['result' => $data]) . ')', Response::TYPE_BLANK, 'application/javascript');
+                       $this->httpExit($_GET['callback'] . '(' . json_encode(['result' => $data]) . ')', Response::TYPE_BLANK, 'application/javascript');
                } else {
                        System::jsonExit(['result' => $data]);
                }
index f1f3266cb81a4f5beb183bc403ffb66e4066b470..1271d85ebe5e4cd999de4116393b65e633b022f1 100644 (file)
@@ -155,6 +155,6 @@ class PubSub extends \Friendica\BaseModule
                        $this->logger->notice('Success for contact.', ['mode' => $hub_mode, 'contact' => $contact_id]);
                }
 
-               System::httpExit($hub_challenge);
+               $this->httpExit($hub_challenge);
        }
 }
index 7c44bc510e48e8b7570b6ea4984b105944c8d66d..1a4303a683c694cb39931657446f038066d5a059 100644 (file)
@@ -85,6 +85,6 @@ class OpenSearch extends BaseModule
                        'template' => "$baseUrl/opensearch",
                ]);
 
-               System::httpExit($xml->saveXML(), Response::TYPE_XML, 'application/opensearchdescription+xml');
+               $this->httpExit($xml->saveXML(), Response::TYPE_XML, 'application/opensearchdescription+xml');
        }
 }
index e2d75ee8447fd70e2c561a4c5f5a021d26a78541..dfccafad42695cdeda008b9a7368aa607adc940c 100644 (file)
@@ -102,7 +102,7 @@ class ParseUrl extends BaseModule
                        if ($format == 'json') {
                                System::jsonExit($arr['text']);
                        } else {
-                               System::httpExit($arr['text']);
+                               $this->httpExit($arr['text']);
                        }
                }
 
@@ -135,7 +135,7 @@ class ParseUrl extends BaseModule
 
                        System::jsonExit($ret);
                } else {
-                       System::httpExit(BBCode::embedURL($url, empty($_GET['noAttachment']), $title, $description, $_GET['tags'] ?? ''));
+                       $this->httpExit(BBCode::embedURL($url, empty($_GET['noAttachment']), $title, $description, $_GET['tags'] ?? ''));
                }
        }
 }
index 4e5191e5a48c3b07e7a9193a62c6931fbb2752e5..9db90538c5c7abe73fba457423956551aace324e 100644 (file)
@@ -166,9 +166,9 @@ class PermissionTooltip extends \Friendica\BaseModule
                }
 
                if (!empty($l)) {
-                       System::httpExit($o . implode(', ', $l));
+                       $this->httpExit($o . implode(', ', $l));
                } else {
-                       System::httpExit($o . $receivers);;
+                       $this->httpExit($o . $receivers);;
                }
        }
 
index db95c38c947215d2e7d36e21df88d8099a404a28..210a3fa6bc9b55ca208bffe923b9f3478b5455db 100644 (file)
@@ -74,6 +74,6 @@ class Share extends \Friendica\BaseModule
                        $content = '[share]' . $item['uri'] . '[/share]';
                }
 
-               System::httpExit($content);
+               $this->httpExit($content);
        }
 }
index 810a9210cdbe72f9399590c6a851864065d3bc53..289e5aaa9a19f9d86212915c260a0082ad54f938 100644 (file)
@@ -45,7 +45,7 @@ class PublicRSAKey extends BaseModule
                        throw new BadRequestException();
                }
 
-               System::httpExit(
+               $this->httpExit(
                        Salmon::salmonKey($user['spubkey']),
                        Response::TYPE_BLANK,
                        'application/magic-public-key'
index 6d608063e73072284e4a5eaf52a4c9e840b2d014..4dc00240aa068b456f6aca9b8fe1ff002347d4a5 100644 (file)
@@ -67,6 +67,6 @@ class ReallySimpleDiscovery extends BaseModule
                                ],
                        ],
                ]);
-               System::httpExit($content, Response::TYPE_XML);
+               $this->httpExit($content, Response::TYPE_XML);
        }
 }
index d38b330bb3bd57fa1a6c337354d05041b50b18bc..78db953bb4a3b7ca24dc501fc389b1ced762eb70 100644 (file)
@@ -89,7 +89,7 @@ class Response implements ICanCreateResponses
        /**
         * {@inheritDoc}
         */
-       public function setType(string $type, ?string $content_type = null): void
+       public function setType(string $type = Response::TYPE_HTML, ?string $content_type = null): void
        {
                if (!in_array($type, static::ALLOWED_TYPES)) {
                        throw new InternalServerErrorException('wrong type');
index eadc9f221a1a602a1178e32c33246a3c9dff5323..d787f109865eb026f89ddab7bb3410e2a37a56e9 100644 (file)
@@ -90,6 +90,6 @@ class HostMeta extends BaseModule
                        ],
                ], $xml, false, ['hm' => 'http://host-meta.net/xrd/1.0', 'mk' => 'http://salmon-protocol.org/ns/magic-key']);
 
-               System::httpExit($xml->saveXML(), Response::TYPE_XML, 'application/xrd+xml');
+               $this->httpExit($xml->saveXML(), Response::TYPE_XML, 'application/xrd+xml');
        }
 }
index af40bc3b026aaed3ebf73c2516965d44b1f424fe..40358250e61962bd8a67f6fcd1ea4368f144fd38 100644 (file)
@@ -330,6 +330,6 @@ class Xrd extends BaseModule
                ]);
 
                header('Access-Control-Allow-Origin: *');
-               System::httpExit($xmlString, Response::TYPE_XML, 'application/xrd+xml');
+               $this->httpExit($xmlString, Response::TYPE_XML, 'application/xrd+xml');
        }
 }