Skip to content

Commit 2dc82de

Browse files
authored
adjusted how headers are passing from ExApp to client (#246)
Hope that this refactoring will not break anything :) Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
1 parent 8d32556 commit 2dc82de

File tree

3 files changed

+31
-54
lines changed

3 files changed

+31
-54
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [2.3.0 - 2024-03-xx]
9+
10+
### Changed
11+
12+
- ExAppProxy: adjusted how `headers` are passing from ExApp to client. #
13+
814
## [2.2.0 - 2024-02-21]
915

1016
### Added

lib/Controller/ExAppProxyController.php

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,15 @@ public function __construct(
3232
}
3333

3434
private function createProxyResponse(string $path, IResponse $response, $cache = true): ProxyResponse {
35+
$headersToIgnore = ['aa-version', 'ex-app-id', 'authorization-app-api', 'ex-app-version', 'aa-request-id'];
36+
$responseHeaders = [];
37+
foreach ($response->getHeaders() as $key => $value) {
38+
if (!in_array(strtolower($key), $headersToIgnore)) {
39+
$responseHeaders[$key] = $value[0];
40+
}
41+
}
3542
$content = $response->getBody();
43+
3644
$isHTML = pathinfo($path, PATHINFO_EXTENSION) === 'html';
3745
if ($isHTML) {
3846
$nonce = $this->nonceManager->getNonce();
@@ -43,29 +51,18 @@ private function createProxyResponse(string $path, IResponse $response, $cache =
4351
);
4452
}
4553

46-
$mime = $response->getHeader('content-type');
47-
if (empty($mime)) {
54+
if (empty($response->getHeader('content-type'))) {
4855
$mime = $this->mimeTypeHelper->detectPath($path);
4956
if (pathinfo($path, PATHINFO_EXTENSION) === 'wasm') {
5057
$mime = 'application/wasm';
5158
}
52-
}
53-
54-
$proxyResponse = new ProxyResponse(
55-
data: $content,
56-
length: strlen($content),
57-
mimeType: $mime,
58-
);
59-
60-
$headersToCopy = ['Content-Disposition', 'Last-Modified', 'Etag'];
61-
foreach ($headersToCopy as $element) {
62-
$headerValue = $response->getHeader($element);
63-
if (empty($headerValue)) {
64-
$proxyResponse->addHeader($element, $headerValue);
59+
if (!empty($mime) && $mime != 'application/octet-stream') {
60+
$responseHeaders['Content-Type'] = $mime;
6561
}
6662
}
6763

68-
if ($cache && !$isHTML) {
64+
$proxyResponse = new ProxyResponse($response->getStatusCode(), $responseHeaders, $content);
65+
if ($cache && !$isHTML && empty($response->getHeader('cache-control'))) {
6966
$proxyResponse->cacheFor(3600);
7067
}
7168
return $proxyResponse;
@@ -75,19 +72,15 @@ private function createProxyResponse(string $path, IResponse $response, $cache =
7572
#[NoCSRFRequired]
7673
public function ExAppGet(string $appId, string $other): Response {
7774
$exApp = $this->exAppService->getExApp($appId);
78-
if ($exApp === null) {
79-
return new NotFoundResponse();
80-
}
81-
if (!$exApp->getEnabled()) {
75+
if ($exApp === null || !$exApp->getEnabled()) {
8276
return new NotFoundResponse();
8377
}
8478

8579
$response = $this->service->requestToExApp(
8680
$exApp, '/' . $other, $this->userId, 'GET', request: $this->request
8781
);
8882
if (is_array($response)) {
89-
$error_response = new Response();
90-
return $error_response->setStatus(500);
83+
return (new Response())->setStatus(500);
9184
}
9285
return $this->createProxyResponse($other, $response);
9386
}
@@ -96,21 +89,16 @@ public function ExAppGet(string $appId, string $other): Response {
9689
#[NoCSRFRequired]
9790
public function ExAppPost(string $appId, string $other): Response {
9891
$exApp = $this->exAppService->getExApp($appId);
99-
if ($exApp === null) {
100-
return new NotFoundResponse();
101-
}
102-
if (!$exApp->getEnabled()) {
92+
if ($exApp === null || !$exApp->getEnabled()) {
10393
return new NotFoundResponse();
10494
}
10595

10696
$response = $this->service->aeRequestToExApp(
10797
$exApp, '/' . $other, $this->userId,
108-
params: $this->request->getParams(),
109-
request: $this->request
98+
params: $this->request->getParams(), request: $this->request
11099
);
111100
if (is_array($response)) {
112-
$error_response = new Response();
113-
return $error_response->setStatus(500);
101+
return (new Response())->setStatus(500);
114102
}
115103
return $this->createProxyResponse($other, $response);
116104
}
@@ -119,21 +107,15 @@ public function ExAppPost(string $appId, string $other): Response {
119107
#[NoCSRFRequired]
120108
public function ExAppPut(string $appId, string $other): Response {
121109
$exApp = $this->exAppService->getExApp($appId);
122-
if ($exApp === null) {
123-
return new NotFoundResponse();
124-
}
125-
if (!$exApp->getEnabled()) {
110+
if ($exApp === null || !$exApp->getEnabled()) {
126111
return new NotFoundResponse();
127112
}
128113

129114
$response = $this->service->aeRequestToExApp(
130-
$exApp, '/' . $other, $this->userId, 'PUT',
131-
params: $this->request->getParams(),
132-
request: $this->request
115+
$exApp, '/' . $other, $this->userId, 'PUT', $this->request->getParams(), request: $this->request
133116
);
134117
if (is_array($response)) {
135-
$error_response = new Response();
136-
return $error_response->setStatus(500);
118+
return (new Response())->setStatus(500);
137119
}
138120
return $this->createProxyResponse($other, $response);
139121
}
@@ -142,21 +124,15 @@ public function ExAppPut(string $appId, string $other): Response {
142124
#[NoCSRFRequired]
143125
public function ExAppDelete(string $appId, string $other): Response {
144126
$exApp = $this->exAppService->getExApp($appId);
145-
if ($exApp === null) {
146-
return new NotFoundResponse();
147-
}
148-
if (!$exApp->getEnabled()) {
127+
if ($exApp === null || !$exApp->getEnabled()) {
149128
return new NotFoundResponse();
150129
}
151130

152131
$response = $this->service->aeRequestToExApp(
153-
$exApp, '/' . $other, $this->userId, 'DELETE',
154-
params: $this->request->getParams(),
155-
request: $this->request
132+
$exApp, '/' . $other, $this->userId, 'DELETE', $this->request->getParams(), request: $this->request
156133
);
157134
if (is_array($response)) {
158-
$error_response = new Response();
159-
return $error_response->setStatus(500);
135+
return (new Response())->setStatus(500);
160136
}
161137
return $this->createProxyResponse($other, $response);
162138
}

lib/ProxyResponse.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,11 @@ class ProxyResponse extends Response implements ICallbackResponse {
1414
private mixed $data;
1515

1616
public function __construct(int $status = HttpAlias::STATUS_OK,
17-
array $headers = [], mixed $data = null, int $length = 0,
18-
string $mimeType = '', int $lastModified = 0) {
17+
array $headers = [], mixed $data = null, int $lastModified = 0) {
1918
parent::__construct();
2019
$this->data = $data;
2120
$this->setStatus($status);
2221
$this->setHeaders(array_merge($this->getHeaders(), $headers));
23-
$this->addHeader('Content-Length', (string)$length);
24-
if (!empty($mimeType)) {
25-
$this->addHeader('Content-Type', $mimeType);
26-
}
2722
if ($lastModified !== 0) {
2823
$lastModifiedDate = new \DateTime();
2924
$lastModifiedDate->setTimestamp($lastModified);

0 commit comments

Comments
 (0)