Skip to content

Commit 19df013

Browse files
authored
Merge pull request DSpace#5288 from tdonohue/port_5276_to_7_x
[Port dspace-7_x] Remove reliance on `Host` HTTP Header
2 parents 348edcd + 83217e3 commit 19df013

18 files changed

Lines changed: 96 additions & 37 deletions

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ jobs:
2424
# Spin up UI on 127.0.0.1 to avoid host resolution issues in e2e tests with Node 18+
2525
DSPACE_UI_HOST: 127.0.0.1
2626
DSPACE_UI_PORT: 4000
27+
DSPACE_UI_BASEURL: http://127.0.0.1:4000
2728
# Ensure all SSR caching is disabled in test environment
2829
DSPACE_CACHE_SERVERSIDE_BOTCACHE_MAX: 0
2930
DSPACE_CACHE_SERVERSIDE_ANONYMOUSCACHE_MAX: 0

.github/workflows/docker.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ jobs:
7474
DSPACE_REST_HOST: 127.0.0.1
7575
# Override default dspace.ui.url to also use 127.0.0.1.
7676
dspace__P__ui__P__url: http://127.0.0.1:4000
77+
# Override default ui.baseUrl to also use 127.0.0.1. This should match 'dspace.ui.url'.
78+
DSPACE_UI_BASEURL: http://127.0.0.1:4000
7779
steps:
7880
# Checkout our codebase (to get access to Docker Compose scripts)
7981
- name: Checkout codebase

config/config.example.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ ui:
88
ssl: false
99
host: localhost
1010
port: 4000
11+
# Specify the public URL that this user interface responds to. This corresponds to the "dspace.ui.url" property in your backend's local.cfg.
12+
# The baseUrl is used for redirects and SEO links (in robots.txt).
13+
baseUrl: http://localhost:4000
1114
# NOTE: Space is capitalized because 'namespace' is a reserved string in TypeScript
1215
nameSpace: /
1316
# The rateLimiter settings limit each IP to a 'max' of 500 requests per 'windowMs' (1 minute).

docker/docker-compose-dist.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ services:
2222
DSPACE_UI_HOST: ${DSPACE_UI_HOST:-dspace-angular}
2323
DSPACE_UI_PORT: ${DSPACE_UI_PORT:-4000}
2424
DSPACE_UI_NAMESPACE: ${DSPACE_UI_NAMESPACE:-/}
25+
DSPACE_UI_BASEURL: ${DSPACE_UI_BASEURL:-http://localhost:4000}
2526
DSPACE_REST_SSL: ${DSPACE_REST_SSL:-false}
2627
DSPACE_REST_HOST: ${DSPACE_REST_HOST:-localhost}
2728
DSPACE_REST_PORT: ${DSPACE_REST_PORT:-8080}

server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export function app() {
166166
server.get('/robots.txt', (req, res) => {
167167
res.setHeader('content-type', 'text/plain');
168168
res.render('assets/robots.txt.ejs', {
169-
'origin': req.protocol + '://' + req.headers.host
169+
'origin': environment.ui.baseUrl,
170170
});
171171
});
172172

src/app/bitstream-page/legacy-bitstream-url-redirect.guard.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { cold } from 'jasmine-marbles';
22
import { EMPTY } from 'rxjs';
33

4+
import { environment } from '../../environments/environment';
45
import { PAGE_NOT_FOUND_PATH } from '../app-routing-paths';
56
import { BitstreamDataService } from '../core/data/bitstream-data.service';
67
import { RemoteData } from '../core/data/remote-data';
@@ -150,7 +151,7 @@ describe('legacyBitstreamURLRedirectGuard', () => {
150151
}));
151152
resolver(route, state, bitstreamDataService, hardRedirectService, router).subscribe(() => {
152153
expect(bitstreamDataService.findByItemHandle).toHaveBeenCalled();
153-
expect(hardRedirectService.redirect).toHaveBeenCalledWith(new URL(`/bitstreams/${bitstream.uuid}/download`, window.location.origin).href, 301);
154+
expect(hardRedirectService.redirect).toHaveBeenCalledWith(new URL(`/bitstreams/${bitstream.uuid}/download`, environment.ui.baseUrl).href, 301);
154155
});
155156
});
156157
});

