Allow x-instance-id and X-Instance-ID header (#354)

<!-- INSTRUCTION: Keep the line below to notify all core developers about this new PR -->
@RocketChat/ReactNative

<!-- INSTRUCTION: Inform the issue number that this PR closes, or remove the line below -->
Closes #137 

<!-- INSTRUCTION: Tell us more about your PR with screen shots if you can -->
Some server configurations may send x-instance-id header with different case.
This commit is contained in:
Diego Mello 2018-07-10 10:41:26 -03:00 committed by Guilherme Gazzo
parent 224c421b69
commit 1faf9bf542
1 changed files with 4 additions and 11 deletions

View File

@ -80,28 +80,21 @@ const RocketChat = {
console.warn(`AsyncStorage error: ${ error.message }`); console.warn(`AsyncStorage error: ${ error.message }`);
} }
}, },
_hasInstanceId(headers) {
return (headers['x-instance-id'] != null && headers['x-instance-id'].length > 0) || (headers['X-Instance-ID'] != null && headers['X-Instance-ID'].length > 0);
},
async testServer(url) { async testServer(url) {
if (/^(https?:\/\/)?(((\w|[0-9-_])+(\.(\w|[0-9-_])+)+)|localhost)(:\d+)?$/.test(url)) { if (/^(https?:\/\/)?(((\w|[0-9-_])+(\.(\w|[0-9-_])+)+)|localhost)(:\d+)?$/.test(url)) {
try { try {
let response = await RNFetchBlob.fetch('HEAD', url); let response = await RNFetchBlob.fetch('HEAD', url);
response = response.respInfo; response = response.respInfo;
if (response.status === 200 && response.headers['x-instance-id'] != null && response.headers['x-instance-id'].length) { if (response.status === 200 && RocketChat._hasInstanceId(response.headers)) {
return url; return url;
} }
} catch (e) { } catch (e) {
log('testServer', e); log('testServer', e);
} }
} }
// if (/^(https?:\/\/)?(((\w|[0-9-_])+(\.(\w|[0-9-_])+)+)|localhost)(:\d+)?$/.test(url)) {
// try {
// const response = await fetch(url, { method: 'HEAD' });
// if (response.status === 200 && response.headers.get('x-instance-id') != null && response.headers.get('x-instance-id').length) {
// return url;
// }
// } catch (error) {
// console.log(error)
// }
// }
throw new Error({ error: 'invalid server' }); throw new Error({ error: 'invalid server' });
}, },
_setUser(ddpMessage) { _setUser(ddpMessage) {