summaryrefslogtreecommitdiffstats
path: root/sca-cpp/branches/cpp-contrib/modules/http/curl.hpp
blob: f2c9458f42e4cad97609f5f38c5d79d817b06c1e (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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * 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$ */

#ifndef tuscany_curl_hpp
#define tuscany_curl_hpp

/**
 * CURL HTTP client functions.
 */

#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include "string.hpp"
#include "gc.hpp"
#include "list.hpp"
#include "value.hpp"
#include "element.hpp"
#include "monad.hpp"
#include "parallel.hpp"
#include "../atom/atom.hpp"
#include "../json/json.hpp"

namespace tuscany {
namespace http {

/**
 * CURL library runtime, one per process.
 */
class CURLRuntime {
public:
    CURLRuntime() {
        curl_global_init(CURL_GLOBAL_ALL);
    }
} curlRuntime;

/**
 * Represents a CURL session handle.
 */
class CURLSession {
public:
    CURLSession() : h(curl_easy_init()), owner(true) {
    }

    CURLSession(const CURLSession& c) : h(c.h), owner(false) {
    }

    ~CURLSession() {
        if (!owner)
            return;
        if (h == NULL)
            return;
        curl_easy_cleanup(h);
    }

private:
    CURL* h;
    const bool owner;

    friend CURL* handle(const CURLSession& c);
};

/**
 * Returns the CURL handle used by a CURL session.
 */
CURL* handle(const CURLSession& c) {
    return c.h;
}

/**
 * Context passed to the read callback function.
 */
class CURLReadContext {
public:
    CURLReadContext(const list<string>& ilist) : ilist(ilist) {
    }
    list<string> ilist;
};

/**
 * Called by CURL to read data to send.
 */
size_t readCallback(void *ptr, size_t size, size_t nmemb, void *data) {
    CURLReadContext& rcx = *static_cast<CURLReadContext*>(data);
    if (isNil(rcx.ilist))
        return 0;
    const list<string> f(fragment(rcx.ilist, size * nmemb));
    const string s = car(f);
    rcx.ilist = cdr(f);
    memcpy(ptr, c_str(s), length(s));
    return length(s);
}

/**
 * Context passed to CURL write callback function.
 */
template<typename R> class CURLWriteContext {
public:
    CURLWriteContext(const lambda<R(const string&, const R)>& reduce, const R& accum) : reduce(reduce), accum(accum) {
    }
    const lambda<R(const string&, const R)> reduce;
    R accum;
};

/**
 * Called by CURL to write received data.
 */
template<typename R> size_t writeCallback(void *ptr, size_t size, size_t nmemb, void *data) {
    CURLWriteContext<R>& wcx = *(static_cast<CURLWriteContext<R>*> (data));
    const size_t realsize = size * nmemb;
    wcx.accum = wcx.reduce(string((const char*)ptr, realsize), wcx.accum);
    return realsize;
}

/**
 * Apply an HTTP verb to a list containing a list of headers and a list of content, and
 * a reduce function used to process the response.
 */
curl_slist* headers(curl_slist* cl, const list<string>& h) {
    if (isNil(h))
        return cl;
    return headers(curl_slist_append(cl, c_str(string(car(h)))), cdr(h));
}

template<typename R> const failable<list<R> > apply(const list<list<string> >& hdr, const lambda<R(const string&, const R)>& reduce, const R& initial, const string& url, const string& verb, const CURLSession& cs) {

    // Init the curl session
    CURL* ch = handle(cs);
    curl_easy_reset(ch);
    curl_easy_setopt(ch, CURLOPT_USERAGENT, "libcurl/1.0");

    //TODO use HTTP chunking, for now just convert request to a single string
    ostringstream os;
    write(cadr(hdr), os);
    const string s = str(os);
    const int sz = length(s);

    // Setup the read, header and write callbacks
    CURLReadContext rcx(mklist(s));
    curl_easy_setopt(ch, CURLOPT_READFUNCTION, (size_t (*)(void*, size_t, size_t, void*))readCallback);
    curl_easy_setopt(ch, CURLOPT_READDATA, &rcx);
    CURLWriteContext<R> hcx(reduce, initial);
    curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, (size_t (*)(void*, size_t, size_t, void*))(writeCallback<R>));
    curl_easy_setopt(ch, CURLOPT_HEADERDATA, &hcx);
    CURLWriteContext<R> wcx(reduce, initial);
    curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, (size_t (*)(void*, size_t, size_t, void*))(writeCallback<R>));
    curl_easy_setopt(ch, CURLOPT_WRITEDATA, &wcx);
    curl_easy_setopt(ch, CURLOPT_TCP_NODELAY, true);

    // Set the request headers
    curl_slist* hl = headers(NULL, car(hdr));
    if (hl != NULL)
        curl_easy_setopt(ch, CURLOPT_HTTPHEADER, hl);

    // Apply the HTTP verb
    curl_easy_setopt(ch, CURLOPT_URL, c_str(url));
    if (verb == "POST") {
        curl_easy_setopt(ch, CURLOPT_POST, true);
        curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, sz);
    } else if (verb == "PUT") {
        curl_easy_setopt(ch, CURLOPT_UPLOAD, true);
        curl_easy_setopt(ch, CURLOPT_INFILESIZE, sz);
    } else if (verb == "DELETE")
        curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, "DELETE");
    const CURLcode rc = curl_easy_perform(ch);

    if (hl != NULL)
        curl_slist_free_all(hl);

    // Return the HTTP return code or content
    if (rc)
        return mkfailure<list<R> >(string(curl_easy_strerror(rc)));
    long httprc;
    curl_easy_getinfo (ch, CURLINFO_RESPONSE_CODE, &httprc);
    if (httprc != 200 && httprc != 201) {
        ostringstream es;
        es << "HTTP code " << httprc;
        return mkfailure<list<R> >(str(es));
    }
    return mklist<R>(hcx.accum, wcx.accum);
}

