Пример #1
0
static void
soup_socket_get_property (GObject *object, guint prop_id,
			  GValue *value, GParamSpec *pspec)
{
	SoupSocketPrivate *priv = SOUP_SOCKET_GET_PRIVATE (object);

	switch (prop_id) {
	case PROP_LOCAL_ADDRESS:
		g_value_set_object (value, soup_socket_get_local_address (SOUP_SOCKET (object)));
		break;
	case PROP_REMOTE_ADDRESS:
		g_value_set_object (value, soup_socket_get_remote_address (SOUP_SOCKET (object)));
		break;
	case PROP_NON_BLOCKING:
		g_value_set_boolean (value, priv->non_blocking);
		break;
	case PROP_IS_SERVER:
		g_value_set_boolean (value, priv->is_server);
		break;
	case PROP_SSL_CREDENTIALS:
		g_value_set_pointer (value, priv->ssl_creds);
		break;
	case PROP_SSL_STRICT:
		g_value_set_boolean (value, priv->ssl_strict);
		break;
	case PROP_SSL_FALLBACK:
		g_value_set_boolean (value, priv->ssl_fallback);
		break;
	case PROP_TRUSTED_CERTIFICATE:
		g_value_set_boolean (value, priv->tls_errors == 0);
		break;
	case PROP_ASYNC_CONTEXT:
		g_value_set_pointer (value, priv->async_context ? g_main_context_ref (priv->async_context) : NULL);
		break;
	case PROP_USE_THREAD_CONTEXT:
		g_value_set_boolean (value, priv->use_thread_context);
		break;
	case PROP_TIMEOUT:
		g_value_set_uint (value, priv->timeout);
		break;
	case PROP_TLS_CERTIFICATE:
		if (G_IS_TLS_CONNECTION (priv->conn))
			g_value_set_object (value, g_tls_connection_get_peer_certificate (G_TLS_CONNECTION (priv->conn)));
		else
			g_value_set_object (value, NULL);
		break;
	case PROP_TLS_ERRORS:
		g_value_set_flags (value, priv->tls_errors);
		break;
	case PROP_PROXY_RESOLVER:
		g_value_set_object (value, priv->proxy_resolver);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
		break;
	}
}
Пример #2
0
static GObject *
constructor (GType                  type,
	     guint                  n_construct_properties,
	     GObjectConstructParam *construct_properties)
{
	GObject *server;
	SoupServerPrivate *priv;

	server = G_OBJECT_CLASS (soup_server_parent_class)->constructor (
		type, n_construct_properties, construct_properties);
	if (!server)
		return NULL;
	priv = SOUP_SERVER_GET_PRIVATE (server);

	if (!priv->interface) {
		priv->interface =
			soup_address_new_any (SOUP_ADDRESS_FAMILY_IPV4,
					      priv->port);
	}

	if (priv->ssl_cert_file && priv->ssl_key_file) {
		priv->ssl_creds = soup_ssl_get_server_credentials (
			priv->ssl_cert_file,
			priv->ssl_key_file);
		if (!priv->ssl_creds) {
			g_object_unref (server);
			return NULL;
		}
	}

	priv->listen_sock =
		soup_socket_new (SOUP_SOCKET_LOCAL_ADDRESS, priv->interface,
				 SOUP_SOCKET_SSL_CREDENTIALS, priv->ssl_creds,
				 SOUP_SOCKET_ASYNC_CONTEXT, priv->async_context,
				 NULL);
	if (!soup_socket_listen (priv->listen_sock)) {
		g_object_unref (server);
		return NULL;
	}

	/* Re-resolve the interface address, in particular in case
	 * the passed-in address had SOUP_ADDRESS_ANY_PORT.
	 */
	g_object_unref (priv->interface);
	priv->interface = soup_socket_get_local_address (priv->listen_sock);
	g_object_ref (priv->interface);
	priv->port = soup_address_get_port (priv->interface);

	return server;
}
Пример #3
0
int main(int argc, char* argv[])
{
    //ARGH: putting it after g_dbus_node_info_new_for_xml works on Fedora but crashes the XO...
    gtk_init(&argc, &argv);

    GError *error = NULL;
    GOptionContext *context;

    context = g_option_context_new (NULL);
    g_option_context_add_main_entries (context, entries, NULL);
    //g_option_context_add_group (context, gtk_get_option_group (TRUE));
    if (!g_option_context_parse (context, &argc, &argv, &error))
    {
        g_print ("option parsing failed: %s\n", error->message);
        exit (1);
    }
    if(bundle_id == NULL || activity_id == NULL)
    {
        g_print ("bundle-id and activity-id are required options\n");
        exit (1);
    }

/////////////////////////////////////////////////

    introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
    g_assert (introspection_data != NULL);

    gchar service_name[256];
    sprintf(service_name, "org.laptop.Activity%s", activity_id);
    
    /*guint owner_id =*/ g_bus_own_name(
        G_BUS_TYPE_SESSION, service_name, G_BUS_NAME_OWNER_FLAGS_NONE,
        on_bus_acquired, on_name_acquired, on_name_lost,
        NULL, NULL);

/////////////////////////////////////////////////
/*
    GDBusProxy *journal = g_dbus_proxy_new_for_bus_sync(
        G_BUS_TYPE_SESSION, G_DBUS_PROXY_FLAGS_NONE, NULL,
        "org.laptop.sugar.DataStore",
        "/org/laptop/sugar/DataStore",
        "org.laptop.sugar.DataStore",
        NULL, NULL);
    GVariant *params = g_variant_new("(a{sv}as)", NULL, NULL);
    //fprintf(stderr, "journal proxy params is %s\n", params == NULL ? "null" : "notnull");
    GVariant *result = g_dbus_proxy_call_sync(
        journal, "find", params, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
    //fprintf(stderr, "journal proxy result is %s\n", result == NULL ? "null" : "notnull");
    if(error != NULL) {fprintf(stderr, "error: %d, %d, %s\n", error->domain, error->code, error->message);}
    if(result != NULL) {
        //fprintf(stderr, "result type: %s\n", g_variant_get_type_string(result));
        GVariant *results = NULL;
        guint32 count = -1;
        g_variant_get(result, "(@aa{sv}u)", &results, &count);
        fprintf(stderr, "results is %s, count is %d\n", results == NULL ? "null" : "notnull", count);
        //fprintf(stderr, "results type: %s\n", g_variant_get_type_string(results));
        GVariant *dictionary = NULL;
        GVariantIter results_iter;
        g_variant_iter_init(&results_iter, results);
        while (g_variant_iter_loop(&results_iter, "@a{sv}", &dictionary)) {
            GVariantIter dictionary_iter;
            g_variant_iter_init(&dictionary_iter, dictionary);
            const char *key = NULL;
            GVariant *value = NULL;
            while (g_variant_iter_loop(&dictionary_iter, "{s@v}", &key, &value)) {
                GVariant *unboxed = g_variant_get_variant(value);
                if(strcmp(g_variant_get_type_string(unboxed), "ay") != 0) {
                    fprintf(stderr, "%s=%s\n", key, g_variant_print(unboxed, TRUE));
                } else if(g_variant_n_children(unboxed) < 256) { // skip preview
                    //ARGH: for some 'clever' reason most of the strings are byte arrays...
                    fprintf(stderr, "%s=", key);
                    guchar c;
                    GVariantIter char_iter;
                    g_variant_iter_init(&char_iter, unboxed);
                    while (g_variant_iter_loop(&char_iter, "y", &c)) {
                        fprintf(stderr, "%c", c);
                    }
                    fprintf(stderr, "\n");
                }
            }
        }
    }
*/                                         
/////////////////////////////////////////////////
        
    WebKitWebView *webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
/*
    guint n = -1, i;
    guint *alma = g_signal_list_ids(G_TYPE_FROM_INSTANCE(webView), &n);
    for(i=0; i<n; ++i) {
        GSignalQuery q;
        g_signal_query(alma[i], &q);
        printf("alma: %d, %s\n", q.signal_id, q.signal_name);
    }
*/                            
    GtkScrolledWindow *scrolledWindow = GTK_SCROLLED_WINDOW(gtk_scrolled_window_new(NULL, NULL));
    gtk_scrolled_window_add_with_viewport(scrolledWindow, GTK_WIDGET(webView));
                                                        
    GtkWidget *main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_decorated(GTK_WINDOW(main_window), FALSE);
    gtk_window_maximize(GTK_WINDOW(main_window));
    //ARGH: do not use fullscreen as it disables the frame
    //gtk_window_fullscreen(GTK_WINDOW(main_window));
    gtk_container_add(GTK_CONTAINER(main_window), GTK_WIDGET(scrolledWindow));
    g_signal_connect_after(main_window, "realize", G_CALLBACK(afterMainWindowRealized), NULL);
    g_signal_connect(main_window, "destroy", G_CALLBACK(destroyWindowCb), NULL);

#ifdef USE_WEBKIT2
    g_signal_connect(webView, "close", G_CALLBACK(closeWebViewCb), main_window);
#else
    g_signal_connect(webView, "close-web-view", G_CALLBACK(closeWebViewCb), main_window);
/*
    //g_signal_connect(webView, "resource-request-starting", G_CALLBACK(resourceRequestStarting), main_window);
    SoupSession *session = webkit_get_default_session();
    fprintf(stderr, "session is %s\n", session == NULL ? "null" : "notnull");
    g_signal_connect(session, "request-started", G_CALLBACK(requestStartedCb), main_window);
*/
#endif

    SoupServer *server = soup_server_new(SOUP_SERVER_PORT, 0, NULL); // use some random port
//ARGH: it crashes
//    SoupAddress *address = soup_address_new("localhost", 0);
//    if(address != NULL) {
//        SoupServer *server = soup_server_new(SOUP_SERVER_INTERFACE, address, NULL); // use some random port
//    }
    guint port = soup_server_get_port(server);
    SoupSocket *listener = soup_server_get_listener(server);
    SoupAddress *addr = soup_socket_get_local_address(listener);
    fprintf(stderr, "server host %s port %d\n", soup_address_get_name(addr), port);
    soup_server_add_handler(server, "/activity", serverHandleStatic, NULL, NULL);
    soup_server_add_handler(server, "/web", serverHandleStatic, NULL, NULL);
    soup_server_add_handler(server, "/journal", serverHandleJournal, NULL, NULL);
//    g_signal_connect(server, "request-started", G_CALLBACK(serverRequestStartedCb), NULL);
    soup_server_run_async(server);
    
/*
    gchar *current_dir = g_get_current_dir();
    gchar index_uri[256];
    //ARGH: a file uri always has to be absolute
    sprintf(index_uri, "file://%s/index.html", current_dir);
    webkit_web_view_load_uri(webView, index_uri);
*/
    //webkit_web_view_load_uri(webView, "http://nell-colors.github.cscott.net");
    //webkit_web_view_load_uri(webView, "http://index.hu");

    gchar buffer[256];
    sprintf(buffer, "http://localhost:%d/web/index.html", port);
    webkit_web_view_load_uri(webView, buffer);

    gtk_widget_grab_focus(GTK_WIDGET(webView));
    gtk_widget_show_all(main_window);

    gtk_main();

    return 0;
}
static guint
parse_request_headers (SoupMessage *msg, char *headers, guint headers_len,
		       SoupEncoding *encoding, gpointer sock)
{
	SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg);
	char *req_method, *req_path, *url;
	SoupHTTPVersion version;
	const char *req_host;
	guint status;
	SoupURI *uri;

	status = soup_headers_parse_request (headers, headers_len,
					     msg->request_headers,
					     &req_method,
					     &req_path,
					     &version);
	if (!SOUP_STATUS_IS_SUCCESSFUL (status))
		return status;

	g_object_set (G_OBJECT (msg),
		      SOUP_MESSAGE_METHOD, req_method,
		      SOUP_MESSAGE_HTTP_VERSION, version,
		      NULL);
	g_free (req_method);

	/* Handle request body encoding */
	*encoding = soup_message_headers_get_encoding (msg->request_headers);
	if (*encoding == SOUP_ENCODING_UNRECOGNIZED) {
		if (soup_message_headers_get_list (msg->request_headers, "Transfer-Encoding"))
			return SOUP_STATUS_NOT_IMPLEMENTED;
		else
			return SOUP_STATUS_BAD_REQUEST;
	}

	/* Generate correct context for request */
	req_host = soup_message_headers_get_one (msg->request_headers, "Host");
	if (req_host && strchr (req_host, '/')) {
		g_free (req_path);
		return SOUP_STATUS_BAD_REQUEST;
	}

	if (!strcmp (req_path, "*") && req_host) {
		/* Eg, "OPTIONS * HTTP/1.1" */
		url = g_strdup_printf ("%s://%s",
				       soup_socket_is_ssl (sock) ? "https" : "http",
				       req_host);
		uri = soup_uri_new (url);
		if (uri)
			soup_uri_set_path (uri, "*");
		g_free (url);
	} else if (*req_path != '/') {
		/* Must be an absolute URI */
		uri = soup_uri_new (req_path);
	} else if (req_host) {
		url = g_strdup_printf ("%s://%s%s",
				       soup_socket_is_ssl (sock) ? "https" : "http",
				       req_host, req_path);
		uri = soup_uri_new (url);
		g_free (url);
	} else if (priv->http_version == SOUP_HTTP_1_0) {
		/* No Host header, no AbsoluteUri */
		SoupAddress *addr = soup_socket_get_local_address (sock);

		uri = soup_uri_new (NULL);
		soup_uri_set_scheme (uri, soup_socket_is_ssl (sock) ?
				     SOUP_URI_SCHEME_HTTPS :
				     SOUP_URI_SCHEME_HTTP);
		soup_uri_set_host (uri, soup_address_get_physical (addr));
		soup_uri_set_port (uri, soup_address_get_port (addr));
		soup_uri_set_path (uri, req_path);
	} else
		uri = NULL;

	g_free (req_path);

	if (!SOUP_URI_VALID_FOR_HTTP (uri)) {
		/* certainly not "a valid host on the server" (RFC2616 5.2.3)
		 * SOUP_URI_VALID_FOR_HTTP also guards against uri == NULL
		 */
		if (uri)
			soup_uri_free (uri);
		return SOUP_STATUS_BAD_REQUEST;
	}

	soup_message_set_uri (msg, uri);
	soup_uri_free (uri);

	return SOUP_STATUS_OK;
}
Пример #5
0
static void
server_api_status_callback (SoupServer * server, SoupMessage * msg, const char *path, GHashTable * query, SoupClientContext * client, gpointer user_data)
{
  (void) server;
  (void) path;
  (void) query;
  (void) client;
  (void) user_data;
  char *response = NULL;
  GPtrArray *r_array = NULL;
  GPtrArray *unr_array = NULL;
  GPtrArray *connected_sensors = NULL;
  GHashTable *hash;
  GString *st_sensors = NULL;
  GString *st_unrsensors = NULL;
  GString *st_csensors = NULL;
  gchar *per_session_stats = NULL;
  gchar *engine_stats = NULL;
  gchar *server_stats = NULL;
  GTimeVal current_time;
  SoupSocket *sock = NULL;
  SoupAddress *sockaddr = NULL;
  SoupURI *uri = NULL;
  guint i;
  /* Only get methods and no params */
  g_get_current_time (&current_time);
  if (msg->method != SOUP_METHOD_GET || query != NULL)
  {
    soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
    return;
  }
 /* Verificación anti listos */
  if ((sock = soup_client_context_get_socket (client)) == NULL) 
  {
    soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
    return;
  }
  /* If host == 127.0.0.1 or host == localhost, verify that the localaddress == 127.0.0.1 */
  if ((uri = soup_message_get_uri (msg)) == NULL)
  {
    soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
    return;
  }
  if (strcmp (uri->host,"127.0.0.1") == 0 || strcmp (uri->host,"localhost") == 0)
  {
    if ((sockaddr = soup_socket_get_local_address (sock)) == NULL)
    {
      soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
      return;
    }
    else
    {
      /* Aquí hay un listo */
      if (strcmp ( soup_address_get_physical(sockaddr),"127.0.0.1") != 0)
      {
        soup_message_set_status (msg, SOUP_STATUS_UNAUTHORIZED);
        return;
      }
    }
  }

  if (server_api_get_info_sensors (&r_array, &unr_array, &hash) == FALSE)
  {
    soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
    return;
  }
/* Load the info of connected sensors */
  if (sim_server_get_sensor_uuids_unique (ossim.server, &connected_sensors) == FALSE)
  {
    soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
    return;
  }


  if ((st_sensors = g_string_new ("")) == NULL)
  {
    soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
    return;
  }
  if ((st_unrsensors = g_string_new ("")) == NULL)
  {
    soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
    return;
  }
  if ((st_csensors = g_string_new ("")) == NULL)
  {
    soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR);
    return;
  }


  /* For the register sensors */
  if (r_array->len > 0)
  {

    for (i = 0; i < r_array->len; i++)
    {
      g_string_append_printf (st_sensors, "\"%s\"", (gchar *) g_ptr_array_index (r_array, i));
      if ((i != (r_array->len - 1)))
      {
        g_string_append_c (st_sensors, ',');
      }
    }
    g_string_prepend_c (st_sensors, '[');
    g_string_append_c (st_sensors, ']');
  }
  else
  {
    g_string_printf (st_sensors, "[null]");
  }

  /* For the unregister sensors */
  if (unr_array->len > 0)
  {
    for (i = 0; i < unr_array->len; i++)
    {
      g_string_append_printf (st_unrsensors, "\"%s\"", (gchar *) g_ptr_array_index (unr_array, i));
      if ((i != (unr_array->len - 1)))
      {
        g_string_append_c (st_unrsensors, ',');
      }
    }
    g_string_prepend_c (st_unrsensors, '[');
    g_string_append_c (st_unrsensors, ']');
  }
  else
  {
    g_string_printf (st_unrsensors, "[null]");
  }
  /* The connected sensors */
  if (connected_sensors->len > 0)
  {
    int count = 0;
    for (i = 0; i < connected_sensors->len; i++)
    {
      char *av_uuid;
      if ((av_uuid = g_hash_table_lookup (hash, (gchar *) g_ptr_array_index (connected_sensors, i))) != NULL)
      {
        g_string_append_printf (st_csensors, "\"%s\"", av_uuid);
        count++;
        if ((i != (connected_sensors->len - 1)))
        {
          g_string_append_c (st_csensors, ',');
        }
      }
    }
    if (count > 0)
    {
      g_string_prepend_c (st_csensors, '[');
      g_string_append_c (st_csensors, ']');
    }
    else
    {
      g_string_printf (st_csensors, "[null]");

    }
  }
  else
  {
    g_string_printf (st_csensors, "[null]");
  }




  per_session_stats = sim_server_api_get_per_session_stats ();
  server_stats = server_api_get_server_stats (&current_time);
  engine_stats = server_api_get_engine_stats (&current_time);
  old_time.tv_sec = current_time.tv_sec;

