Exemplo n.º 1
0
gboolean _xr_server_service_run(GThreadedSocketService *service, GSocketConnection *connection, GObject *source_object, gpointer user_data)
{
  GError* local_err = NULL;
  xr_server* server = user_data;
  xr_server_conn* conn;

  //xr_trace(XR_DEBUG_SERVER_TRACE, "(conn=%p, server=%p)", conn, server);

  xr_set_nodelay(g_socket_connection_get_socket(connection));

  // new connection accepted
  conn = g_new0(xr_server_conn, 1);
  conn->servlets = g_ptr_array_sized_new(3);
  conn->running = TRUE;

  // setup TLS
  if (server->secure)
  {
    conn->tls_conn = g_tls_server_connection_new(G_IO_STREAM(connection), server->cert, &local_err);
    if (local_err)
    {
      g_error_free(local_err);
      goto out;
    }

    //g_object_set(conn->conn, "authentication-mode", test->auth_mode, NULL);
    //g_signal_connect(conn->conn, "accept-certificate", G_CALLBACK(on_accept_certificate), server);

    conn->http = xr_http_new(conn->tls_conn);
  }
  else
  {
    conn->http = xr_http_new(G_IO_STREAM(connection));
  }

  conn->conn = connection;

  while (conn->running)
  {
    if (!_xr_server_serve_request(server, conn))
      break;
  }

out:
  xr_http_free(conn->http);
  if (conn->tls_conn)
    g_object_unref(conn->tls_conn);
  g_ptr_array_foreach(conn->servlets, (GFunc)xr_servlet_free_fini, NULL);
  g_ptr_array_free(conn->servlets, TRUE);
  memset(conn, 0, sizeof(*conn));
  g_free(conn);

  return FALSE;
}
Exemplo n.º 2
0
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;
}