Skip to content

Commit e139400

Browse files
committed
use https & http when unsure
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
1 parent 0810fbb commit e139400

11 files changed

+46
-33
lines changed

composer.lock

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/Command/CirclesTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
127127
$this->configService->setAppValue(ConfigService::TEST_NC_BASE, $input->getOption('url'));
128128

129129
if (!$this->testRequest($output, 'GET', 'core.CSRFToken.index')) {
130+
$this->configService->setAppValue(ConfigService::TEST_NC_BASE, '');
131+
130132
return 0;
131133
}
132134

133135
if (!$this->testRequest(
134136
$output, 'POST', 'circles.GlobalScale.asyncBroadcast',
135137
['token' => 'test-dummy-token']
136138
)) {
139+
$this->configService->setAppValue(ConfigService::TEST_NC_BASE, '');
140+
137141
return 0;
138142
}
139143

@@ -182,6 +186,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
182186
private function testRequest(OutputInterface $o, string $type, string $route, array $args = []): bool {
183187
$request = new NC19Request('', Request::type($type));
184188
$this->configService->configureRequest($request, $route, $args);
189+
$request->setFollowLocation(false);
185190

186191
$o->write('- ' . $type . ' request on ' . $request->getCompleteUrl() . ': ');
187192
$this->doRequest($request);

lib/Command/MembersCreate.php

-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ private function findUserFromLookup(string $search, string &$instance = ''): str
177177

178178
$request = new NC19Request(ConfigService::GS_LOOKUP_USERS, Request::TYPE_GET);
179179
$this->configService->configureRequest($request);
180-
$request->setProtocols(['https', 'http']);
181180
$request->basedOnUrl($lookup);
182181
$request->addParam('search', $search);
183182

lib/Controller/GlobalScaleController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function asyncBroadcast(string $token) {
100100
$this->async();
101101
foreach ($wrappers as $wrapper) {
102102
try {
103-
$this->gsUpstreamService->broadcastWrapper($wrapper, $this->request->getServerProtocol());
103+
$this->gsUpstreamService->broadcastWrapper($wrapper);
104104
} catch (GSStatusException $e) {
105105
}
106106
}

lib/Controller/MembersController.php

-3
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,6 @@ public function searchGlobal(string $search, int $order) {
243243
try {
244244
$this->mustHaveFrontEndEnabled();
245245

246-
if ($search === 't') {
247-
sleep(5);
248-
}
249246
$result = $this->searchService->searchGlobal($search);
250247
} catch (\Exception $e) {
251248
return

lib/Search/GlobalScaleUsers.php

-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ public function search($search) {
8484

8585
$request = new NC19Request(ConfigService::GS_LOOKUP_USERS, Request::TYPE_GET);
8686
$this->configService->configureRequest($request);
87-
$request->setProtocols(['https', 'http']);
8887
$request->basedOnUrl($lookup);
8988
$request->addParam('search', $search);
9089

lib/Service/ConfigService.php

+31-4
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,10 @@ public function isLocalInstance(string $instance): bool {
592592
public function configureRequest(NC19Request $request, string $routeName = '', array $args = []) {
593593
$this->configureRequestAddress($request, $routeName, $args);
594594

595+
if ($this->getForcedNcBase() === '') {
596+
$request->setProtocols(['https', 'http']);
597+
}
598+
595599
$request->setVerifyPeer($this->getAppValue(ConfigService::CIRCLES_SELF_SIGNED) !== '1');
596600
$request->setLocalAddressAllowed(true);
597601
}
@@ -613,11 +617,9 @@ private function configureRequestAddress(NC19Request $request, string $routeName
613617
return;
614618
}
615619

616-
$ncBase = ($this->getAppValue(self::TEST_NC_BASE)) ?
617-
$this->getAppValue(self::TEST_NC_BASE) : $this->getForcedNcBase();
618-
620+
$ncBase = $this->getForcedNcBase();
619621
if ($ncBase !== '') {
620-
$absolute = rtrim($ncBase, '/') . $this->urlGenerator->linkToRoute($routeName, $args);
622+
$absolute = $this->cleanLinkToRoute($ncBase, $routeName, $args);
621623
} else {
622624
$absolute = $this->urlGenerator->linkToRouteAbsolute($routeName, $args);
623625
}
@@ -632,6 +634,10 @@ private function configureRequestAddress(NC19Request $request, string $routeName
632634
* @return string
633635
*/
634636
private function getForcedNcBase(): string {
637+
if ($this->getAppValue(self::TEST_NC_BASE) !== '') {
638+
return $this->getAppValue(self::TEST_NC_BASE);
639+
}
640+
635641
$fromConfig = $this->config->getSystemValue('circles.force_nc_base', '');
636642
if ($fromConfig !== '') {
637643
return $fromConfig;
@@ -640,5 +646,26 @@ private function getForcedNcBase(): string {
640646
return $this->getAppValue(self::FORCE_NC_BASE);
641647
}
642648

649+
650+
/**
651+
* sometimes, linkToRoute will include the base path to the nextcloud which will be duplicate with ncBase
652+
*
653+
* @param string $ncBase
654+
* @param string $routeName
655+
* @param array $args
656+
*
657+
* @return string
658+
*/
659+
private function cleanLinkToRoute(string $ncBase, string $routeName, array $args): string {
660+
$link = $this->urlGenerator->linkToRoute($routeName, $args);
661+
$forcedPath = rtrim(parse_url($ncBase, PHP_URL_PATH), '/');
662+
663+
if ($forcedPath !== '' && strpos($link, $forcedPath) === 0) {
664+
$ncBase = substr($ncBase, 0, -strlen($forcedPath));
665+
}
666+
667+
return rtrim($ncBase, '/') . $link;
668+
}
669+
643670
}
644671

lib/Service/GSUpstreamService.php

+4-15
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ public function newEvent(GSEvent $event): string {
162162
* @param GSWrapper $wrapper
163163
* @param string $protocol
164164
*/
165-
public function broadcastWrapper(GSWrapper $wrapper, string $protocol): void {
165+
public function broadcastWrapper(GSWrapper $wrapper): void {
166166
$status = GSWrapper::STATUS_FAILED;
167167

168168
try {
169-
$this->broadcastEvent($wrapper->getEvent(), $wrapper->getInstance(), $protocol);
169+
$this->broadcastEvent($wrapper->getEvent(), $wrapper->getInstance());
170170
$status = GSWrapper::STATUS_DONE;
171171
} catch (RequestContentException | RequestNetworkException | RequestResultSizeException | RequestServerException | RequestResultNotJsonException $e) {
172172
}
@@ -192,7 +192,7 @@ public function broadcastWrapper(GSWrapper $wrapper, string $protocol): void {
192192
* @throws RequestResultSizeException
193193
* @throws RequestServerException
194194
*/
195-
public function broadcastEvent(GSEvent $event, string $instance, string $protocol = ''): void {
195+
public function broadcastEvent(GSEvent $event, string $instance): void {
196196
$this->signEvent($event);
197197

198198
if ($this->configService->isLocalInstance($instance)) {
@@ -202,12 +202,7 @@ public function broadcastEvent(GSEvent $event, string $instance, string $protoco
202202
$path = $this->urlGenerator->linkToRoute('circles.GlobalScale.broadcast');
203203
$request = new NC19Request($path, Request::TYPE_POST);
204204
$this->configService->configureRequest($request);
205-
$protocols = ['https', 'http'];
206-
if ($protocol !== '') {
207-
$protocols = [$protocol];
208-
}
209205
$request->setInstance($instance);
210-
$request->setProtocols($protocols);
211206
}
212207

213208
$request->setDataSerialize($event);
@@ -235,14 +230,9 @@ public function confirmEvent(GSEvent &$event): void {
235230
$path = $this->urlGenerator->linkToRoute('circles.GlobalScale.event');
236231

237232
$request = new NC19Request($path, Request::TYPE_POST);
238-
$request->basedOnUrl($owner->getInstance());
239233
$this->configService->configureRequest($request);
234+
$request->basedOnUrl($owner->getInstance());
240235

241-
if ($this->get('REQUEST_SCHEME', $_SERVER) !== '') {
242-
$request->setProtocols([$_SERVER['REQUEST_SCHEME']]);
243-
} else {
244-
$request->setProtocols(['https', 'http']);
245-
}
246236
$request->setDataSerialize($event);
247237

248238
$result = $this->retrieveJson($request);
@@ -423,7 +413,6 @@ public function confirmCircleStatus(Circle $circle): bool {
423413
$path = $this->urlGenerator->linkToRoute('circles.GlobalScale.status');
424414
$request = new NC19Request($path, Request::TYPE_POST);
425415
$this->configService->configureRequest($request);
426-
$request->setProtocols(['https', 'http']);
427416
$request->setDataSerialize($event);
428417

429418
$requestIssue = false;

lib/Service/GlobalScaleService.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,8 @@ public function getInstances(bool $all = false): array {
222222
try {
223223
$lookup = $this->configService->getGSStatus(ConfigService::GS_LOOKUP);
224224
$request = new NC19Request(ConfigService::GS_LOOKUP_INSTANCES, Request::TYPE_POST);
225-
$request->setProtocols(['https', 'http']);
226-
$request->basedOnUrl($lookup);
227225
$this->configService->configureRequest($request);
226+
$request->basedOnUrl($lookup);
228227
$request->addData('authKey', $this->configService->getGSStatus(ConfigService::GS_KEY));
229228

230229
try {

lib/Service/MembersService.php

-1
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,6 @@ private function getGlobalScaleUserDisplayName(string $ident): string {
739739

740740
$request = new NC19Request(ConfigService::GS_LOOKUP_USERS, Request::TYPE_GET);
741741
$this->configService->configureRequest($request);
742-
$request->setProtocols(['https', 'http']);
743742
$request->basedOnUrl($lookup);
744743
$request->addParam('search', $ident);
745744
$request->addParam('exact', '1');

lib/Service/SharingFrameService.php

-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ public function receiveFrame($token, $uniqueId, SharingFrame $frame) {
305305
*/
306306
public function initiateShare(string $circleUniqueId, string $frameUniqueId) {
307307
$request = new NC19Request('', Request::TYPE_POST);
308-
$request->setProtocols(['https', 'http']);
309308
$this->configService->configureRequest($request, 'circles.Shares.initShareDelivery');
310309
$request->addParam('circleId', $circleUniqueId);
311310
$request->addParam('frameId', $frameUniqueId);

0 commit comments

Comments
 (0)