~ubuntu-branches/ubuntu/raring/grilo/raring-proposed

« back to all changes in this revision

Viewing changes to libs/net/grl-net-mock.c

  • Committer: Package Import Robot
  • Author(s): Alberto Garcia
  • Date: 2012-11-12 18:12:56 UTC
  • mfrom: (1.1.7)
  • Revision ID: package-import@ubuntu.com-20121112181256-2ee4rg7dbkh03a65
Tags: 0.2.3-1
* New upstream version.
* debian/rules: new API, bump package version in dh_makeshlibs.
* debian/rules: configure with --disable-debug, otherwise it's enabled
  by default.
* Drop all lintian-overrides files, there are no longer hardening
  warnings.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2012 Openismus GmbH
 
3
 *
 
4
 * Authors: Jens Georg <jensg@openismus.com>
 
5
 *          Mathias Hasselmann <mathias@openismus.com>
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public License
 
9
 * as published by the Free Software Foundation; version 2.1 of
 
10
 * the License, or (at your option) any later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful, but
 
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 
20
 * 02110-1301 USA
 
21
 *
 
22
 */
 
23
 
 
24
#ifdef HAVE_CONFIG_H
 
25
#include "config.h"
 
26
#endif
 
27
 
 
28
#include <glib/gstdio.h>
 
29
#include <gio/gio.h>
 
30
#include <libsoup/soup.h>
 
31
#include <string.h>
 
32
 
 
33
#define _GRILO_H_INSIDE_
 
34
#include <grl-log.h>
 
35
 
 
36
#include "grl-net-mock-private.h"
 
37
#include "grl-net-private.h"
 
38
 
 
39
static GKeyFile *config = NULL;
 
40
static GRegex *ignored_parameters = NULL;
 
41
static char *base_path = NULL;
 
42
static gboolean enable_mocking = FALSE;
 
43
 
 
44
gboolean
 
45
is_mocked (void)
 
46
{
 
47
  return enable_mocking;
 
48
}
 
49
 
 
50
void
 
51
get_url_mocked (GrlNetWc *self,
 
52
                const char *url,
 
53
                GHashTable *headers,
 
54
                GAsyncResult *result,
 
55
                GCancellable *cancellable)
 
56
{
 
57
  char *data_file, *full_path;
 
58
  GError *error = NULL;
 
59
  GStatBuf stat_buf;
 
60
  char *new_url;
 
61
 
 
62
  if (ignored_parameters) {
 
63
    SoupURI *uri = soup_uri_new (url);
 
64
    char *new_query = g_regex_replace (ignored_parameters,
 
65
                                       soup_uri_get_query (uri), -1, 0,
 
66
                                       "", 0, NULL);
 
67
    soup_uri_set_query (uri, *new_query ? new_query : NULL);
 
68
    new_url = soup_uri_to_string (uri, FALSE);
 
69
    soup_uri_free (uri);
 
70
  } else {
 
71
    new_url = g_strdup (url);
 
72
  }
 
73
 
 
74
  if (!config) {
 
75
    g_simple_async_result_set_error (G_SIMPLE_ASYNC_RESULT (result),
 
76
                                     GRL_NET_WC_ERROR,
 
77
                                     GRL_NET_WC_ERROR_NETWORK_ERROR,
 
78
                                     "%s",
 
79
                                     "No mock definition found");
 
80
    g_simple_async_result_complete_in_idle (G_SIMPLE_ASYNC_RESULT (result));
 
81
    return;
 
82
  }
 
83
 
 
84
  data_file = g_key_file_get_value (config, new_url, "data", &error);
 
85
  if (error) {
 
86
    g_simple_async_result_set_error (G_SIMPLE_ASYNC_RESULT (result),
 
87
                                     GRL_NET_WC_ERROR,
 
88
                                     GRL_NET_WC_ERROR_NOT_FOUND,
 
89
                                     "Could not find mock content: %s",
 
90
                                     error->message);
 
91
    g_error_free (error);
 
92
    g_simple_async_result_complete_in_idle (G_SIMPLE_ASYNC_RESULT (result));
 
93
    return;
 
94
  }
 
95
  if (data_file[0] != '/') {
 
96
    full_path = g_build_filename (base_path, data_file, NULL);
 
97
  } else {
 
98
    full_path = data_file;
 
99
    data_file = NULL;
 
100
  }
 
101
 
 
102
  if (g_stat (full_path, &stat_buf) < 0) {
 
103
    g_simple_async_result_set_error (G_SIMPLE_ASYNC_RESULT (result),
 
104
                                     GRL_NET_WC_ERROR,
 
105
                                     GRL_NET_WC_ERROR_NOT_FOUND,
 
106
                                     "%s",
 
107
                                     "Could not access mock content");
 
108
    g_simple_async_result_complete_in_idle (G_SIMPLE_ASYNC_RESULT (result));
 
109
    if (data_file)
 
110
      g_free (data_file);
 
111
    if (full_path)
 
112
      g_free (full_path);
 
113
    return;
 
114
  }
 
115
  if (data_file)
 
116
    g_free (data_file);
 
117
  if (full_path)
 
118
    g_free (full_path);
 
119
 
 
120
  g_simple_async_result_set_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (result),
 
121
                                             new_url,
 
122
                                             NULL);
 
