Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
our_own_cloud_project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
die_coolen_jungs
our_own_cloud_project
Commits
bacf1603
Commit
bacf1603
authored
9 years ago
by
Thomas Müller
Browse files
Options
Downloads
Patches
Plain Diff
Adding ocs/v2.php with status code mapper
parent
af7bcb43
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
lib/private/api.php
+56
-8
56 additions, 8 deletions
lib/private/api.php
ocs/v2.php
+22
-0
22 additions, 0 deletions
ocs/v2.php
tests/ocs/response.php
+40
-0
40 additions, 0 deletions
tests/ocs/response.php
with
118 additions
and
8 deletions
lib/private/api.php
+
56
−
8
View file @
bacf1603
<?php
use
OCP\API
;
use
OCP\AppFramework\Http
;
/**
* @author Bart Visscher <bartv@thisnet.nl>
* @author Bernhard Posselt <dev@bernhard-posselt.com>
...
...
@@ -82,7 +85,7 @@ class OC_API {
* @param array $requirements
*/
public
static
function
register
(
$method
,
$url
,
$action
,
$app
,
$authLevel
=
\OCP\
API
::
USER_AUTH
,
$authLevel
=
API
::
USER_AUTH
,
$defaults
=
array
(),
$requirements
=
array
())
{
$name
=
strtolower
(
$method
)
.
$url
;
...
...
@@ -123,7 +126,7 @@ class OC_API {
if
(
!
self
::
isAuthorised
(
$action
))
{
$responses
[]
=
array
(
'app'
=>
$action
[
'app'
],
'response'
=>
new
OC_OCS_Result
(
null
,
\OCP\
API
::
RESPOND_UNAUTHORISED
,
'Unauthorised'
),
'response'
=>
new
OC_OCS_Result
(
null
,
API
::
RESPOND_UNAUTHORISED
,
'Unauthorised'
),
'shipped'
=>
OC_App
::
isShipped
(
$action
[
'app'
]),
);
continue
;
...
...
@@ -131,7 +134,7 @@ class OC_API {
if
(
!
is_callable
(
$action
[
'action'
]))
{
$responses
[]
=
array
(
'app'
=>
$action
[
'app'
],
'response'
=>
new
OC_OCS_Result
(
null
,
\OCP\
API
::
RESPOND_NOT_FOUND
,
'Api method not found'
),
'response'
=>
new
OC_OCS_Result
(
null
,
API
::
RESPOND_NOT_FOUND
,
'Api method not found'
),
'shipped'
=>
OC_App
::
isShipped
(
$action
[
'app'
]),
);
continue
;
...
...
@@ -252,15 +255,15 @@ class OC_API {
private
static
function
isAuthorised
(
$action
)
{
$level
=
$action
[
'authlevel'
];
switch
(
$level
)
{
case
\OCP\
API
::
GUEST_AUTH
:
case
API
::
GUEST_AUTH
:
// Anyone can access
return
true
;
break
;
case
\OCP\
API
::
USER_AUTH
:
case
API
::
USER_AUTH
:
// User required
return
self
::
loginUser
();
break
;
case
\OCP\
API
::
SUBADMIN_AUTH
:
case
API
::
SUBADMIN_AUTH
:
// Check for subadmin
$user
=
self
::
loginUser
();
if
(
!
$user
)
{
...
...
@@ -275,7 +278,7 @@ class OC_API {
}
}
break
;
case
\OCP\
API
::
ADMIN_AUTH
:
case
API
::
ADMIN_AUTH
:
// Check for admin
$user
=
self
::
loginUser
();
if
(
!
$user
)
{
...
...
@@ -342,10 +345,18 @@ class OC_API {
*/
public
static
function
respond
(
$result
,
$format
=
'xml'
)
{
// Send 401 headers if unauthorised
if
(
$result
->
getStatusCode
()
===
\OCP\
API
::
RESPOND_UNAUTHORISED
)
{
if
(
$result
->
getStatusCode
()
===
API
::
RESPOND_UNAUTHORISED
)
{
header
(
'WWW-Authenticate: Basic realm="Authorisation Required"'
);
header
(
'HTTP/1.0 401 Unauthorized'
);
}
if
(
self
::
isV2
())
{
$statusCode
=
self
::
mapStatusCodes
(
$result
->
getStatusCode
());
if
(
!
is_null
(
$statusCode
))
{
OC_Response
::
setStatus
(
$statusCode
);
}
}
$response
=
array
(
'ocs'
=>
array
(
'meta'
=>
$result
->
getMeta
(),
...
...
@@ -415,5 +426,42 @@ class OC_API {
header
(
'Content-Type: application/octet-stream; charset=utf-8'
);
}
/**
* @return boolean
*/
private
static
function
isV2
()
{
$request
=
\OC
::
$server
->
getRequest
();
$script
=
$request
->
getScriptName
();
return
$script
===
'/ocs/v2.php'
;
}
/**
* @param integer $sc
*/
public
static
function
mapStatusCodes
(
$sc
)
{
switch
(
$sc
)
{
case
API
::
RESPOND_NOT_FOUND
:
return
Http
::
STATUS_NOT_FOUND
;
case
API
::
RESPOND_SERVER_ERROR
:
return
Http
::
STATUS_INTERNAL_SERVER_ERROR
;
case
API
::
RESPOND_UNKNOWN_ERROR
:
return
Http
::
STATUS_INTERNAL_SERVER_ERROR
;
case
API
::
RESPOND_UNAUTHORISED
:
// already handled for v1
return
null
;
case
100
:
return
Http
::
STATUS_OK
;
}
// any 2xx, 4xx and 5xx will be used as is
if
(
$sc
>=
200
&&
$sc
<
600
)
{
return
$sc
;
}
// any error codes > 100 are treated as client errors
if
(
$sc
>
100
&&
$sc
<
200
)
{
return
Http
::
STATUS_BAD_REQUEST
;
}
return
null
;
}
}
This diff is collapsed.
Click to expand it.
ocs/v2.php
0 → 100644
+
22
−
0
View file @
bacf1603
<?php
/**
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
require_once
'v1.php'
;
This diff is collapsed.
Click to expand it.
tests/ocs/response.php
0 → 100644
+
40
−
0
View file @
bacf1603
<?php
use
OCP\AppFramework\Http
;
/**
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
class
OcsResponseTest
extends
\Test\TestCase
{
/**
* @dataProvider providesStatusCodes
*/
public
function
testStatusCodeMapper
(
$expected
,
$sc
)
{
$result
=
OC_API
::
mapStatusCodes
(
$sc
);
$this
->
assertEquals
(
$expected
,
$result
);
}
public
function
providesStatusCodes
()
{
return
[
[
Http
::
STATUS_OK
,
100
],
[
Http
::
STATUS_BAD_REQUEST
,
104
],
];
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment