2015-11-23 10:16:51 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-07-12 12:51:57 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-07-12 12:51:57 +00:00
|
|
|
*
|
2018-05-11 20:32:37 +00:00
|
|
|
* @format
|
2015-11-23 10:16:51 +00:00
|
|
|
* @flow
|
|
|
|
*/
|
2018-05-11 20:32:37 +00:00
|
|
|
|
2015-11-23 10:16:51 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-09 03:36:40 +00:00
|
|
|
var React = require('react');
|
|
|
|
var ReactNative = require('react-native');
|
2018-05-11 20:32:37 +00:00
|
|
|
var {StyleSheet, Text, TouchableHighlight, View, WebView} = ReactNative;
|
2015-11-23 10:16:51 +00:00
|
|
|
|
|
|
|
var RCTNetworking = require('RCTNetworking');
|
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class XHRExampleCookies extends React.Component<any, any> {
|
2016-02-25 10:44:58 +00:00
|
|
|
cancelled: boolean;
|
|
|
|
|
2015-11-23 10:16:51 +00:00
|
|
|
constructor(props: any) {
|
|
|
|
super(props);
|
|
|
|
this.cancelled = false;
|
|
|
|
this.state = {
|
|
|
|
status: '',
|
|
|
|
a: 1,
|
|
|
|
b: 2,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
setCookie(domain: string) {
|
|
|
|
var {a, b} = this.state;
|
|
|
|
var url = `https://${domain}/cookies/set?a=${a}&b=${b}`;
|
2018-05-11 20:32:37 +00:00
|
|
|
fetch(url).then(response => {
|
2015-11-23 10:16:51 +00:00
|
|
|
this.setStatus(`Cookies a=${a}, b=${b} set`);
|
2016-04-25 16:43:12 +00:00
|
|
|
this.refreshWebview();
|
2015-11-23 10:16:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
status: 'Setting cookies...',
|
|
|
|
a: a + 1,
|
|
|
|
b: b + 2,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getCookies(domain: string) {
|
2018-05-11 20:32:37 +00:00
|
|
|
fetch(`https://${domain}/cookies`)
|
|
|
|
.then(response => {
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then(data => {
|
|
|
|
this.setStatus(
|
|
|
|
`Got cookies ${JSON.stringify(data.cookies)} from server`,
|
|
|
|
);
|
|
|
|
this.refreshWebview();
|
|
|
|
});
|
2015-11-23 10:16:51 +00:00
|
|
|
|
|
|
|
this.setStatus('Getting cookies...');
|
|
|
|
}
|
|
|
|
|
|
|
|
clearCookies() {
|
2018-05-11 20:32:37 +00:00
|
|
|
RCTNetworking.clearCookies(cleared => {
|
Add responseType as a concept to RCTNetworking, send binary data as base64
Summary:
In preparation for Blob support (wherein binary XHR and WebSocket responses can be retained as native data blobs on the native side and JS receives a web-like opaque Blob object), this change makes RCTNetworking aware of the responseType that JS requests. A `xhr.responseType` of `''` or `'text'` translates to a native response type of `'text'`. A `xhr.responseType` of `arraybuffer` translates to a native response type of `base64`, as we currently lack an API to transmit TypedArrays directly to JS. This is analogous to how the WebSocket module already works, and it's a lot more versatile and much less brittle than converting a JS *string* back to a TypedArray, which is what's currently going on.
Now that we don't always send text down to JS, JS consumers might still want to get progress updates about a binary download. This is what the `'progress'` event is designed for, so this change also implements that. This change also follows the XHR spec with regards to `xhr.response` and `xhr.responseText`:
- if the response type is `'text'`, `xhr.responseText` can be peeked at by the JS consumer. It will be updated periodically as the download progresses, so long as there's either an `onreadystatechange` or `onprogress` handler on the XHR.
- if the response type is not `'text'`, `xhr.responseText` can't be accessed and `xhr.response` remains `null` until the response is fully received. `'progress'` events containing response details (total bytes, downloaded so far) are dispatched if there's an `onprogress` handler.
Once Blobs are landed, `xhr.responseType` of `'blob'` will correspond to the same native response type, which will cause RCTNetworking to only send a blob ID down to JS, which can then create a `Blob` object from that for consumers.
Closes https://github.com/facebook/react-native/pull/8324
Reviewed By: javache
Differential Revision: D3508822
Pulled By: davidaurelio
fbshipit-source-id: 441b2d4d40265b6036559c3ccb9fa962999fa5df
2016-07-13 11:53:54 +00:00
|
|
|
this.setStatus('Cookies cleared, had cookies=' + cleared.toString());
|
2016-04-25 16:43:12 +00:00
|
|
|
this.refreshWebview();
|
2015-11-23 10:16:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-25 16:43:12 +00:00
|
|
|
refreshWebview() {
|
|
|
|
this.refs.webview.reload();
|
|
|
|
}
|
|
|
|
|
2015-11-23 10:16:51 +00:00
|
|
|
setStatus(status: string) {
|
|
|
|
this.setState({status});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View>
|
|
|
|
<TouchableHighlight
|
|
|
|
style={styles.wrapper}
|
|
|
|
onPress={this.setCookie.bind(this, 'httpbin.org')}>
|
|
|
|
<View style={styles.button}>
|
|
|
|
<Text>Set cookie</Text>
|
|
|
|
</View>
|
|
|
|
</TouchableHighlight>
|
|
|
|
<TouchableHighlight
|
|
|
|
style={styles.wrapper}
|
|
|
|
onPress={this.setCookie.bind(this, 'eu.httpbin.org')}>
|
|
|
|
<View style={styles.button}>
|
|
|
|
<Text>Set cookie (EU)</Text>
|
|
|
|
</View>
|
|
|
|
</TouchableHighlight>
|
|
|
|
<TouchableHighlight
|
|
|
|
style={styles.wrapper}
|
|
|
|
onPress={this.getCookies.bind(this, 'httpbin.org')}>
|
|
|
|
<View style={styles.button}>
|
|
|
|
<Text>Get cookies</Text>
|
|
|
|
</View>
|
|
|
|
</TouchableHighlight>
|
|
|
|
<TouchableHighlight
|
|
|
|
style={styles.wrapper}
|
|
|
|
onPress={this.getCookies.bind(this, 'eu.httpbin.org')}>
|
|
|
|
<View style={styles.button}>
|
|
|
|
<Text>Get cookies (EU)</Text>
|
|
|
|
</View>
|
|
|
|
</TouchableHighlight>
|
|
|
|
<TouchableHighlight
|
|
|
|
style={styles.wrapper}
|
|
|
|
onPress={this.clearCookies.bind(this)}>
|
|
|
|
<View style={styles.button}>
|
|
|
|
<Text>Clear cookies</Text>
|
|
|
|
</View>
|
|
|
|
</TouchableHighlight>
|
|
|
|
<Text>{this.state.status}</Text>
|
2016-04-25 16:43:12 +00:00
|
|
|
<TouchableHighlight
|
|
|
|
style={styles.wrapper}
|
|
|
|
onPress={this.refreshWebview.bind(this)}>
|
|
|
|
<View style={styles.button}>
|
|
|
|
<Text>Refresh Webview</Text>
|
|
|
|
</View>
|
|
|
|
</TouchableHighlight>
|
|
|
|
<WebView
|
|
|
|
ref="webview"
|
|
|
|
source={{uri: 'http://httpbin.org/cookies'}}
|
|
|
|
style={{height: 100}}
|
|
|
|
/>
|
2015-11-23 10:16:51 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
wrapper: {
|
|
|
|
borderRadius: 5,
|
|
|
|
marginBottom: 5,
|
|
|
|
},
|
|
|
|
button: {
|
|
|
|
backgroundColor: '#eeeeee',
|
|
|
|
padding: 8,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = XHRExampleCookies;
|