examples/test-http-proxy.c
author Mikael Berthe <mikael@lilotux.net>
Mon, 25 Jan 2016 18:35:10 +0100
changeset 685 dfa02c1c7fda
parent 664 f57b1b61e1fe
child 690 7ccf2113ec5f
permissions -rw-r--r--
Bumped version to 1.5.2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
517
6fabea75bea7 Make the emacs header changes to tests and examples
Mikael Hallendal <micke@imendio.com>
parents: 140
diff changeset
     1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
     2
/*
99
81e9d035de02 2004-10-05 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 86
diff changeset
     3
 * Copyright (C) 2004 Imendio AB
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
     4
 *
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
     6
 * modify it under the terms of the GNU Lesser General Public License as
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
     7
 * published by the Free Software Foundation; either version 2 of the
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
     8
 * License, or (at your option) any later version.
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
     9
 *
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    13
 * Lesser General Public License for more details.
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    14
 *
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    15
 * You should have received a copy of the GNU Lesser General Public
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    16
 * License along with this program; if not, write to the
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    17
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    18
 * Boston, MA 02111-1307, USA.
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    19
 */
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    20
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    21
#include <config.h>
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    22
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    23
#include <glib.h>
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    24
#include <string.h>
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    25
#include <stdlib.h>
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    26
#include <loudmouth/loudmouth.h>
140
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    27
#ifdef G_OS_WIN32
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    28
#include <winsock2.h>
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    29
#endif
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    30
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    31
typedef struct {
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    32
    gchar *name;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    33
    gchar *passwd;
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    34
} UserInfo;
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    35
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    36
static void
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    37
free_user_info (UserInfo *info)
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    38
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    39
    g_free (info->name);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    40
    g_free (info->passwd);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    41
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    42
    g_free (info);
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    43
}
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    44
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    45
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    46
static void
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    47
authentication_cb (LmConnection *connection, gboolean result, gpointer ud)
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    48
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    49
    g_print ("Auth: %d\n", result);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    50
    free_user_info ((UserInfo *) ud);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    51
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    52
    if (result == TRUE) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    53
        LmMessage *m;
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    54
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    55
        m = lm_message_new_with_sub_type (NULL,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    56
                                          LM_MESSAGE_TYPE_PRESENCE,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    57
                                          LM_MESSAGE_SUB_TYPE_AVAILABLE);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    58
        g_print (":: %s\n", lm_message_node_to_string (m->node));
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    59
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    60
        lm_connection_send (connection, m, NULL);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    61
        lm_message_unref (m);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    62
    }
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    63
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    64
}
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    65
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    66
static void
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    67
connection_open_cb (LmConnection *connection, gboolean result, UserInfo *info)
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    68
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    69
    g_print ("Connected callback\n");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    70
    lm_connection_authenticate (connection,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    71
                                info->name, info->passwd, "TestLM",
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    72
                                authentication_cb, info, FALSE,  NULL);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    73
    g_print ("Sent auth message\n");
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    74
}
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    75
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    76
static LmHandlerResult
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    77
handle_messages (LmMessageHandler *handler,
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    78
                 LmConnection     *connection,
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    79
                 LmMessage        *m,
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    80
                 gpointer          user_data)
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    81
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    82
    g_print ("Incoming message from: %s\n",
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    83
             lm_message_node_get_attribute (m->node, "from"));
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    84
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    85
    return LM_HANDLER_RESULT_REMOVE_MESSAGE;
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    86
}
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    87
int
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    88
main (int argc, char **argv)
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    89
{
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    90
    GMainLoop        *main_loop;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    91
    LmConnection     *connection;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    92
    LmMessageHandler *handler;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    93
    gboolean          result;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    94
    UserInfo         *info;
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    95
    LmProxy          *proxy;
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    96
    guint             proxy_port;
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    97
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    98
    if (argc < 6) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
    99
        g_print ("Usage: %s <server> <username> <password> <proxyserver> <proxyport>\n", argv[0]);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   100
        return 1;
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   101
    }
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   102
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   103
    connection = lm_connection_new (argv[1]);
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   104
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   105
    proxy = lm_proxy_new (LM_PROXY_TYPE_HTTP);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   106
    lm_proxy_set_server (proxy, argv[4]);
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   107
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   108
    proxy_port = strtol (argv[5], (char **) NULL, 10);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   109
    lm_proxy_set_port (proxy, proxy_port);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   110
    lm_connection_set_proxy (connection, proxy);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   111
    lm_proxy_unref (proxy);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   112
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   113
    handler = lm_message_handler_new (handle_messages, NULL, NULL);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   114
    lm_connection_register_message_handler (connection, handler,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   115
                                            LM_MESSAGE_TYPE_MESSAGE,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   116
                                            LM_HANDLER_PRIORITY_NORMAL);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   117
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   118
    lm_message_handler_unref (handler);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   119
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   120
    info = g_new0 (UserInfo, 1);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   121
    info->name = g_strdup (argv[2]);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   122
    info->passwd = g_strdup (argv[3]);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   123
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   124
    result = lm_connection_open (connection,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   125
                                 (LmResultFunction) connection_open_cb,
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   126
                                 info, NULL, NULL);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   127
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   128
    if (!result) {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   129
        g_print ("Opening connection failed: %d\n", result);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   130
    } else {
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   131
        g_print ("Returned from the connection_open\n");
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   132
    }
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   133
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   134
    main_loop = g_main_loop_new (NULL, FALSE);
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   135
    g_main_loop_run (main_loop);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   136
518
cdd6a0c5b439 Went over all of the files and made sure they all were indented the same way.
Mikael Hallendal <micke@imendio.com>
parents: 517
diff changeset
   137
    return 0;
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   138
}
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   139