Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
api_proxy
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
api_proxy
Commits
0bdf3feb
Unverified
Commit
0bdf3feb
authored
5 years ago
by
Mateu Aguiló Bosch
Committed by
Mateu Aguiló Bosch
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3069526
by e0ipso: Make the _api_proxy_uri parameter name configurable
parent
257f920d
Branches
Branches containing commit
Tags
8.x-1.0
8.x-1.0-beta3
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
api_proxy.services.yml
+3
-0
3 additions, 0 deletions
api_proxy.services.yml
src/Controller/Forwarder.php
+27
-4
27 additions, 4 deletions
src/Controller/Forwarder.php
src/EventSubscriber/OptionsRequestSubscriber.php
+12
-2
12 additions, 2 deletions
src/EventSubscriber/OptionsRequestSubscriber.php
with
42 additions
and
6 deletions
api_proxy.services.yml
+
3
−
0
View file @
0bdf3feb
parameters
:
api_proxy.uri_param_name
:
_api_proxy_uri
services
:
Drupal\api_proxy\Plugin\HttpApiPluginManager
:
autowire
:
true
...
...
@@ -11,5 +13,6 @@ services:
arguments
:
-
'
@router.route_provider'
-
'
@Drupal\api_proxy\EventSubscriber\OptionsRequestSubscriber.inner'
-
'
%api_proxy.uri_param_name%'
tags
:
-
{
name
:
event_subscriber
}
This diff is collapsed.
Click to expand it.
src/Controller/Forwarder.php
+
27
−
4
View file @
0bdf3feb
...
...
@@ -8,6 +8,7 @@ use Drupal\Core\Cache\CacheableResponse;
use
Drupal\Core\Controller\ControllerBase
;
use
Drupal\Core\Http\Exception\CacheableAccessDeniedHttpException
;
use
Drupal\Core\Http\Exception\CacheableBadRequestHttpException
;
use
Symfony\Component\DependencyInjection\ContainerInterface
;
use
Symfony\Component\HttpFoundation\Request
;
use
Symfony\Component\HttpFoundation\Response
;
...
...
@@ -16,7 +17,29 @@ use Symfony\Component\HttpFoundation\Response;
*/
final
class
Forwarder
extends
ControllerBase
{
const
QUERY_PARAM_URI
=
'_api_proxy_uri'
;
/**
* The name of the query string parameter containing the URI.
*
* @param string
*/
private
$uriParamName
;
/**
* Forwarder constructor.
*
* @param string $uri_param_name
* The name of the query string parameter containing the URI.
*/
public
function
__construct
(
string
$uri_param_name
)
{
$this
->
uriParamName
=
$uri_param_name
;
}
/**
* {@inheritdoc}
*/
public
static
function
create
(
ContainerInterface
$container
)
{
return
new
static
(
$container
->
getParameter
(
'api_proxy.uri_param_name'
));
}
/**
* Forwards incoming requests to the connected API.
...
...
@@ -33,7 +56,7 @@ final class Forwarder extends ControllerBase {
// TODO: This belongs to the routing system.
$account
=
$this
->
currentUser
();
$cache_contexts
=
[
'url.query_args:'
.
Forwarder
::
QUERY_PARAM_URI
,
'url.query_args:'
.
$this
->
uriParamName
,
'headers:Origin'
,
'user.permissions'
,
];
...
...
@@ -46,11 +69,11 @@ final class Forwarder extends ControllerBase {
'The current user does not have access to this proxy'
);
}
$third_party_uri
=
$request
->
query
->
get
(
static
::
QUERY_PARAM_URI
);
$third_party_uri
=
$request
->
query
->
get
(
$this
->
uriParamName
);
if
(
empty
(
$third_party_uri
))
{
throw
new
CacheableBadRequestHttpException
(
$cacheability
,
sprintf
(
'Unable to find a valid URI in the %s query parameter.'
,
static
::
QUERY_PARAM_URI
)
sprintf
(
'Unable to find a valid URI in the %s query parameter.'
,
$this
->
uriParamName
)
);
}
$response
=
$api_proxy
->
forward
(
$request
,
$third_party_uri
);
...
...
This diff is collapsed.
Click to expand it.
src/EventSubscriber/OptionsRequestSubscriber.php
+
12
−
2
View file @
0bdf3feb
...
...
@@ -33,6 +33,13 @@ class OptionsRequestSubscriber implements EventSubscriberInterface {
*/
protected
$subject
;
/**
* The name of the query string parameter containing the URI.
*
* @param string
*/
private
$uriParamName
;
/**
* Creates a new OptionsRequestSubscriber instance.
*
...
...
@@ -40,10 +47,13 @@ class OptionsRequestSubscriber implements EventSubscriberInterface {
* The route provider.
* @param \Symfony\Component\EventDispatcher\EventSubscriberInterface
* The decorated service.
* @param string $uri_param_name
* The name of the query string parameter containing the URI.
*/
public
function
__construct
(
RouteProviderInterface
$route_provider
,
EventSubscriberInterface
$subject
)
{
public
function
__construct
(
RouteProviderInterface
$route_provider
,
EventSubscriberInterface
$subject
,
string
$uri_param_name
)
{
$this
->
routeProvider
=
$route_provider
;
$this
->
subject
=
$subject
;
$this
->
uriParamName
=
$uri_param_name
;
}
/**
...
...
@@ -79,7 +89,7 @@ class OptionsRequestSubscriber implements EventSubscriberInterface {
}
$response
=
$proxy
->
corsResponse
(
$request
);
$cache_contexts
=
[
'url.query_args:'
.
Forwarder
::
QUERY_PARAM_URI
,
'url.query_args:'
.
$this
->
uriParamName
,
'headers:Origin'
,
];
$cacheability
=
(
new
CacheableMetadata
())
...
...
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