blob: 03ae09e69f54ea50999e68b0460863b2ed899325 [file] [log] [blame]
Bri Prebilic Coleeb28b0b2015-01-15 14:20:58 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Bri Prebilic Coleeb28b0b2015-01-15 14:20:58 -08003 *
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 Coleeb28b0b2015-01-15 14:20:58 -080019 */
20(function () {
21 'use strict';
22
Simon Hunt1e4a0012015-01-21 11:36:08 -080023 var uiContext = '/onos/ui/',
24 rsSuffix = uiContext + 'rs/',
Thomas Vachuska329af532015-03-10 02:08:33 -070025 wsSuffix = uiContext + 'websock/';
Simon Hunt1e4a0012015-01-21 11:36:08 -080026
Bri Prebilic Coleeb28b0b2015-01-15 14:20:58 -080027 angular.module('onosRemote')
28 .factory('UrlFnService', ['$location', function ($loc) {
29
Simon Hunt1e4a0012015-01-21 11:36:08 -080030 function matchSecure(protocol) {
31 var p = $loc.protocol(),
32 secure = (p === 'https' || p === 'wss');
33 return secure ? protocol + 's' : protocol;
34 }
35
Simon Hunt8b6d2d42015-03-11 13:04:52 -070036 function urlBase(protocol, port, host) {
David K. Bainbridge8ba356c2016-08-23 16:50:10 -070037 // A little bit of funky here. It is possible that ONOS sits behind a proxy
38 // and has an app prefix, e.g. http://host:port/my/app/onos/ui... This bit
39 // of regex grabs everything after the host:port and before the uiContext
40 // (/onos/ui/) and uses that as an app prefix by inserting it back into
41 // the WS URL, if no prefix, then no insert.
42 var prefix = ""
43 if ($loc.absUrl) {
44 var p = $loc.absUrl().match(".*//[^/]+/(.+)"+uiContext);
45 prefix = p ? '/' + p[1] : '';
46 }
47
Simon Hunt1e4a0012015-01-21 11:36:08 -080048 return matchSecure(protocol) + '://' +
David K. Bainbridge8ba356c2016-08-23 16:50:10 -070049 (host || $loc.host()) + ':' + (port || $loc.port()) + prefix;
Simon Hunt1e4a0012015-01-21 11:36:08 -080050 }
51
52 function httpPrefix(suffix) {
53 return urlBase('http') + suffix;
54 }
55
Simon Hunt8b6d2d42015-03-11 13:04:52 -070056 function wsPrefix(suffix, wsport, host) {
57 return urlBase('ws', wsport, host) + suffix;
Simon Hunt1e4a0012015-01-21 11:36:08 -080058 }
59
60 function rsUrl(path) {
61 return httpPrefix(rsSuffix) + path;
62 }
63
Simon Hunt8b6d2d42015-03-11 13:04:52 -070064 function wsUrl(path, wsport, host) {
65 return wsPrefix(wsSuffix, wsport, host) + path;
Bri Prebilic Coleeb28b0b2015-01-15 14:20:58 -080066 }
67
68 return {
Simon Hunt1e4a0012015-01-21 11:36:08 -080069 rsUrl: rsUrl,
70 wsUrl: wsUrl
Bri Prebilic Coleeb28b0b2015-01-15 14:20:58 -080071 };
72 }]);
73
74}());