src/app/bitstream-page/legacy-bitstream-url-redirect.guard.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import {
99
import { Observable } from 'rxjs';
1010
import { map } from 'rxjs/operators';
1111

12-
import { PAGE_NOT_FOUND_PATH } from '../app-routing-paths';
12+
import {
13+
getBitstreamDownloadRoute,
14+
PAGE_NOT_FOUND_PATH,
15+
} from '../app-routing-paths';
1316
import { BitstreamDataService } from '../core/data/bitstream-data.service';
1417
import { RemoteData } from '../core/data/remote-data';
1518
import { HardRedirectService } from '../core/services/hard-redirect.service';
@@ -46,7 +49,7 @@ export const legacyBitstreamURLRedirectGuard: CanActivateFn = (
4649
getFirstCompletedRemoteData(),
4750
map((rd: RemoteData<Bitstream>) => {
4851
if (rd.hasSucceeded && !rd.hasNoContent) {
49-
serverHardRedirectService.redirect(new URL(`/bitstreams/${rd.payload.uuid}/download`, serverHardRedirectService.getCurrentOrigin()).href, 301);
52+
serverHardRedirectService.redirect(new URL(getBitstreamDownloadRoute(rd.payload), serverHardRedirectService.getBaseUrl()).href, 301);
5053
return false;
5154
} else {
5255
return router.createUrlTree([PAGE_NOT_FOUND_PATH]);

src/app/core/metadata/metadata.service.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe('MetadataService', () => {
8484
}
8585
} as any as Router;
8686
hardRedirectService = jasmine.createSpyObj( {
87-
getCurrentOrigin: 'https://request.org',
87+
getBaseUrl: 'https://request.org',
8888
});
8989
authorizationService = jasmine.createSpyObj('authorizationService', {
9090
isAuthorized: observableOf(true)

src/app/core/metadata/metadata.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ export class MetadataService {
299299
if (this.currentObject.value instanceof Item) {
300300
let url = this.getMetaTagValue('dc.identifier.uri');
301301
if (hasNoValue(url)) {
302-
url = new URLCombiner(this.hardRedirectService.getCurrentOrigin(), this.router.url).toString();
302+
url = new URLCombiner(this.hardRedirectService.getBaseUrl(), this.router.url).toString();
303303
}
304304
this.addMetaTag('citation_abstract_html_url', url);
305305
}
@@ -382,7 +382,7 @@ export class MetadataService {
382382
// Use the found link to set the <meta> tag
383383
this.addMetaTag(
384384
'citation_pdf_url',
385-
new URLCombiner(this.hardRedirectService.getCurrentOrigin(), link).toString()
385+
new URLCombiner(this.hardRedirectService.getBaseUrl(), link).toString()
386386
);
387387
});
388388
}

src/app/core/services/browser-hard-redirect.service.spec.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { TestBed } from '@angular/core/testing';
2+
3+
import { environment } from '../../../environments/environment';
24
import { BrowserHardRedirectService } from './browser-hard-redirect.service';
35

46
describe('BrowserHardRedirectService', () => {
57
let origin: string;
68
let mockLocation: Location;
79
let service: BrowserHardRedirectService;
10+
let originalBaseUrl;
811

912
beforeEach(() => {
1013
origin = 'https://test-host.com:4000';
@@ -19,11 +22,22 @@ describe('BrowserHardRedirectService', () => {
1922
} as Location;
2023
spyOn(mockLocation, 'replace');
2124

25+
// Store original environment variable to restore after tests
26+
originalBaseUrl = environment.ui.baseUrl;
27+
28+
// Set environment variable to match our mock location origin for testing
29+
environment.ui.baseUrl = origin;
30+
2231
service = new BrowserHardRedirectService(mockLocation);
2332

2433
TestBed.configureTestingModule({});
2534
});
2635

36+
afterEach(() => {
37+
// Restore original environment variable after tests
38+
environment.ui.baseUrl = originalBaseUrl;
39+
});
40+
2741
it('should be created', () => {
2842
expect(service).toBeTruthy();
2943
});
@@ -51,7 +65,7 @@ describe('BrowserHardRedirectService', () => {
5165
describe('when requesting the origin', () => {
5266

5367
it('should return the location origin', () => {
54-
expect(service.getCurrentOrigin()).toEqual(origin);
68+
expect(service.getBaseUrl()).toEqual(origin);
5569
});
5670
});
5771

0 commit comments

Comments
 (0)