response =
      g_strdup_printf ("{\"result\":{\"request\":\"/server/status\",\n\"timestamp\":\"%lld\",\n\"rsensors\":%s,\n\"unrsensors\":%s,\n\"csensors\":%s,\n\"engine_stats\":[%s],\n\"server_stats\":%s,\n\"per_session_stats\":%s\n},\n\"status\":\"OK\"\n}",
                       (long long) time (NULL),
                       st_sensors->str,
                       st_unrsensors->str,
                       st_csensors->str,
                       engine_stats,
                       server_stats,
                       per_session_stats
        );

  soup_message_set_response (msg, "application/json", SOUP_MEMORY_TAKE, response, strlen (response));
  soup_message_set_status (msg, SOUP_STATUS_OK);
  if (r_array != NULL)
    g_ptr_array_unref (r_array);
  if (unr_array != NULL)
    g_ptr_array_unref (unr_array);
  if (hash != NULL)
    g_hash_table_destroy (hash);
  if (st_sensors != NULL)
  {
    g_string_free (st_sensors, TRUE);
  }
  if (st_unrsensors != NULL)
  {
    g_string_free (st_unrsensors, TRUE);
  }
  if (st_csensors != NULL)
  {
    g_string_free (st_csensors, TRUE);
  }
  if (connected_sensors != NULL)
     g_ptr_array_unref (connected_sensors);
  g_free (per_session_stats);
  g_free (engine_stats);
  g_free (server_stats);
}