1/*
2 * Copyright (C) 2011 Igalia S.L.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20#include "config.h"
21#include "WebKitTestServer.h"
22
23#include "TestMain.h"
24#include <wtf/gobject/GOwnPtr.h>
25
26WebKitTestServer::WebKitTestServer(ServerType type)
27{
28    GOwnPtr<char> sslCertificateFile;
29    GOwnPtr<char> sslKeyFile;
30    if (type == ServerHTTPS) {
31        CString resourcesDir = Test::getResourcesDir();
32        sslCertificateFile.set(g_build_filename(resourcesDir.data(), "test-cert.pem", NULL));
33        sslKeyFile.set(g_build_filename(resourcesDir.data(), "test-key.pem", NULL));
34    }
35
36    GRefPtr<SoupAddress> address = adoptGRef(soup_address_new("127.0.0.1", SOUP_ADDRESS_ANY_PORT));
37    soup_address_resolve_sync(address.get(), 0);
38
39    m_soupServer = adoptGRef(soup_server_new(SOUP_SERVER_INTERFACE, address.get(),
40                                             SOUP_SERVER_SSL_CERT_FILE, sslCertificateFile.get(),
41                                             SOUP_SERVER_SSL_KEY_FILE, sslKeyFile.get(), NULL));
42    m_baseURI = type == ServerHTTPS ? soup_uri_new("https://127.0.0.1/") : soup_uri_new("http://127.0.0.1/");
43    soup_uri_set_port(m_baseURI, soup_server_get_port(m_soupServer.get()));
44}
45
46WebKitTestServer::~WebKitTestServer()
47{
48    soup_uri_free(m_baseURI);
49}
50
51void WebKitTestServer::run(SoupServerCallback serverCallback)
52{
53    soup_server_run_async(m_soupServer.get());
54    soup_server_add_handler(m_soupServer.get(), 0, serverCallback, 0, 0);
55}
56
57CString WebKitTestServer::getURIForPath(const char* path)
58{
59    SoupURI* uri = soup_uri_new_with_base(m_baseURI, path);
60    GOwnPtr<gchar> uriString(soup_uri_to_string(uri, FALSE));
61    soup_uri_free(uri);
62    return uriString.get();
63}
64
65