<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][14096] </title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=14096">14096</a></dd>
<dt>Author</dt> <dd>rupa</dd>
<dt>Date</dt> <dd>2009-07-01 17:26:59 -0500 (Wed, 01 Jul 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>add json support
fix dialplan app</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunksrcmodapplicationsmod_curlMakefile">freeswitch/trunk/src/mod/applications/mod_curl/Makefile</a></li>
<li><a href="#freeswitchtrunksrcmodapplicationsmod_curlmod_curlc">freeswitch/trunk/src/mod/applications/mod_curl/mod_curl.c</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunksrcmodapplicationsmod_curlMakefile"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/applications/mod_curl/Makefile (14095 => 14096)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_curl/Makefile        2009-07-01 21:27:02 UTC (rev 14095)
+++ freeswitch/trunk/src/mod/applications/mod_curl/Makefile        2009-07-01 22:26:59 UTC (rev 14096)
</span><span class="lines">@@ -1,2 +1,26 @@
</span><ins>+json-c=json-c-0.8
</ins><span class="cx"> BASE=../../../..
</span><ins>+
+JSON_DIR=$(BASE)/libs/$(json-c)
+
+JSONLA=$(JSON_DIR)/libjson.la
+
+LOCAL_CFLAGS=-I$(JSON_DIR)
+LOCAL_LIBADD=$(JSONLA)
+
</ins><span class="cx"> include $(BASE)/build/modmake.rules
</span><ins>+
+DEFAULT_ARGS=--prefix=$(PREFIX) --disable-shared --with-pic
+
+$(LOCAL_OBJS): $(LOCAL_SOURCES)
+
+$(JSON_DIR):
+        $(GETLIB) $(json-c).tar.gz
+
+$(JSON_DIR)/Makefile: $(JSON_DIR)
+        cd $(JSON_DIR) &amp;&amp; CFLAGS=$(AM_CFLAGS) CC=$(CC) CXX=$(CXX) ./configure --disable-shared --with-pic CPPFLAGS= LDFLAGS=
+        $(TOUCH_TARGET)
+
+$(JSONLA): $(JSON_DIR)/Makefile
+        cd $(JSON_DIR) &amp;&amp; $(MAKE)
+        $(TOUCH_TARGET)
</ins></span></pre></div>
<a id="freeswitchtrunksrcmodapplicationsmod_curlmod_curlc"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/src/mod/applications/mod_curl/mod_curl.c (14095 => 14096)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/applications/mod_curl/mod_curl.c        2009-07-01 21:27:02 UTC (rev 14095)
+++ freeswitch/trunk/src/mod/applications/mod_curl/mod_curl.c        2009-07-01 22:26:59 UTC (rev 14096)
</span><span class="lines">@@ -31,6 +31,7 @@
</span><span class="cx">  
</span><span class="cx"> #include &lt;switch.h&gt;
</span><span class="cx"> #include &lt;curl/curl.h&gt;
</span><ins>+#include &lt;json.h&gt;
</ins><span class="cx"> 
</span><span class="cx"> /* Prototypes */
</span><span class="cx"> SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_curl_shutdown);
</span><span class="lines">@@ -94,16 +95,6 @@
</span><span class="cx">         switch_copy_string(header, ptr, realsize);
</span><span class="cx">         header[realsize] = '\0';
</span><span class="cx"> 
</span><del>-        /* parse data - placeholder
-        if ((data = index(header, ':')) {
-                *data = '\0';
-                data++;
-                while(*data == ' ' &amp;&amp; *data != '\0') {
-                        data++;
-                }
-        }
-        */
-        
</del><span class="cx">         http_data-&gt;headers = curl_slist_append(http_data-&gt;headers, header);
</span><span class="cx">         
</span><span class="cx">         return realsize;
</span><span class="lines">@@ -134,8 +125,8 @@
</span><span class="cx">                 curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0);
</span><span class="cx">         }
</span><span class="cx">         curl_easy_setopt(curl_handle, CURLOPT_POST, SWITCH_FALSE);
</span><del>-        curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1);
-        curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, 10);
</del><ins>+        curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 0);
+        curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, 0);
</ins><span class="cx">         curl_easy_setopt(curl_handle, CURLOPT_URL, url);
</span><span class="cx">         curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, file_callback);
</span><span class="cx">         curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *) http_data);
</span><span class="lines">@@ -161,6 +152,69 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> 
</span><ins>+static char *print_json(switch_memory_pool_t *pool, http_data_t *http_data) {
+        struct json_object *top = NULL;
+        struct json_object *headers = NULL;
+        char *data = NULL;
+        struct curl_slist *header = http_data-&gt;headers;
+        
+        top = json_object_new_object();
+        headers = json_object_new_array();
+        json_object_object_add(top, &quot;status_code&quot;, json_object_new_int(http_data-&gt;http_response_code));
+        if (http_data-&gt;http_response) {
+                json_object_object_add(top, &quot;body&quot;, json_object_new_string(http_data-&gt;http_response));
+        }
+        
+        /* parse header data */
+        while(header) {
+                struct json_object *obj = NULL;
+                /* remove trailing \r */
+                if ((data=rindex(header-&gt;data, '\r'))) {
+                        *data = '\0';
+                }
+                
+                if switch_strlen_zero(header-&gt;data) {
+                        header = header-&gt;next;
+                        continue;
+                }
+                
+                if ((data = index(header-&gt;data, ':'))) {
+                        *data = '\0';
+                        data++;
+                        while(*data == ' ' &amp;&amp; *data != '\0') {
+                                data++;
+                        }
+                        obj = json_object_new_object(); 
+                        json_object_object_add(obj, &quot;key&quot;, json_object_new_string(header-&gt;data));
+                        json_object_object_add(obj, &quot;value&quot;, json_object_new_string(data));
+                        json_object_array_add(headers, obj);
+                } else {
+                        if (!strncmp(&quot;HTTP&quot;, header-&gt;data, 4)) {
+                                char *argv[3] = { 0 };
+                                int argc;
+                                if ((argc = switch_separate_string(header-&gt;data, ' ', argv, (sizeof(argv) / sizeof(argv[0]))))) {
+                                        if (argc &gt; 2) {
+                                                json_object_object_add(top, &quot;version&quot;, json_object_new_string(argv[0]));
+                                                json_object_object_add(top, &quot;phrase&quot;, json_object_new_string(argv[2]));
+                                        } else {
+                                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Unparsable header: argc: %d\n&quot;, argc);
+                                        }
+                                } else {
+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Starts with HTTP but not parsable: %s\n&quot;, header-&gt;data);
+                                }
+                        } else {
+                                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Unparsable header: %s\n&quot;, header-&gt;data);
+                        }
+                }
+                header = header-&gt;next;
+        }
+        json_object_object_add(top, &quot;headers&quot;, headers);
+        data = switch_core_strdup(pool, json_object_to_json_string(top));
+        json_object_put(top); /* should free up all children */
+        
+        return data;
+}
+
</ins><span class="cx"> SWITCH_STANDARD_APP(curl_app_function)
</span><span class="cx"> {
</span><span class="cx">         switch_status_t status = SWITCH_STATUS_SUCCESS;
</span><span class="lines">@@ -172,8 +226,13 @@
</span><span class="cx">         switch_memory_pool_t *pool = NULL;
</span><span class="cx">         switch_channel_t *channel = switch_core_session_get_channel(session);
</span><span class="cx">         char *url = NULL;
</span><ins>+        switch_bool_t do_headers = SWITCH_FALSE;
+        switch_bool_t do_json = SWITCH_FALSE;
</ins><span class="cx">         http_data_t *http_data = NULL;
</span><ins>+        struct curl_slist *slist = NULL;
+        switch_stream_handle_t stream = { 0 };
</ins><span class="cx">         
</span><ins>+        
</ins><span class="cx">         if (session) {
</span><span class="cx">                 pool = switch_core_session_get_pool(session);
</span><span class="cx">         } else {
</span><span class="lines">@@ -188,15 +247,37 @@
</span><span class="cx">                         switch_goto_status(SWITCH_STATUS_SUCCESS, usage);
</span><span class="cx">                 }
</span><span class="cx">                 
</span><del>-                if (argc &gt; 0) {
-                        url = switch_core_strdup(pool, argv[0]);
</del><ins>+                url = switch_core_strdup(pool, argv[0]);
+
+                if (argc &gt; 1) {
+                        if (!strcasecmp(&quot;headers&quot;, argv[1])) {
+                                do_headers = SWITCH_TRUE;
+                        }
+                        if (!strcasecmp(&quot;json&quot;, argv[1])) {
+                                do_json = SWITCH_TRUE;
+                        }
</ins><span class="cx">                 }
</span><span class="cx">         }
</span><span class="cx">         
</span><span class="cx">         http_data = do_lookup_url(pool, url);
</span><del>-        switch_channel_set_variable(channel, &quot;curl_response_data&quot;, http_data-&gt;http_response);
</del><ins>+        if (do_json) {
+                switch_channel_set_variable(channel, &quot;curl_response_data&quot;, print_json(pool, http_data));
+        } else {
+                SWITCH_STANDARD_STREAM(stream);
+                stream.write_function(&amp;stream, &quot;%ld\n&quot;, http_data-&gt;http_response_code);
+                if(do_headers) {
+                        slist = http_data-&gt;headers;
+                        while(slist) {
+                                stream.write_function(&amp;stream, &quot;%s\n&quot;, slist-&gt;data);
+                                slist = slist-&gt;next;
+                        }
+                        stream.write_function(&amp;stream, &quot;\n&quot;);
+                }
+                stream.write_function(&amp;stream, &quot;%s&quot;, http_data-&gt;http_response ? http_data-&gt;http_response : &quot;&quot;);
+                switch_channel_set_variable(channel, &quot;curl_response_data&quot;, stream.data);
+        }
</ins><span class="cx">         switch_channel_set_variable(channel, &quot;curl_response_code&quot;, 
</span><del>-                switch_core_sprintf(pool, &quot;%ld&quot;, http_data-&gt;http_response_code));
</del><ins>+        switch_core_sprintf(pool, &quot;%ld&quot;, http_data-&gt;http_response_code));
</ins><span class="cx"> 
</span><span class="cx">         switch_goto_status(SWITCH_STATUS_SUCCESS, done);
</span><span class="cx">         
</span><span class="lines">@@ -205,6 +286,8 @@
</span><span class="cx">         switch_goto_status(status, done);
</span><span class="cx">         
</span><span class="cx"> done:
</span><ins>+        switch_safe_free(stream.data);
+        curl_slist_free_all(http_data-&gt;headers);
</ins><span class="cx">         if (!session) {
</span><span class="cx">                 switch_core_destroy_memory_pool(&amp;pool);
</span><span class="cx">         }
</span><span class="lines">@@ -216,7 +299,8 @@
</span><span class="cx">         int argc;
</span><span class="cx">         char *mydata = NULL;
</span><span class="cx">         char *url = NULL;
</span><del>-        switch_bool_t headers = SWITCH_FALSE;
</del><ins>+        switch_bool_t do_headers = SWITCH_FALSE;
+        switch_bool_t do_json = SWITCH_FALSE;
</ins><span class="cx">         struct curl_slist *slist = NULL;
</span><span class="cx">         http_data_t *http_data = NULL;
</span><span class="cx"> 
</span><span class="lines">@@ -242,21 +326,28 @@
</span><span class="cx">                 
</span><span class="cx">                 if (argc &gt; 1) {
</span><span class="cx">                         if (!strcasecmp(&quot;headers&quot;, argv[1])) {
</span><del>-                                headers = SWITCH_TRUE;
</del><ins>+                                do_headers = SWITCH_TRUE;
</ins><span class="cx">                         }
</span><ins>+                        if (!strcasecmp(&quot;json&quot;, argv[1])) {
+                                do_json = SWITCH_TRUE;
+                        }
</ins><span class="cx">                 }
</span><span class="cx">                 
</span><span class="cx">                 http_data = do_lookup_url(pool, url);
</span><del>-                stream-&gt;write_function(stream, &quot;%ld\n&quot;, http_data-&gt;http_response_codenot);
-                if(headers) {
-                        slist = http_data-&gt;headers;
-                        while(slist) {
-                                stream-&gt;write_function(stream, &quot;%s\n&quot;, slist-&gt;data);
-                                slist = slist-&gt;next;
</del><ins>+                if (do_json) {
+                        stream-&gt;write_function(stream, &quot;%s&quot;, print_json(pool, http_data));
+                } else {
+                        stream-&gt;write_function(stream, &quot;%ld\n&quot;, http_data-&gt;http_response_code);
+                        if(do_headers) {
+                                slist = http_data-&gt;headers;
+                                while(slist) {
+                                        stream-&gt;write_function(stream, &quot;%s\n&quot;, slist-&gt;data);
+                                        slist = slist-&gt;next;
+                                }
+                                stream-&gt;write_function(stream, &quot;\n&quot;);
</ins><span class="cx">                         }
</span><del>-                        stream-&gt;write_function(stream, &quot;\n&quot;);
</del><ins>+                        stream-&gt;write_function(stream, &quot;%s&quot;, http_data-&gt;http_response ? http_data-&gt;http_response : &quot;&quot;);
</ins><span class="cx">                 }
</span><del>-                stream-&gt;write_function(stream, &quot;%s&quot;, http_data-&gt;http_response ? http_data-&gt;http_response : &quot;&quot;);
</del><span class="cx">         }
</span><span class="cx">         switch_goto_status(SWITCH_STATUS_SUCCESS, done);
</span><span class="cx"> 
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>