+
+error:
+ g_string_free(data->str, TRUE);
+ g_free(data);
+
+ return FALSE;
+}
+
+static void password_callback(const char *value, gpointer user_data)
+{
+ struct wispr_session *wispr = user_data;
+
+ g_free(wispr->password);
+ wispr->password = g_strdup(value);
+
+ printf("\n");
+
+ execute_login(wispr);
+}
+
+static void username_callback(const char *value, gpointer user_data)
+{
+ struct wispr_session *wispr = user_data;
+
+ g_free(wispr->username);
+ wispr->username = g_strdup(value);
+
+ if (wispr->password == NULL) {
+ user_input("Password", TRUE, password_callback, wispr);
+ return;
+ }
+
+ printf("\n");
+
+ execute_login(wispr);
+}
+
+static gboolean wispr_input(const guint8 **data, gsize *length,
+ gpointer user_data)
+{
+ struct wispr_session *wispr = user_data;
+ GString *buf;
+ gsize count;
+
+ buf = g_string_sized_new(100);
+
+ g_string_append(buf, "button=Login&UserName=");
+ g_string_append_uri_escaped(buf, wispr->username, NULL, FALSE);
+ g_string_append(buf, "&Password=");
+ g_string_append_uri_escaped(buf, wispr->password, NULL, FALSE);
+ g_string_append(buf, "&FNAME=0&OriginatingServer=");
+ g_string_append_uri_escaped(buf, wispr->originurl, NULL, FALSE);
+
+ count = buf->len;
+
+ g_free(wispr->formdata);
+ wispr->formdata = g_string_free(buf, FALSE);
+
+ *data = (guint8 *) wispr->formdata;
+ *length = count;
+
+ return FALSE;
+}
+
+static gboolean wispr_result(GWebResult *result, gpointer user_data)
+{
+ struct wispr_session *wispr = user_data;
+ const guint8 *chunk;
+ gsize length;
+ guint16 status;
+ gdouble elapsed;
+
+ g_web_result_get_chunk(result, &chunk, &length);
+
+ if (length > 0) {
+ //printf("%s\n", (char *) chunk);
+ g_web_parser_feed_data(wispr->parser, chunk, length);
+ return TRUE;
+ }
+
+ g_web_parser_end_data(wispr->parser);
+
+ status = g_web_result_get_status(result);
+
+ g_print("status: %03u\n", status);
+
+ elapsed = g_timer_elapsed(timer, NULL);
+
+ g_print("elapse: %f seconds\n", elapsed);
+
+ 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),
+ wispr->msg.message_type);
+ printf("Response code: %s (%d)\n",
+ response_code_to_string(wispr->msg.response_code),
+ wispr->msg.response_code);
+ if (wispr->msg.access_procedure != NULL)
+ printf("Access procedure: %s\n", wispr->msg.access_procedure);
+ if (wispr->msg.access_location != NULL)
+ printf("Access location: %s\n", wispr->msg.access_location);
+ if (wispr->msg.location_name != NULL)
+ printf("Location name: %s\n", wispr->msg.location_name);
+ if (wispr->msg.login_url != NULL)
+ printf("Login URL: %s\n", wispr->msg.login_url);
+ if (wispr->msg.abort_login_url != NULL)
+ printf("Abort login URL: %s\n", wispr->msg.abort_login_url);
+ if (wispr->msg.logoff_url != NULL)
+ printf("Logoff URL: %s\n", wispr->msg.logoff_url);
+ printf("\n");
+
+ 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;
+ }
+
+ if (wispr->password == NULL) {
+ user_input("Password", TRUE, password_callback, wispr);
+ return FALSE;
+ }
+
+ g_idle_add(execute_login, wispr);
+ return FALSE;
+ } 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 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;