Skip to content

Commit

Permalink
Updates on return codes and README
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan-iancu authored and razvancrainea committed Jan 21, 2015
1 parent 870ab01 commit 92334e6
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 22 deletions.
32 changes: 32 additions & 0 deletions modules/rest_client/README
Expand Up @@ -40,6 +40,11 @@ Liviu Chircu
1.4.2. rest_post(url, send_body_pv, send_ctype_pv,
recv_body_pv[, recv_ctype_pv[, retcode_pv]])

1.5. Exported Asyncronous Functions

1.5.1. rest_get(url, body_pv[, ctype_pv[,
retcode_pv]])

List of Examples

1.1. Setting the connection_timeout parameter
Expand All @@ -49,6 +54,7 @@ Liviu Chircu
1.5. Setting the ssl_capath parameter
1.6. rest_get usage
1.7. rest_post usage
1.8. async rest_post usage

Chapter 1. Admin Guide

Expand Down Expand Up @@ -201,3 +207,29 @@ if (!rest_post("http://myserver.org/register_user", "$fU", "text/plain",
exit;
}
...

1.5. Exported Asyncronous Functions

1.5.1. rest_get(url, body_pv[, ctype_pv[, retcode_pv]])

Runs a GET HTTP requests. This function does exactly the same
as Section 1.4.1, “ rest_get(url, body_pv[, ctype_pv[,
retcode_pv]]) ” (in terms of input, output and processing), but
in an asynchronous way. The script execution is suspended until
the HTTP response if available for read (the full response).

Example 1.8. async rest_post usage
{
...
async( rest_post("http://myserver.org/register_user", "$fU", "text/plain
", "$var(body)", "$var(ct)", "$var(rcode)"), resume );
}

route[resume] {
if ( $rc < 0 ) {
xlog("Error code $var(rcode) in HTTP POST!\n");
send_reply("403", "POST Forbidden");
exit;
}
......
}
38 changes: 37 additions & 1 deletion modules/rest_client/doc/rest_client_admin.xml
Expand Up @@ -148,7 +148,7 @@ modparam("rest_client", "ssl_capath", "/home/opensips/ca_certificates")

<section>
<title>Exported Functions</title>
<section>
<section id="rest_get">
<title>
<function moreinfo="none">rest_get(url, body_pv[, ctype_pv[, retcode_pv]])
</function>
Expand Down Expand Up @@ -253,10 +253,46 @@ if (!rest_post("http://myserver.org/register_user", "$fU", "text/plain", "$var(b
exit;
}
...
</programlisting>
</example>
</section>
</section>

<section>
<title>Exported Asyncronous Functions</title>
<section>
<title>
<function moreinfo="none">rest_get(url, body_pv[, ctype_pv[, retcode_pv]])
</function>
</title>
<para>
Runs a GET HTTP requests. This function does exactly the same as
<xref linkend="rest_get"/> (in terms of input, output and processing),
but in an asynchronous way. The script execution is suspended until the HTTP
response if available for read (the full response).
</para>
<example>
<title><function moreinfo="none">async rest_post</function> usage</title>
<programlisting format="linespecific">
{
...
async( rest_post("http://myserver.org/register_user", "$fU", "text/plain", "$var(body)", "$var(ct)", "$var(rcode)"), resume );
}

route[resume] {
if ( $rc &lt; 0 ) {
xlog("Error code $var(rcode) in HTTP POST!\n");
send_reply("403", "POST Forbidden");
exit;
}
......
}
</programlisting>
</example>
</section>

</section>


</chapter>

24 changes: 13 additions & 11 deletions modules/rest_client/rest_client.c
@@ -1,7 +1,5 @@
/*
* $Id$
*
* Copyright (C) 2013 OpenSIPS Solutions
* Copyright (C) 2013-2015 OpenSIPS Solutions
*
* This file is part of opensips, a free SIP server.
*
Expand Down Expand Up @@ -335,16 +333,20 @@ static int w_async_rest_get(struct sip_msg *msg, async_resume_module **resume_f,
if (read_fd < 0) {
*resume_param = NULL;
*resume_f = NULL;
} else {
*resume_f = resume_async_get;

param->body_pv = (pv_spec_p)body_pv;
param->ctype_pv = (pv_spec_p)ctype_pv;
param->code_pv = (pv_spec_p)code_pv;
*resume_param = param;
/* keep default async status of NO_IO */
return -1;
}

return read_fd;
*resume_f = resume_async_get;

param->body_pv = (pv_spec_p)body_pv;
param->ctype_pv = (pv_spec_p)ctype_pv;
param->code_pv = (pv_spec_p)code_pv;
*resume_param = param;
/* async started with success */
async_status = read_fd;

return 1;
}

static int w_async_rest_post(struct sip_msg *msg, async_resume_module **resume_f,
Expand Down
30 changes: 20 additions & 10 deletions modules/rest_client/rest_methods.c
Expand Up @@ -221,12 +221,15 @@ enum async_ret_code resume_async_get(int fd, struct sip_msg *msg, void *_param)
LM_DBG("running handles: %d\n", running);
LM_DBG("DATA:\n%.*s\n", param->body.len, param->body.s);

if (running == running_handles)
return ASYNC_CONTINUE;
if (running == running_handles) {
async_status = ASYNC_CONTINUE;
return 1;
}

if (running > running_handles) {
LM_BUG("incremented handles!!");
return ASYNC_ERROR;
/* default async status is DONE */
return -1;
}

running_handles = running;
Expand All @@ -235,37 +238,43 @@ enum async_ret_code resume_async_get(int fd, struct sip_msg *msg, void *_param)
mrc = curl_multi_fdset(multi_handle, &rset, &wset, &eset, &max_fd);
if (mrc != CURLM_OK) {
LM_ERR("curl_multi_fdset: %s\n", curl_multi_strerror(mrc));
return ASYNC_ERROR;
/* default async status is DONE */
return -1;
}

if (max_fd == -1) {
if (running_handles != 0) {
LM_BUG("running_handles == %d", running_handles);
abort();
return ASYNC_ERROR;
/* default async status is DONE */
return -1;
}

if (FD_ISSET(fd, &rset)) {
LM_BUG("fd %d is still in rset!", fd);
abort();
return ASYNC_ERROR;
/* default async status is DONE */
return -1;
}

} else if (FD_ISSET(fd, &rset)) {
LM_DBG("fd %d still transfering...\n", fd);
return ASYNC_CONTINUE;
async_status = ASYNC_CONTINUE;
return 1;
}

if (del_transfer(fd) != 0) {
LM_BUG("failed to delete fd %d", fd);
abort();
return ASYNC_ERROR;
/* default async status is DONE */
return -1;
}

mrc = curl_multi_remove_handle(multi_handle, param->handle);
if (mrc != CURLM_OK) {
LM_ERR("curl_multi_remove_handle: %s\n", curl_multi_strerror(mrc));
return ASYNC_ERROR;
/* default async status is DONE */
return -1;
}

val.flags = PV_VAL_STR;
Expand Down Expand Up @@ -300,7 +309,8 @@ enum async_ret_code resume_async_get(int fd, struct sip_msg *msg, void *_param)
curl_easy_cleanup(param->handle);
shm_free(param);

return ASYNC_DONE;
/* default async status is DONE */
return 1;
}

/**
Expand Down

0 comments on commit 92334e6

Please sign in to comment.