-
Notifications
You must be signed in to change notification settings - Fork 909
/
curl_admin.xml
480 lines (470 loc) · 14.6 KB
/
curl_admin.xml
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<?xml version="1.0" encoding='ISO-8859-1'?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.4//EN"
"http://www.oasis-open.org/docbook/xml/4.4/docbookx.dtd" [
<!-- Include general documentation entities -->
<!ENTITY % docentities SYSTEM "../../../docbook/entities.xml">
%docentities;
]>
<!-- Module User's Guide -->
<chapter>
<title>&adminguide;</title>
<section>
<title>Overview</title>
<para>
Note: This module is still in development. Not all options documented
here work yet.
</para>
<para>
This module implements protocol functions that use the libcurl library
to fetch data from external HTTP servers or post data to HTTP servers.
The module is using a concept of "connections" to define properties
of HTTP sessions in a simple way.
</para>
<para>
The curl module has multiple settings, some of them applies to
a defined connection. You can set timeouts, max data sizes for download
and much more either using modparam settings or parameters to the
connection definition.
</para>
<para>
Like in SIP, the HTTP URL may need encoding to be transported safely
over the network. Check the string encoding functions in the Transformation
Cookbook (as used in the curl_http_query example below).
</para>
<para>
Function curl_http_query allows &kamailio; to issue an HTTP GET
request and get access to parts of the reply. This function has
been ported from the utils module and now use the same libcurl
functions.
</para>
</section>
<section>
<title>Dependencies</title>
<section>
<title>&kamailio; Modules</title>
<para>
The following modules must be loaded before this module:
<itemizedlist>
<listitem>
<para>
None.
</para>
</listitem>
</itemizedlist>
</para>
</section>
<section>
<title>External Libraries or Applications</title>
<para>
The following libraries or applications must be
installed before
running &kamailio; with this module loaded:
<itemizedlist>
<listitem>
<para>
<emphasis>libcurl</emphasis>.
</para>
</listitem>
</itemizedlist>
</para>
</section>
</section>
<section>
<title>Parameters</title>
<section id="curl.p.httpredirect">
<title><varname>httpredirect</varname> (int)</title>
<para>
If set to 1, enabled, CURL will follow HTTP 302 Redirects.
If set to 0, CURL will not follow redirects. Default is 1, enabled.
</para>
<para>
The latest redirect URL will be stored in the <emphasis>$curlredirect</emphasis>
pseudovariable.
</para>
<example>
<title>Set <varname>httpredirect</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("curl", "httpredirect", 0)
...
</programlisting>
</example>
</section>
<section id="curl.p.useragent">
<title><varname>useragent</varname> (string)</title>
<para>
Useragent to use in the HTTP protocol for requests. Defaults to
the &kamailio; SIP useragent string - including software version and platform.
</para>
<example>
<title>Set <varname>useragent</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("curl", "useragent", "Secret HTTP REST grabber 0.42")
...
</programlisting>
</example>
</section>
<section id="curl.p.maxdatasize">
<title><varname>maxdatasize</varname> (int)</title>
<para>
Defines the maximum size in bytes for a response. Note that this
is allocated from pkg memory (process memory) dynamically.
</para>
<para>
<emphasis>
Default value is zero, i.e.,
the limit on the datasize is disabled.
</emphasis>
</para>
<example>
<title>Set <varname>maxdatasize</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("curl", "maxdatasize", 2000)
...
</programlisting>
</example>
</section>
<section id="curl.p.connection_timeout">
<title><varname>connection_timeout</varname> (int)</title>
<para>
Defines in seconds how long &kamailio; waits for response
from servers.
</para>
<para>
<emphasis>
Default value is zero, i.e.,
the timeout function is disabled.
</emphasis>
</para>
<example>
<title>Set <varname>connection_timeout</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("curl", "connection_timeout", 2)
...
</programlisting>
</example>
</section>
<section id="curl.p.tlsclientcert">
<title><varname>tlsclientcert</varname> (string)</title>
<para>
File name for a TLS client certificate. The certificate needs to be encoded
in PEM format.
</para>
<para>
<emphasis>
Default value is empty string, i.e.
no client certificate used. Note that if
you specify a client cert, you also need to specify
the <varname>tlsclientkey</varname>.
</emphasis>
</para>
<example>
<title>Set <varname>tlsclientcert</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("curl", "tlsclientcert", "/var/certs/sollentuna.example.com.cert")
...
</programlisting>
</example>
</section>
<section id="curl.p.tlsclientkey">
<title><varname>tlsclientkey</varname> (string)</title>
<para>
File name for a TLS client key. The key needs to be encoded
in PEM format.
</para>
<para>
<emphasis>
Default value is empty string, i.e.
no client certificate or key is used. Note that if
you specify a client key, you also need to specify
the <varname>tlsclientcert</varname>.
</emphasis>
</para>
<example>
<title>Set <varname>tlsclientkey</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("curl", "tlsclientkey", "/var/certs/sollentuna.example.com.key")
...
</programlisting>
</example>
</section>
<section id="curl.p.tlscacert">
<title><varname>tlscacert</varname> (string)</title>
<para>
File name for the trusted TLS CA cert used to verify servers.
The certificates need to be encoded in PEM format.
</para>
<para>
<emphasis>
Default value is empty string, i.e.
no CA certificate is used to verify the host. If
<varname>tlsverifyhost</varname> is on, all TLS
connections will fail without any CA certificate
to validate with.
</emphasis>
</para>
<example>
<title>Set <varname>tlscacert</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("curl", "tlscacert", "/var/certs/ca/edvina-sip-ca.pem")
...
</programlisting>
</example>
</section>
<section id="curl.p.tlsverifyserver">
<title><varname>tlsverifyserver</varname> (int)</title>
<para>
If set to 0, TLS verification of the server certificate
is disabled. This means that the connection will get
encrypted, but there's no authentication. There's no
proof that the transmission of data is to the host
that is meant to receive data.
</para>
<para>
IF set to 1, default setting, and one or more CA certificates
is configured, the server TLS certificate will be validated.
If validation fails, the connection fails.
</para>
<para>
</para>
<example>
<title>Set <varname>tlsverifyserver</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("curl", "tlsverifyserver", 1)
...
</programlisting>
</example>
</section>
<section id="curl.p.curlcon">
<title><varname>curlcon</varname> (string)</title>
<para>
Defines a connection and credentials for the connection for use
in a connection-oriented function call in this module.
</para>
<para>
<emphasis>Syntax:</emphasis>
<connection-name>=><schema>://[<username>:<password>@]<hostname/address>[;param=value]
</para>
<para>
The address in the URL is the base for the URL in the <function>curlcon_query()</function> call. The
address given in the function call will be appended to the base URL in the connection definition.
</para>
<para>
<emphasis>
By default, no connections are defined.
</emphasis>
</para>
<para>
Parameters
<itemizedlist>
<listitem><para>
<emphasis>httpredirect</emphasis> Set to 1 for following HTTP 302
redirect. 0 to disable. Default is the setting for the httpredirect modparam.
</para></listitem>
<listitem><para>
<emphasis>maxdatasize</emphasis> The maximum datasize for a response. Overrides
the maxdatasize modparam setting.
</para></listitem>
<listitem><para>
<emphasis>timeout</emphasis> Timeout used for this connection. Overrides the
default timeout for the module.
</para></listitem>
<listitem><para>
<emphasis>useragent</emphasis> Useragent used for HTTP requests. Overrides
useragent modparam.
</para></listitem>
</itemizedlist>
</para>
<example>
<title>Set <varname>curlcon</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("curl", "curlcon", "apione=>http://atlanta.example.com")
modparam("curl", "curlcon", "apitwo=>http://atlanta.example.com/api/12")
modparam("curl", "curlcon", "apithree=>http://annabella:mysecret@atlanta.example.com/api/12")
modparam("curl", "curlcon", "apifour=>http://stockholm.example.com/api/getstuff;timeout=12")
...
</programlisting>
</example>
</section>
</section>
<section>
<title>Functions</title>
<section id="curl.f.curl_connect">
<title>
<function moreinfo="none">curl_connect(connection, url, [content_type, data,] result)</function>
</title>
<para>
Sends HTTP GET or POST request to a given connection. For a
POST request, content-type can be specified.
</para>
<itemizedlist>
<listitem>
<para>
<emphasis>connection</emphasis> - the name of an existing
curl connection, definied by a modparam.
</para>
<para>
<emphasis>url</emphasis> - the part of the URL to add to the
predefined URL in the connection definition.
</para>
<para>
<emphasis>content_type</emphasis> - Used only when posting
data with HTTP POST. An Internet Media type, like
"application/json" or "text/plain". Will be added to the
HTTP request as a header.
</para>
<para>
<emphasis>data</emphasis> - Data or a pseudo variable holding
data to be posted.
</para>
<para>
<emphasis>result</emphasis> - The name of a pseudo variable that
will have the data of the response from the HTTP server.
</para>
</listitem>
</itemizedlist>
<para>
The return value is the HTTP return code (if >=100) or the
CURL error code if below 100. See the $curlerror pseudovariable
below for more information about CURL error codes.
</para>
<para>
This function can be used from REQUEST_ROUTE,
ONREPLY_ROUTE, FAILURE_ROUTE, and BRANCH_ROUTE.
</para>
<example>
<title><function>curl_connect()</function> usage</title>
<programlisting format="linespecific">
...
modparam("curl", "curlcon", "apiserver=>http://kamailio.org/api/");
...
# POST Request
$var(res) = curl_connect("apiserver", "/mailbox", "application/json", "{ ok, {200, ok}}", "$avp(gurka)");
xlog("L_INFO", "API-server Curl connection: $avp(gurka) Result code $var(res)\n");
$var(res) = curl_connect("apiserver", "/callroute", "application/json", "$var(jsondata)", "$avp(route)");
xlog("L_INFO", "API-server Curl connection: $avp(route) Result code $var(res)\n");
...
</programlisting>
</example>
</section>
<section id="curl.f.curl_http_query">
<title>
<function moreinfo="none">curl_http_query(url, [post-data], result)</function>
</title>
<para>
Sends HTTP GET or POST request according to URL given in
<quote>url</quote> parameter, which is a string that may
contain pseudo variables.
</para>
<para>
If you want to make a POST-Request, you have to define
the <quote>post</quote>-data, that should be submitted
in that request as the second parameter.
</para>
<para>
If HTTP server returns a class 2xx, 3xx or 4xx reply,
the first line of the reply's body (if any) is
stored in <quote>result</quote> parameter,
which must be a writable pseudo variable.
</para>
<para>
Function returns reply code of HTTP reply or -1
if something went wrong.
</para>
<para>
This function can be used from REQUEST_ROUTE,
ONREPLY_ROUTE, FAILURE_ROUTE, and BRANCH_ROUTE.
</para>
<para>
Note that this function is based on the http_query function in the utils module.
It is changed to use the same base library and settings as the rest of the functions in this module.
</para>
<example>
<title><function>curl_http_query()</function> usage</title>
<programlisting format="linespecific">
...
# GET-Request
curl_http_query("http://tutpro.com/index.php?r_uri=$(ru{s.escape.param})&f_uri=$(fu{s.escape.param})",
"$var(result)")
switch ($retcode) {
...
}
...
</programlisting>
<programlisting format="linespecific">
...
# POST-Request
curl_http_query("http://tutpro.com/index.php", "r_uri=$(ru{s.escape.param})&f_uri=$(fu{s.escape.param})",
"$var(result)")
switch ($retcode) {
...
}
...
</programlisting>
</example>
</section>
</section>
<section>
<title>Pseudovariables</title>
<section >
<title>
<function moreinfo="none">$curlerror(error)</function>
</title>
<para>
cURL returns error codes from the protocol used. If an error
happens, a cURL specific error code below 100 is returned.
The $curlerror pv returns a text string representing the error.
For more information on cURL error codes, please visit
http://curl.haxx.se/libcurl/c/libcurl-errors.html
</para>
</section>
</section>
<section>
<title>RPC Commands</title>
<section>
<title><function moreinfo="none">curl.listcon</function></title>
<para>
Lists all defined CURL connections
</para>
<para>Parameters:</para>
<itemizedlist>
<listitem><para>No parameters</para></listitem>
</itemizedlist>
</section>
</section>
<section>
<title>Counters</title>
<section >
<title>
<function moreinfo="none">curl.connections</function>
</title>
<para>
The number of connection definitions that are in-memory.
</para>
</section>
<section >
<title>
<function moreinfo="none">curl.connok</function>
</title>
<para>
The number of successful connections since &kamailio; start
</para>
</section>
<section >
<title>
<function moreinfo="none">curl.connfail</function>
</title>
<para>
The number of failed connections since &kamailio; start
</para>
</section>
</section>
</chapter>