Skip to content

Commit c953020

Browse files
[IIIF-188] refactor, set header also on redirect page
1 parent 20c3e62 commit c953020

6 files changed

Lines changed: 20 additions & 5 deletions

File tree

server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const _window = domino.createWindow(indexHtml);
9090
// The REST server base URL
9191
const REST_BASE_URL = environment.rest.ssrBaseUrl || environment.rest.baseUrl;
9292

93-
const IIIF_ALLOWED_ORIGINS = environment.mirador.allowedOrigins;
93+
const IIIF_ALLOWED_ORIGINS = environment.rest.allowedOrigins || [];
9494

9595
// Assign the DOM window and document objects to the global object
9696
(_window as any).screen = {deviceXDPI: 0, logicalXDPI: 0};

src/app/core/services/server-hard-redirect.service.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from '../../../express.tokens';
1818
import { isNotEmpty } from '../../shared/empty.util';
1919
import { HardRedirectService } from './hard-redirect.service';
20+
import { ServerResponseService } from './server-response.service';
2021

2122
/**
2223
* Service for performing hard redirects within the server app module
@@ -28,6 +29,7 @@ export class ServerHardRedirectService extends HardRedirectService {
2829
@Inject(APP_CONFIG) protected appConfig: AppConfig,
2930
@Inject(REQUEST) protected req: Request,
3031
@Inject(RESPONSE) protected res: Response,
32+
private responseService: ServerResponseService,
3133
) {
3234
super();
3335
}
@@ -72,8 +74,10 @@ export class ServerHardRedirectService extends HardRedirectService {
7274

7375
console.info(`Redirecting from ${this.req.url} to ${redirectUrl} with ${status}`);
7476

77+
this.setCorsHeader();
78+
7579
this.res.redirect(status, redirectUrl);
76-
this.res.end();
80+
//this.res.end();
7781
// I haven't found a way to correctly stop Angular rendering.
7882
// So we just let it end its work, though we have already closed
7983
// the response.
@@ -96,4 +100,17 @@ export class ServerHardRedirectService extends HardRedirectService {
96100
getCurrentOrigin(): string {
97101
return this.req.protocol + '://' + this.req.headers.host;
98102
}
103+
104+
/**
105+
* Set CORS header to allow embedding of redirected content
106+
*/
107+
setCorsHeader() {
108+
const currentOrigin = this.getCurrentOrigin();
109+
const allowedOrigins = this.appConfig.rest.allowedOrigins;
110+
111+
if (currentOrigin && allowedOrigins?.length && allowedOrigins.includes(currentOrigin)) {
112+
console.info('Setting cors header for origin ', currentOrigin);
113+
this.responseService.setHeader('Access-Control-Allow-Origin', currentOrigin);
114+
}
115+
}
99116
}

src/config/default-app-config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,6 @@ export class DefaultAppConfig implements AppConfig {
943943

944944
mirador: MiradorConfig = {
945945
enableDownloadPlugin: true,
946-
allowedOrigins: []
947946
};
948947

949948
loader: LoaderConfig = {

src/config/mirador-config.interfaces.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ import { Config } from './config.interface';
22

33
export interface MiradorConfig extends Config {
44
enableDownloadPlugin: boolean;
5-
allowedOrigins: string[];
65
}

src/config/server-config.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ export class ServerConfig implements Config {
1010
// This boolean will be automatically set on server startup based on whether "baseUrl" and "ssrBaseUrl"
1111
// have different values.
1212
public hasSsrBaseUrl?: boolean;
13+
public allowedOrigins?: string[];
1314
}

src/environments/environment.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,6 @@ export const environment: BuildConfig = {
711711

712712
mirador: {
713713
enableDownloadPlugin: true,
714-
allowedOrigins: []
715714
},
716715

717716
loader: {

0 commit comments

Comments
 (0)