r21103: This seems to do the 'right thing' in applying the correct access
[kai/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     /*
33      * Set a new saved resource.
34      */
35     function _set(resource, type, error)
36     {
37         /* Do they already have the maximum number of resources allocated? */
38         if (this.resourceList.count >= this.RESOURCE_LIMIT)
39         {
40             /* Yup. */
41             error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
42             error.setError(jsonrpc.Constant.ServerError.ResourceError,
43                            "Session limit on resources (" +
44                            RESOURCE_LIMIT +
45                            ") exceeded.");
46             return error;
47         }
48
49         /* Allocate an object to hold the new resource and its type */
50         var r = new Object();
51
52         /* Save the resource and its type */
53         r.resource = resource;
54         r.type = type;
55
56         /* Add this resource to the list */
57         this.resourceList[this.resourceList.id] = r;
58
59         /* There's a new resource in the list! */
60         this.resourceList.count++;
61
62         /*
63          * Return the index of the resource, its resource id, and advance to
64          * the next resource id for next time.
65          */
66         var id = this.resourceList.id;
67         this.resourceList.id++;
68         return id;
69     }
70     o.set = _set;
71
72     /*
73      * Get a previously-saved resource
74      */
75     function _get(resourceId, error)
76     {
77         /* Does the specified resource id exist? */
78         if (! this.resourceList[resourceId])
79         {
80             /* Nope. */
81             error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
82             error.setError(jsonrpc.Constant.ServerError.ResourceError,
83                            "Resource not found.");
84             return error;
85         }
86
87         /* Retrieve the resource */
88         var r = this.resourceList[resourceId];
89
90         /* Give 'em what they came for! */
91         return r.resource;
92     }
93     o.get = _get;
94
95     /*
96      * Find a previously-saved resource
97      */
98     function _find(type, error)
99     {
100         /* Does the specified resource id exist? */
101         for (var resourceId in this.resourceList)
102         {
103             /* Retrieve the resource */
104             var r = this.resourceList[resourceId];
105
106             /* Ignore "id" and "count" integer fields */
107             if (typeof(r) == "object")
108             {
109                 /* Is the specified resource the correct type? */
110                 if (r.type == type)
111                 {
112                     /* Yup, this is the one they want. */
113                     return resourceId;
114                 }
115             }
116         }
117
118         /* It wasn't found. */
119         return undefined;
120     }
121     o.find = _find;
122
123     /*
124      * Release a previously-saved resource, allowing it to be freed
125      */
126     function _release(resourceId, error)
127     {
128         /* Does the specified resource id exist? */
129         if (! this.resourceList[resourceId])
130         {
131             /* Nope. */
132             error.setOrigin(jsonrpc.Constant.ErrorOrigin.Server);
133             error.setError(jsonrpc.Constant.ServerError.ResourceError,
134                            "Resource not found.");
135             return error;
136         }
137
138         /* It exists.  Delete it. */
139         delete this.resourceList[resourceId];
140
141         /* There's now one fewer resources in the list */
142         this.resourceList.count--;
143     }
144     o.release = _release;
145
146     /*
147      * Retrieve the list of resources (for debugging) */
148      */
149     function _getList(error)
150     {
151         return this.resourceList;
152     }
153     o.getList = _getList;
154
155     return o;
156 }
157
158 /* singleton: create session resources list */
159 if (! session.resources)
160 {
161     session.resources = _resourcesCreate();
162 }
163
164
165 /*
166  * Local Variables:
167  * mode: c
168  * End:
169  */
170 %>