tools: Add x6_options target handling to iptables_test
[framework/connectivity/connman.git] / tools / wispr.c
index 7193651..88f09c1 100644 (file)
@@ -23,6 +23,7 @@
 #include <config.h>
 #endif
 
+#define _GNU_SOURCE
 #include <stdio.h>
 #include <fcntl.h>
 #include <unistd.h>
@@ -148,7 +149,7 @@ struct wispr_session {
        char *formdata;
 };
 
-static void execute_login(struct wispr_session *wispr);
+static gboolean execute_login(gpointer user_data);
 
 static struct {
        const char *str;
@@ -307,10 +308,14 @@ struct user_input_data {
 static void user_callback(struct user_input_data *data)
 {
        char *value;
-       int len;
 
-       if (data->hidden == TRUE)
+       if (data->hidden == TRUE) {
+               ssize_t len;
+
                len = write(data->fd, "\n", 1);
+               if (len < 0)
+                       return;
+       }
 
        tcsetattr(data->fd, TCSADRAIN, &data->saved_termios);
 
@@ -358,7 +363,7 @@ static gboolean user_input(const char *label, gboolean hidden,
        struct termios new_termios;
        GIOChannel *channel;
        guint watch;
-       int len;
+       ssize_t len;
 
        data = g_try_new0(struct user_input_data, 1);
        if (data == NULL)
@@ -369,7 +374,7 @@ static gboolean user_input(const char *label, gboolean hidden,
        data->user_data = user_data;
        data->hidden = hidden;
 
-       data->fd = open("/dev/tty", O_RDWR | O_NOCTTY);
+       data->fd = open("/dev/tty", O_RDWR | O_NOCTTY | O_CLOEXEC);
        if (data->fd < 0)
                goto error;
 
@@ -398,7 +403,12 @@ static gboolean user_input(const char *label, gboolean hidden,
                goto error;
 
        len = write(data->fd, label, strlen(label));
+       if (len < 0)
+               goto error;
+
        len = write(data->fd, ": ", 2);
+       if (len < 0)
+               goto error;
 
        return TRUE;
 
@@ -491,8 +501,24 @@ static gboolean wispr_result(GWebResult *result, gpointer user_data)
 
        g_print("elapse: %f seconds\n", elapsed);
 
-       if (wispr->msg.message_type < 0)
-               goto done;
+       if (wispr->msg.message_type < 0) {
+               const char *redirect;
+
+               if (status != 302)
+                       goto done;
+
+               if (g_web_result_get_header(result, "Location",
+                                                       &redirect) == FALSE)
+                       goto done;
+
+               printf("Redirect URL: %s\n", redirect);
+               printf("\n");
+
+               wispr->request = g_web_request_get(wispr->web, redirect,
+                                                       wispr_result, wispr);
+
+               return FALSE;
+       }
 
        printf("Message type: %s (%d)\n",
                        message_type_to_string(wispr->msg.message_type),
@@ -514,7 +540,10 @@ static gboolean wispr_result(GWebResult *result, gpointer user_data)
                printf("Logoff URL: %s\n", wispr->msg.logoff_url);
        printf("\n");
 
-       if (status == 302 && wispr->msg.message_type == 100) {
+       if (status != 200 && status != 302 && status != 404)
+               goto done;
+
+       if (wispr->msg.message_type == 100) {
                if (wispr->username == NULL) {
                        user_input("Username", FALSE, username_callback, wispr);
                        return FALSE;
@@ -525,27 +554,49 @@ static gboolean wispr_result(GWebResult *result, gpointer user_data)
                        return FALSE;
                }
 
-               execute_login(wispr);
+               g_idle_add(execute_login, wispr);
                return FALSE;
-       } else if (status == 200 && wispr->msg.message_type == 120) {
+       } else if (wispr->msg.message_type == 120 ||
+                                       wispr->msg.message_type == 140) {
                int code = wispr->msg.response_code;
                printf("Login process: %s\n",
                                        code == 50 ? "SUCCESS" : "FAILURE");
        }
 
+       if (status == 302) {
+               const char *redirect;
+
+               if (g_web_result_get_header(result, "Location",
+                                                       &redirect) == FALSE)
+                       goto done;
+
+               printf("\n");
+               printf("Redirect URL: %s\n", redirect);
+               printf("\n");
+
+               wispr->request = g_web_request_get(wispr->web, redirect,
+                                                       wispr_result, wispr);
+
+               return FALSE;
+       }
+
 done:
        g_main_loop_quit(main_loop);
 
        return FALSE;
 }
 
-static void execute_login(struct wispr_session *wispr)
+static gboolean execute_login(gpointer user_data)
 {
+       struct wispr_session *wispr = user_data;
+
        wispr->request = g_web_request_post(wispr->web, wispr->msg.login_url,
                                        "application/x-www-form-urlencoded",
                                        wispr_input, wispr_result, wispr);
 
        wispr_msg_init(&wispr->msg);
+
+       return FALSE;
 }
 
 static gboolean option_debug = FALSE;