-
Notifications
You must be signed in to change notification settings - Fork 5
/
response.cfc
106 lines (93 loc) · 2.72 KB
/
response.cfc
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
component accessors="true" {
/**
* @getter true
* @setter true
* @type struct
* @hint The response from cfhttp request
**/
property name="response";
/**
* @getter true
* @setter true
* @type struct
* @hint The options passed into the response class
**/
property name="options";
/**
* @getter true
* @setter true
* @type any
* @hint parsed
**/
property name="parsed";
/**
* @hint Initializes a Response instance
**/
variables['parsed'] = "";
public oauth2.response function init(struct response, opts = {}) {
setResponse(arguments.response);
setOptions(arguments.opts);
structAppend(getOptions(),{ 'parse': 'automatic' },false);
return this;
}
public function headers() {
return structKeyExists(getResponse(),'responseheader') ? getResponse()['headers'] : {};
}
public function content_type() {
return structKeyExists(getResponse(),'mimetype') ? getResponse()['mimetype'] : {};
}
public function status() {
return structKeyExists(getResponse(),'statuscode') ? getResponse()['statuscode'] : {};
}
public function body() {
return structKeyExists(getResponse(),'filecontent') ? getResponse()['filecontent'] : '';
}
public function parsed() {
if (structKeyExists(variables.PARSERS,parser())) {
if (isEmpty(getParsed())) {
setParsed(PARSERS[parser()](body()));
}
return getParsed();
}
}
// determines the parser that will be used to supply the content to parsed()
public function parser() {
if (structKeyExists(variables.PARSERS,getOptions()['parse'])) {
return getOptions()['parse'];
}
return CONTENT_TYPES[content_type()];
}
public function register_parser(string key, array mime_types, function method) {
variables.PARSERS[arguments.key] = method;
}
variables.PARSERS = {
'json': function(body) {
return deserializeJson(arguments.body);
},
'query': function(body) {
var myStruct = StructNew();
var i = 0;
var delimiter = ",";
var tempList = arrayNew(1);
if (ArrayLen(arguments) gt 1) {
delimiter = arguments[2];
}
tempList = listToArray(list, delimiter);
for (i=1; i LTE ArrayLen(tempList); i=i+1){
if (not structkeyexists(myStruct, trim(ListFirst(tempList[i], "=")))) {
StructInsert(myStruct, trim(ListFirst(tempList[i], "=")), trim(ListLast(tempList[i], "=")));
}
}
return myStruct;
return listTo(arguments.body);
}
}
//CONTENT TYPE ASSIGNMENTS FOR VARIOUS HTTP CONTENT TYPES
variables.CONTENT_TYPES = {
'application/json': 'json',
'text/javascript': 'json',
'application/x-www-form-urlencoded': 'query',
'text/html': 'text',
'text/plain': 'text'
}
}