/**
 * Evaluate an expression remotely, at the given URL.
 */
const failable<value> evalExpr(const value& expr, const string& url, const CURLSession& ch) {
    debug(url, "http::evalExpr::url");
    debug(expr, "http::evalExpr::input");

    // Convert expression to a JSON-RPC request
    json::JSONContext cx;
    const failable<list<string> > jsreq = json::jsonRequest(1, car<value>(expr), cdr<value>(expr), cx);
    if (!hasContent(jsreq))
        return mkfailure<value>(reason(jsreq));

    // POST it to the URL
    const list<string> h = mklist<string>("Content-Type: application/json-rpc");
    const failable<list<list<string> > > res = apply<list<string> >(mklist<list<string> >(h, content(jsreq)), rcons<string>, list<string>(), url, "POST", ch);
    if (!hasContent(res))
        return mkfailure<value>(reason(res));

    // Parse and return JSON-RPC result
    const failable<value> rval = json::jsonResultValue(cadr<list<string> >(content(res)), cx);
    if (!hasContent(rval))
        return mkfailure<value>(reason(rval));
    debug(content(rval), "http::evalExpr::result");
    return content(rval);
}

/**
 * Find and return a header.
 */
const failable<string> header(const char* prefix, const list<string>& h) {
    if (isNil(h))
        return mkfailure<string>(string("Couldn't find header: ") + prefix);
    const string s = car(h);
    if (find(s, prefix) != 0)
        return header(prefix, cdr(h));
    const string l(substr(s, length(prefix)));
    return substr(l, 0, find_first_of(l, "\r\n"));
}

/**
 * Find and return a location header.
 */
const failable<string> location(const list<string>& h) {
    return header("Location: ", h);
}

/**
 * Convert a location to an entry id.
 */
const failable<value> entryId(const failable<string> l) {
    if (!hasContent(l))
        return mkfailure<value>(reason(l));
    const string ls(content(l));
    return value(mklist<value>(string(substr(ls, find_last(ls, '/') + 1))));
}

/**
 * Find and return a content-type header.
 */
const failable<string> contentType(const list<string>& h) {
    return header("Content-Type: ", h);
}

/**
 * HTTP GET, return the resource at the given URL.
 */
template<typename R> const failable<list<R> > get(const lambda<R(const string&, const R)>& reduce, const R& initial, const string& url, const CURLSession& ch) {
    debug(url, "http::get::url");
    const list<list<string> > req = mklist(list<string>(), list<string>());
    return apply(req, reduce, initial, url, "GET", ch);
}

