Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 1 | /* |
| 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 Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 16 | import { Injectable, Inject } from '@angular/core'; |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 17 | import { LogService } from '../log.service'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 18 | |
Sean Condon | bf7ff4f | 2019-03-17 16:18:42 +0000 | [diff] [blame] | 19 | const UICONTEXT = '/onos/ui/'; |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 20 | const RSSUFFIX = UICONTEXT + 'rs/'; |
| 21 | const WSSUFFIX = UICONTEXT + 'websock/'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * ONOS GUI -- Remote -- General Purpose URL Functions |
| 25 | */ |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 26 | @Injectable({ |
| 27 | providedIn: 'root', |
| 28 | }) |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 29 | export class UrlFnService { |
| 30 | constructor( |
| 31 | private log: LogService, |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 32 | @Inject('Window') private w: any |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 33 | ) { |
| 34 | this.log.debug('UrlFnService constructed'); |
| 35 | } |
| 36 | |
| 37 | matchSecure(protocol: string): string { |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 38 | const p: string = this.w.location.protocol; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 39 | 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 Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 47 | * before the UICONTEXT (/onos/ui/) and uses that as an app |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 48 | * prefix by inserting it back into the WS URL. |
| 49 | * If no prefix, then no insert. |
| 50 | */ |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 51 | urlBase(protocol: string, port: string = '', host: string = ''): string { |
| 52 | const match = this.w.location.href.match('.*//[^/]+/(.+)' + UICONTEXT); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 53 | const appPrefix = match ? '/' + match[1] : ''; |
| 54 | |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 55 | return this.matchSecure(protocol) + |
| 56 | '://' + |
| 57 | (host === '' ? this.w.location.hostname : host) + |
| 58 | ':' + |
| 59 | (port === '' ? this.w.location.port : port) + |
| 60 | appPrefix; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | httpPrefix(suffix: string): string { |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 64 | return this.urlBase('http') + suffix; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 67 | wsPrefix(suffix: string, wsport: string, host: string): string { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 68 | return this.urlBase('ws', wsport, host) + suffix; |
| 69 | } |
| 70 | |
| 71 | rsUrl(path: string): string { |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 72 | return this.httpPrefix(RSSUFFIX) + path; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 75 | wsUrl(path: string, wsport?: string, host?: string): string { |
| 76 | return this.wsPrefix(WSSUFFIX, wsport, host) + path; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 77 | } |
| 78 | } |