2018-03-24 14:13:27 +00:00
|
|
|
import ClientDriver from './client-driver.js'
|
2018-03-23 17:43:59 +00:00
|
|
|
|
2018-03-24 14:13:27 +00:00
|
|
|
export default class XHRClientDriver extends ClientDriver{
|
2018-03-23 17:43:59 +00:00
|
|
|
|
|
|
|
/* (1) Creates a client driver
|
|
|
|
*
|
2018-03-25 11:40:35 +00:00
|
|
|
* @_resource<String> Target resource
|
|
|
|
* (typically a hostname without trailing HTTP:// nor HTTPS://)
|
|
|
|
* @_auth<AuthObject> Authentication object
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* [fornat::AuthObject]
|
|
|
|
* {
|
|
|
|
* ssl: <boolean>
|
|
|
|
* }
|
2018-03-23 17:43:59 +00:00
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-03-25 11:40:35 +00:00
|
|
|
constructor(_resource, _auth={ssl:true}){
|
2018-03-23 17:43:59 +00:00
|
|
|
|
|
|
|
/* (0) Parent check */
|
2018-03-25 11:40:35 +00:00
|
|
|
if( super(_resource, _auth).error ){
|
|
|
|
this.force_close = true;
|
2018-03-23 17:43:59 +00:00
|
|
|
return;
|
2018-03-25 11:40:35 +00:00
|
|
|
}
|
2018-03-23 17:43:59 +00:00
|
|
|
|
2018-03-25 11:40:35 +00:00
|
|
|
/* (1) Create useful attributes */
|
2018-03-23 17:43:59 +00:00
|
|
|
this.xhr = null;
|
2018-03-25 11:40:35 +00:00
|
|
|
|
|
|
|
/* (2) Manage _resource format (remove http://, https://) */
|
|
|
|
this.resource = _resource.replace(/^https?:\/\//, '');
|
|
|
|
|
|
|
|
/* (3) Manage @proto default values */
|
2018-03-27 14:34:30 +00:00
|
|
|
this.proto = typeof _auth !== 'object' || _auth['ssl'] == null || _auth.ssl !== true;
|
2018-03-25 11:40:35 +00:00
|
|
|
this.proto = (this.proto) ? 'https://' : 'http://';
|
2018-03-23 17:43:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Binds the client to the resource
|
|
|
|
*
|
|
|
|
* @return bound<boolean> Whether the binding has been successful
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
bind(){
|
|
|
|
|
|
|
|
/* (0) Parent check */
|
|
|
|
if( !super.bind() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (1) Create XHR instance */
|
|
|
|
this.xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttpRequest');
|
|
|
|
|
|
|
|
/* (2) Manage after request callbacks */
|
|
|
|
this.xhr.onreadystatechange = function(){
|
|
|
|
|
|
|
|
switch(this.xhr.readyState){
|
|
|
|
|
|
|
|
// Request sent
|
|
|
|
case 4:
|
|
|
|
|
|
|
|
// Got response
|
2018-03-28 18:29:11 +00:00
|
|
|
if( this.xhr.status === 200 || this.xhr.status === 401 ){
|
2018-03-23 17:43:59 +00:00
|
|
|
|
2018-03-24 14:13:27 +00:00
|
|
|
this.event.onreceive(this.xhr.responseText);
|
|
|
|
this.event.onclose();
|
2018-03-23 17:43:59 +00:00
|
|
|
|
|
|
|
// Request error
|
2018-03-24 14:13:27 +00:00
|
|
|
}else
|
|
|
|
this.event.onclose();
|
2018-03-23 17:43:59 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}.bind(this);
|
|
|
|
|
2018-03-24 14:13:27 +00:00
|
|
|
/* (3) State is now CONNECTED (ready to send) */
|
2018-03-23 17:43:59 +00:00
|
|
|
this.state = ClientDriver.STATE.CONNECTED;
|
|
|
|
|
2018-03-24 14:13:27 +00:00
|
|
|
/* (4) Bind to callback onconnected() */
|
|
|
|
this.event.onconnected();
|
2018-03-23 17:43:59 +00:00
|
|
|
|
|
|
|
/* (5) Return success */
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Send request
|
|
|
|
*
|
|
|
|
* @_request<Object> Request data
|
|
|
|
*
|
|
|
|
* @return sent<boolean> Whether the request has been successful
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
send(_request){
|
|
|
|
|
|
|
|
/* (1) Fast argument check
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Parent check */
|
|
|
|
if( !super.send(_request) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (2) Error: invalid _request.path */
|
2018-03-24 14:13:27 +00:00
|
|
|
if( typeof _request.path !== 'string' ){
|
|
|
|
this.state = ClientDriver.STATE.CONNECTED;
|
2018-03-23 17:43:59 +00:00
|
|
|
return false;
|
2018-03-24 14:13:27 +00:00
|
|
|
}
|
2018-03-23 17:43:59 +00:00
|
|
|
|
2018-03-25 11:55:31 +00:00
|
|
|
/* (3) Default @http_token value */
|
|
|
|
let http_token = null;
|
|
|
|
if( typeof _request.http_token === 'string' )
|
|
|
|
http_token = _request.http_token;
|
|
|
|
|
|
|
|
|
2018-03-23 17:43:59 +00:00
|
|
|
/* (2) Manage _request.path argument
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Error: invalid path format */
|
2018-03-24 14:13:27 +00:00
|
|
|
if( !/^([A-Z]+) (.+)/i.test(_request.path) ){
|
|
|
|
this.state = ClientDriver.STATE.CONNECTED;
|
2018-03-23 17:43:59 +00:00
|
|
|
return;
|
2018-03-24 14:13:27 +00:00
|
|
|
}
|
2018-03-23 17:43:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (2) Extract path data */
|
|
|
|
let http_method = RegExp.$1;
|
|
|
|
let http_uri = RegExp.$2;
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Manage _request.form argument
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Helpers */
|
|
|
|
let is_object = typeof _request.form === 'object';
|
|
|
|
let is_formdata = is_object && _request.form instanceof FormData;
|
|
|
|
|
|
|
|
/* (2) Create default value for _request.form */
|
2018-03-27 12:52:14 +00:00
|
|
|
var form_data = new FormData();
|
2018-03-23 17:43:59 +00:00
|
|
|
|
|
|
|
/* (3) If already FormData object -> store as it is */
|
|
|
|
if( is_object && is_formdata )
|
2018-03-27 12:52:14 +00:00
|
|
|
form_data = _request.form;
|
|
|
|
|
2018-03-23 17:43:59 +00:00
|
|
|
|
|
|
|
/* (4) If just a raw object -> convert to FormData */
|
|
|
|
if( is_object && !is_formdata )
|
2018-03-27 12:52:14 +00:00
|
|
|
Object.keys(_request.form).map( (k) => form_data.append(k, _request.form[k]) );
|
2018-03-23 17:43:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (4) Open connection
|
|
|
|
---------------------------------------------------------*/
|
2018-03-25 11:55:31 +00:00
|
|
|
/* (1) Build full request URI */
|
|
|
|
let request_uri = this.resource.split(/\/$/).concat(http_uri.split(/^\//)).filter((v) => v.trim().length).join('/');
|
2018-03-23 17:43:59 +00:00
|
|
|
|
2018-03-27 14:34:30 +00:00
|
|
|
/* (2) Open connection */
|
|
|
|
console.log(`API >>> ${this.proto}${request_uri}`);
|
|
|
|
this.xhr.open(http_method, `${this.proto}${request_uri}`, true);
|
2018-03-29 10:31:23 +00:00
|
|
|
if( http_token != null )
|
|
|
|
this.xhr.setRequestHeader('Authorization', `Basic ${btoa(http_token)}`);
|
2018-03-25 11:40:35 +00:00
|
|
|
|
2018-03-27 14:34:30 +00:00
|
|
|
/* (3) Send request */
|
2018-03-23 17:43:59 +00:00
|
|
|
this.xhr.send(form_data);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-25 11:40:35 +00:00
|
|
|
|
2018-03-23 17:43:59 +00:00
|
|
|
}
|