Misc (#1018):
 * httpdemo: make the 2nd parameter (output filename) optional (result will be printed to stdout if output file is not provided.
 * remove trailing "\n" from PJ_LOG.
 * change response.status_code from pj_str_t to pj_uint16_t.
 * remove PJ_EPENDING status checking from on_complete.



git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@3089 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjlib-util/include/pjlib-util/http_client.h b/pjlib-util/include/pjlib-util/http_client.h
index 9c4e6da..bcd6e42 100644
--- a/pjlib-util/include/pjlib-util/http_client.h
+++ b/pjlib-util/include/pjlib-util/http_client.h
@@ -133,7 +133,7 @@
 typedef struct pj_http_resp
 {
     pj_str_t        version;        /**< HTTP version of the server */
-    pj_str_t        status_code;    /**< Status code of the request */
+    pj_uint16_t     status_code;    /**< Status code of the request */
     pj_str_t        reason;         /**< Reason phrase */
     pj_http_headers headers;        /**< Response headers */
     /**
diff --git a/pjlib-util/src/pjlib-util-test/http_client.c b/pjlib-util/src/pjlib-util-test/http_client.c
index d179bbf..e31caf2 100644
--- a/pjlib-util/src/pjlib-util-test/http_client.c
+++ b/pjlib-util/src/pjlib-util-test/http_client.c
@@ -133,7 +133,7 @@
     PJ_UNUSED_ARG(hreq);
     PJ_UNUSED_ARG(data);
 
-    PJ_LOG(5, (THIS_FILE, "\nData received: %d bytes\n", size));
+    PJ_LOG(5, (THIS_FILE, "\nData received: %d bytes", size));
     if (size > 0) {
 #ifdef VERBOSE
         printf("%.*s\n", (int)size, (char *)data);
@@ -160,7 +160,7 @@
     *data = sdata;
     *size = sendsz;
 
-    PJ_LOG(5, (THIS_FILE, "\nSending data progress: %d out of %d bytes\n", 
+    PJ_LOG(5, (THIS_FILE, "\nSending data progress: %d out of %d bytes", 
            send_size, total_size));
 }
 
@@ -171,16 +171,16 @@
     PJ_UNUSED_ARG(hreq);
 
     if (status == PJ_ECANCELLED) {
-        PJ_LOG(5, (THIS_FILE, "Request cancelled\n"));
+        PJ_LOG(5, (THIS_FILE, "Request cancelled"));
         return;
     } else if (status == PJ_ETIMEDOUT) {
-        PJ_LOG(5, (THIS_FILE, "Request timed out!\n"));
+        PJ_LOG(5, (THIS_FILE, "Request timed out!"));
         return;
-    } else if (status != PJ_SUCCESS && status != PJ_EPENDING) {
-        PJ_LOG(3, (THIS_FILE, "Error %d\n", status));
+    } else if (status != PJ_SUCCESS) {
+        PJ_LOG(3, (THIS_FILE, "Error %d", status));
         return;
     }
-    PJ_LOG(5, (THIS_FILE, "\nData completed: %d bytes\n", resp->size));
+    PJ_LOG(5, (THIS_FILE, "\nData completed: %d bytes", resp->size));
     if (resp->size > 0 && resp->data) {
 #ifdef VERBOSE
         printf("%.*s\n", (int)resp->size, (char *)resp->data);
@@ -197,8 +197,8 @@
     PJ_UNUSED_ARG(i);
 
 #ifdef VERBOSE
-    printf("%.*s, %.*s, %.*s\n", STR_PREC(resp->version),
-           STR_PREC(resp->status_code), STR_PREC(resp->reason));
+    printf("%.*s, %d, %.*s\n", STR_PREC(resp->version),
+           resp->status_code, STR_PREC(resp->reason));
     for (i = 0; i < resp->headers.count; i++) {
         printf("%.*s : %.*s\n", 
                STR_PREC(resp->headers.header[i].name),
diff --git a/pjlib-util/src/pjlib-util/http_client.c b/pjlib-util/src/pjlib-util/http_client.c
index 681799b..7bd2b23 100644
--- a/pjlib-util/src/pjlib-util/http_client.c
+++ b/pjlib-util/src/pjlib-util/http_client.c
@@ -217,7 +217,7 @@
     } 
 
     hreq->tcp_state.current_send_size += sent;
-    TRACE_((THIS_FILE, "\nData sent: %d out of %d bytes\n", 
+    TRACE_((THIS_FILE, "\nData sent: %d out of %d bytes", 
            hreq->tcp_state.current_send_size, hreq->tcp_state.send_size));
     if (hreq->tcp_state.current_send_size == hreq->tcp_state.send_size) {
         /* Find out whether there is a request body to send. */
@@ -279,7 +279,7 @@
 {
     pj_http_req *hreq = (pj_http_req*) pj_activesock_get_user_data(asock);
 
-    TRACE_((THIS_FILE, "\nData received: %d bytes\n", size));
+    TRACE_((THIS_FILE, "\nData received: %d bytes", size));
 
     if (hreq->state == ABORTING)
         return PJ_FALSE;
@@ -471,6 +471,7 @@
     char *cptr;
     void *newdata;
     pj_scanner scanner;
+    pj_str_t s;
     pj_status_t status;
 
     PJ_USE_EXCEPTION;
@@ -508,7 +509,8 @@
     PJ_TRY {
         pj_scan_get_until_ch(&scanner, ' ', &response->version);
         pj_scan_advance_n(&scanner, 1, PJ_FALSE);
-        pj_scan_get_until_ch(&scanner, ' ', &response->status_code);
+        pj_scan_get_until_ch(&scanner, ' ', &s);
+        response->status_code = (pj_uint16_t)pj_strtoul(&s);
         pj_scan_advance_n(&scanner, 1, PJ_FALSE);
         pj_scan_get_until_ch(&scanner, '\n', &response->reason);
         if (response->reason.ptr[response->reason.slen-1] == '\r')
@@ -908,7 +910,7 @@
 
         pj_strcat2(&pkt, "\n");
         pkt.ptr[pkt.slen] = 0;
-        TRACE_((THIS_FILE, "%s\n", pkt.ptr));
+        TRACE_((THIS_FILE, "%s", pkt.ptr));
     } else {
         pkt.ptr = hreq->param.reqdata.data;
         pkt.slen = hreq->param.reqdata.size;
diff --git a/pjsip-apps/src/samples/httpdemo.c b/pjsip-apps/src/samples/httpdemo.c
index 9dc3c0e..def97d2 100644
--- a/pjsip-apps/src/samples/httpdemo.c
+++ b/pjsip-apps/src/samples/httpdemo.c
@@ -43,6 +43,18 @@
 //#define VERBOSE
 #define THIS_FILE	    "http_demo"
 
+static void on_response(pj_http_req *http_req, const pj_http_resp *resp)
+{
+    PJ_LOG(3,(THIS_FILE, "%.*s %d %.*s", (int)resp->version.slen, resp->version.ptr,
+				           resp->status_code,
+				           (int)resp->reason.slen, resp->reason.ptr));
+}
+
+static void on_send_data(pj_http_req *http_req, void **data, pj_size_t *size)
+{
+
+}
+
 static void on_data_read(pj_http_req *hreq, void *data, pj_size_t size)
 {
     PJ_UNUSED_ARG(hreq);
@@ -51,7 +63,7 @@
         fwrite(data, 1, size, f);
         fflush(f);
 #ifdef VERBOSE
-        PJ_LOG(3, (THIS_FILE, "\nData received: %d bytes\n", size));
+        PJ_LOG(3, (THIS_FILE, "Data received: %d bytes", size));
         printf("%.*s\n", (int)size, (char *)data);
 #endif
     }
@@ -62,17 +74,11 @@
 {
     PJ_UNUSED_ARG(hreq);
 
-    if (status == PJ_ECANCELLED) {
-        PJ_LOG(3, (THIS_FILE, "Request cancelled\n"));
-        return;
-    } else if (status == PJ_ETIMEDOUT) {
-        PJ_LOG(3, (THIS_FILE, "Request timed out!\n"));
-        return;
-    } else if (status != PJ_SUCCESS && status != PJ_EPENDING) {
-        PJ_LOG(3, (THIS_FILE, "Error %d\n", status));
+    if (status != PJ_SUCCESS) {
+        PJ_PERROR(1, (THIS_FILE, status, "HTTP request completed with error"));
         return;
     }
-    PJ_LOG(3, (THIS_FILE, "\nData completed: %d bytes\n", resp->size));
+    PJ_LOG(3, (THIS_FILE, "Data completed: %d bytes", resp->size));
     if (resp->size > 0 && resp->data) {
 #ifdef VERBOSE
         printf("%.*s\n", (int)resp->size, (char *)resp->data);
@@ -89,6 +95,8 @@
     pj_bzero(&hcb, sizeof(hcb));
     hcb.on_complete = &on_complete;
     hcb.on_data_read = &on_data_read;
+    hcb.on_send_data = &on_send_data;
+    hcb.on_response = &on_response;
 
     /* Create pool, timer, and ioqueue */
     pool = pj_pool_create(mem, NULL, 8192, 4096, NULL);
@@ -127,25 +135,32 @@
     pj_caching_pool cp;
     pj_status_t status;
 
-    if (argc != 3) {
-	puts("Usage: httpdemo URL filename");
+    if (argc < 2 || argc > 3) {
+	puts("Usage: httpdemo URL [output-filename]");
 	return 1;
     }
 
-    pj_log_set_level(3);
+    pj_log_set_level(5);
 
     pj_init();
     pj_caching_pool_init(&cp, NULL, 0);
     mem = &cp.factory;
     pjlib_util_init();
 
-    f = fopen(argv[2], "wb");
+    if (argc > 2)
+	f = fopen(argv[2], "wb");
+    else
+	f = stdout;
+
     status = getURL(argv[1]);
     if (status != PJ_SUCCESS) {
         PJ_PERROR(1, (THIS_FILE, status, "Error"));
     }
-    fclose(f);
 
+    if (f != stdout)
+	fclose(f);
+
+    pj_caching_pool_destroy(&cp);
     pj_shutdown();
     return 0;
 }