gboolean xr_client_open(xr_client_conn* conn, const char* uri, GError** err) { GError* local_err = NULL; g_return_val_if_fail(conn != NULL, FALSE); g_return_val_if_fail(uri != NULL, FALSE); g_return_val_if_fail(!conn->is_open, FALSE); g_return_val_if_fail(err == NULL || *err == NULL, FALSE); xr_trace(XR_DEBUG_CLIENT_TRACE, "(conn=%p, uri=%s)", conn, uri); // parse URI format: http://host:8080/RES g_free(conn->host); g_free(conn->resource); conn->host = NULL; conn->resource = NULL; if (!_parse_uri(uri, &conn->secure, &conn->host, &conn->resource)) { g_set_error(err, XR_CLIENT_ERROR, XR_CLIENT_ERROR_FAILED, "invalid URI format: %s", uri); return FALSE; } // enable/disable TLS if (conn->secure) { g_socket_client_set_tls(conn->client, TRUE); g_socket_client_set_tls_validation_flags(conn->client, G_TLS_CERTIFICATE_VALIDATE_ALL & ~G_TLS_CERTIFICATE_UNKNOWN_CA & ~G_TLS_CERTIFICATE_BAD_IDENTITY); } else { g_socket_client_set_tls(conn->client, FALSE); } conn->conn = g_socket_client_connect_to_host(conn->client, conn->host, 80, NULL, &local_err); if (local_err) { g_propagate_prefixed_error(err, local_err, "Connection failed: "); return FALSE; } xr_set_nodelay(g_socket_connection_get_socket(conn->conn)); conn->http = xr_http_new(G_IO_STREAM(conn->conn)); g_free(conn->session_id); conn->session_id = g_strdup_printf("%08x%08x%08x%08x", g_random_int(), g_random_int(), g_random_int(), g_random_int()); conn->is_open = 1; xr_client_set_http_header(conn, "X-SESSION-ID", conn->session_id); return TRUE; }
void idle_server_connection_set_tls(IdleServerConnection *conn, gboolean tls) { IdleServerConnectionPrivate *priv = IDLE_SERVER_CONNECTION_GET_PRIVATE(conn); g_socket_client_set_tls(priv->socket_client, tls); g_socket_client_set_tls_validation_flags(priv->socket_client, G_TLS_CERTIFICATE_VALIDATE_ALL & ~G_TLS_CERTIFICATE_UNKNOWN_CA & ~G_TLS_CERTIFICATE_BAD_IDENTITY & ~G_TLS_CERTIFICATE_EXPIRED); }
static gboolean do_connect(MegaHttpClient* http_client, GCancellable* cancellable, GError** err) { GError* local_err = NULL; g_return_val_if_fail(MEGA_IS_HTTP_CLIENT(http_client), FALSE); g_return_val_if_fail(err == NULL || *err == NULL, FALSE); MegaHttpClientPrivate* priv = http_client->priv; do_disconnect(http_client); // enable/disable TLS if (priv->https) { if (!g_tls_backend_supports_tls(g_tls_backend_get_default())) { g_set_error(err, MEGA_HTTP_CLIENT_ERROR, MEGA_HTTP_CLIENT_ERROR_OTHER, "TLS backend not found, please install glib-networking."); return FALSE; } g_socket_client_set_tls(priv->client, TRUE); } else { g_socket_client_set_tls(priv->client, FALSE); } priv->conn = g_socket_client_connect_to_host(priv->client, priv->host, priv->port, cancellable, &local_err); if (!priv->conn) { g_propagate_prefixed_error(err, local_err, "Connection failed: "); return FALSE; } GDataInputStream* data_stream = g_data_input_stream_new(g_io_stream_get_input_stream(G_IO_STREAM(http_client->priv->conn))); g_data_input_stream_set_newline_type(data_stream, G_DATA_STREAM_NEWLINE_TYPE_ANY); priv->istream = G_INPUT_STREAM(data_stream); priv->ostream = g_object_ref(g_io_stream_get_output_stream(G_IO_STREAM(http_client->priv->conn))); return TRUE; }
SocketStreamHandle::SocketStreamHandle(const KURL& url, SocketStreamHandleClient* client) : SocketStreamHandleBase(url, client) , m_readBuffer(0) { unsigned int port = url.hasPort() ? url.port() : (url.protocolIs("wss") ? 443 : 80); m_id = activateHandle(this); GRefPtr<GSocketClient> socketClient = adoptGRef(g_socket_client_new()); if (url.protocolIs("wss")) g_socket_client_set_tls(socketClient.get(), TRUE); g_socket_client_connect_to_host_async(socketClient.get(), url.host().utf8().data(), port, 0, reinterpret_cast<GAsyncReadyCallback>(connectedCallback), m_id); }
Ref<SocketStreamHandle> SocketStreamHandle::create(const URL& url, SocketStreamHandleClient& client, NetworkingContext&, SessionID) { Ref<SocketStreamHandle> socket = adoptRef(*new SocketStreamHandle(url, client)); unsigned port = url.hasPort() ? url.port() : (url.protocolIs("wss") ? 443 : 80); GRefPtr<GSocketClient> socketClient = adoptGRef(g_socket_client_new()); if (url.protocolIs("wss")) g_socket_client_set_tls(socketClient.get(), TRUE); Ref<SocketStreamHandle> protectedSocketStreamHandle = socket.copyRef(); g_socket_client_connect_to_host_async(socketClient.get(), url.host().utf8().data(), port, socket->m_cancellable.get(), reinterpret_cast<GAsyncReadyCallback>(connectedCallback), &protectedSocketStreamHandle.leakRef()); return socket; }
static void g_socket_client_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) { GSocketClient *client = G_SOCKET_CLIENT (object); switch (prop_id) { case PROP_FAMILY: g_socket_client_set_family (client, g_value_get_enum (value)); break; case PROP_TYPE: g_socket_client_set_socket_type (client, g_value_get_enum (value)); break; case PROP_PROTOCOL: g_socket_client_set_protocol (client, g_value_get_enum (value)); break; case PROP_LOCAL_ADDRESS: g_socket_client_set_local_address (client, g_value_get_object (value)); break; case PROP_TIMEOUT: g_socket_client_set_timeout (client, g_value_get_uint (value)); break; case PROP_ENABLE_PROXY: g_socket_client_set_enable_proxy (client, g_value_get_boolean (value)); break; case PROP_TLS: g_socket_client_set_tls (client, g_value_get_boolean (value)); break; case PROP_TLS_VALIDATION_FLAGS: g_socket_client_set_tls_validation_flags (client, g_value_get_flags (value)); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); } }
static void web_socket_client_constructed (GObject *object) { WebSocketClient *self = WEB_SOCKET_CLIENT (object); WebSocketConnection *conn = WEB_SOCKET_CONNECTION (object); GSocketClient *client; const gchar *url; guint16 default_port; gchar *scheme; G_OBJECT_CLASS (web_socket_client_parent_class)->constructed (object); if (web_socket_connection_get_io_stream (conn)) { /* Start handshake from the main context */ self->idle_start = g_idle_source_new (); g_source_set_priority (self->idle_start, G_PRIORITY_HIGH); g_source_set_callback (self->idle_start, (GSourceFunc)on_idle_do_handshake, self, NULL); g_source_attach (self->idle_start, _web_socket_connection_get_main_context (conn)); } else { client = g_socket_client_new (); self->cancellable = g_cancellable_new (); url = web_socket_connection_get_url (WEB_SOCKET_CONNECTION (self)); scheme = g_uri_parse_scheme (url); if (scheme && (g_str_equal (scheme, "wss") || g_str_equal (scheme, "https"))) { g_socket_client_set_tls (client, TRUE); default_port = 443; } else { default_port = 80; } g_free (scheme); g_socket_client_connect_to_uri_async (client, url, default_port, self->cancellable, on_connect_to_uri, g_object_ref (self)); g_object_unref (client); } }