Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /*
2 * Copyright (c) 2006-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 #ifndef _EVRPC_INTERNAL_H_
28 #define _EVRPC_INTERNAL_H_
30 #include "http-internal.h"
32 struct evrpc;
33 struct evrpc_request_wrapper;
35 #define EVRPC_URI_PREFIX "/.rpc."
37 struct evrpc_hook {
38 TAILQ_ENTRY(evrpc_hook) next;
40 /* returns EVRPC_TERMINATE; if the rpc should be aborted.
41 * a hook is is allowed to rewrite the evbuffer
42 */
43 int (*process)(void *, struct evhttp_request *,
44 struct evbuffer *, void *);
45 void *process_arg;
46 };
48 TAILQ_HEAD(evrpc_hook_list, evrpc_hook);
50 /*
51 * this is shared between the base and the pool, so that we can reuse
52 * the hook adding functions; we alias both evrpc_pool and evrpc_base
53 * to this common structure.
54 */
56 struct evrpc_hook_ctx;
57 TAILQ_HEAD(evrpc_pause_list, evrpc_hook_ctx);
59 struct _evrpc_hooks {
60 /* hooks for processing outbound and inbound rpcs */
61 struct evrpc_hook_list in_hooks;
62 struct evrpc_hook_list out_hooks;
64 struct evrpc_pause_list pause_requests;
65 };
67 #define input_hooks common.in_hooks
68 #define output_hooks common.out_hooks
69 #define paused_requests common.pause_requests
71 struct evrpc_base {
72 struct _evrpc_hooks common;
74 /* the HTTP server under which we register our RPC calls */
75 struct evhttp* http_server;
77 /* a list of all RPCs registered with us */
78 TAILQ_HEAD(evrpc_list, evrpc) registered_rpcs;
79 };
81 struct evrpc_req_generic;
82 void evrpc_reqstate_free(struct evrpc_req_generic* rpc_state);
84 /* A pool for holding evhttp_connection objects */
85 struct evrpc_pool {
86 struct _evrpc_hooks common;
88 struct event_base *base;
90 struct evconq connections;
92 int timeout;
94 TAILQ_HEAD(evrpc_requestq, evrpc_request_wrapper) (requests);
95 };
97 struct evrpc_hook_ctx {
98 TAILQ_ENTRY(evrpc_hook_ctx) next;
100 void *ctx;
101 void (*cb)(void *, enum EVRPC_HOOK_RESULT);
102 };
104 struct evrpc_meta {
105 TAILQ_ENTRY(evrpc_meta) next;
106 char *key;
108 void *data;
109 size_t data_size;
110 };
112 TAILQ_HEAD(evrpc_meta_list, evrpc_meta);
114 struct evrpc_hook_meta {
115 struct evrpc_meta_list meta_data;
116 struct evhttp_connection *evcon;
117 };
119 /* allows association of meta data with a request */
120 static void evrpc_hook_associate_meta(struct evrpc_hook_meta **pctx,
121 struct evhttp_connection *evcon);
123 /* creates a new meta data store */
124 static struct evrpc_hook_meta *evrpc_hook_meta_new(void);
126 /* frees the meta data associated with a request */
127 static void evrpc_hook_context_free(struct evrpc_hook_meta *ctx);
129 /* the server side of an rpc */
131 /* We alias the RPC specific structs to this voided one */
132 struct evrpc_req_generic {
133 /*
134 * allows association of meta data via hooks - needs to be
135 * synchronized with evrpc_request_wrapper
136 */
137 struct evrpc_hook_meta *hook_meta;
139 /* the unmarshaled request object */
140 void *request;
142 /* the empty reply object that needs to be filled in */
143 void *reply;
145 /*
146 * the static structure for this rpc; that can be used to
147 * automatically unmarshal and marshal the http buffers.
148 */
149 struct evrpc *rpc;
151 /*
152 * the http request structure on which we need to answer.
153 */
154 struct evhttp_request* http_req;
156 /*
157 * Temporary data store for marshaled data
158 */
159 struct evbuffer* rpc_data;
160 };
162 /* the client side of an rpc request */
163 struct evrpc_request_wrapper {
164 /*
165 * allows association of meta data via hooks - needs to be
166 * synchronized with evrpc_req_generic.
167 */
168 struct evrpc_hook_meta *hook_meta;
170 TAILQ_ENTRY(evrpc_request_wrapper) next;
172 /* pool on which this rpc request is being made */
173 struct evrpc_pool *pool;
175 /* connection on which the request is being sent */
176 struct evhttp_connection *evcon;
178 /* the actual request */
179 struct evhttp_request *req;
181 /* event for implementing request timeouts */
182 struct event ev_timeout;
184 /* the name of the rpc */
185 char *name;
187 /* callback */
188 void (*cb)(struct evrpc_status*, void *request, void *reply, void *arg);
189 void *cb_arg;
191 void *request;
192 void *reply;
194 /* unmarshals the buffer into the proper request structure */
195 void (*request_marshal)(struct evbuffer *, void *);
197 /* removes all stored state in the reply */
198 void (*reply_clear)(void *);
200 /* marshals the reply into a buffer */
201 int (*reply_unmarshal)(void *, struct evbuffer*);
202 };
204 #endif /* _EVRPC_INTERNAL_H_ */