Bri Prebilic Cole | eb28b0b | 2015-01-15 14:20:58 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Bri Prebilic Cole | eb28b0b | 2015-01-15 14:20:58 -0800 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | /* |
| 18 | ONOS GUI -- Remote -- General Purpose Functions |
Bri Prebilic Cole | eb28b0b | 2015-01-15 14:20:58 -0800 | [diff] [blame] | 19 | */ |
| 20 | (function () { |
| 21 | 'use strict'; |
| 22 | |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 23 | var uiContext = '/onos/ui/', |
| 24 | rsSuffix = uiContext + 'rs/', |
Thomas Vachuska | 329af53 | 2015-03-10 02:08:33 -0700 | [diff] [blame] | 25 | wsSuffix = uiContext + 'websock/'; |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 26 | |
Bri Prebilic Cole | eb28b0b | 2015-01-15 14:20:58 -0800 | [diff] [blame] | 27 | angular.module('onosRemote') |
| 28 | .factory('UrlFnService', ['$location', function ($loc) { |
| 29 | |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 30 | function matchSecure(protocol) { |
| 31 | var p = $loc.protocol(), |
| 32 | secure = (p === 'https' || p === 'wss'); |
| 33 | return secure ? protocol + 's' : protocol; |
| 34 | } |
| 35 | |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 36 | function urlBase(protocol, port, host) { |
Simon Hunt | fa875bb | 2016-08-24 11:41:02 -0700 | [diff] [blame] | 37 | // A little bit of funky here. It is possible that ONOS sits |
| 38 | // behind a proxy and has an app prefix, e.g. |
| 39 | // http://host:port/my/app/onos/ui... |
| 40 | // This bit of regex grabs everything after the host:port and |
| 41 | // before the uiContext (/onos/ui/) and uses that as an app |
| 42 | // prefix by inserting it back into the WS URL. |
| 43 | // If no prefix, then no insert. |
| 44 | |
| 45 | var match = $loc.absUrl().match('.*//[^/]+/(.+)' + uiContext), |
| 46 | appPrefix = match ? '/' + match[1] : ''; |
David K. Bainbridge | 8ba356c | 2016-08-23 16:50:10 -0700 | [diff] [blame] | 47 | |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 48 | return matchSecure(protocol) + '://' + |
Simon Hunt | fa875bb | 2016-08-24 11:41:02 -0700 | [diff] [blame] | 49 | (host || $loc.host()) + ':' + |
| 50 | (port || $loc.port()) + appPrefix; |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | function httpPrefix(suffix) { |
| 54 | return urlBase('http') + suffix; |
| 55 | } |
| 56 | |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 57 | function wsPrefix(suffix, wsport, host) { |
| 58 | return urlBase('ws', wsport, host) + suffix; |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | function rsUrl(path) { |
| 62 | return httpPrefix(rsSuffix) + path; |
| 63 | } |
| 64 | |
Simon Hunt | 8b6d2d4 | 2015-03-11 13:04:52 -0700 | [diff] [blame] | 65 | function wsUrl(path, wsport, host) { |
| 66 | return wsPrefix(wsSuffix, wsport, host) + path; |
Bri Prebilic Cole | eb28b0b | 2015-01-15 14:20:58 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | return { |
Simon Hunt | 1e4a001 | 2015-01-21 11:36:08 -0800 | [diff] [blame] | 70 | rsUrl: rsUrl, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 71 | wsUrl: wsUrl, |
Bri Prebilic Cole | eb28b0b | 2015-01-15 14:20:58 -0800 | [diff] [blame] | 72 | }; |
| 73 | }]); |
| 74 | |
| 75 | }()); |