| 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 |
| 28 | #include "config.h" |
| 29 |
| 30 | #ifdef WIN32 |
| 31 | #define WIN32_LEAN_AND_MEAN |
| 32 | #include <winsock2.h> |
| 33 | #include <windows.h> |
| 34 | #undef WIN32_LEAN_AND_MEAN |
| 35 | #endif |
| 36 |
| 37 | #include <sys/types.h> |
| 38 | #ifndef WIN32 |
| 39 | #include <sys/socket.h> |
| 40 | #endif |
| 41 | #ifdef HAVE_SYS_TIME_H |
| 42 | #include <sys/time.h> |
| 43 | #endif |
| 44 | #include <sys/queue.h> |
| 45 | #include <stdio.h> |
| 46 | #include <stdlib.h> |
| 47 | #ifndef WIN32 |
| 48 | #include <unistd.h> |
| 49 | #endif |
| 50 | #include <errno.h> |
| 51 | #include <signal.h> |
| 52 | #include <string.h> |
| 53 | #include <assert.h> |
| 54 |
| 55 | #include "event.h" |
| 56 | #include "evrpc.h" |
| 57 | #include "evrpc-internal.h" |
| 58 | #include "evhttp.h" |
| 59 | #include "evutil.h" |
| 60 | #include "log.h" |
| 61 |
| 62 | struct evrpc_base * |
| 63 | evrpc_init(struct evhttp *http_server) |
| 64 | { |
| 65 | struct evrpc_base* base = calloc(1, sizeof(struct evrpc_base)); |
| 66 | if (base == NULL) |
| 67 | return (NULL); |
| 68 |
| 69 | /* we rely on the tagging sub system */ |
| 70 | evtag_init(); |
| 71 |
| 72 | TAILQ_INIT(&base->registered_rpcs); |
| 73 | TAILQ_INIT(&base->input_hooks); |
| 74 | TAILQ_INIT(&base->output_hooks); |
| 75 | base->http_server = http_server; |
| 76 |
| 77 | return (base); |
| 78 | } |
| 79 |
| 80 | void |
| 81 | evrpc_free(struct evrpc_base *base) |
| 82 | { |
| 83 | struct evrpc *rpc; |
| 84 | struct evrpc_hook *hook; |
| 85 |
| 86 | while ((rpc = TAILQ_FIRST(&base->registered_rpcs)) != NULL) { |
| 87 | assert(evrpc_unregister_rpc(base, rpc->uri)); |
| 88 | } |
| 89 | while ((hook = TAILQ_FIRST(&base->input_hooks)) != NULL) { |
| 90 | assert(evrpc_remove_hook(base, EVRPC_INPUT, hook)); |
| 91 | } |
| 92 | while ((hook = TAILQ_FIRST(&base->output_hooks)) != NULL) { |
| 93 | assert(evrpc_remove_hook(base, EVRPC_OUTPUT, hook)); |
| 94 | } |
| 95 | free(base); |
| 96 | } |
| 97 |
| 98 | void * |
| 99 | evrpc_add_hook(void *vbase, |
| 100 | enum EVRPC_HOOK_TYPE hook_type, |
| 101 | int (*cb)(struct evhttp_request *, struct evbuffer *, void *), |
| 102 | void *cb_arg) |
| 103 | { |
| 104 | struct _evrpc_hooks *base = vbase; |
| 105 | struct evrpc_hook_list *head = NULL; |
| 106 | struct evrpc_hook *hook = NULL; |
| 107 | switch (hook_type) { |
| 108 | case EVRPC_INPUT: |
| 109 | head = &base->in_hooks; |
| 110 | break; |
| 111 | case EVRPC_OUTPUT: |
| 112 | head = &base->out_hooks; |
| 113 | break; |
| 114 | default: |
| 115 | assert(hook_type == EVRPC_INPUT || hook_type == EVRPC_OUTPUT); |
| 116 | } |
| 117 |
| 118 | hook = calloc(1, sizeof(struct evrpc_hook)); |
| 119 | assert(hook != NULL); |
| 120 |
| 121 | hook->process = cb; |
| 122 | hook->process_arg = cb_arg; |
| 123 | TAILQ_INSERT_TAIL(head, hook, next); |
| 124 |
| 125 | return (hook); |
| 126 | } |
| 127 |
| 128 | static int |
| 129 | evrpc_remove_hook_internal(struct evrpc_hook_list *head, void *handle) |
| 130 | { |
| 131 | struct evrpc_hook *hook = NULL; |
| 132 | TAILQ_FOREACH(hook, head, next) { |
| 133 | if (hook == handle) { |
| 134 | TAILQ_REMOVE(head, hook, next); |
| 135 | free(hook); |
| 136 | return (1); |
| 137 | } |
| 138 | } |
| 139 |
| 140 | return (0); |
| 141 | } |
| 142 |
| 143 | /* |
| 144 | * remove the hook specified by the handle |
| 145 | */ |
| 146 |
| 147 | int |
| 148 | evrpc_remove_hook(void *vbase, enum EVRPC_HOOK_TYPE hook_type, void *handle) |
| 149 | { |
| 150 | struct _evrpc_hooks *base = vbase; |
| 151 | struct evrpc_hook_list *head = NULL; |
| 152 | switch (hook_type) { |
| 153 | case EVRPC_INPUT: |
| 154 | head = &base->in_hooks; |
| 155 | break; |
| 156 | case EVRPC_OUTPUT: |
| 157 | head = &base->out_hooks; |
| 158 | break; |
| 159 | default: |
| 160 | assert(hook_type == EVRPC_INPUT || hook_type == EVRPC_OUTPUT); |
| 161 | } |
| 162 |
| 163 | return (evrpc_remove_hook_internal(head, handle)); |
| 164 | } |
| 165 |
| 166 | static int |
| 167 | evrpc_process_hooks(struct evrpc_hook_list *head, |
| 168 | struct evhttp_request *req, struct evbuffer *evbuf) |
| 169 | { |
| 170 | struct evrpc_hook *hook; |
| 171 | TAILQ_FOREACH(hook, head, next) { |
| 172 | if (hook->process(req, evbuf, hook->process_arg) == -1) |
| 173 | return (-1); |
| 174 | } |
| 175 |
| 176 | return (0); |
| 177 | } |
| 178 |
| 179 | static void evrpc_pool_schedule(struct evrpc_pool *pool); |
| 180 | static void evrpc_request_cb(struct evhttp_request *, void *); |
| 181 | void evrpc_request_done(struct evrpc_req_generic*); |
| 182 |
| 183 | /* |
| 184 | * Registers a new RPC with the HTTP server. The evrpc object is expected |
| 185 | * to have been filled in via the EVRPC_REGISTER_OBJECT macro which in turn |
| 186 | * calls this function. |
| 187 | */ |
| 188 |
| 189 | static char * |
| 190 | evrpc_construct_uri(const char *uri) |
| 191 | { |
| 192 | char *constructed_uri; |
| 193 | int constructed_uri_len; |
| 194 |
| 195 | constructed_uri_len = strlen(EVRPC_URI_PREFIX) + strlen(uri) + 1; |
| 196 | if ((constructed_uri = malloc(constructed_uri_len)) == NULL) |
| 197 | event_err(1, "%s: failed to register rpc at %s", |
| 198 | __func__, uri); |
| 199 | memcpy(constructed_uri, EVRPC_URI_PREFIX, strlen(EVRPC_URI_PREFIX)); |
| 200 | memcpy(constructed_uri + strlen(EVRPC_URI_PREFIX), uri, strlen(uri)); |
| 201 | constructed_uri[constructed_uri_len - 1] = '\0'; |
| 202 |
| 203 | return (constructed_uri); |
| 204 | } |
| 205 |
| 206 | int |
| 207 | evrpc_register_rpc(struct evrpc_base *base, struct evrpc *rpc, |
| 208 | void (*cb)(struct evrpc_req_generic *, void *), void *cb_arg) |
| 209 | { |
| 210 | char *constructed_uri = evrpc_construct_uri(rpc->uri); |
| 211 |
| 212 | rpc->base = base; |
| 213 | rpc->cb = cb; |
| 214 | rpc->cb_arg = cb_arg; |
| 215 |
| 216 | TAILQ_INSERT_TAIL(&base->registered_rpcs, rpc, next); |
| 217 |
| 218 | evhttp_set_cb(base->http_server, |
| 219 | constructed_uri, |
| 220 | evrpc_request_cb, |
| 221 | rpc); |
| 222 |
| 223 | free(constructed_uri); |
| 224 |
| 225 | return (0); |
| 226 | } |
| 227 |
| 228 | int |
| 229 | evrpc_unregister_rpc(struct evrpc_base *base, const char *name) |
| 230 | { |
| 231 | char *registered_uri = NULL; |
| 232 | struct evrpc *rpc; |
| 233 |
| 234 | /* find the right rpc; linear search might be slow */ |
| 235 | TAILQ_FOREACH(rpc, &base->registered_rpcs, next) { |
| 236 | if (strcmp(rpc->uri, name) == 0) |
| 237 | break; |
| 238 | } |
| 239 | if (rpc == NULL) { |
| 240 | /* We did not find an RPC with this name */ |
| 241 | return (-1); |
| 242 | } |
| 243 | TAILQ_REMOVE(&base->registered_rpcs, rpc, next); |
| 244 |
| 245 | free((char *)rpc->uri); |
| 246 | free(rpc); |
| 247 |
| 248 | registered_uri = evrpc_construct_uri(name); |
| 249 |
| 250 | /* remove the http server callback */ |
| 251 | assert(evhttp_del_cb(base->http_server, registered_uri) == 0); |
| 252 |
| 253 | free(registered_uri); |
| 254 | return (0); |
| 255 | } |
| 256 |
| 257 | static void |
| 258 | evrpc_request_cb(struct evhttp_request *req, void *arg) |
| 259 | { |
| 260 | struct evrpc *rpc = arg; |
| 261 | struct evrpc_req_generic *rpc_state = NULL; |
| 262 |
| 263 | /* let's verify the outside parameters */ |
| 264 | if (req->type != EVHTTP_REQ_POST || |
| 265 | EVBUFFER_LENGTH(req->input_buffer) <= 0) |
| 266 | goto error; |
| 267 |
| 268 | /* |
| 269 | * we might want to allow hooks to suspend the processing, |
| 270 | * but at the moment, we assume that they just act as simple |
| 271 | * filters. |
| 272 | */ |
| 273 | if (evrpc_process_hooks(&rpc->base->input_hooks, |
| 274 | req, req->input_buffer) == -1) |
| 275 | goto error; |
| 276 |
| 277 | rpc_state = calloc(1, sizeof(struct evrpc_req_generic)); |
| 278 | if (rpc_state == NULL) |
| 279 | goto error; |
| 280 |
| 281 | /* let's check that we can parse the request */ |
| 282 | rpc_state->request = rpc->request_new(); |
| 283 | if (rpc_state->request == NULL) |
| 284 | goto error; |
| 285 |
| 286 | rpc_state->rpc = rpc; |
| 287 |
| 288 | if (rpc->request_unmarshal( |
| 289 | rpc_state->request, req->input_buffer) == -1) { |
| 290 | /* we failed to parse the request; that's a bummer */ |
| 291 | goto error; |
| 292 | } |
| 293 |
| 294 | /* at this point, we have a well formed request, prepare the reply */ |
| 295 |
| 296 | rpc_state->reply = rpc->reply_new(); |
| 297 | if (rpc_state->reply == NULL) |
| 298 | goto error; |
| 299 |
| 300 | rpc_state->http_req = req; |
| 301 | rpc_state->done = evrpc_request_done; |
| 302 |
| 303 | /* give the rpc to the user; they can deal with it */ |
| 304 | rpc->cb(rpc_state, rpc->cb_arg); |
| 305 |
| 306 | return; |
| 307 |
| 308 | error: |
| 309 | evrpc_reqstate_free(rpc_state); |
| 310 | evhttp_send_error(req, HTTP_SERVUNAVAIL, "Service Error"); |
| 311 | return; |
| 312 | } |
| 313 |
| 314 | void |
| 315 | evrpc_reqstate_free(struct evrpc_req_generic* rpc_state) |
| 316 | { |
| 317 | /* clean up all memory */ |
| 318 | if (rpc_state != NULL) { |
| 319 | struct evrpc *rpc = rpc_state->rpc; |
| 320 |
| 321 | if (rpc_state->request != NULL) |
| 322 | rpc->request_free(rpc_state->request); |
| 323 | if (rpc_state->reply != NULL) |
| 324 | rpc->reply_free(rpc_state->reply); |
| 325 | free(rpc_state); |
| 326 | } |
| 327 | } |
| 328 |
| 329 | void |
| 330 | evrpc_request_done(struct evrpc_req_generic* rpc_state) |
| 331 | { |
| 332 | struct evhttp_request *req = rpc_state->http_req; |
| 333 | struct evrpc *rpc = rpc_state->rpc; |
| 334 | struct evbuffer* data = NULL; |
| 335 |
| 336 | if (rpc->reply_complete(rpc_state->reply) == -1) { |
| 337 | /* the reply was not completely filled in. error out */ |
| 338 | goto error; |
| 339 | } |
| 340 |
| 341 | if ((data = evbuffer_new()) == NULL) { |
| 342 | /* out of memory */ |
| 343 | goto error; |
| 344 | } |
| 345 |
| 346 | /* serialize the reply */ |
| 347 | rpc->reply_marshal(data, rpc_state->reply); |
| 348 |
| 349 | /* do hook based tweaks to the request */ |
| 350 | if (evrpc_process_hooks(&rpc->base->output_hooks, |
| 351 | req, data) == -1) |
| 352 | goto error; |
| 353 |
| 354 | /* on success, we are going to transmit marshaled binary data */ |
| 355 | if (evhttp_find_header(req->output_headers, "Content-Type") == NULL) { |
| 356 | evhttp_add_header(req->output_headers, |
| 357 | "Content-Type", "application/octet-stream"); |
| 358 | } |
| 359 |
| 360 | evhttp_send_reply(req, HTTP_OK, "OK", data); |
| 361 |
| 362 | evbuffer_free(data); |
| 363 |
| 364 | evrpc_reqstate_free(rpc_state); |
| 365 |
| 366 | return; |
| 367 |
| 368 | error: |
| 369 | if (data != NULL) |
| 370 | evbuffer_free(data); |
| 371 | evrpc_reqstate_free(rpc_state); |
| 372 | evhttp_send_error(req, HTTP_SERVUNAVAIL, "Service Error"); |
| 373 | return; |
| 374 | } |
| 375 |
| 376 | /* Client implementation of RPC site */ |
| 377 |
| 378 | static int evrpc_schedule_request(struct evhttp_connection *connection, |
| 379 | struct evrpc_request_wrapper *ctx); |
| 380 |
| 381 | struct evrpc_pool * |
| 382 | evrpc_pool_new(struct event_base *base) |
| 383 | { |
| 384 | struct evrpc_pool *pool = calloc(1, sizeof(struct evrpc_pool)); |
| 385 | if (pool == NULL) |
| 386 | return (NULL); |
| 387 |
| 388 | TAILQ_INIT(&pool->connections); |
| 389 | TAILQ_INIT(&pool->requests); |
| 390 |
| 391 | TAILQ_INIT(&pool->input_hooks); |
| 392 | TAILQ_INIT(&pool->output_hooks); |
| 393 |
| 394 | pool->base = base; |
| 395 | pool->timeout = -1; |
| 396 |
| 397 | return (pool); |
| 398 | } |
| 399 |
| 400 | static void |
| 401 | evrpc_request_wrapper_free(struct evrpc_request_wrapper *request) |
| 402 | { |
| 403 | free(request->name); |
| 404 | free(request); |
| 405 | } |
| 406 |
| 407 | void |
| 408 | evrpc_pool_free(struct evrpc_pool *pool) |
| 409 | { |
| 410 | struct evhttp_connection *connection; |
| 411 | struct evrpc_request_wrapper *request; |
| 412 | struct evrpc_hook *hook; |
| 413 |
| 414 | while ((request = TAILQ_FIRST(&pool->requests)) != NULL) { |
| 415 | TAILQ_REMOVE(&pool->requests, request, next); |
| 416 | /* if this gets more complicated we need our own function */ |
| 417 | evrpc_request_wrapper_free(request); |
| 418 | } |
| 419 |
| 420 | while ((connection = TAILQ_FIRST(&pool->connections)) != NULL) { |
| 421 | TAILQ_REMOVE(&pool->connections, connection, next); |
| 422 | evhttp_connection_free(connection); |
| 423 | } |
| 424 |
| 425 | while ((hook = TAILQ_FIRST(&pool->input_hooks)) != NULL) { |
| 426 | assert(evrpc_remove_hook(pool, EVRPC_INPUT, hook)); |
| 427 | } |
| 428 |
| 429 | while ((hook = TAILQ_FIRST(&pool->output_hooks)) != NULL) { |
| 430 | assert(evrpc_remove_hook(pool, EVRPC_OUTPUT, hook)); |
| 431 | } |
| 432 |
| 433 | free(pool); |
| 434 | } |
| 435 |
| 436 | /* |
| 437 | * Add a connection to the RPC pool. A request scheduled on the pool |
| 438 | * may use any available connection. |
| 439 | */ |
| 440 |
| 441 | void |
| 442 | evrpc_pool_add_connection(struct evrpc_pool *pool, |
| 443 | struct evhttp_connection *connection) { |
| 444 | assert(connection->http_server == NULL); |
| 445 | TAILQ_INSERT_TAIL(&pool->connections, connection, next); |
| 446 |
| 447 | /* |
| 448 | * associate an event base with this connection |
| 449 | */ |
| 450 | if (pool->base != NULL) |
| 451 | evhttp_connection_set_base(connection, pool->base); |
| 452 |
| 453 | /* |
| 454 | * unless a timeout was specifically set for a connection, |
| 455 | * the connection inherits the timeout from the pool. |
| 456 | */ |
| 457 | if (connection->timeout == -1) |
| 458 | connection->timeout = pool->timeout; |
| 459 |
| 460 | /* |
| 461 | * if we have any requests pending, schedule them with the new |
| 462 | * connections. |
| 463 | */ |
| 464 |
| 465 | if (TAILQ_FIRST(&pool->requests) != NULL) { |
| 466 | struct evrpc_request_wrapper *request = |
| 467 | TAILQ_FIRST(&pool->requests); |
| 468 | TAILQ_REMOVE(&pool->requests, request, next); |
| 469 | evrpc_schedule_request(connection, request); |
| 470 | } |
| 471 | } |
| 472 |
| 473 | void |
| 474 | evrpc_pool_set_timeout(struct evrpc_pool *pool, int timeout_in_secs) |
| 475 | { |
| 476 | struct evhttp_connection *evcon; |
| 477 | TAILQ_FOREACH(evcon, &pool->connections, next) { |
| 478 | evcon->timeout = timeout_in_secs; |
| 479 | } |
| 480 | pool->timeout = timeout_in_secs; |
| 481 | } |
| 482 |
| 483 |
| 484 | static void evrpc_reply_done(struct evhttp_request *, void *); |
| 485 | static void evrpc_request_timeout(int, short, void *); |
| 486 |
| 487 | /* |
| 488 | * Finds a connection object associated with the pool that is currently |
| 489 | * idle and can be used to make a request. |
| 490 | */ |
| 491 | static struct evhttp_connection * |
| 492 | evrpc_pool_find_connection(struct evrpc_pool *pool) |
| 493 | { |
| 494 | struct evhttp_connection *connection; |
| 495 | TAILQ_FOREACH(connection, &pool->connections, next) { |
| 496 | if (TAILQ_FIRST(&connection->requests) == NULL) |
| 497 | return (connection); |
| 498 | } |
| 499 |
| 500 | return (NULL); |
| 501 | } |
| 502 |
| 503 | /* |
| 504 | * We assume that the ctx is no longer queued on the pool. |
| 505 | */ |
| 506 | static int |
| 507 | evrpc_schedule_request(struct evhttp_connection *connection, |
| 508 | struct evrpc_request_wrapper *ctx) |
| 509 | { |
| 510 | struct evhttp_request *req = NULL; |
| 511 | struct evrpc_pool *pool = ctx->pool; |
| 512 | struct evrpc_status status; |
| 513 | char *uri = NULL; |
| 514 | int res = 0; |
| 515 |
| 516 | if ((req = evhttp_request_new(evrpc_reply_done, ctx)) == NULL) |
| 517 | goto error; |
| 518 |
| 519 | /* serialize the request data into the output buffer */ |
| 520 | ctx->request_marshal(req->output_buffer, ctx->request); |
| 521 |
| 522 | uri = evrpc_construct_uri(ctx->name); |
| 523 | if (uri == NULL) |
| 524 | goto error; |
| 525 |
| 526 | /* we need to know the connection that we might have to abort */ |
| 527 | ctx->evcon = connection; |
| 528 |
| 529 | /* apply hooks to the outgoing request */ |
| 530 | if (evrpc_process_hooks(&pool->output_hooks, |
| 531 | req, req->output_buffer) == -1) |
| 532 | goto error; |
| 533 |
| 534 | if (pool->timeout > 0) { |
| 535 | /* |
| 536 | * a timeout after which the whole rpc is going to be aborted. |
| 537 | */ |
| 538 | struct timeval tv; |
| 539 | evutil_timerclear(&tv); |
| 540 | tv.tv_sec = pool->timeout; |
| 541 | evtimer_add(&ctx->ev_timeout, &tv); |
| 542 | } |
| 543 |
| 544 | /* start the request over the connection */ |
| 545 | res = evhttp_make_request(connection, req, EVHTTP_REQ_POST, uri); |
| 546 | free(uri); |
| 547 |
| 548 | if (res == -1) |
| 549 | goto error; |
| 550 |
| 551 | return (0); |
| 552 |
| 553 | error: |
| 554 | memset(&status, 0, sizeof(status)); |
| 555 | status.error = EVRPC_STATUS_ERR_UNSTARTED; |
| 556 | (*ctx->cb)(&status, ctx->request, ctx->reply, ctx->cb_arg); |
| 557 | evrpc_request_wrapper_free(ctx); |
| 558 | return (-1); |
| 559 | } |
| 560 |
| 561 | int |
| 562 | evrpc_make_request(struct evrpc_request_wrapper *ctx) |
| 563 | { |
| 564 | struct evrpc_pool *pool = ctx->pool; |
| 565 |
| 566 | /* initialize the event structure for this rpc */ |
| 567 | evtimer_set(&ctx->ev_timeout, evrpc_request_timeout, ctx); |
| 568 | if (pool->base != NULL) |
| 569 | event_base_set(pool->base, &ctx->ev_timeout); |
| 570 |
| 571 | /* we better have some available connections on the pool */ |
| 572 | assert(TAILQ_FIRST(&pool->connections) != NULL); |
| 573 |
| 574 | /* |
| 575 | * if no connection is available, we queue the request on the pool, |
| 576 | * the next time a connection is empty, the rpc will be send on that. |
| 577 | */ |
| 578 | TAILQ_INSERT_TAIL(&pool->requests, ctx, next); |
| 579 |
| 580 | evrpc_pool_schedule(pool); |
| 581 |
| 582 | return (0); |
| 583 | } |
| 584 |
| 585 | static void |
| 586 | evrpc_reply_done(struct evhttp_request *req, void *arg) |
| 587 | { |
| 588 | struct evrpc_request_wrapper *ctx = arg; |
| 589 | struct evrpc_pool *pool = ctx->pool; |
| 590 | struct evrpc_status status; |
| 591 | int res = -1; |
| 592 |
| 593 | /* cancel any timeout we might have scheduled */ |
| 594 | event_del(&ctx->ev_timeout); |
| 595 |
| 596 | memset(&status, 0, sizeof(status)); |
| 597 | status.http_req = req; |
| 598 |
| 599 | /* we need to get the reply now */ |
| 600 | if (req != NULL) { |
| 601 | /* apply hooks to the incoming request */ |
| 602 | if (evrpc_process_hooks(&pool->input_hooks, |
| 603 | req, req->input_buffer) == -1) { |
| 604 | status.error = EVRPC_STATUS_ERR_HOOKABORTED; |
| 605 | res = -1; |
| 606 | } else { |
| 607 | res = ctx->reply_unmarshal(ctx->reply, |
| 608 | req->input_buffer); |
| 609 | if (res == -1) { |
| 610 | status.error = EVRPC_STATUS_ERR_BADPAYLOAD; |
| 611 | } |
| 612 | } |
| 613 | } else { |
| 614 | status.error = EVRPC_STATUS_ERR_TIMEOUT; |
| 615 | } |
| 616 |
| 617 | if (res == -1) { |
| 618 | /* clear everything that we might have written previously */ |
| 619 | ctx->reply_clear(ctx->reply); |
| 620 | } |
| 621 |
| 622 | (*ctx->cb)(&status, ctx->request, ctx->reply, ctx->cb_arg); |
| 623 |
| 624 | evrpc_request_wrapper_free(ctx); |
| 625 |
| 626 | /* the http layer owns the request structure */ |
| 627 |
| 628 | /* see if we can schedule another request */ |
| 629 | evrpc_pool_schedule(pool); |
| 630 | } |
| 631 |
| 632 | static void |
| 633 | evrpc_pool_schedule(struct evrpc_pool *pool) |
| 634 | { |
| 635 | struct evrpc_request_wrapper *ctx = TAILQ_FIRST(&pool->requests); |
| 636 | struct evhttp_connection *evcon; |
| 637 |
| 638 | /* if no requests are pending, we have no work */ |
| 639 | if (ctx == NULL) |
| 640 | return; |
| 641 |
| 642 | if ((evcon = evrpc_pool_find_connection(pool)) != NULL) { |
| 643 | TAILQ_REMOVE(&pool->requests, ctx, next); |
| 644 | evrpc_schedule_request(evcon, ctx); |
| 645 | } |
| 646 | } |
| 647 |
| 648 | static void |
| 649 | evrpc_request_timeout(int fd, short what, void *arg) |
| 650 | { |
| 651 | struct evrpc_request_wrapper *ctx = arg; |
| 652 | struct evhttp_connection *evcon = ctx->evcon; |
| 653 | assert(evcon != NULL); |
| 654 |
| 655 | evhttp_connection_fail(evcon, EVCON_HTTP_TIMEOUT); |
| 656 | } |
| 657 |