1/*
2 * Copyright (C) 2011 Igalia S.L.
3 * Copyright (C) 2012 Intel Corporation
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (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 GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "config.h"
22#include "EWK2UnitTestServer.h"
23
24EWK2UnitTestServer::EWK2UnitTestServer(GTlsCertificate* tlsCert)
25{
26    SoupAddress* address = soup_address_new("127.0.0.1", SOUP_ADDRESS_ANY_PORT);
27    soup_address_resolve_sync(address, 0);
28
29    if (tlsCert) {
30        m_soupServer = soup_server_new(
31            SOUP_SERVER_TLS_CERTIFICATE, tlsCert,
32            SOUP_SERVER_INTERFACE, address, static_cast<char*>(0));
33
34        m_baseURL = soup_uri_new("https://127.0.0.1/");
35    } else {
36        m_soupServer = soup_server_new(SOUP_SERVER_INTERFACE, address, static_cast<char*>(0));
37        m_baseURL = soup_uri_new("http://127.0.0.1/");
38    }
39
40    soup_uri_set_port(m_baseURL, soup_server_get_port(m_soupServer));
41    g_object_unref(address);
42}
43
44EWK2UnitTestServer::~EWK2UnitTestServer()
45{
46    soup_uri_free(m_baseURL);
47    g_object_unref(m_soupServer);
48}
49
50void EWK2UnitTestServer::run(SoupServerCallback serverCallback)
51{
52    soup_server_run_async(m_soupServer);
53    soup_server_add_handler(m_soupServer, 0, serverCallback, 0, 0);
54}
55
56CString EWK2UnitTestServer::getURLForPath(const char* path) const
57{
58    SoupURI* soupURL = soup_uri_new_with_base(m_baseURL, path);
59    char* url = soup_uri_to_string(soupURL, false);
60    CString urlString = url;
61    free(url);
62    soup_uri_free(soupURL);
63
64    return urlString;
65}
66
67unsigned EWK2UnitTestServer::port() const
68{
69    return soup_server_get_port(m_soupServer);
70}
71