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/Threading.h> |
25 | #include <wtf/glib/GUniquePtr.h> |
26 | |
27 | WebKitTestServer::WebKitTestServer(ServerOptions options) |
28 | { |
29 | if (options & ServerRunInThread) { |
30 | WTF::initializeThreading(); |
31 | m_queue = WorkQueue::create("WebKitTestServer" ); |
32 | } |
33 | |
34 | GUniquePtr<char> sslCertificateFile; |
35 | GUniquePtr<char> sslKeyFile; |
36 | if (options & ServerHTTPS) { |
37 | CString resourcesDir = Test::getResourcesDir(); |
38 | sslCertificateFile.reset(g_build_filename(resourcesDir.data(), "test-cert.pem" , NULL)); |
39 | sslKeyFile.reset(g_build_filename(resourcesDir.data(), "test-key.pem" , NULL)); |
40 | } |
41 | |
42 | GRefPtr<SoupAddress> address = adoptGRef(soup_address_new("127.0.0.1" , SOUP_ADDRESS_ANY_PORT)); |
43 | soup_address_resolve_sync(address.get(), 0); |
44 | |
45 | m_soupServer = adoptGRef(soup_server_new(SOUP_SERVER_INTERFACE, address.get(), |
46 | SOUP_SERVER_ASYNC_CONTEXT, m_queue ? m_queue->runLoop().mainContext() : nullptr, |
47 | SOUP_SERVER_SSL_CERT_FILE, sslCertificateFile.get(), |
48 | SOUP_SERVER_SSL_KEY_FILE, sslKeyFile.get(), nullptr)); |
49 | m_baseURI = options & ServerHTTPS ? soup_uri_new("https://127.0.0.1/" ) : soup_uri_new("http://127.0.0.1/" ); |
50 | soup_uri_set_port(m_baseURI, soup_server_get_port(m_soupServer.get())); |
51 | } |
52 | |
53 | WebKitTestServer::~WebKitTestServer() |
54 | { |
55 | soup_uri_free(m_baseURI); |
56 | #if SOUP_CHECK_VERSION(2, 50, 0) |
57 | if (m_baseWebSocketURI) |
58 | soup_uri_free(m_baseWebSocketURI); |
59 | #endif |
60 | } |
61 | |
62 | void WebKitTestServer::run(SoupServerCallback serverCallback) |
63 | { |
64 | if (m_queue) { |
65 | m_queue->dispatch([this, serverCallback] { |
66 | soup_server_run_async(m_soupServer.get()); |
67 | soup_server_add_handler(m_soupServer.get(), nullptr, serverCallback, nullptr, nullptr); |
68 | }); |
69 | } else { |
70 | soup_server_run_async(m_soupServer.get()); |
71 | soup_server_add_handler(m_soupServer.get(), nullptr, serverCallback, nullptr, nullptr); |
72 | } |
73 | } |
74 | |
75 | #if SOUP_CHECK_VERSION(2, 50, 0) |
76 | void WebKitTestServer::addWebSocketHandler(SoupServerWebsocketCallback callback, gpointer userData) |
77 | { |
78 | m_baseWebSocketURI = soup_uri_new_with_base(m_baseURI, "/websocket/" ); |
79 | m_baseWebSocketURI->scheme = m_baseWebSocketURI->scheme == SOUP_URI_SCHEME_HTTP ? SOUP_URI_SCHEME_WS : SOUP_URI_SCHEME_WSS; |
80 | |
81 | if (m_queue) { |
82 | m_queue->dispatch([this, callback, userData] { |
83 | soup_server_add_websocket_handler(m_soupServer.get(), "/websocket" , nullptr, nullptr, callback, userData, nullptr); |
84 | }); |
85 | } else |
86 | soup_server_add_websocket_handler(m_soupServer.get(), "/websocket" , nullptr, nullptr, callback, userData, nullptr); |
87 | } |
88 | |
89 | void WebKitTestServer::removeWebSocketHandler() |
90 | { |
91 | soup_uri_free(m_baseWebSocketURI); |
92 | m_baseWebSocketURI = nullptr; |
93 | |
94 | if (m_queue) { |
95 | m_queue->dispatch([this] { |
96 | soup_server_remove_handler(m_soupServer.get(), "/websocket" ); |
97 | }); |
98 | } else |
99 | soup_server_remove_handler(m_soupServer.get(), "/websocket" ); |
100 | } |
101 | |
102 | CString WebKitTestServer::getWebSocketURIForPath(const char* path) const |
103 | { |
104 | g_assert_nonnull(m_baseWebSocketURI); |
105 | g_assert_true(path && *path == '/'); |
106 | SoupURI* uri = soup_uri_new_with_base(m_baseWebSocketURI, path + 1); // Ignore the leading slash. |
107 | GUniquePtr<gchar> uriString(soup_uri_to_string(uri, FALSE)); |
108 | soup_uri_free(uri); |
109 | return uriString.get(); |
110 | } |
111 | #endif // SOUP_CHECK_VERSION(2, 50, 0) |
112 | |
113 | CString WebKitTestServer::getURIForPath(const char* path) const |
114 | { |
115 | SoupURI* uri = soup_uri_new_with_base(m_baseURI, path); |
116 | GUniquePtr<gchar> uriString(soup_uri_to_string(uri, FALSE)); |
117 | soup_uri_free(uri); |
118 | return uriString.get(); |
119 | } |
120 | |