4 * Various JSON-RPC calls will want to maintain open resources within a
5 * session, across multiple calls. We'll provide a standardized way to
6 * maintain those open resources here, with some protection against rogue
10 function _resourcesCreate()
12 /* The being-created resources object */
16 * The maximum number of resources available to a single session. This
17 * should be more than is ever needed (even by reasonable recursive
18 * functions) but limits rogue scripts ability to generate DOS attacks.
20 o.RESOURCE_LIMIT = 100;
22 /* List of current resources */
23 o.resourceList = new Object();
25 /* Resource id values will be constantly incrementing; never reset. */
26 o.resourceList.id = 0;
28 /* We'll maintain our own count of the number of open resources */
29 o.resourceList.count = 0;
33 * Set a new saved resource.
35 function _set(resource, type, error)
37 /* Do they already have the maximum number of resources allocated? */
38 if (this.resourceList.count >= this.RESOURCE_LIMIT)
41 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
42 error.setError(jsonrpc.Constant.ServerError.ResourceError,
43 "Session limit on resources (" +
49 /* Allocate an object to hold the new resource and its type */
52 /* Save the resource and its type */
53 r.resource = resource;
55 r.id = this.resourceList.id;
57 /* Add this resource to the list */
58 this.resourceList[this.resourceList.id] = r;
60 /* There's a new resource in the list! */
61 this.resourceList.count++;
64 * Return the index of the resource, its resource id, and advance to
65 * the next resource id for next time.
67 var id = this.resourceList.id;
68 this.resourceList.id++;
74 * Get a previously-saved resource
76 function _get(resourceId, error)
78 /* Does the specified resource id exist? */
79 if (! this.resourceList[resourceId])
82 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
83 error.setError(jsonrpc.Constant.ServerError.ResourceError,
84 "Resource not found.");
88 /* Retrieve the resource */
89 var r = this.resourceList[resourceId];
91 /* Give 'em what they came for! */
97 * Find a previously-saved resource
99 function _find(type, error)
101 /* Does the specified resource id exist? */
102 for (var resourceId in this.resourceList)
104 /* Retrieve the resource */
105 var r = this.resourceList[resourceId];
107 /* Ignore "id" and "count" integer fields */
108 if (typeof(r) == "object")
110 /* Is the specified resource the correct type? */
113 /* Yup, this is the one they want. */
119 /* It wasn't found. */
125 * Release a previously-saved resource, allowing it to be freed
127 function _release(resourceId, error)
129 /* Does the specified resource id exist? */
130 if (! this.resourceList[resourceId])
133 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
134 error.setError(jsonrpc.Constant.ServerError.ResourceError,
135 "Resource not found.");
139 /* It exists. Delete it. */
140 delete this.resourceList[resourceId];
142 /* There's now one fewer resources in the list */
143 this.resourceList.count--;
145 o.release = _release;
148 * Retrieve the list of resources (for debugging) */
150 function _getList(error)
152 return this.resourceList;
154 o.getList = _getList;
159 /* singleton: create session resources list */
160 if (! session.resources)
162 session.resources = _resourcesCreate();