[lib.client.xhr][lib.client.client-driver] manages authentication and HTTP token for XHR
This commit is contained in:
parent
d56e2810a1
commit
0a0fd9a27f
|
@ -18,9 +18,10 @@ export default class ClientDriver{
|
||||||
/* (1) Creates a client driver
|
/* (1) Creates a client driver
|
||||||
*
|
*
|
||||||
* @_resource<String> Target resource (typically an URL)
|
* @_resource<String> Target resource (typically an URL)
|
||||||
|
* @_auth<mixed> Authentication data (depends on Driver implementation)
|
||||||
*
|
*
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
constructor(_resource){
|
constructor(_resource, _auth={}){
|
||||||
|
|
||||||
/* (1) Initialize driver
|
/* (1) Initialize driver
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
|
@ -28,6 +29,7 @@ export default class ClientDriver{
|
||||||
this.force_close = false;
|
this.force_close = false;
|
||||||
this.state = ClientDriver.STATE.READY;
|
this.state = ClientDriver.STATE.READY;
|
||||||
this.stack = [];
|
this.stack = [];
|
||||||
|
this.error = false;
|
||||||
|
|
||||||
/* (2) Default User callbacks */
|
/* (2) Default User callbacks */
|
||||||
this.callback = {
|
this.callback = {
|
||||||
|
@ -90,6 +92,7 @@ export default class ClientDriver{
|
||||||
|
|
||||||
/* (2) Set explicit attributes */
|
/* (2) Set explicit attributes */
|
||||||
this.resource = _resource;
|
this.resource = _resource;
|
||||||
|
this.auth = _auth;
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
|
@ -5,20 +5,35 @@ export default class XHRClientDriver extends ClientDriver{
|
||||||
|
|
||||||
/* (1) Creates a client driver
|
/* (1) Creates a client driver
|
||||||
*
|
*
|
||||||
* @_resource<String> Target resource (typically an URL)
|
* @_resource<String> Target resource
|
||||||
|
* (typically a hostname without trailing HTTP:// nor HTTPS://)
|
||||||
|
* @_auth<AuthObject> Authentication object
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* [fornat::AuthObject]
|
||||||
|
* {
|
||||||
|
* ssl: <boolean>
|
||||||
|
* }
|
||||||
*
|
*
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
constructor(_resource){
|
constructor(_resource, _auth={ssl:true}){
|
||||||
|
|
||||||
/* (0) Parent check */
|
/* (0) Parent check */
|
||||||
if( !super().error )
|
if( super(_resource, _auth).error ){
|
||||||
|
this.force_close = true;
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/* (1) Set inherited attributes */
|
/* (1) Create useful attributes */
|
||||||
this.resource = _resource;
|
|
||||||
|
|
||||||
/* (2) Create useful attributes */
|
|
||||||
this.xhr = null;
|
this.xhr = null;
|
||||||
|
this.http_token = null;
|
||||||
|
|
||||||
|
/* (2) Manage _resource format (remove http://, https://) */
|
||||||
|
this.resource = _resource.replace(/^https?:\/\//, '');
|
||||||
|
|
||||||
|
/* (3) Manage @proto default values */
|
||||||
|
this.proto = typeof _auth !== 'object' && _auth['ssl'] != null && _auth.ssl === true;
|
||||||
|
this.proto = (this.proto) ? 'https://' : 'http://';
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,8 +147,11 @@ export default class XHRClientDriver extends ClientDriver{
|
||||||
/* (1) Build full URL */
|
/* (1) Build full URL */
|
||||||
let request_url = this.resource.split(/\/$/).concat(http_uri.split(/^\//)).filter((v) => v.trim().length).join('/');
|
let request_url = this.resource.split(/\/$/).concat(http_uri.split(/^\//)).filter((v) => v.trim().length).join('/');
|
||||||
|
|
||||||
/* (2) Open connection */
|
/* (2) Build protocol (http, https) */
|
||||||
this.xhr.open(http_method, request_url, true);
|
let protocol = this.http_token != null ? `${this.proto}${this.http_token}@` : this.proto;
|
||||||
|
|
||||||
|
/* (3) Open connection */
|
||||||
|
this.xhr.open(http_method, `${protocol}${request_url}`, true);
|
||||||
|
|
||||||
/* (3) Send request */
|
/* (3) Send request */
|
||||||
this.xhr.send(form_data);
|
this.xhr.send(form_data);
|
||||||
|
@ -142,4 +160,20 @@ export default class XHRClientDriver extends ClientDriver{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* (4) Set HTTP token
|
||||||
|
*
|
||||||
|
* @_token<String> HTTP token
|
||||||
|
*
|
||||||
|
---------------------------------------------------------*/
|
||||||
|
setHttpToken(_token=null){
|
||||||
|
|
||||||
|
/* Manage _token default values */
|
||||||
|
if( typeof _token === 'string' )
|
||||||
|
this.http_token = _token;
|
||||||
|
else
|
||||||
|
this.http_token = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue