-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
fix: Move OC_API into \OC\ApiHelper in standard namespace #47685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+195
−460
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ea32d17
fix: Move OC_API into \OC\ApiHelper in standard namespace
come-nc 3756946
fix: A bit of code cleanup in OC\AppFramework\OCS\ApiHelper
come-nc 4c9104e
fix: Move ApiHelper to \OC\OCS next to related classes
come-nc e184784
fix: Remove duplicated code in \OC\OCS\ApiHelper, use Response classe…
come-nc 359bbce
chore: Adapt tests to OC_API refactoring
come-nc 76d1b11
chore: Deleted now unused classes from \OC\OCS
come-nc 42c5a60
fix: Force 503 HTTP status code for maintenance mode on v1
come-nc 90a9485
chore: Make parameter name more explicit
come-nc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-FileCopyrightText: 2016 ownCloud, Inc. | ||
| * SPDX-License-Identifier: AGPL-3.0-only | ||
| */ | ||
|
|
||
| namespace OC\OCS; | ||
|
|
||
| use OC\AppFramework\OCS\V1Response; | ||
| use OC\AppFramework\OCS\V2Response; | ||
| use OCP\AppFramework\Http\DataResponse; | ||
| use OCP\AppFramework\OCSController; | ||
| use OCP\IRequest; | ||
| use OCP\Server; | ||
|
|
||
| class ApiHelper { | ||
| /** | ||
| * Respond to a call | ||
| * @psalm-taint-escape html | ||
| * @param int $overrideHttpStatusCode force the HTTP status code, only used for the special case of maintenance mode which return 503 even for v1 | ||
| */ | ||
| public static function respond(int $statusCode, string $statusMessage, array $headers = [], ?int $overrideHttpStatusCode = null): void { | ||
| $request = Server::get(IRequest::class); | ||
| $format = $request->getParam('format', 'xml'); | ||
| if (self::isV2($request)) { | ||
| $response = new V2Response(new DataResponse([], $statusCode, $headers), $format, $statusMessage); | ||
| } else { | ||
| $response = new V1Response(new DataResponse([], $statusCode, $headers), $format, $statusMessage); | ||
| } | ||
|
|
||
| // Send 401 headers if unauthorised | ||
| if ($response->getOCSStatus() === OCSController::RESPOND_UNAUTHORISED) { | ||
| // If request comes from JS return dummy auth request | ||
| if ($request->getHeader('X-Requested-With') === 'XMLHttpRequest') { | ||
| header('WWW-Authenticate: DummyBasic realm="Authorisation Required"'); | ||
| } else { | ||
| header('WWW-Authenticate: Basic realm="Authorisation Required"'); | ||
| } | ||
| http_response_code(401); | ||
| } | ||
|
|
||
| foreach ($response->getHeaders() as $name => $value) { | ||
| header($name . ': ' . $value); | ||
| } | ||
|
|
||
| http_response_code($overrideHttpStatusCode ?? $response->getStatus()); | ||
|
|
||
| self::setContentType($format); | ||
| $body = $response->render(); | ||
| echo $body; | ||
Check failureCode scanning / Psalm TaintedTextWithQuotes
Detected tainted text with possible quotes
|
||
| } | ||
|
|
||
| /** | ||
| * Based on the requested format the response content type is set | ||
| */ | ||
| public static function setContentType(?string $format = null): void { | ||
| $format ??= Server::get(IRequest::class)->getParam('format', 'xml'); | ||
| if ($format === 'xml') { | ||
| header('Content-type: text/xml; charset=UTF-8'); | ||
| return; | ||
| } | ||
|
|
||
| if ($format === 'json') { | ||
| header('Content-Type: application/json; charset=utf-8'); | ||
| return; | ||
| } | ||
|
|
||
| header('Content-Type: application/octet-stream; charset=utf-8'); | ||
| } | ||
|
|
||
| protected static function isV2(IRequest $request): bool { | ||
| $script = $request->getScriptName(); | ||
|
|
||
| return str_ends_with($script, '/ocs/v2.php'); | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / Psalm
TaintedHtml