d4a77f790729c64e46aefe8cbb82fb3095e3e4f2
[mat/samba.git] / services / resources.esp
1 <%
2
3 /*
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
7  * scripts.
8  */
9
10 function _resourcesCreate()
11 {
12     /* The being-created resources object */
13     var o = new Object();
14
15     /*
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.
19      */
20     o.RESOURCE_LIMIT = 100;
21
22     /* List of current resources */
23     o.resourceList = new Object();
24
25     /* Resource id values will be constantly incrementing; never reset. */
26     o.resourceList.id = 0;
27
28     /* We'll maintain our own count of the number of open resources */
29     o.resourceList.count = 0;
30
31     /*
32      * Resource types
33      */
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. */
39
40
41     /*
42      * Set a new saved resource.
43      */
44     function _set(resource, type, error)
45     {
46         /* Do they already have the maximum number of resources allocated? */
47         if (this.resourceList.count >= this.RESOURCE_LIMIT)
48         {
49             /* Yup. */
50             error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
51             error.setError(JsonRpcError_ResourceError,
52                            "Session limit on resources (" +
53                            RESOURCE_LIMIT +
54                            ") exceeded.");
55             return error;
56         }
57
58         /* Allocate an object to hold the new resource and its type */
59         var r = new Object();
60
61         /* Save the resource and its type */
62         r.resource = resource;
63         r.type = type;
64
65         /* Add this resource to the list */
66         this.resourceList[this.resourceList.id] = r;
67
68         /* There's a new resource in the list! */
69         this.resourceList.count++;
70
71         /*
72          * Return the index of the resource, its resource id, and advance to
73          * the next resource id for next time.
74          */
75         var id = this.resourceList.id;
76         this.resourceList.id++;
77         return id;
78     }
79     o.set = _set;
80
81     /*
82      * Get a previously-saved resource
83      */
84     function _get(resourceId, type, error)
85     {
86         /* Does the specified resource id exist? */
87         if (! this.resourceList[resourceId])
88         {
89             /* Nope. */
90             error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
91             error.setError(jsonrpc.Constant.ErrorCode.ResourceError,
92                            "Resource not found.");
93             return error;
94         }
95
96         /* Retrieve the resource */
97         var r = this.resourceList[resourceId];
98
99         /* Is the specified resource the correct type? */
100         if (r.type != type)
101         {
102             /* Nope. */
103             error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
104             error.setError(jsonrpc.Constant.ErrorCode.ResourceError,
105                            "Incorrect type for specified resource id.");
106             return error;
107         }
108
109         /* Give 'em what they came for! */
110         return r.resource;
111     }
112     o.get = _get;
113
114     /*
115      * Release a previously-saved resource, allowing it to be freed
116      */
117     function _release(resourceId, error)
118     {
119         /* Does the specified resource id exist? */
120         if (! this.resourceList[resourceId])
121         {
122             /* Nope. */
123             error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
124             error.setError(jsonrpc.Constant.ErrorCode.ResourceError,
125                            "Resource not found.");
126             return error;
127         }
128
129         /* It exists.  Delete it. */
130         delete this.resourceList[resourceId];
131
132         /* There's now one fewer resources in the list */
133         this.resourceList.count--;
134     }
135     o.release = _release;
136
137     /*
138      * Retrieve the list of resources (for debugging) */
139      */
140     function _getList(error)
141     {
142         return this.resourceList;
143     }
144     o.getList = _getList;
145
146     return o;
147 }
148
149 /* singleton: create session resources list */
150 if (! session.resources)
151 {
152     session.resources = _resourcesCreate();
153 }
154
155
156 /*
157  * Local Variables:
158  * mode: c
159  * End:
160  */
161 %>