root/src/noit_rest.c

Revision 66881597a764d99057ca0b381ab4d311d86dec79, 4.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 5 years ago)

remove some debugging, register with the eventer for sane output in the capability set, refs #171

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  *       copyright notice, this list of conditions and the following
13  *       disclaimer in the documentation and/or other materials provided
14  *       with the distribution.
15  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
16  *       of its contributors may be used to endorse or promote products
17  *       derived from this software without specific prior written
18  *       permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "noit_defines.h"
34 #include "noit_listener.h"
35 #include "noit_http.h"
36 #include "noit_rest.h"
37
38 static noit_http_rest_closure_t *
39 noit_http_rest_closure_alloc() {
40   noit_http_rest_closure_t *restc;
41   restc = calloc(1, sizeof(*restc));
42   return restc;
43 }
44 static void
45 noit_http_rest_closure_free(noit_http_rest_closure_t *restc) {
46   free(restc);
47 }
48
49 int
50 noit_rest_request_dispatcher(noit_http_session_ctx *ctx) {
51   noit_http_response_status_set(ctx, 200, "OK");
52   noit_http_response_header_set(ctx, "Content-Type", "application/json");
53   noit_http_response_option_set(ctx, NOIT_HTTP_CHUNKED);
54   noit_http_response_option_set(ctx, NOIT_HTTP_DEFLATE);
55   noit_http_response_append(ctx, "{error: 'Foo'}", 14);
56   noit_http_response_flush(ctx, noit_false);
57   noit_http_response_end(ctx);
58   return 0;
59 }
60
61 int
62 noit_http_rest_handler(eventer_t e, int mask, void *closure,
63                        struct timeval *now) {
64   int newmask = EVENTER_READ | EVENTER_EXCEPTION;
65   acceptor_closure_t *ac = closure;
66   noit_http_rest_closure_t *restc = ac->service_ctx;
67
68   if(mask & EVENTER_EXCEPTION || (restc && restc->wants_shutdown)) {
69 socket_error:
70     /* Exceptions cause us to simply snip the connection */
71     eventer_remove_fd(e->fd);
72     e->opset->close(e->fd, &newmask, e);
73     if(restc) noit_http_rest_closure_free(restc);
74     if(ac) acceptor_closure_free(ac);
75     return 0;
76   }
77
78   if(!ac->service_ctx) {
79     const char *primer = "";
80     ac->service_ctx = restc = noit_http_rest_closure_alloc();
81     restc->http_ctx =
82         noit_http_session_ctx_new(noit_rest_request_dispatcher,
83                                   restc, e);
84    
85     switch(ac->cmd) {
86       case NOIT_CONTROL_POST:
87         primer = "POST";
88         break;
89       case NOIT_CONTROL_GET:
90         primer = "GET ";
91         break;
92       case NOIT_CONTROL_HEAD:
93         primer = "HEAD";
94         break;
95       default:
96         goto socket_error;
97     }
98     noit_http_session_prime_input(restc->http_ctx, primer, 4);
99   }
100   return restc->http_ctx->drive(e, mask, restc->http_ctx, now);
101 }
102
103 void noit_http_rest_init() {
104   eventer_name_callback("http_rest_api/1.0", noit_http_rest_handler);
105   noit_control_dispatch_delegate(noit_control_dispatch,
106                                  NOIT_CONTROL_GET,
107                                  noit_http_rest_handler);
108   noit_control_dispatch_delegate(noit_control_dispatch,
109                                  NOIT_CONTROL_HEAD,
110                                  noit_http_rest_handler);
111   noit_control_dispatch_delegate(noit_control_dispatch,
112                                  NOIT_CONTROL_POST,
113                                  noit_http_rest_handler);
114 }
115
Note: See TracBrowser for help on using the browser.