This repository has been archived by the owner on Sep 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
querystring-handler.js
76 lines (72 loc) · 2.07 KB
/
querystring-handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* MOST Web Framework
* A JavaScript Web Framework
* http://themost.io
*
* Copyright (c) 2014, Kyriakos Barbounakis k.barbounakis@gmail.com, Anthi Oikonomou anthioikonomou@gmail.com
*
* Released under the BSD3-Clause license
* Date: 2014-12-02
*/
/**
* @ignore
*/
var util = require('util'),
querystring = require('querystring');
/**
* Provides a case insensitive attribute getter
* @param name
* @returns {*}
* @private
*/
function caseInsensitiveAttribute(name) {
if (typeof name === 'string') {
if (this[name])
return this[name];
//otherwise make a case insensitive search
var re = new RegExp('^' + name + '$','i');
var p = Object.keys(this).filter(function(x) { return re.test(x); })[0];
if (p)
return this[p];
}
}
function caseInsensitiveHasAttribute(name) {
if (typeof name === 'string') {
if (this[name])
return true;
//otherwise make a case insensitive search
var re = new RegExp('^' + name + '$','i');
var p = Object.keys(this).filter(function(x) { return re.test(x); })[0];
if (p)
return true;
}
return false;
}
function QuerystringHandler() {
//
}
QuerystringHandler.prototype.beginRequest = function(context, callback) {
context = context || {};
callback = callback || function() {};
var request = context.request;
if (typeof request === 'undefined') {
callback();
return;
}
try {
context.params = context.params || {};
//apply case insensitivity search in params object
context.params.attr = caseInsensitiveAttribute;
context.params.hasAttr = caseInsensitiveHasAttribute;
//add query string params
if (request.url.indexOf('?') > 0)
util._extend(context.params, querystring.parse(request.url.substring(request.url.indexOf('?') + 1)));
callback();
}
catch(e) {
callback(e);
}
};
if (typeof exports !== 'undefined') {
module.exports.createInstance = function() { return new QuerystringHandler(); };
}