summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/modules/server/mod-wiring.cpp
blob: 296181acfa8ff5cc72bfd7e5ff05e390928179e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/* $Rev$ $Date$ */

/**
 * HTTPD module used to wire component references and route requests to
 * target service components.
 */

#include <sys/stat.h>

#include "string.hpp"
#include "stream.hpp"
#include "list.hpp"
#include "tree.hpp"
#include "value.hpp"
#include "monad.hpp"
#include "../scdl/scdl.hpp"
#include "../http/httpd.hpp"

extern "C" {
extern module AP_MODULE_DECLARE_DATA mod_tuscany_wiring;
}

namespace tuscany {
namespace server {
namespace modwiring {

/**
 * Server configuration.
 */
class ServerConf {
public:
    ServerConf(server_rec* s) : s(s), contributionPath(""), compositeName("") {
    }
    const server_rec* s;
    string contributionPath;
    string compositeName;
    list<value> references;
    list<value> services;
};

/**
 * Set to true to wire using mod_proxy, false to wire using HTTP client redirects.
 */
const bool useModProxy = true;

/**
 * Returns true if a URI is absolute.
 */
const bool isAbsolute(const string& uri) {
    return contains(uri, "://");
}

/**
 * Route a /references/component-name/reference-name request,
 * to the target of the component reference.
 */
int translateReference(request_rec *r) {
    httpdDebugRequest(r, "modwiring::translateReference::input");
    debug(r->uri, "modwiring::translateReference::uri");

    // Find the requested component
    const ServerConf& sc = httpd::serverConf<ServerConf>(r, &mod_tuscany_wiring);
    const list<value> rpath(pathValues(r->uri));
    const list<value> comp(assoctree(cadr(rpath), sc.references));
    if (isNil(comp))
        return HTTP_NOT_FOUND;

    // Find the requested reference and target configuration
    const list<value> ref(assoctree<value>(caddr(rpath), cadr(comp)));
    if (isNil(ref))
        return HTTP_NOT_FOUND;
    const string target(cadr(ref));
    debug(target, "modwiring::translateReference::target");

    // Route to an absolute target URI using mod_proxy or an HTTP client redirect
    if (isAbsolute(target)) {
        if (useModProxy) {
            r->filename = apr_pstrdup(r->pool, c_str(string("proxy:") + target));
            r->proxyreq = PROXYREQ_REVERSE;
            r->handler = "proxy-server";
            return OK;
        }

        r->status = HTTP_MOVED_TEMPORARILY;
        apr_table_setn(r->headers_out, "Location", apr_pstrdup(r->pool, c_str(target)));
        r->handler = "mod_tuscany_wiring";
        return OK;
    }

    // Route to a relative target URI using a local internal redirect
    r->filename = apr_pstrdup(r->pool, c_str(string("/redirect:/components/") + target));
    r->handler = "mod_tuscany_wiring";
    return OK;
}

/**
 * Find a leaf matching a request path in a tree of URI paths.
 */
const int matchPath(const list<value>& k, const list<value>& p) {
    if (isNil(p))
        return true;
    if (isNil(k))
        return false;
    if (car(k) != car(p))
        return false;
    return matchPath(cdr(k), cdr(p));
}

const list<value> assocPath(const value& k, const list<value>& tree) {
    if (isNil(tree))
        return tree;
    if (matchPath(k, car<value>(car(tree))))
        return car(tree);
    if (k < car<value>(car(tree)))
        return assocPath(k, cadr(tree));
    return assocPath(k, caddr(tree));
}

/**
 * Route a service request to the component providing the requested service.
 */
int translateService(request_rec *r) {
    httpdDebugRequest(r, "modwiring::translateService::input");
    debug(r->uri, "modwiring::translateService::uri");

    // Find the requested component
    const ServerConf& sc = httpd::serverConf<ServerConf>(r, &mod_tuscany_wiring);
    debug(sc.services, "modwiring::translateService::services");
    const list<value> p(pathValues(r->uri));
    const list<value> svc(assocPath(p, sc.services));
    if (isNil(svc))
        return DECLINED;
    debug(svc, "modwiring::translateService::service");

    // Build a component-name + path-info URI
    const list<value> target(cons<value>(cadr(svc), httpd::pathInfo(p, car(svc))));
    debug(target, "modwiring::translateService::target");

    // Dispatch to the target component using a local internal redirect
    const string tp(path(target));
    debug(tp, "modwiring::translateService::path");
    const string redir(string("/redirect:/components") + tp);
    debug(redir, "modwiring::translateService::redirect");
    r->filename = apr_pstrdup(r->pool, c_str(redir));
    r->handler = "mod_tuscany_wiring";
    return OK;
}

/**
 * Translate an HTTP service or reference request and route it
 * to the target component.
 */
int translate(request_rec *r) {
    gc_scoped_pool pool(r->pool);
    if (!strncmp(r->uri, "/components/", 12) != 0)
        return DECLINED;

    // Translate a component reference request
    if (!strncmp(r->uri, "/references/", 12) != 0)
        return translateReference(r);

    // Translate a service request
    return translateService(r);
}

/**
 * HTTP request handler, redirect to a target component.
 */
int handler(request_rec *r) {
    gc_scoped_pool pool(r->pool);
    if(strcmp(r->handler, "mod_tuscany_wiring"))
        return DECLINED;
    httpdDebugRequest(r, "modwiring::handler::input");

    // Do an internal redirect
    if (r->filename == NULL || strncmp(r->filename, "/redirect:", 10) != 0)
        return DECLINED;
    debug(r->uri, "modwiring::handler::uri");
    debug(r->filename, "modwiring::handler::filename");
    debug(r->path_info, "modwiring::handler::path info");

    if (r->args == NULL)
        return httpd::internalRedirect(httpd::redirectURI(string(r->filename + 10), string(r->path_info)), r);
    return httpd::internalRedirect(httpd::redirectURI(string(r->filename + 10), string(r->path_info), string(r->args)), r);
}

/**
 * Read the components declared in a composite.
 */
const failable<list<value> > readComponents(const string& path) {
    ifstream is(path);
    if (fail(is))
        return mkfailure<list<value> >(string("Could not read composite: ") + path);
    return scdl::components(readXML(streamList(is)));
}

/**
 * Return a list of component-name + references pairs. The references are
 * arranged in trees of reference-name + reference-target pairs.
 */
const list<value> componentReferenceToTargetTree(const value& c) {
    return mklist<value>(scdl::name(c), mkbtree(sort(scdl::referenceToTargetAssoc(scdl::references(c)))));
}

const list<value> componentReferenceToTargetAssoc(const list<value>& c) {
    if (isNil(c))
        return c;
    return cons<value>(componentReferenceToTargetTree(car(c)), componentReferenceToTargetAssoc(cdr(c)));
}

/**
 * Return a list of service-URI-path + component-name pairs. Service-URI-paths are
 * represented as lists of URI path fragments.
 */
const list<value> defaultBindingURI(const string& cn, const string& sn) {
    return mklist<value>(cn, sn);
}

const list<value> bindingToComponentAssoc(const string& cn, const string& sn, const list<value>& b) {
    if (isNil(b))
        return b;
    const value uri(scdl::uri(car(b)));
    if (isNil(uri))
        return cons<value>(mklist<value>(defaultBindingURI(cn, sn), cn), bindingToComponentAssoc(cn, sn, cdr(b)));
    return cons<value>(mklist<value>(pathValues(c_str(string(uri))), cn), bindingToComponentAssoc(cn, sn, cdr(b)));
}

const list<value> serviceToComponentAssoc(const string& cn, const list<value>& s) {
    if (isNil(s))
        return s;
    const string sn(scdl::name(car(s)));
    const list<value> btoc(bindingToComponentAssoc(cn, sn, scdl::bindings(car(s))));
    if (isNil(btoc))
        return cons<value>(mklist<value>(defaultBindingURI(cn, sn), cn), serviceToComponentAssoc(cn, cdr(s)));
    return append<value>(btoc, serviceToComponentAssoc(cn, cdr(s)));
}

const list<value> uriToComponentAssoc(const list<value>& c) {
    if (isNil(c))
        return c;
    return append<value>(serviceToComponentAssoc(scdl::name(car(c)), scdl::services(car(c))), uriToComponentAssoc(cdr(c)));
}

/**
 * Configure the components declared in the server's deployment composite.
 */
const bool confComponents(ServerConf& sc) {
    if (sc.contributionPath == "" || sc.compositeName == "")
        return true;

    // Read the component configuration and store the references and service URIs
    // in trees for fast retrieval later
    const failable<list<value> > comps = readComponents(sc.contributionPath + sc.compositeName);
    if (!hasContent(comps))
        return true;
    const list<value> refs = componentReferenceToTargetAssoc(content(comps));
    debug(refs, "modwiring::confComponents::references");
    sc.references = mkbtree(sort(refs));

    const list<value> svcs = uriToComponentAssoc(content(comps));
    debug(svcs, "modwiring::confComponents::services");
    sc.services = mkbtree(sort(svcs));
    return true;
}

/**
 * Called after all the configuration commands have been run.
 * Process the server configuration and configure the wiring for the deployed components.
 */
const int postConfigMerge(const ServerConf& mainsc, server_rec* s) {
    if (s == NULL)
        return OK;
    ServerConf& sc = httpd::serverConf<ServerConf>(s, &mod_tuscany_wiring);
    sc.contributionPath = mainsc.contributionPath;
    sc.compositeName = mainsc.compositeName;
    sc.references = mainsc.references;
    sc.services = mainsc.services;
    return postConfigMerge(mainsc, s->next);
}

int postConfig(unused apr_pool_t *p, unused apr_pool_t *plog, unused apr_pool_t *ptemp, server_rec *s) {
    // Count the calls to post config, skip the first one as
    // postConfig is always called twice
    const string k("tuscany::modwiring::postConfig");
    const int count = (int)httpd::userData(k, s);
    httpd::putUserData(k, (void*)(count + 1), s);
    if (count == 0)
        return OK;

    // Configure the wiring for the deployed components
    ServerConf& sc = httpd::serverConf<ServerConf>(s, &mod_tuscany_wiring);
    debug(sc.contributionPath, "modwiring::postConfig::contributionPath");
    debug(sc.compositeName, "modwiring::postConfig::compositeName");
    confComponents(sc);

    // Merge the config into any virtual hosts
    return postConfigMerge(sc, s->next);
}

/**
 * Child process initialization.
 */
void childInit(apr_pool_t* p, server_rec* svr_rec) {
    gc_scoped_pool pool(p);
    ServerConf *conf = (ServerConf*)ap_get_module_config(svr_rec->module_config, &mod_tuscany_wiring);
    if(conf == NULL) {
        cerr << "[Tuscany] Due to one or more errors mod_tuscany_wiring loading failed. Causing apache to stop loading." << endl;
        exit(APEXIT_CHILDFATAL);
    }
}

/**
 * Configuration commands.
 */
const char *confContribution(cmd_parms *cmd, unused void *c, const char *arg) {
    gc_scoped_pool pool(cmd->pool);
    ServerConf& sc = httpd::serverConf<ServerConf>(cmd, &mod_tuscany_wiring);
    sc.contributionPath = arg;
    return NULL;
}
const char *confComposite(cmd_parms *cmd, unused void *c, const char *arg) {
    gc_scoped_pool pool(cmd->pool);
    ServerConf& sc = httpd::serverConf<ServerConf>(cmd, &mod_tuscany_wiring);
    sc.compositeName = arg;
    return NULL;
}

/**
 * HTTP server module declaration.
 */
const command_rec commands[] = {
    AP_INIT_TAKE1("SCAContribution", (const char*(*)())confContribution, NULL, RSRC_CONF, "SCA contribution location"),
    AP_INIT_TAKE1("SCAComposite", (const char*(*)())confComposite, NULL, RSRC_CONF, "SCA composite location"),
    {NULL, NULL, NULL, 0, NO_ARGS, NULL}
};

void registerHooks(unused apr_pool_t *p) {
    ap_hook_post_config(postConfig, NULL, NULL, APR_HOOK_MIDDLE);
    ap_hook_child_init(childInit, NULL, NULL, APR_HOOK_MIDDLE);
    ap_hook_handler(handler, NULL, NULL, APR_HOOK_MIDDLE);
    ap_hook_translate_name(translate, NULL, NULL, APR_HOOK_FIRST);
}

}
}
}

extern "C" {

module AP_MODULE_DECLARE_DATA mod_tuscany_wiring = {
    STANDARD20_MODULE_STUFF,
    // dir config and merger
    NULL, NULL,
    // server config and merger
    tuscany::httpd::makeServerConf<tuscany::server::modwiring::ServerConf>, NULL,
    // commands and hooks
    tuscany::server::modwiring::commands, tuscany::server::modwiring::registerHooks
};

}