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;
34 o.Type = new Object();
35 o.Type.ldb = 1; /* database handle */
36 o.Type.tid = 2; /* tree id */
37 o.Type.fid = 3; /* file id */
38 /* etc., etc., etc. */
42 * Set a new saved resource.
44 function _set(resource, type, error)
46 /* Do they already have the maximum number of resources allocated? */
47 if (this.resourceList.count >= this.RESOURCE_LIMIT)
50 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
51 error.setError(JsonRpcError_ResourceError,
52 "Session limit on resources (" +
58 /* Allocate an object to hold the new resource and its type */
61 /* Save the resource and its type */
62 r.resource = resource;
65 /* Add this resource to the list */
66 this.resourceList[this.resourceList.id] = r;
68 /* There's a new resource in the list! */
69 this.resourceList.count++;
72 * Return the index of the resource, its resource id, and advance to
73 * the next resource id for next time.
75 var id = this.resourceList.id;
76 this.resourceList.id++;
82 * Get a previously-saved resource
84 function _get(resourceId, type, error)
86 /* Does the specified resource id exist? */
87 if (! this.resourceList[resourceId])
90 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
91 error.setError(jsonrpc.Constant.ErrorCode.ResourceError,
92 "Resource not found.");
96 /* Retrieve the resource */
97 var r = this.resourceList[resourceId];
99 /* Is the specified resource the correct type? */
103 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
104 error.setError(jsonrpc.Constant.ErrorCode.ResourceError,
105 "Incorrect type for specified resource id.");
109 /* Give 'em what they came for! */
115 * Release a previously-saved resource, allowing it to be freed
117 function _release(resourceId, error)
119 /* Does the specified resource id exist? */
120 if (! this.resourceList[resourceId])
123 error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
124 error.setError(jsonrpc.Constant.ErrorCode.ResourceError,
125 "Resource not found.");
129 /* It exists. Delete it. */
130 delete this.resourceList[resourceId];
132 /* There's now one fewer resources in the list */
133 this.resourceList.count--;
135 o.release = _release;
138 * Retrieve the list of resources (for debugging) */
140 function _getList(error)
142 return this.resourceList;
144 o.getList = _getList;
149 /* singleton: create session resources list */
150 if (! session.resources)
152 session.resources = _resourcesCreate();