2 * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include "event2/event-config.h"
28 #include "evconfig-private.h"
31 #define WIN32_LEAN_AND_MEAN
34 #undef WIN32_LEAN_AND_MEAN
37 #include <sys/types.h>
39 #include <sys/socket.h>
41 #ifdef EVENT__HAVE_SYS_TIME_H
44 #include <sys/queue.h>
54 #include <sys/queue.h>
56 #include "event2/event.h"
57 #include "event2/event_struct.h"
58 #include "event2/rpc.h"
59 #include "event2/rpc_struct.h"
60 #include "evrpc-internal.h"
61 #include "event2/http.h"
62 #include "event2/buffer.h"
63 #include "event2/tag.h"
64 #include "event2/http_struct.h"
65 #include "event2/http_compat.h"
66 #include "event2/util.h"
67 #include "util-internal.h"
68 #include "log-internal.h"
69 #include "mm-internal.h"
72 evrpc_init(struct evhttp
*http_server
)
74 struct evrpc_base
* base
= mm_calloc(1, sizeof(struct evrpc_base
));
78 /* we rely on the tagging sub system */
81 TAILQ_INIT(&base
->registered_rpcs
);
82 TAILQ_INIT(&base
->input_hooks
);
83 TAILQ_INIT(&base
->output_hooks
);
85 TAILQ_INIT(&base
->paused_requests
);
87 base
->http_server
= http_server
;
93 evrpc_free(struct evrpc_base
*base
)
96 struct evrpc_hook
*hook
;
97 struct evrpc_hook_ctx
*pause
;
100 while ((rpc
= TAILQ_FIRST(&base
->registered_rpcs
)) != NULL
) {
101 r
= evrpc_unregister_rpc(base
, rpc
->uri
);
102 EVUTIL_ASSERT(r
== 0);
104 while ((pause
= TAILQ_FIRST(&base
->paused_requests
)) != NULL
) {
105 TAILQ_REMOVE(&base
->paused_requests
, pause
, next
);
108 while ((hook
= TAILQ_FIRST(&base
->input_hooks
)) != NULL
) {
109 r
= evrpc_remove_hook(base
, EVRPC_INPUT
, hook
);
112 while ((hook
= TAILQ_FIRST(&base
->output_hooks
)) != NULL
) {
113 r
= evrpc_remove_hook(base
, EVRPC_OUTPUT
, hook
);
120 evrpc_add_hook(void *vbase
,
121 enum EVRPC_HOOK_TYPE hook_type
,
122 int (*cb
)(void *, struct evhttp_request
*, struct evbuffer
*, void *),
125 struct evrpc_hooks_
*base
= vbase
;
126 struct evrpc_hook_list
*head
= NULL
;
127 struct evrpc_hook
*hook
= NULL
;
130 head
= &base
->in_hooks
;
133 head
= &base
->out_hooks
;
136 EVUTIL_ASSERT(hook_type
== EVRPC_INPUT
|| hook_type
== EVRPC_OUTPUT
);
139 hook
= mm_calloc(1, sizeof(struct evrpc_hook
));
140 EVUTIL_ASSERT(hook
!= NULL
);
143 hook
->process_arg
= cb_arg
;
144 TAILQ_INSERT_TAIL(head
, hook
, next
);
150 evrpc_remove_hook_internal(struct evrpc_hook_list
*head
, void *handle
)
152 struct evrpc_hook
*hook
= NULL
;
153 TAILQ_FOREACH(hook
, head
, next
) {
154 if (hook
== handle
) {
155 TAILQ_REMOVE(head
, hook
, next
);
165 * remove the hook specified by the handle
169 evrpc_remove_hook(void *vbase
, enum EVRPC_HOOK_TYPE hook_type
, void *handle
)
171 struct evrpc_hooks_
*base
= vbase
;
172 struct evrpc_hook_list
*head
= NULL
;
175 head
= &base
->in_hooks
;
178 head
= &base
->out_hooks
;
181 EVUTIL_ASSERT(hook_type
== EVRPC_INPUT
|| hook_type
== EVRPC_OUTPUT
);
184 return (evrpc_remove_hook_internal(head
, handle
));
188 evrpc_process_hooks(struct evrpc_hook_list
*head
, void *ctx
,
189 struct evhttp_request
*req
, struct evbuffer
*evbuf
)
191 struct evrpc_hook
*hook
;
192 TAILQ_FOREACH(hook
, head
, next
) {
193 int res
= hook
->process(ctx
, req
, evbuf
, hook
->process_arg
);
194 if (res
!= EVRPC_CONTINUE
)
198 return (EVRPC_CONTINUE
);
201 static void evrpc_pool_schedule(struct evrpc_pool
*pool
);
202 static void evrpc_request_cb(struct evhttp_request
*, void *);
205 * Registers a new RPC with the HTTP server. The evrpc object is expected
206 * to have been filled in via the EVRPC_REGISTER_OBJECT macro which in turn
207 * calls this function.
211 evrpc_construct_uri(const char *uri
)
213 char *constructed_uri
;
214 size_t constructed_uri_len
;
216 constructed_uri_len
= strlen(EVRPC_URI_PREFIX
) + strlen(uri
) + 1;
217 if ((constructed_uri
= mm_malloc(constructed_uri_len
)) == NULL
)
218 event_err(1, "%s: failed to register rpc at %s",
220 memcpy(constructed_uri
, EVRPC_URI_PREFIX
, strlen(EVRPC_URI_PREFIX
));
221 memcpy(constructed_uri
+ strlen(EVRPC_URI_PREFIX
), uri
, strlen(uri
));
222 constructed_uri
[constructed_uri_len
- 1] = '\0';
224 return (constructed_uri
);
228 evrpc_register_rpc(struct evrpc_base
*base
, struct evrpc
*rpc
,
229 void (*cb
)(struct evrpc_req_generic
*, void *), void *cb_arg
)
231 char *constructed_uri
= evrpc_construct_uri(rpc
->uri
);
235 rpc
->cb_arg
= cb_arg
;
237 TAILQ_INSERT_TAIL(&base
->registered_rpcs
, rpc
, next
);
239 evhttp_set_cb(base
->http_server
,
244 mm_free(constructed_uri
);
250 evrpc_unregister_rpc(struct evrpc_base
*base
, const char *name
)
252 char *registered_uri
= NULL
;
256 /* find the right rpc; linear search might be slow */
257 TAILQ_FOREACH(rpc
, &base
->registered_rpcs
, next
) {
258 if (strcmp(rpc
->uri
, name
) == 0)
262 /* We did not find an RPC with this name */
265 TAILQ_REMOVE(&base
->registered_rpcs
, rpc
, next
);
267 registered_uri
= evrpc_construct_uri(name
);
269 /* remove the http server callback */
270 r
= evhttp_del_cb(base
->http_server
, registered_uri
);
271 EVUTIL_ASSERT(r
== 0);
273 mm_free(registered_uri
);
275 mm_free((char *)rpc
->uri
);
280 static int evrpc_pause_request(void *vbase
, void *ctx
,
281 void (*cb
)(void *, enum EVRPC_HOOK_RESULT
));
282 static void evrpc_request_cb_closure(void *, enum EVRPC_HOOK_RESULT
);
285 evrpc_request_cb(struct evhttp_request
*req
, void *arg
)
287 struct evrpc
*rpc
= arg
;
288 struct evrpc_req_generic
*rpc_state
= NULL
;
290 /* let's verify the outside parameters */
291 if (req
->type
!= EVHTTP_REQ_POST
||
292 evbuffer_get_length(req
->input_buffer
) <= 0)
295 rpc_state
= mm_calloc(1, sizeof(struct evrpc_req_generic
));
296 if (rpc_state
== NULL
)
298 rpc_state
->rpc
= rpc
;
299 rpc_state
->http_req
= req
;
300 rpc_state
->rpc_data
= NULL
;
302 if (TAILQ_FIRST(&rpc
->base
->input_hooks
) != NULL
) {
305 evrpc_hook_associate_meta_(&rpc_state
->hook_meta
, req
->evcon
);
308 * allow hooks to modify the outgoing request
310 hook_res
= evrpc_process_hooks(&rpc
->base
->input_hooks
,
311 rpc_state
, req
, req
->input_buffer
);
313 case EVRPC_TERMINATE
:
316 evrpc_pause_request(rpc
->base
, rpc_state
,
317 evrpc_request_cb_closure
);
322 EVUTIL_ASSERT(hook_res
== EVRPC_TERMINATE
||
323 hook_res
== EVRPC_CONTINUE
||
324 hook_res
== EVRPC_PAUSE
);
328 evrpc_request_cb_closure(rpc_state
, EVRPC_CONTINUE
);
332 evrpc_reqstate_free_(rpc_state
);
333 evhttp_send_error(req
, HTTP_SERVUNAVAIL
, NULL
);
338 evrpc_request_cb_closure(void *arg
, enum EVRPC_HOOK_RESULT hook_res
)
340 struct evrpc_req_generic
*rpc_state
= arg
;
342 struct evhttp_request
*req
;
344 EVUTIL_ASSERT(rpc_state
);
345 rpc
= rpc_state
->rpc
;
346 req
= rpc_state
->http_req
;
348 if (hook_res
== EVRPC_TERMINATE
)
351 /* let's check that we can parse the request */
352 rpc_state
->request
= rpc
->request_new(rpc
->request_new_arg
);
353 if (rpc_state
->request
== NULL
)
356 if (rpc
->request_unmarshal(
357 rpc_state
->request
, req
->input_buffer
) == -1) {
358 /* we failed to parse the request; that's a bummer */
362 /* at this point, we have a well formed request, prepare the reply */
364 rpc_state
->reply
= rpc
->reply_new(rpc
->reply_new_arg
);
365 if (rpc_state
->reply
== NULL
)
368 /* give the rpc to the user; they can deal with it */
369 rpc
->cb(rpc_state
, rpc
->cb_arg
);
374 evrpc_reqstate_free_(rpc_state
);
375 evhttp_send_error(req
, HTTP_SERVUNAVAIL
, NULL
);
381 evrpc_reqstate_free_(struct evrpc_req_generic
* rpc_state
)
384 EVUTIL_ASSERT(rpc_state
!= NULL
);
385 rpc
= rpc_state
->rpc
;
387 /* clean up all memory */
388 if (rpc_state
->hook_meta
!= NULL
)
389 evrpc_hook_context_free_(rpc_state
->hook_meta
);
390 if (rpc_state
->request
!= NULL
)
391 rpc
->request_free(rpc_state
->request
);
392 if (rpc_state
->reply
!= NULL
)
393 rpc
->reply_free(rpc_state
->reply
);
394 if (rpc_state
->rpc_data
!= NULL
)
395 evbuffer_free(rpc_state
->rpc_data
);
400 evrpc_request_done_closure(void *, enum EVRPC_HOOK_RESULT
);
403 evrpc_request_done(struct evrpc_req_generic
*rpc_state
)
405 struct evhttp_request
*req
;
408 EVUTIL_ASSERT(rpc_state
);
410 req
= rpc_state
->http_req
;
411 rpc
= rpc_state
->rpc
;
413 if (rpc
->reply_complete(rpc_state
->reply
) == -1) {
414 /* the reply was not completely filled in. error out */
418 if ((rpc_state
->rpc_data
= evbuffer_new()) == NULL
) {
423 /* serialize the reply */
424 rpc
->reply_marshal(rpc_state
->rpc_data
, rpc_state
->reply
);
426 if (TAILQ_FIRST(&rpc
->base
->output_hooks
) != NULL
) {
429 evrpc_hook_associate_meta_(&rpc_state
->hook_meta
, req
->evcon
);
431 /* do hook based tweaks to the request */
432 hook_res
= evrpc_process_hooks(&rpc
->base
->output_hooks
,
433 rpc_state
, req
, rpc_state
->rpc_data
);
435 case EVRPC_TERMINATE
:
438 if (evrpc_pause_request(rpc
->base
, rpc_state
,
439 evrpc_request_done_closure
) == -1)
445 EVUTIL_ASSERT(hook_res
== EVRPC_TERMINATE
||
446 hook_res
== EVRPC_CONTINUE
||
447 hook_res
== EVRPC_PAUSE
);
451 evrpc_request_done_closure(rpc_state
, EVRPC_CONTINUE
);
455 evrpc_reqstate_free_(rpc_state
);
456 evhttp_send_error(req
, HTTP_SERVUNAVAIL
, NULL
);
461 evrpc_get_request(struct evrpc_req_generic
*req
)
467 evrpc_get_reply(struct evrpc_req_generic
*req
)
473 evrpc_request_done_closure(void *arg
, enum EVRPC_HOOK_RESULT hook_res
)
475 struct evrpc_req_generic
*rpc_state
= arg
;
476 struct evhttp_request
*req
;
477 EVUTIL_ASSERT(rpc_state
);
478 req
= rpc_state
->http_req
;
480 if (hook_res
== EVRPC_TERMINATE
)
483 /* on success, we are going to transmit marshaled binary data */
484 if (evhttp_find_header(req
->output_headers
, "Content-Type") == NULL
) {
485 evhttp_add_header(req
->output_headers
,
486 "Content-Type", "application/octet-stream");
488 evhttp_send_reply(req
, HTTP_OK
, "OK", rpc_state
->rpc_data
);
490 evrpc_reqstate_free_(rpc_state
);
495 evrpc_reqstate_free_(rpc_state
);
496 evhttp_send_error(req
, HTTP_SERVUNAVAIL
, NULL
);
501 /* Client implementation of RPC site */
503 static int evrpc_schedule_request(struct evhttp_connection
*connection
,
504 struct evrpc_request_wrapper
*ctx
);
507 evrpc_pool_new(struct event_base
*base
)
509 struct evrpc_pool
*pool
= mm_calloc(1, sizeof(struct evrpc_pool
));
513 TAILQ_INIT(&pool
->connections
);
514 TAILQ_INIT(&pool
->requests
);
516 TAILQ_INIT(&pool
->paused_requests
);
518 TAILQ_INIT(&pool
->input_hooks
);
519 TAILQ_INIT(&pool
->output_hooks
);
528 evrpc_request_wrapper_free(struct evrpc_request_wrapper
*request
)
530 if (request
->hook_meta
!= NULL
)
531 evrpc_hook_context_free_(request
->hook_meta
);
532 mm_free(request
->name
);
537 evrpc_pool_free(struct evrpc_pool
*pool
)
539 struct evhttp_connection
*connection
;
540 struct evrpc_request_wrapper
*request
;
541 struct evrpc_hook_ctx
*pause
;
542 struct evrpc_hook
*hook
;
545 while ((request
= TAILQ_FIRST(&pool
->requests
)) != NULL
) {
546 TAILQ_REMOVE(&pool
->requests
, request
, next
);
547 evrpc_request_wrapper_free(request
);
550 while ((pause
= TAILQ_FIRST(&pool
->paused_requests
)) != NULL
) {
551 TAILQ_REMOVE(&pool
->paused_requests
, pause
, next
);
555 while ((connection
= TAILQ_FIRST(&pool
->connections
)) != NULL
) {
556 TAILQ_REMOVE(&pool
->connections
, connection
, next
);
557 evhttp_connection_free(connection
);
560 while ((hook
= TAILQ_FIRST(&pool
->input_hooks
)) != NULL
) {
561 r
= evrpc_remove_hook(pool
, EVRPC_INPUT
, hook
);
565 while ((hook
= TAILQ_FIRST(&pool
->output_hooks
)) != NULL
) {
566 r
= evrpc_remove_hook(pool
, EVRPC_OUTPUT
, hook
);
574 * Add a connection to the RPC pool. A request scheduled on the pool
575 * may use any available connection.
579 evrpc_pool_add_connection(struct evrpc_pool
*pool
,
580 struct evhttp_connection
*connection
)
582 EVUTIL_ASSERT(connection
->http_server
== NULL
);
583 TAILQ_INSERT_TAIL(&pool
->connections
, connection
, next
);
586 * associate an event base with this connection
588 if (pool
->base
!= NULL
)
589 evhttp_connection_set_base(connection
, pool
->base
);
592 * unless a timeout was specifically set for a connection,
593 * the connection inherits the timeout from the pool.
595 if (!evutil_timerisset(&connection
->timeout
))
596 evhttp_connection_set_timeout(connection
, pool
->timeout
);
599 * if we have any requests pending, schedule them with the new
603 if (TAILQ_FIRST(&pool
->requests
) != NULL
) {
604 struct evrpc_request_wrapper
*request
=
605 TAILQ_FIRST(&pool
->requests
);
606 TAILQ_REMOVE(&pool
->requests
, request
, next
);
607 evrpc_schedule_request(connection
, request
);
612 evrpc_pool_remove_connection(struct evrpc_pool
*pool
,
613 struct evhttp_connection
*connection
)
615 TAILQ_REMOVE(&pool
->connections
, connection
, next
);
619 evrpc_pool_set_timeout(struct evrpc_pool
*pool
, int timeout_in_secs
)
621 struct evhttp_connection
*evcon
;
622 TAILQ_FOREACH(evcon
, &pool
->connections
, next
) {
623 evhttp_connection_set_timeout(evcon
, timeout_in_secs
);
625 pool
->timeout
= timeout_in_secs
;
629 static void evrpc_reply_done(struct evhttp_request
*, void *);
630 static void evrpc_request_timeout(evutil_socket_t
, short, void *);
633 * Finds a connection object associated with the pool that is currently
634 * idle and can be used to make a request.
636 static struct evhttp_connection
*
637 evrpc_pool_find_connection(struct evrpc_pool
*pool
)
639 struct evhttp_connection
*connection
;
640 TAILQ_FOREACH(connection
, &pool
->connections
, next
) {
641 if (TAILQ_FIRST(&connection
->requests
) == NULL
)
649 * Prototypes responsible for evrpc scheduling and hooking
652 static void evrpc_schedule_request_closure(void *ctx
, enum EVRPC_HOOK_RESULT
);
655 * We assume that the ctx is no longer queued on the pool.
658 evrpc_schedule_request(struct evhttp_connection
*connection
,
659 struct evrpc_request_wrapper
*ctx
)
661 struct evhttp_request
*req
= NULL
;
662 struct evrpc_pool
*pool
= ctx
->pool
;
663 struct evrpc_status status
;
665 if ((req
= evhttp_request_new(evrpc_reply_done
, ctx
)) == NULL
)
668 /* serialize the request data into the output buffer */
669 ctx
->request_marshal(req
->output_buffer
, ctx
->request
);
671 /* we need to know the connection that we might have to abort */
672 ctx
->evcon
= connection
;
674 /* if we get paused we also need to know the request */
677 if (TAILQ_FIRST(&pool
->output_hooks
) != NULL
) {
680 evrpc_hook_associate_meta_(&ctx
->hook_meta
, connection
);
682 /* apply hooks to the outgoing request */
683 hook_res
= evrpc_process_hooks(&pool
->output_hooks
,
684 ctx
, req
, req
->output_buffer
);
687 case EVRPC_TERMINATE
:
690 /* we need to be explicitly resumed */
691 if (evrpc_pause_request(pool
, ctx
,
692 evrpc_schedule_request_closure
) == -1)
696 /* we can just continue */
699 EVUTIL_ASSERT(hook_res
== EVRPC_TERMINATE
||
700 hook_res
== EVRPC_CONTINUE
||
701 hook_res
== EVRPC_PAUSE
);
705 evrpc_schedule_request_closure(ctx
, EVRPC_CONTINUE
);
709 memset(&status
, 0, sizeof(status
));
710 status
.error
= EVRPC_STATUS_ERR_UNSTARTED
;
711 (*ctx
->cb
)(&status
, ctx
->request
, ctx
->reply
, ctx
->cb_arg
);
712 evrpc_request_wrapper_free(ctx
);
717 evrpc_schedule_request_closure(void *arg
, enum EVRPC_HOOK_RESULT hook_res
)
719 struct evrpc_request_wrapper
*ctx
= arg
;
720 struct evhttp_connection
*connection
= ctx
->evcon
;
721 struct evhttp_request
*req
= ctx
->req
;
722 struct evrpc_pool
*pool
= ctx
->pool
;
723 struct evrpc_status status
;
727 if (hook_res
== EVRPC_TERMINATE
)
730 uri
= evrpc_construct_uri(ctx
->name
);
734 if (pool
->timeout
> 0) {
736 * a timeout after which the whole rpc is going to be aborted.
739 evutil_timerclear(&tv
);
740 tv
.tv_sec
= pool
->timeout
;
741 evtimer_add(&ctx
->ev_timeout
, &tv
);
744 /* start the request over the connection */
745 res
= evhttp_make_request(connection
, req
, EVHTTP_REQ_POST
, uri
);
754 memset(&status
, 0, sizeof(status
));
755 status
.error
= EVRPC_STATUS_ERR_UNSTARTED
;
756 (*ctx
->cb
)(&status
, ctx
->request
, ctx
->reply
, ctx
->cb_arg
);
757 evrpc_request_wrapper_free(ctx
);
760 /* we just queue the paused request on the pool under the req object */
762 evrpc_pause_request(void *vbase
, void *ctx
,
763 void (*cb
)(void *, enum EVRPC_HOOK_RESULT
))
765 struct evrpc_hooks_
*base
= vbase
;
766 struct evrpc_hook_ctx
*pause
= mm_malloc(sizeof(*pause
));
773 TAILQ_INSERT_TAIL(&base
->pause_requests
, pause
, next
);
778 evrpc_resume_request(void *vbase
, void *ctx
, enum EVRPC_HOOK_RESULT res
)
780 struct evrpc_hooks_
*base
= vbase
;
781 struct evrpc_pause_list
*head
= &base
->pause_requests
;
782 struct evrpc_hook_ctx
*pause
;
784 TAILQ_FOREACH(pause
, head
, next
) {
785 if (pause
->ctx
== ctx
)
792 (*pause
->cb
)(pause
->ctx
, res
);
793 TAILQ_REMOVE(head
, pause
, next
);
799 evrpc_make_request(struct evrpc_request_wrapper
*ctx
)
801 struct evrpc_pool
*pool
= ctx
->pool
;
803 /* initialize the event structure for this rpc */
804 evtimer_assign(&ctx
->ev_timeout
, pool
->base
, evrpc_request_timeout
, ctx
);
806 /* we better have some available connections on the pool */
807 EVUTIL_ASSERT(TAILQ_FIRST(&pool
->connections
) != NULL
);
810 * if no connection is available, we queue the request on the pool,
811 * the next time a connection is empty, the rpc will be send on that.
813 TAILQ_INSERT_TAIL(&pool
->requests
, ctx
, next
);
815 evrpc_pool_schedule(pool
);
821 struct evrpc_request_wrapper
*
822 evrpc_make_request_ctx(
823 struct evrpc_pool
*pool
, void *request
, void *reply
,
825 void (*req_marshal
)(struct evbuffer
*, void *),
826 void (*rpl_clear
)(void *),
827 int (*rpl_unmarshal
)(void *, struct evbuffer
*),
828 void (*cb
)(struct evrpc_status
*, void *, void *, void *),
831 struct evrpc_request_wrapper
*ctx
= (struct evrpc_request_wrapper
*)
832 mm_malloc(sizeof(struct evrpc_request_wrapper
));
837 ctx
->hook_meta
= NULL
;
839 ctx
->name
= mm_strdup(rpcname
);
840 if (ctx
->name
== NULL
) {
846 ctx
->request
= request
;
848 ctx
->request_marshal
= req_marshal
;
849 ctx
->reply_clear
= rpl_clear
;
850 ctx
->reply_unmarshal
= rpl_unmarshal
;
856 evrpc_reply_done_closure(void *, enum EVRPC_HOOK_RESULT
);
859 evrpc_reply_done(struct evhttp_request
*req
, void *arg
)
861 struct evrpc_request_wrapper
*ctx
= arg
;
862 struct evrpc_pool
*pool
= ctx
->pool
;
863 int hook_res
= EVRPC_CONTINUE
;
865 /* cancel any timeout we might have scheduled */
866 event_del(&ctx
->ev_timeout
);
870 /* we need to get the reply now */
872 evrpc_reply_done_closure(ctx
, EVRPC_CONTINUE
);
876 if (TAILQ_FIRST(&pool
->input_hooks
) != NULL
) {
877 evrpc_hook_associate_meta_(&ctx
->hook_meta
, ctx
->evcon
);
879 /* apply hooks to the incoming request */
880 hook_res
= evrpc_process_hooks(&pool
->input_hooks
,
881 ctx
, req
, req
->input_buffer
);
884 case EVRPC_TERMINATE
:
889 * if we get paused we also need to know the
890 * request. unfortunately, the underlying
891 * layer is going to free it. we need to
892 * request ownership explicitly
895 evhttp_request_own(req
);
897 evrpc_pause_request(pool
, ctx
,
898 evrpc_reply_done_closure
);
901 EVUTIL_ASSERT(hook_res
== EVRPC_TERMINATE
||
902 hook_res
== EVRPC_CONTINUE
||
903 hook_res
== EVRPC_PAUSE
);
907 evrpc_reply_done_closure(ctx
, hook_res
);
909 /* http request is being freed by underlying layer */
913 evrpc_reply_done_closure(void *arg
, enum EVRPC_HOOK_RESULT hook_res
)
915 struct evrpc_request_wrapper
*ctx
= arg
;
916 struct evhttp_request
*req
= ctx
->req
;
917 struct evrpc_pool
*pool
= ctx
->pool
;
918 struct evrpc_status status
;
921 memset(&status
, 0, sizeof(status
));
922 status
.http_req
= req
;
924 /* we need to get the reply now */
926 status
.error
= EVRPC_STATUS_ERR_TIMEOUT
;
927 } else if (hook_res
== EVRPC_TERMINATE
) {
928 status
.error
= EVRPC_STATUS_ERR_HOOKABORTED
;
930 res
= ctx
->reply_unmarshal(ctx
->reply
, req
->input_buffer
);
932 status
.error
= EVRPC_STATUS_ERR_BADPAYLOAD
;
936 /* clear everything that we might have written previously */
937 ctx
->reply_clear(ctx
->reply
);
940 (*ctx
->cb
)(&status
, ctx
->request
, ctx
->reply
, ctx
->cb_arg
);
942 evrpc_request_wrapper_free(ctx
);
944 /* the http layer owned the original request structure, but if we
945 * got paused, we asked for ownership and need to free it here. */
946 if (req
!= NULL
&& evhttp_request_is_owned(req
))
947 evhttp_request_free(req
);
949 /* see if we can schedule another request */
950 evrpc_pool_schedule(pool
);
954 evrpc_pool_schedule(struct evrpc_pool
*pool
)
956 struct evrpc_request_wrapper
*ctx
= TAILQ_FIRST(&pool
->requests
);
957 struct evhttp_connection
*evcon
;
959 /* if no requests are pending, we have no work */
963 if ((evcon
= evrpc_pool_find_connection(pool
)) != NULL
) {
964 TAILQ_REMOVE(&pool
->requests
, ctx
, next
);
965 evrpc_schedule_request(evcon
, ctx
);
970 evrpc_request_timeout(evutil_socket_t fd
, short what
, void *arg
)
972 struct evrpc_request_wrapper
*ctx
= arg
;
973 struct evhttp_connection
*evcon
= ctx
->evcon
;
974 EVUTIL_ASSERT(evcon
!= NULL
);
976 evhttp_connection_fail_(evcon
, EVREQ_HTTP_TIMEOUT
);
980 * frees potential meta data associated with a request.
984 evrpc_meta_data_free(struct evrpc_meta_list
*meta_data
)
986 struct evrpc_meta
*entry
;
987 EVUTIL_ASSERT(meta_data
!= NULL
);
989 while ((entry
= TAILQ_FIRST(meta_data
)) != NULL
) {
990 TAILQ_REMOVE(meta_data
, entry
, next
);
992 mm_free(entry
->data
);
997 static struct evrpc_hook_meta
*
998 evrpc_hook_meta_new_(void)
1000 struct evrpc_hook_meta
*ctx
;
1001 ctx
= mm_malloc(sizeof(struct evrpc_hook_meta
));
1002 EVUTIL_ASSERT(ctx
!= NULL
);
1004 TAILQ_INIT(&ctx
->meta_data
);
1011 evrpc_hook_associate_meta_(struct evrpc_hook_meta
**pctx
,
1012 struct evhttp_connection
*evcon
)
1014 struct evrpc_hook_meta
*ctx
= *pctx
;
1016 *pctx
= ctx
= evrpc_hook_meta_new_();
1021 evrpc_hook_context_free_(struct evrpc_hook_meta
*ctx
)
1023 evrpc_meta_data_free(&ctx
->meta_data
);
1027 /* Adds meta data */
1029 evrpc_hook_add_meta(void *ctx
, const char *key
,
1030 const void *data
, size_t data_size
)
1032 struct evrpc_request_wrapper
*req
= ctx
;
1033 struct evrpc_hook_meta
*store
= NULL
;
1034 struct evrpc_meta
*meta
= NULL
;
1036 if ((store
= req
->hook_meta
) == NULL
)
1037 store
= req
->hook_meta
= evrpc_hook_meta_new_();
1039 meta
= mm_malloc(sizeof(struct evrpc_meta
));
1040 EVUTIL_ASSERT(meta
!= NULL
);
1041 meta
->key
= mm_strdup(key
);
1042 EVUTIL_ASSERT(meta
->key
!= NULL
);
1043 meta
->data_size
= data_size
;
1044 meta
->data
= mm_malloc(data_size
);
1045 EVUTIL_ASSERT(meta
->data
!= NULL
);
1046 memcpy(meta
->data
, data
, data_size
);
1048 TAILQ_INSERT_TAIL(&store
->meta_data
, meta
, next
);
1052 evrpc_hook_find_meta(void *ctx
, const char *key
, void **data
, size_t *data_size
)
1054 struct evrpc_request_wrapper
*req
= ctx
;
1055 struct evrpc_meta
*meta
= NULL
;
1057 if (req
->hook_meta
== NULL
)
1060 TAILQ_FOREACH(meta
, &req
->hook_meta
->meta_data
, next
) {
1061 if (strcmp(meta
->key
, key
) == 0) {
1063 *data_size
= meta
->data_size
;
1071 struct evhttp_connection
*
1072 evrpc_hook_get_connection(void *ctx
)
1074 struct evrpc_request_wrapper
*req
= ctx
;
1075 return (req
->hook_meta
!= NULL
? req
->hook_meta
->evcon
: NULL
);
1079 evrpc_send_request_generic(struct evrpc_pool
*pool
,
1080 void *request
, void *reply
,
1081 void (*cb
)(struct evrpc_status
*, void *, void *, void *),
1083 const char *rpcname
,
1084 void (*req_marshal
)(struct evbuffer
*, void *),
1085 void (*rpl_clear
)(void *),
1086 int (*rpl_unmarshal
)(void *, struct evbuffer
*))
1088 struct evrpc_status status
;
1089 struct evrpc_request_wrapper
*ctx
;
1090 ctx
= evrpc_make_request_ctx(pool
, request
, reply
,
1091 rpcname
, req_marshal
, rpl_clear
, rpl_unmarshal
, cb
, cb_arg
);
1094 return (evrpc_make_request(ctx
));
1096 memset(&status
, 0, sizeof(status
));
1097 status
.error
= EVRPC_STATUS_ERR_UNSTARTED
;
1098 (*(cb
))(&status
, request
, reply
, cb_arg
);
1102 /** Takes a request object and fills it in with the right magic */
1103 static struct evrpc
*
1104 evrpc_register_object(const char *name
,
1105 void *(*req_new
)(void*), void *req_new_arg
, void (*req_free
)(void *),
1106 int (*req_unmarshal
)(void *, struct evbuffer
*),
1107 void *(*rpl_new
)(void*), void *rpl_new_arg
, void (*rpl_free
)(void *),
1108 int (*rpl_complete
)(void *),
1109 void (*rpl_marshal
)(struct evbuffer
*, void *))
1111 struct evrpc
* rpc
= (struct evrpc
*)mm_calloc(1, sizeof(struct evrpc
));
1114 rpc
->uri
= mm_strdup(name
);
1115 if (rpc
->uri
== NULL
) {
1119 rpc
->request_new
= req_new
;
1120 rpc
->request_new_arg
= req_new_arg
;
1121 rpc
->request_free
= req_free
;
1122 rpc
->request_unmarshal
= req_unmarshal
;
1123 rpc
->reply_new
= rpl_new
;
1124 rpc
->reply_new_arg
= rpl_new_arg
;
1125 rpc
->reply_free
= rpl_free
;
1126 rpc
->reply_complete
= rpl_complete
;
1127 rpc
->reply_marshal
= rpl_marshal
;
1132 evrpc_register_generic(struct evrpc_base
*base
, const char *name
,
1133 void (*callback
)(struct evrpc_req_generic
*, void *), void *cbarg
,
1134 void *(*req_new
)(void *), void *req_new_arg
, void (*req_free
)(void *),
1135 int (*req_unmarshal
)(void *, struct evbuffer
*),
1136 void *(*rpl_new
)(void *), void *rpl_new_arg
, void (*rpl_free
)(void *),
1137 int (*rpl_complete
)(void *),
1138 void (*rpl_marshal
)(struct evbuffer
*, void *))
1141 evrpc_register_object(name
, req_new
, req_new_arg
, req_free
, req_unmarshal
,
1142 rpl_new
, rpl_new_arg
, rpl_free
, rpl_complete
, rpl_marshal
);
1145 evrpc_register_rpc(base
, rpc
,
1146 (void (*)(struct evrpc_req_generic
*, void *))callback
, cbarg
);
1150 /** accessors for obscure and undocumented functionality */
1152 evrpc_request_get_pool(struct evrpc_request_wrapper
*ctx
)
1158 evrpc_request_set_pool(struct evrpc_request_wrapper
*ctx
,
1159 struct evrpc_pool
*pool
)
1165 evrpc_request_set_cb(struct evrpc_request_wrapper
*ctx
,
1166 void (*cb
)(struct evrpc_status
*, void *request
, void *reply
, void *arg
),
1170 ctx
->cb_arg
= cb_arg
;