blob: a7576c02c462f6451549ecc69a271a60c2306632 [file] [log] [blame]
Sean Condon83fc39f2018-04-19 18:56:13 +01001/*
2 * Copyright 2018-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Sean Condonfd6d11b2018-06-02 20:29:49 +010016import { Injectable, Inject } from '@angular/core';
Sean Condon5ca00262018-09-06 17:55:25 +010017import { LogService } from '../log.service';
Sean Condon83fc39f2018-04-19 18:56:13 +010018
Sean Condonbf7ff4f2019-03-17 16:18:42 +000019const UICONTEXT = '/onos/ui/';
Sean Condonfd6d11b2018-06-02 20:29:49 +010020const RSSUFFIX = UICONTEXT + 'rs/';
21const WSSUFFIX = UICONTEXT + 'websock/';
Sean Condon83fc39f2018-04-19 18:56:13 +010022
23/**
24 * ONOS GUI -- Remote -- General Purpose URL Functions
25 */
Sean Condonfd6d11b2018-06-02 20:29:49 +010026@Injectable({
27 providedIn: 'root',
28})
Sean Condon83fc39f2018-04-19 18:56:13 +010029export class UrlFnService {
30 constructor(
31 private log: LogService,
Sean Condon5ca00262018-09-06 17:55:25 +010032 @Inject('Window') private w: any
Sean Condon83fc39f2018-04-19 18:56:13 +010033 ) {
34 this.log.debug('UrlFnService constructed');
35 }
36
37 matchSecure(protocol: string): string {
Sean Condonfd6d11b2018-06-02 20:29:49 +010038 const p: string = this.w.location.protocol;
Sean Condon83fc39f2018-04-19 18:56:13 +010039 const secure: boolean = (p === 'https' || p === 'wss');
40 return secure ? protocol + 's' : protocol;
41 }
42
43 /* A little bit of funky here. It is possible that ONOS sits
44 * behind a proxy and has an app prefix, e.g.
45 * http://host:port/my/app/onos/ui...
46 * This bit of regex grabs everything after the host:port and
Sean Condonfd6d11b2018-06-02 20:29:49 +010047 * before the UICONTEXT (/onos/ui/) and uses that as an app
Sean Condon83fc39f2018-04-19 18:56:13 +010048 * prefix by inserting it back into the WS URL.
49 * If no prefix, then no insert.
50 */
Sean Condonfd6d11b2018-06-02 20:29:49 +010051 urlBase(protocol: string, port: string = '', host: string = ''): string {
52 const match = this.w.location.href.match('.*//[^/]+/(.+)' + UICONTEXT);
Sean Condon83fc39f2018-04-19 18:56:13 +010053 const appPrefix = match ? '/' + match[1] : '';
54
Sean Condonfd6d11b2018-06-02 20:29:49 +010055 return this.matchSecure(protocol) +
56 '://' +
57 (host === '' ? this.w.location.hostname : host) +
58 ':' +
59 (port === '' ? this.w.location.port : port) +
60 appPrefix;
Sean Condon83fc39f2018-04-19 18:56:13 +010061 }
62
63 httpPrefix(suffix: string): string {
Sean Condonfd6d11b2018-06-02 20:29:49 +010064 return this.urlBase('http') + suffix;
Sean Condon83fc39f2018-04-19 18:56:13 +010065 }
66
Sean Condonfd6d11b2018-06-02 20:29:49 +010067 wsPrefix(suffix: string, wsport: string, host: string): string {
Sean Condon83fc39f2018-04-19 18:56:13 +010068 return this.urlBase('ws', wsport, host) + suffix;
69 }
70
71 rsUrl(path: string): string {
Sean Condonfd6d11b2018-06-02 20:29:49 +010072 return this.httpPrefix(RSSUFFIX) + path;
Sean Condon83fc39f2018-04-19 18:56:13 +010073 }
74
Sean Condonfd6d11b2018-06-02 20:29:49 +010075 wsUrl(path: string, wsport?: string, host?: string): string {
76 return this.wsPrefix(WSSUFFIX, wsport, host) + path;
Sean Condon83fc39f2018-04-19 18:56:13 +010077 }
78}