Skip to content

Commit cacbc9e

Browse files
authored
DM-7825: Use custom http headers to pass ws connection info. Merge pr #199.
DM-7825: Use custom http headers to pass ws connection info
2 parents e6a031e + abde390 commit cacbc9e

File tree

5 files changed

+33
-43
lines changed

5 files changed

+33
-43
lines changed

src/firefly/java/edu/caltech/ipac/firefly/core/ClientEventQueue.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public static void onMessage(String msg) {
5050
if (name.equals(Name.EVT_CONN_EST)) {
5151
JSONObject dataJ = JSONParser.parseStrict(sEvent.getData().toString()).isObject();
5252
String sEventInfo = dataJ.get("connID").isString().stringValue() + "_" + dataJ.get("channel").isString().stringValue();
53-
Cookies.setCookie("seinfo", sEventInfo);
5453
GwtUtil.getClientLogger().log(Level.INFO, "Websocket connection established: " + sEventInfo );
5554
} else if (data instanceof BackgroundStatus) {
5655
WebEvent<String> ev= new WebEvent<String>(ClientEventQueue.class,name,((BackgroundStatus)data).serialize());
@@ -86,10 +85,6 @@ public static native void sendMessage(String msg) /*-{
8685
}
8786
}-*/;
8887

89-
private native void closeConnection() /*-{
90-
$wnd.firefly.ClientEventQueue.close();
91-
}-*/;
92-
9388
public static native void start(String baseurl) /*-{
9489
9590
var nRetries = 0;
@@ -138,10 +133,6 @@ function doWSConnect() {
138133
doWSConnect();
139134
}-*/;
140135

141-
//====================================================================
142-
//
143-
//====================================================================
144-
145136
private static ServerEvent parseJsonEvent(String msg) {
146137
try {
147138
JSONObject eventJ = JSONParser.parseStrict(msg).isObject();
@@ -167,4 +158,12 @@ private static ServerEvent parseJsonEvent(String msg) {
167158
return null;
168159
}
169160
}
161+
162+
//====================================================================
163+
//
164+
//====================================================================
165+
166+
private native void closeConnection() /*-{
167+
$wnd.firefly.ClientEventQueue.close();
168+
}-*/;
170169
}

src/firefly/java/edu/caltech/ipac/firefly/server/RequestOwner.java

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@
3232
*/
3333
public class RequestOwner implements Cloneable {
3434

35+
private static final Logger.LoggerImpl LOG = Logger.getLogger();
3536
public static String USER_KEY = "usrkey";
3637
// private static final String[] ID_COOKIE_NAMES = new String[]{WebAuthModule.AUTH_KEY, "ISIS"};
3738
private static boolean ignoreAuth = AppProperties.getBooleanProperty("ignore.auth", false);
38-
private static final Logger.LoggerImpl LOG = Logger.getLogger();
39-
4039
private RequestAgent requestAgent;
4140
private Date startTime;
4241
private File workingDir;
@@ -67,15 +66,8 @@ public void setRequestAgent(RequestAgent requestAgent) {
6766
if (requestAgent != null) {
6867
host = requestAgent.getHeader("host");
6968
referrer = requestAgent.getHeader("Referer");
70-
71-
String sei = requestAgent.getCookie("seinfo");
72-
if (!StringUtils.isEmpty(sei)) {
73-
String [] parts = sei.split("_");
74-
if (parts.length > 1) {
75-
eventConnID = parts[0];
76-
eventChannel = parts[1];
77-
}
78-
}
69+
eventChannel = requestAgent.getHeader("FF-channel");
70+
eventConnID = requestAgent.getHeader("FF-connID");
7971
}
8072
}
8173

@@ -149,11 +141,6 @@ public boolean isAuthUser() {
149141
return !StringUtils.isEmpty(getAuthToken());
150142
}
151143

152-
// should only use this as a way to bypass the web-based access.
153-
public void setUserInfo(UserInfo userInfo) {
154-
this.userInfo = userInfo;
155-
}
156-
157144
public UserInfo getUserInfo() {
158145
if (userInfo == null) {
159146
if (isAuthUser() && !ignoreAuth) {
@@ -177,6 +164,11 @@ public UserInfo getUserInfo() {
177164
return userInfo;
178165
}
179166

167+
// should only use this as a way to bypass the web-based access.
168+
public void setUserInfo(UserInfo userInfo) {
169+
this.userInfo = userInfo;
170+
}
171+
180172
@Override
181173
public Object clone() throws CloneNotSupportedException {
182174
RequestOwner ro = new RequestOwner(getUserKey(), startTime);

src/firefly/js/core/messaging/WebSocketClient.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import {get, pickBy} from 'lodash';
6-
import {setCookie, parseUrl} from '../../util/WebUtil.js';
6+
import {setCookie, parseUrl, getModuleName} from '../../util/WebUtil.js';
77
import {getRootURL} from '../../util/BrowserUtil.js';
88
import {dispatchUpdateAppData} from '../AppDataCntlr.js';
99

@@ -70,8 +70,6 @@ function addListener( {matches, onEvent} ) {
7070
}
7171

7272
function onOpen() {
73-
console.log('WS open: WebSocketClient started');
74-
7573
if (pinger) clearInterval(pinger);
7674
pinger = setInterval(() => wsSend({}), 5000);
7775
}
@@ -97,8 +95,8 @@ function onMessage(event) {
9795
if (eventData.name === 'EVT_CONN_EST') {
9896
// connection established.. doing handshake.
9997
[connId, channel] = [eventData.data.connID, eventData.data.channel];
100-
setCookie('seinfo', `${connId}_${channel}`);
10198
dispatchUpdateAppData({channel});
99+
console.log(`WebSocket connected. connId:${connId} channel:${channel}`);
102100
} else {
103101
listenters.forEach( (l) => {
104102
if (!l.matches || l.matches(eventData)) {

src/firefly/js/util/WebUtil.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Enum from 'enum';
88
import update from 'react-addons-update';
99
import {get, set, has, omit, isObject, union, isFunction, isEqual, isNil, last} from 'lodash';
1010
import { getRootURL } from './BrowserUtil.js';
11+
import {getWsConnId, getWsChannel} from '../core/messaging/WebSocketClient.js';
1112

1213
const MEG = 1048576;
1314
const GIG = 1048576 * 1024;
@@ -143,7 +144,10 @@ export function fetchUrl(url, options, returnAllResponses= false) {
143144
};
144145
options = Object.assign(req, options);
145146

146-
const headers = {};
147+
const headers = {
148+
'FF-channel': getWsChannel(),
149+
'FF-connID': getWsConnId()
150+
};
147151
options.headers = Object.assign(headers, options.headers);
148152

149153
if (options.params) {

src/firefly/python/display/firefly_client.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,11 @@ def received_message(self, m):
182182
conn_info = ev['data']
183183
if self.channel is None:
184184
self.channel = conn_info['channel']
185-
conn_id = ''
186-
if 'conn_id' in conn_info:
187-
conn_id = conn_info['conn_id']
188-
seinfo = self.channel
189-
if (len(conn_id)) > 0:
190-
seinfo = conn_id + '_' + seinfo
191-
192-
self.session.cookies['seinfo'] = seinfo
185+
if 'connID' in conn_info:
186+
self.conn_id = conn_info['connID']
187+
188+
self.headers = {'FF_FF-channel': self.channel,
189+
'FF-connID': self.conn_id}
193190
except:
194191
print('from callback exception: ')
195192
print(m)
@@ -199,14 +196,14 @@ def received_message(self, m):
199196
def _send_url_as_get(self, url):
200197
"""Send URL in 'GET' request and return status."""
201198

202-
response = self.session.get(url)
199+
response = self.session.get(url, headers=self.headers)
203200
status = json.loads(response.text)
204201
return status[0]
205202

206203
def _send_url_as_post(self, data):
207204
"""Send URL in 'POST' request and return status."""
208205

209-
response = self.session.post(self.url_root, data=data)
206+
response = self.session.post(self.url_root, data=data, headers=self.headers)
210207
status = json.loads(response.text)
211208
return status[0]
212209

@@ -367,7 +364,7 @@ def upload_file(self, path, pre_load=True):
367364

368365
url = 'http://' + self.this_host + '/firefly/sticky/Firefly_FileUpload?preload=%s' % pre_load
369366
files = {'file': open(path, 'rb')}
370-
result = self.session.post(url, files=files)
367+
result = self.session.post(url, files=files, headers=self.headers)
371368
index = result.text.find('$')
372369
return result.text[index:]
373370

@@ -390,7 +387,7 @@ def upload_fits_data(self, stream):
390387

391388
url = 'http://' + self.this_host + '/firefly/sticky/Firefly_FileUpload?preload=true'
392389
data_pack = {'data': stream}
393-
result = self.session.post(url, files=data_pack)
390+
result = self.session.post(url, files=data_pack, headers=self.headers)
394391
index = result.text.find('$')
395392
return result.text[index:]
396393

@@ -434,7 +431,7 @@ def upload_data(self, stream, data_type):
434431
url = 'http://' + self.this_host + '/firefly/sticky/Firefly_FileUpload?preload='
435432
url += 'true&type=FITS' if data_type.upper() == 'FITS' else 'false&type=UNKNOWN'
436433
data_pack = {'data': stream}
437-
result = self.session.post(url, files=data_pack)
434+
result = self.session.post(url, files=data_pack, headers=self.headers)
438435
index = result.text.find('$')
439436
return result.text[index:]
440437

0 commit comments

Comments
 (0)