File: simple-publisher.c

package info (click to toggle)
libepc 0.4.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 3,420 kB
  • sloc: ansic: 8,574; sh: 4,257; makefile: 253
file content (243 lines) | stat: -rw-r--r-- 7,183 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* This program demonstrates and tests publishing of values.
 *
 * Usage: test-publisher [KEY=VALUE [KEY=VALUE...]]
 *
 * The arguments have the form "key=value". When a key is prefix with
 * the string "file:" the file of the name passed as value is published.
 *
 * When no arguments are passed some default values are published,
 * including the contents of this file by the key "source-code".
 *
 * To access the default resource called "sensitive"
 * use your login name as user name and the word "secret" as password.
 *
 * This example (simple-publisher.c) is in the public domain.
 */
#include <libepc/publisher.h>

#include <glib/gi18n.h>
#include <locale.h>
#include <string.h>

static EpcProtocol protocol = EPC_PROTOCOL_HTTPS;

static EpcContents*
timestamp_handler_date (EpcPublisher *publisher G_GNUC_UNUSED,
                   const gchar  *key G_GNUC_UNUSED,
                   gpointer      data)
{
  time_t now = time (NULL);
  struct tm *tm = localtime (&now);
  gsize length = 60;
  gchar *buffer;

  /* Create custom content */

  buffer = g_malloc (length);

  // Ignore the gcc warning:
  // error: ‘%x’ yields only last 2 digits of year in some locales [-Werror=format-y2k]
  // because there doesn't seem to be any way to show 4 digits in the correct format for the locale.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-y2k"
  length = strftime (buffer, length, "%x", tm);
#pragma GCC diagnostic pop

  return epc_contents_new ("text/plain", buffer, length, g_free);
}

static EpcContents*
timestamp_handler_time (EpcPublisher *publisher G_GNUC_UNUSED,
                   const gchar  *key G_GNUC_UNUSED,
                   gpointer      data)
{
  time_t now = time (NULL);
  struct tm *tm = localtime (&now);
  gsize length = 60;
  gchar *buffer;

  /* Create custom content */

  buffer = g_malloc (length);
  length = strftime (buffer, length, "%X", tm);

  return epc_contents_new ("text/plain", buffer, length, g_free);
}

static EpcContents*
timestamp_handler_date_time (EpcPublisher *publisher G_GNUC_UNUSED,
                   const gchar  *key G_GNUC_UNUSED,
                   gpointer      data)
{
  time_t now = time (NULL);
  struct tm *tm = localtime (&now);
  gsize length = 60;
  gchar *buffer;

  /* Create custom content */

  buffer = g_malloc (length);

  // Ignore the gcc warning:
  // error: ‘%x’ yields only last 2 digits of year in some locales [-Werror=format-y2k]
  // because there doesn't seem to be any way to show 4 digits in the correct format for the locale.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-y2k"
  length = strftime (buffer, length, "%x %X", tm);
#pragma GCC diagnostic pop

  return epc_contents_new ("text/plain", buffer, length, g_free);
}

static gboolean
authorization_handler (EpcAuthContext *context,
                       const gchar    *user_name,
                       gpointer        user_data)
{
  const gchar *password;

  /* Dump the supplied password when plain-text authentication was choosen.
   * See: EPC_AUTH_PASSWORD_TEXT_NEEDED */

  if (NULL != (password = epc_auth_context_get_password (context)))
    g_print ("%s: password='%s'\n", G_STRLOC, password);

  /* Check if he password supplied matches @user_data */

  return
    NULL != user_name &&
    g_str_equal (user_name, g_get_user_name ()) &&
    epc_auth_context_check_password (context, user_data);
}

static gboolean
parse_protocol (const gchar *option G_GNUC_UNUSED,
                const gchar *text,
                gpointer     data G_GNUC_UNUSED,
                GError     **error)
{
  /* Parse @text */

  protocol = epc_protocol_from_name (text, EPC_PROTOCOL_UNKNOWN);

  if (EPC_PROTOCOL_UNKNOWN != protocol)
    return TRUE;

  /* Report parsing error */

  g_set_error (error,
               G_OPTION_ERROR_FAILED,
               G_OPTION_ERROR_BAD_VALUE,
               _("Invalid transport protocol: '%s'"),
               text);

  return FALSE;
}

int
main (int   argc,
      char *argv[])
{
  gboolean basic_auth = FALSE;
  EpcPublisher *publisher;
  GOptionContext *options;
  GError *error = NULL;

  /* Declare command line options. */

  GOptionEntry entries[] =
    {
      { "protocol", 'p', 0, G_OPTION_ARG_CALLBACK, parse_protocol, N_("Transport protocol"), N_("PROTOCOL") },
      { "basic-auth", 'b', 0, G_OPTION_ARG_NONE, &basic_auth, N_("Use plain text authentication"), NULL },
      { NULL, 0, 0, 0, NULL, NULL, NULL }
    };

  setlocale (LC_ALL, "");

  /* Parse command line options. */

  options = g_option_context_new (NULL);
  g_option_context_add_main_entries (options, entries, NULL);

  if (!g_option_context_parse (options, &argc, &argv, &error))
    {
      g_print ("%s\n", error->message);
      g_error_free (error);
      return 2;
    }

  g_option_context_free (options);

  /* Create a new publisher. */

  publisher = epc_publisher_new ("Easy Publisher Test",
                                 "test-publisher", NULL);

  epc_publisher_set_protocol (publisher, protocol);

  if (basic_auth)
    epc_publisher_set_auth_flags (publisher, EPC_AUTH_PASSWORD_TEXT_NEEDED);

  /* Create dynamic bookmark for the builtin HTTP server */

  epc_publisher_add_bookmark (publisher, NULL, NULL);

  if (1 == argc)
    {
      /* Publish some default values,
       * as no arguments are passed on the command line. */

      epc_publisher_add (publisher, "test", "value", -1);
      epc_publisher_add_handler (publisher, "date", timestamp_handler_date, NULL, NULL);
      epc_publisher_add_handler (publisher, "time", timestamp_handler_time, NULL, NULL);
      epc_publisher_add_handler (publisher, "date-time", timestamp_handler_date_time, NULL, NULL);

      epc_publisher_add (publisher, "sensitive",
                         "This value is top secret.", -1);
      epc_publisher_set_auth_handler (publisher, "sensitive",
                                      authorization_handler,
                                      "secret" /* user_data */, NULL);

      epc_publisher_add_file (publisher, "source-code", __FILE__);

      /* Create dynamic bookmark for this example */

      epc_publisher_add_bookmark (publisher, "source-code",
                                  "Source code of a simple publisher");
    }
  else
    {
      /* Publish the values passed on the command line. Each argument has the
       * form "key=value". When a key is prefix with the string "file:" the
       * file of the name passed as value is published. */

      int i;

      for (i = 1; i < argc; ++i)
        {
          char **pair = g_strsplit (argv[i], "=", 2);
          const char *key = pair[0], *value = pair[1];

          if (g_str_has_prefix (key, "file:"))
            epc_publisher_add_file (publisher, key + 5, value);
          else
            epc_publisher_add (publisher, key, value, -1);

          g_strfreev (pair);
        }
    }

  /* Actually run the publisher. */

  if (!epc_publisher_run (publisher, &error))
    {
      g_print ("Cannot start publisher: %s.\n",
               error ? error->message : "Unknown error");
      g_error_free (error);
      return 2;
    }

  g_object_unref (publisher);

  return 0;
}