BotanSS/utils/WebParam.js

54 lines
822 B
JavaScript
Raw Normal View History

2016-02-12 21:03:21 +00:00
"use strict";
2015-07-23 09:15:24 +00:00
2016-02-12 21:03:21 +00:00
class WebParam
{
constructor( paramStr )
2015-07-23 09:15:24 +00:00
{
2016-02-12 21:03:21 +00:00
var list = {};
2015-07-23 09:15:24 +00:00
2016-02-12 21:03:21 +00:00
paramStr && paramStr.split( ";" ).forEach( function( param )
{
var parts = param.split( "=" );
list[ parts.shift().trim() ] = decodeURI( parts.join( "=" ) );
} );
2015-07-23 09:15:24 +00:00
2016-02-12 21:03:21 +00:00
this.param = list;
}
2015-07-23 09:15:24 +00:00
2016-02-12 21:03:21 +00:00
set( name, value )
{
this.param[ name ] = value;
}
2015-07-23 09:15:24 +00:00
2016-02-12 21:03:21 +00:00
get( name )
2015-07-23 09:15:24 +00:00
{
2016-02-12 21:03:21 +00:00
return this.param[ name ];
2015-07-23 09:15:24 +00:00
}
2016-02-12 21:03:21 +00:00
toString()
{
var paramStr = "";
2016-02-28 21:46:29 +00:00
for( var i in this.param )
2016-02-12 21:03:21 +00:00
{
2016-02-28 21:46:29 +00:00
paramStr += i + "=" + this.param[i] + ";";
2016-02-12 21:03:21 +00:00
}
return paramStr;
}
}
2015-07-23 09:15:24 +00:00
WebParam.ExtractHeader = function( hstr )
{
var headers = {};
hstr.split( "\r\n" ).forEach( function( v )
{
if( !v ) return;
var colx = v.indexOf( ':' );
headers[ v.substring( 0, colx ) ] = new WebParam( v.substr( colx + 1 ) );
} );
return headers;
};
module.exports = WebParam;