| 1 | /* |
| 2 | * Copyright (C) 2017 Igalia S.L. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "HTTPServer.h" |
| 28 | |
| 29 | #include <libsoup/soup.h> |
| 30 | #include <wtf/Function.h> |
| 31 | #include <wtf/glib/GUniquePtr.h> |
| 32 | |
| 33 | namespace WebDriver { |
| 34 | |
| 35 | static bool soupServerListen(SoupServer* server, const Optional<String>& host, unsigned port, GError** error) |
| 36 | { |
| 37 | static const auto options = static_cast<SoupServerListenOptions>(0); |
| 38 | if (!host || host.value() == "local" ) |
| 39 | return soup_server_listen_local(server, port, options, error); |
| 40 | |
| 41 | if (host.value() == "all" ) |
| 42 | return soup_server_listen_all(server, port, options, error); |
| 43 | |
| 44 | GRefPtr<GSocketAddress> address = adoptGRef(g_inet_socket_address_new_from_string(host.value().utf8().data(), port)); |
| 45 | if (!address) { |
| 46 | g_set_error(error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT, "Invalid host IP address '%s'" , host.value().utf8().data()); |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | return soup_server_listen(server, address.get(), options, error); |
| 51 | } |
| 52 | |
| 53 | bool HTTPServer::listen(const Optional<String>& host, unsigned port) |
| 54 | { |
| 55 | m_soupServer = adoptGRef(soup_server_new(SOUP_SERVER_SERVER_HEADER, "WebKitWebDriver" , nullptr)); |
| 56 | GUniqueOutPtr<GError> error; |
| 57 | if (!soupServerListen(m_soupServer.get(), host, port, &error.outPtr())) { |
| 58 | WTFLogAlways("Failed to start HTTP server at port %u: %s" , port, error->message); |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | soup_server_add_handler(m_soupServer.get(), nullptr, [](SoupServer* server, SoupMessage* message, const char* path, GHashTable*, SoupClientContext*, gpointer userData) { |
| 63 | auto* httpServer = static_cast<HTTPServer*>(userData); |
| 64 | GRefPtr<SoupMessage> protectedMessage = message; |
| 65 | soup_server_pause_message(server, message); |
| 66 | httpServer->m_requestHandler.handleRequest({ String::fromUTF8(message->method), String::fromUTF8(path), message->request_body->data, static_cast<size_t>(message->request_body->length) }, |
| 67 | [server, message = WTFMove(protectedMessage)](HTTPRequestHandler::Response&& response) { |
| 68 | soup_message_set_status(message.get(), response.statusCode); |
| 69 | if (!response.data.isNull()) { |
| 70 | // ยง6.3 Processing Model. |
| 71 | // https://w3c.github.io/webdriver/webdriver-spec.html#dfn-send-a-response |
| 72 | soup_message_headers_append(message->response_headers, "Content-Type" , response.contentType.utf8().data()); |
| 73 | soup_message_headers_append(message->response_headers, "Cache-Control" , "no-cache" ); |
| 74 | soup_message_body_append(message->response_body, SOUP_MEMORY_COPY, response.data.data(), response.data.length()); |
| 75 | } |
| 76 | soup_server_unpause_message(server, message.get()); |
| 77 | }); |
| 78 | }, this, nullptr); |
| 79 | |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | void HTTPServer::disconnect() |
| 84 | { |
| 85 | soup_server_disconnect(m_soupServer.get()); |
| 86 | m_soupServer = nullptr; |
| 87 | } |
| 88 | |
| 89 | } // namespace WebDriver |
| 90 | |