summaryrefslogtreecommitdiffstats
path: root/cpp/sca/modules/http/mod-wiring.cpp
blob: 965d5a87fb33fa61ee401a82815acec40f7c45a5 (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
/*
 * 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 components.
 */

#include <sys/stat.h>

#include <string>
#include <iostream>
#include <sstream>
#include <fstream>

#include "list.hpp"
#include "slist.hpp"
#include "value.hpp"
#include "monad.hpp"
#include "../scdl/scdl.hpp"
#include "../cache/cache.hpp"
#include "httpd.hpp"

extern "C" {
extern module AP_MODULE_DECLARE_DATA mod_tuscany_wiring;
}

namespace tuscany {
namespace httpd {
namespace modwiring {

/**
 * Server configuration.
 */
class ServerConf {
public:
    std::string home;
    ServerConf() : home("") {
    }
};

const ServerConf& serverConf(const request_rec* r) {
    return *(ServerConf*)ap_get_module_config(r->server->module_config, &mod_tuscany_wiring);
}

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

/**
 * Directory configuration.
 */
class DirConf {
public:
    DirConf() : contributionPath(""), compositeName("") {
    }
    std::string contributionPath;
    std::string compositeName;
    cache::cached<failable<list<value>, std::string> > components;
};

DirConf& dirConf(const request_rec* r) {
    return *(DirConf*)ap_get_module_config(r->per_dir_config, &mod_tuscany_wiring);
}

/**
 * Read the SCDL configuration of the deployed components.
 */
const failable<list<value>, std::string> readComponents(const std::string& path) {
    std::ifstream is(path);
    if (is.fail() || is.bad())
        return mkfailure<list<value>, std::string>("Could not read composite: " + path);
    return scdl::components(readXML(streamList(is)));
}

const cache::cached<failable<list<value>, std::string> > components(DirConf* conf) {
    const std::string path(conf->contributionPath + conf->compositeName);
    const lambda<failable<list<value>, std::string>(std::string)> rc(readComponents);
    const lambda<unsigned long(std::string)> ft(cache::latestFileTime);
    return cache::cached<failable<list<value>, std::string> >(curry(rc, path), curry(ft, path));
}

/**
 * Returns true if a URI is absolute.
 */
const bool isAbsolute(const std::string& uri) {
    return uri.find("://") != std::string::npos;
}

/**
 * Translate an HTTP request URI. Wire a URI in the form /references/component-name/reference-name
 * to the target of the reference.
 */
int translate(request_rec *r) {
    if (strncmp(r->uri, "/references/", 12) != 0)
        return DECLINED;
    const list<value> rpath(path(r->uri));

    // Log the request
    if(logRequests)
        logRequest(r, "mod_tuscany_wiring::translate");

    // Find the requested component, reference and its target configuration
    DirConf&  conf = dirConf(r);
    conf.components = cache::latest(conf.components);
    const failable<list<value>, std::string> comps(conf.components);
    if (!hasValue(comps))
        return HTTP_INTERNAL_SERVER_ERROR;
    const value comp(scdl::named(cadr(rpath), list<value>(comps)));
    if (isNil(comp))
        return HTTP_NOT_FOUND;
    const value ref(scdl::named(caddr(rpath), scdl::references(comp)));
    if (isNil(ref))
        return HTTP_NOT_FOUND;
    const std::string target(scdl::target(ref));

    // Absolute target URI
    if (isAbsolute(target)) {

        // Wire using mod_proxy
        if (useModProxy) {
            r->filename = apr_pstrdup(r->pool, std::string("proxy:" + target).c_str());
            r->proxyreq = PROXYREQ_REVERSE;
            r->handler = "proxy-server";
            return OK;
        }

        // Wire using an HTTP client redirect
        r->status = HTTP_MOVED_TEMPORARILY;
        apr_table_setn(r->headers_out, "Location", apr_pstrdup(r->pool, target.c_str()));
        r->handler = "mod_tuscany_wiring";
        return OK;

    }

    // Local internal redirect
    r->filename = apr_pstrdup(r->pool, std::string("/redirect:/" + target).c_str());
    r->handler = "mod_tuscany_wiring";
    return OK;
}

/**
 * Construct a redirect URI.
 */
const std::string redirect(const std::string& file, const std::string& pi) {
    return file + pi;
}

const std::string redirect(const std::string& file, const std::string& pi, const std::string& args) {
    return file + pi + "?" + args;
}

/**
 * HTTP request internal redirect handler.
 */
int handler(request_rec *r) {
    if(strcmp(r->handler, "mod_tuscany_wiring"))
        return DECLINED;

    // Log the request
    if(logRequests)
        logRequest(r, "mod_tuscany_wiring::handler");

    // Do an internal redirect
    if (r->filename == NULL || strncmp(r->filename, "/redirect:", 10) != 0)
        return DECLINED;
    if (r->args == NULL) {
        ap_internal_redirect(apr_pstrdup(r->pool, redirect(r->filename + 10, r->path_info).c_str()), r);
        return OK;
    }
    ap_internal_redirect(apr_pstrdup(r->pool, redirect(r->filename + 10, r->path_info, r->args).c_str()), r);
    return OK;
}

/**
 * Configuration commands.
 */
const char *confHome(cmd_parms *cmd, void *dummy, const char *arg) {
    ServerConf *c = (ServerConf*)ap_get_module_config(cmd->server->module_config, &mod_tuscany_wiring);
    c->home = arg;
    return NULL;
}
const char *confContribution(cmd_parms *cmd, void *c, const char *arg) {
    DirConf* conf = (DirConf*)c;
    conf->contributionPath = arg;
    conf->components = components(conf);
    return NULL;
}
const char *confComposite(cmd_parms *cmd, void *c, const char *arg) {
    DirConf* conf = (DirConf*)c;
    conf->compositeName = arg;
    conf->components = components(conf);
    return NULL;
}

void *makeDirConf(apr_pool_t *p, char *dirspec) {
    DirConf* c = new (apr_palloc(p, sizeof(DirConf))) DirConf();
    apr_pool_cleanup_register(p, c, gc_pool_cleanupCallback<DirConf>, apr_pool_cleanup_null) ;
    return c;
}
void* makeServerConf(apr_pool_t *p, server_rec *s) {
    ServerConf* c = new (apr_palloc(p, sizeof(ServerConf))) ServerConf();
    apr_pool_cleanup_register(p, c, gc_pool_cleanupCallback<ServerConf>, apr_pool_cleanup_null) ;
    return c;
}

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

int postConfig(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s) {
   return OK;
}

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

void registerHooks(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
    tuscany::httpd::modwiring::makeDirConf,
    // dir merger, default is to override
    NULL,
    // server config
    tuscany::httpd::modwiring::makeServerConf,
    // merge server config
    NULL,
    // command table
    tuscany::httpd::modwiring::commands,
    // register hooks
    tuscany::httpd::modwiring::registerHooks
};

}