1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Copyright (C) 2009 Kouhei Sutou <kou@clear-code.com>
4 *
5 * This library is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 #include "soupcutter/soupcut-server.h"
21 #include <cutter.h>
22
23 static void
cb_destroy(gpointer data)24 cb_destroy (gpointer data)
25 {
26 SoupServer *server = data;
27
28 soup_server_quit(server);
29 g_object_unref(server);
30 }
31
32 SoupServer *
soupcut_server_take(SoupServer * server)33 soupcut_server_take (SoupServer *server)
34 {
35 cut_take(server, cb_destroy);
36
37 return server;
38 }
39
40 SoupServer *
soupcut_server_take_new(GMainContext * context)41 soupcut_server_take_new (GMainContext *context)
42 {
43 SoupServer *server;
44 SoupAddress *address;
45
46 address = soup_address_new("localhost", SOUP_ADDRESS_ANY_PORT);
47 soup_address_resolve_sync(address, NULL);
48 server = soup_server_new(SOUP_SERVER_INTERFACE, address,
49 SOUP_SERVER_ASYNC_CONTEXT, context,
50 NULL);
51 g_object_unref(address);
52
53 return soupcut_server_take(server);
54 }
55
56 const gchar *
soupcut_server_build_uri(SoupServer * server,const gchar * path)57 soupcut_server_build_uri(SoupServer *server,
58 const gchar *path)
59 {
60 SoupAddress *address;
61 SoupURI *uri;
62 const gchar *uri_string;
63
64 g_object_get(server,
65 SOUP_SERVER_INTERFACE, &address,
66 NULL);
67
68 uri = soup_uri_new(NULL);
69 soup_uri_set_scheme(uri, SOUP_URI_SCHEME_HTTP);
70 soup_uri_set_host(uri, "localhost");
71 soup_uri_set_port(uri, soup_address_get_port(address));
72 soup_uri_set_path(uri, path);
73
74 uri_string = cut_take_string(soup_uri_to_string(uri, FALSE));
75 soup_uri_free(uri);
76
77 return uri_string;
78 }
79
80 /*
81 vi:ts=4:nowrap:ai:expandtab:sw=4
82 */
83