123
  g_simple_async_result_complete_in_idle (G_SIMPLE_ASYNC_RESULT (result));
 
124
}
 
125
 
 
126
void
 
127
get_content_mocked (GrlNetWc *self,
 
128
                    void *op,
 
129
                    gchar **content,
 
130
                    gsize *length)
 
131
{
 
132
  char *url = (char *) op;
 
133
  char *data_file = NULL, *full_path = NULL;
 
134
  GError *error = NULL;
 
135
 
 
136
  data_file = g_key_file_get_value (config, url, "data", NULL);
 
137
  if (data_file[0] != '/') {
 
138
    full_path = g_build_filename (base_path, data_file, NULL);
 
139
  } else {
 
140
    full_path = data_file;
 
141
    data_file = NULL;
 
142
  }
 
143
  g_file_get_contents (full_path, content, length, &error);
 
144
 
 
145
  if (data_file)
 
146
    g_free (data_file);
 
147
 
 
148
  if (full_path)
 
149
    g_free (full_path);
 
150
}
 
151
 
 
152
void init_mock_requester (GrlNetWc *self)
 
153
{
 
154
  char *config_filename = g_strdup (g_getenv (GRL_NET_MOCKED_VAR));
 
155
  enable_mocking = FALSE;
 
156
 
 
157
  if (config_filename == NULL) {
 
158
    return;
 
159
  }
 
160
 
 
161
  /* Read configuration file. */
 
162
  GError *error = NULL;
 
163
  config = g_key_file_new ();
 
164
 
 
165
  GRL_DEBUG ("Loading mock responses from \"%s\"", config_filename);
 
166
  g_key_file_load_from_file (config, config_filename, G_KEY_FILE_NONE, &error);
 
167
 
 
168
  int version = 0;
 
169
 
 
170
  if (error) {
 
171
    GRL_WARNING ("Failed to load mock file \"%s\": %s",
 
172
                 config_filename, error->message);
 
173
    g_clear_error (&error);
 
174
  } else {
 
175
    /* Check if we managed to load a file */
 
176
    version = g_key_file_get_integer (config, "default", "version", &error);
 
177
 
 
178
    if (error || version < GRL_NET_MOCK_VERSION) {
 
179
      GRL_WARNING ("Unsupported mock version.");
 
180
      g_clear_error (&error);
 
181
    } else {
 
182
      enable_mocking = TRUE;
 
183
    }
 
184
  }
 
185
 
 
186
  if (!enable_mocking) {
 
187
    g_free (config_filename);
 
188
    g_key_file_unref (config);
 
189
    config = NULL;
 
190
    return;
 
191
  }
 
192
 
 
193
  char **parameter_names = g_key_file_get_string_list (config, "default",
 
194
                                                       "ignored-parameters",
 
195
                                                       NULL, NULL);
 
196
 
 
197
  /* Build regular expressions for ignored query parameters. */
 
198
  if (parameter_names) {
 
199
    GString *pattern = g_string_new ("(?:^|\\&)");
 
200
 
 
201
    if (parameter_names[0] && strcmp(parameter_names[0], "*") == 0) {
 
202
      g_string_append (pattern, "[^=&]+");
 
203
    } else {
 
204
      g_string_append (pattern, "(?:");
 
205
 
 
206
      for (int i = 0; parameter_names[i]; ++i) {
 
207
        if (i)
 
208
          g_string_append (pattern, "|");
 
209
 
 
210
        char *escaped = g_regex_escape_string (parameter_names[i], -1);
 
211
        g_string_append (pattern, escaped);
 
212
        g_free (escaped);
 
213
      }
 
214
 
 
215
      g_string_append (pattern, ")(?:=[^&]*)?");
 
216
    }
 
217
 
 
218
    ignored_parameters = g_regex_new (pattern->str, G_REGEX_OPTIMIZE, 0, &error);
 
219
 
 
220
    if (error) {
 
221
      GRL_WARNING ("Failed to compile regular expression "
 
222
                   "for ignored query parameters: %s", error->message);
 
223
      g_clear_error (&error);
 
224
    }
 
225
  }
 
226
 
 
227
  /* Find base path for mock data. */
 
228
  GFile *file = g_file_new_for_commandline_arg (config_filename);
 
229
  GFile *parent = g_file_get_parent (file);
 
230
 
 
231
  base_path = g_file_get_path (parent);
 
232
 
 
233
  g_object_unref (parent);
 
234
  g_object_unref (file);
 
235
  g_free (config_filename);
 
236
}
 
237
 
 
238
void finalize_mock_requester (GrlNetWc *self)
 
239
{
 
240
  if (config) {
 
241
    g_key_file_unref (config);
 
242
  }
 
243
 
 
244
  if (base_path) {
 
245
    g_free (base_path);
 
246
  }
 
247
 
 
248
  if (ignored_parameters) {
 
249
    g_regex_unref (ignored_parameters);
 
250
  }
 
251
}
 
252
 
 
253
void free_mock_op_res (void *op)
 
254
{
 
255
  g_free (op);
 
256
}