summaryrefslogtreecommitdiff
path: root/client/httpuri.c
blob: 0e65c7952753f32121545e0ef5bc2d96e0c92964 (plain)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2008 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Author: Christian Kellner <gicmo@gnome.org>
 */

#include <config.h>
#include <string.h>

#include <stdlib.h> /* atoi */

#include <gio/gio.h>
#include <gvfsurimapper.h>
#include <gvfsuriutils.h>

typedef struct _GVfsUriMapperHttp GVfsUriMapperHttp;
typedef struct _GVfsUriMapperHttpClass GVfsUriMapperHttpClass;

struct _GVfsUriMapperHttp
{
  GVfsUriMapper parent;
};

struct _GVfsUriMapperHttpClass
{
  GVfsUriMapperClass parent_class;
};

GType g_vfs_uri_mapper_http_get_type (void);
void  g_vfs_uri_mapper_http_register (GIOModule *module);

G_DEFINE_DYNAMIC_TYPE (GVfsUriMapperHttp, g_vfs_uri_mapper_http, G_VFS_TYPE_URI_MAPPER)

static void
g_vfs_uri_mapper_http_init (GVfsUriMapperHttp *vfs)
{
}

static const char * const *
http_get_handled_schemes (GVfsUriMapper *mapper)
{
  static const char *schemes[] = {
    "http",
    "https",
    "dav",
    "davs",
    NULL
  };
  return schemes;
}

static inline gboolean
port_is_default_port (int port, gboolean ssl)
{
  if (ssl)
    return port == 443;
  else
    return port == 80;
}

static GMountSpec *
http_from_uri (GVfsUriMapper  *mapper,
               const char     *uri_str,
               char          **path)
{
  GMountSpec *spec;
  gboolean ssl;
  GDecodedUri *uri;

  uri = g_vfs_decode_uri (uri_str);

  if (uri == NULL)
    return NULL;

  if (!g_ascii_strncasecmp (uri->scheme, "http", 4))
    {
      spec = g_mount_spec_new ("http");
      g_mount_spec_set (spec, "uri", uri_str);
    }
  else
    {
      spec = g_mount_spec_new ("dav");
      ssl = !g_ascii_strcasecmp (uri->scheme, "davs");
      g_mount_spec_set (spec, "ssl", ssl ? "true" : "false");

      if (uri->host && *uri->host)
        g_mount_spec_set (spec, "host", uri->host);

      if (uri->userinfo && *uri->userinfo)
        g_mount_spec_set (spec, "user", uri->userinfo);

      /* only set the port if it isn't the default port */
      if (uri->port != -1 && ! port_is_default_port (uri->port, ssl))
        {
          char *port = g_strdup_printf ("%d", uri->port);
          g_mount_spec_set (spec, "port", port);
          g_free (port);
        }
    }

  *path = uri->path;
  uri->path = NULL;
  g_vfs_decoded_uri_free (uri);

  return spec;
}

static GMountSpec *
http_get_mount_spec_for_path (GVfsUriMapper *mapper,
			      GMountSpec *spec,
                              const char *old_path,
			      const char *new_path)
{
  const char *type;

  type = g_mount_spec_get (spec, "type");

  if (strcmp (type, "http") == 0)
    {
      const char *uri_str;
      char *new_uri;
      GDecodedUri *uri;
      GMountSpec *new_spec;

      uri_str = g_mount_spec_get (spec, "uri");

      uri = g_vfs_decode_uri (uri_str);

      if (uri == NULL)
        return NULL;

      if (strcmp (uri->path, new_path) == 0)
        {
          g_vfs_decoded_uri_free (uri);
          return NULL;
        }

      g_free (uri->path);
      uri->path = g_strdup (new_path);

      g_free (uri->query);
      uri->query = NULL;

      g_free (uri->fragment);
      uri->fragment = NULL;

      new_spec = g_mount_spec_new ("http");

      new_uri = g_vfs_encode_uri (uri, TRUE);
      g_mount_spec_set (new_spec, "uri", new_uri);
      g_free (new_uri);

      g_vfs_decoded_uri_free (uri);

      return new_spec;
    }
  else
    return NULL;
}

static const char * const *
http_get_handled_mount_types (GVfsUriMapper *mapper)
{
  static const char *types[] = {
    "http",
    "dav", 
    NULL
  };
  return types;
}

static char *
http_to_uri (GVfsUriMapper *mapper,
             GMountSpec    *spec,
             const char    *path,
             gboolean       allow_utf8)
{
  char       *res;
  const char *type;
  const char *host;
  const char *user;
  const char *port;
  const char *ssl;

  type = g_mount_spec_get (spec, "type");

  if (strcmp (type, "http") == 0)
    {
      res = g_strdup (g_mount_spec_get (spec, "uri"));
    }
  else
    {
      GDecodedUri *decoded_uri;
      int          port_num;

      decoded_uri = g_new0 (GDecodedUri, 1);

      ssl  = g_mount_spec_get (spec, "ssl");
      host = g_mount_spec_get (spec, "host");
      user = g_mount_spec_get (spec, "user");
      port = g_mount_spec_get (spec, "port");

      if (ssl && strcmp (ssl, "true") == 0)
          decoded_uri->scheme = g_strdup ("davs");
      else
          decoded_uri->scheme = g_strdup ("dav");

      decoded_uri->host = g_strdup (host);
      decoded_uri->userinfo = g_strdup (user);

      if (port && (port_num = atoi (port)))
          decoded_uri->port = port_num;
      else
          decoded_uri->port = -1;

      decoded_uri->path = g_strdup (path);

      res = g_vfs_encode_uri (decoded_uri, allow_utf8);
      g_vfs_decoded_uri_free (decoded_uri);
    }

  return res;
}

static const char *
http_to_uri_scheme (GVfsUriMapper *mapper,
                    GMountSpec    *spec)
{
  const gchar *ssl;
  const gchar *type;
  gboolean     is_dav;
  gboolean     is_ssl;

  ssl = g_mount_spec_get (spec, "ssl");
  type = g_mount_spec_get (spec, "type");

  if (strcmp (type, "dav") == 0)
     is_dav = TRUE;
  else if (strcmp (type, "http") == 0)
     is_dav = FALSE;
  else
     return NULL;

  is_ssl =
    ssl != NULL &&
    strcmp (ssl, "true") == 0;

  if (is_dav && is_ssl)
    return "davs";
  else if (is_dav && !is_ssl)
    return "dav";
  else if (!is_dav && is_ssl)
    return "https";
  else
    return "http";
}

static void
g_vfs_uri_mapper_http_class_finalize (GVfsUriMapperHttpClass *klass)
{
}

static void
g_vfs_uri_mapper_http_class_init (GVfsUriMapperHttpClass *class)
{
  GVfsUriMapperClass *mapper_class;

  mapper_class = G_VFS_URI_MAPPER_CLASS (class);
  mapper_class->get_handled_schemes     = http_get_handled_schemes;
  mapper_class->from_uri                = http_from_uri;
  mapper_class->get_mount_spec_for_path = http_get_mount_spec_for_path;
  mapper_class->get_handled_mount_types = http_get_handled_mount_types;
  mapper_class->to_uri                  = http_to_uri;
  mapper_class->to_uri_scheme           = http_to_uri_scheme;
}

void
g_vfs_uri_mapper_http_register (GIOModule *module)
{
  g_vfs_uri_mapper_http_register_type (G_TYPE_MODULE (module));
}