/**
 * HTTP GET, return a list of values representing the resource at the given URL.
 */
const failable<value> getcontent(const string& url, const CURLSession& ch) {
    debug(url, "http::get::url");

    // Get the contents of the resource at the given URL
    const failable<list<list<string> > > res = get<list<string>>(rcons<string>, list<string>(), url, ch);
    if (!hasContent(res))
        return mkfailure<value>(reason(res));
    const list<string> ls(reverse(cadr(content(res))));

    // Return the content as a list of values
    const value val(mkvalues(ls));
    debug(val, "http::get::result");
    return val;
}

/**
 * HTTP GET, return a list of values representing the resource at the given URL.
 */
const failable<value> get(const string& url, const CURLSession& ch) {
    debug(url, "http::get::url");

    // Get the contents of the resource at the given URL
    const failable<list<list<string> > > res = get<list<string> >(rcons<string>, list<string>(), url, ch);
    if (!hasContent(res))
        return mkfailure<value>(reason(res));
    const list<string> ls(reverse(cadr(content(res))));

    const string ct(content(contentType(car(content(res)))));
    if (ct == "application/atom+xml;type=entry") {
        const value val(atom::entryValue(content(atom::readEntry(ls))));
        debug(val, "http::get::result");
        return val;
    }

    // Return the content as a list of values
    const value val(mkvalues(ls));
    debug(val, "http::get::result");
    return val;
}

/**
 * HTTP POST.
 */
const failable<value> post(const value& val, const string& url, const CURLSession& ch) {

    // Convert value to an ATOM entry
    const failable<list<string> > entry = atom::writeATOMEntry(atom::entryValuesToElements(val));
    if (!hasContent(entry))
        return mkfailure<value>(reason(entry));
    debug(url, "http::post::url");
    debug(content(entry), "http::post::input");

    // POST it to the URL
    const list<string> h = mklist<string>("Content-Type: application/atom+xml");
    const list<list<string> > req = mklist<list<string> >(h, content(entry));
    const failable<list<list<string> > > res = apply<list<string>>(req, rcons<string>, list<string>(), url, "POST", ch);
    if (!hasContent(res))
        return mkfailure<value>(reason(res));

    // Return the new entry id from the HTTP location header
    const failable<value> eid(entryId(location(car(content(res)))));
    debug(eid, "http::post::result");
    return eid;
}

/**
 * HTTP PUT.
 */
const failable<value> put(const value& val, const string& url, const CURLSession& ch) {

    // Convert value to an ATOM entry
    const failable<list<string> > entry = atom::writeATOMEntry(atom::entryValuesToElements(val));
    if (!hasContent(entry))
        return mkfailure<value>(reason(entry));
    debug(url, "http::put::url");
    debug(content(entry), "http::put::input");

    // PUT it to the URL
    const list<string> h = mklist<string>("Content-Type: application/atom+xml");
    const list<list<string> > req = mklist<list<string> >(h, content(entry));
    const failable<list<list<string> > > res = apply<list<string> >(req, rcons<string>, list<string>(), url, "PUT", ch);
    if (!hasContent(res))
        return mkfailure<value>(reason(res));

    debug(true, "http::put::result");
    return value(true);
}

/**
 * HTTP DELETE.
 */
const failable<value, string> del(const string& url, const CURLSession& ch) {
    debug(url, "http::delete::url");

    const list<list<string> > req = mklist(list<string>(), list<string>());
    const failable<list<list<string> > > res = apply<list<string> >(req, rcons<string>, list<string>(), url, "DELETE", ch);
    if (!hasContent(res))
        return mkfailure<value>(reason(res));

    debug(true, "http::delete::result");
    return value(true);
}

/**
 * HTTP client proxy function.
 */
struct proxy {
    proxy(const string& uri) : uri(uri) {
    }

    const value operator()(const list<value>& args) const {
        CURLSession cs;
        failable<value> val = evalExpr(args, uri, cs);
        if (!hasContent(val))
            return value();
        return content(val);
    }

    const string uri;
};

}
}

#endif /* tuscany_curl_hpp */