examples/test-http-proxy.c
author Mikael Berthe <mikael@lilotux.net>
Sat, 10 Jul 2021 09:37:34 +0200
changeset 743 9098ee1972df
parent 690 7ccf2113ec5f
permissions -rw-r--r--
Merge pull request #43 from ryandesign/patch-1 Fix typo: GnuTSL -> GnuTLS committer: GitHub <noreply@github.com>
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
690
7ccf2113ec5f Update the postal address of the FSF
Frank Zschockelt <lm@freakysoft.de>
parents: 664
diff changeset
    16
 * License along with this program; if not, see <https://www.gnu.org/licenses>
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    17
 */
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    18
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    19
#include <config.h>
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 <glib.h>
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    22
#include <string.h>
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    23
#include <stdlib.h>
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    24
#include <loudmouth/loudmouth.h>
140
103227122f45 2006-04-19 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents: 99
diff changeset
    25
#ifdef G_OS_WIN32
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    26
#include <winsock2.h>
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    27
#endif
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    28
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    29
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
    30
    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
    31
    gchar *passwd;
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    32
} UserInfo;
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    33
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    34
static void
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    35
free_user_info (UserInfo *info)
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    36
{
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
    37
    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
    38
    g_free (info->passwd);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    39
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
    40
    g_free (info);
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    41
}
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    42
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    43
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    44
static void
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    45
authentication_cb (LmConnection *connection, gboolean result, gpointer ud)
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    46
{
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
    47
    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
    48
    free_user_info ((UserInfo *) ud);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    49
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
    50
    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
    51
        LmMessage *m;
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    52
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
    53
        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
    54
                                          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
    55
                                          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
    56
        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
    57
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
    58
        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
    59
        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
    60
    }
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    61
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    62
}
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    63
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    64
static void
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    65
connection_open_cb (LmConnection *connection, gboolean result, UserInfo *info)
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    66
{
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
    67
    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
    68
    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
    69
                                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
    70
                                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
    71
    g_print ("Sent auth message\n");
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    72
}
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    73
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    74
static LmHandlerResult
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    75
handle_messages (LmMessageHandler *handler,
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    76
                 LmConnection     *connection,
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    77
                 LmMessage        *m,
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    78
                 gpointer          user_data)
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    79
{
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
    80
    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
    81
             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
    82
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
    83
    return LM_HANDLER_RESULT_REMOVE_MESSAGE;
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    84
}
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    85
int
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    86
main (int argc, char **argv)
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
    87
{
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
    88
    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
    89
    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
    90
    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
    91
    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
    92
    UserInfo         *info;
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    93
    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
    94
    guint             proxy_port;
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
    95
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
    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
    97
        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
    98
        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
    99
    }
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   100
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
   101
    connection = lm_connection_new (argv[1]);
83
9c89653e79a1 added files
hallski <hallski>
parents:
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
    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
   104
    lm_proxy_set_server (proxy, argv[4]);
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   105
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
   106
    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
   107
    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
   108
    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
   109
    lm_proxy_unref (proxy);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   110
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
   111
    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
   112
    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
   113
                                            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
   114
                                            LM_HANDLER_PRIORITY_NORMAL);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   115
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
   116
    lm_message_handler_unref (handler);
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
    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
   119
    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
   120
    info->passwd = g_strdup (argv[3]);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   121
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
   122
    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
   123
                                 (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
   124
                                 info, NULL, NULL);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   125
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
   126
    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
   127
        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
   128
    } 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
   129
        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
   130
    }
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   131
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
   132
    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
   133
    g_main_loop_run (main_loop);
664
f57b1b61e1fe Remove trailing whitespace and mixed indenting
Till Maas <opensource@till.name>
parents: 518
diff changeset
   134
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
   135
    return 0;
83
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   136
}
9c89653e79a1 added files
hallski <hallski>
parents:
diff changeset
   137