Example #1
0
void
mono_g_hash_table_destroy (MonoGHashTable *hash)
{
	int i;
	
	g_return_if_fail (hash != NULL);

#ifdef HAVE_SGEN_GC
	mono_gc_deregister_root ((char*)hash);
#endif

	for (i = 0; i < hash->table_size; i++){
		Slot *s, *next;

		for (s = hash->table [i]; s != NULL; s = next){
			next = s->next;
			
			if (hash->key_destroy_func != NULL)
				(*hash->key_destroy_func)(s->key);
			if (hash->value_destroy_func != NULL)
				(*hash->value_destroy_func)(s->value);
			free_slot (hash, s);
		}
	}
	mg_free (hash->table);
	mg_free (hash);
}
Example #2
0
static void
rehash (MonoGHashTable *hash)
{
	MONO_REQ_GC_UNSAFE_MODE; //we must run in unsafe mode to make rehash safe

	int diff = ABS (hash->last_rehash - hash->in_use);
	RehashData data;
	void *old_table G_GNUC_UNUSED; /* unused on Boehm */

	/* These are the factors to play with to change the rehashing strategy */
	/* I played with them with a large range, and could not really get */
	/* something that was too good, maybe the tests are not that great */
	if (!(diff * 0.75 > hash->table_size * 2))
		return;

	data.hash = hash;
	data.new_size = g_spaced_primes_closest (hash->in_use);
	data.table = mg_new0 (Slot *, data.new_size);

	if (!mono_threads_is_coop_enabled ()) {
		old_table = mono_gc_invoke_with_gc_lock (do_rehash, &data);
	} else {
		/* We cannot be preempted */
		old_table = do_rehash (&data);
	}

	mg_free (old_table);
}
Example #3
0
// Runs the given 'code' query string in the lua interpreter. 
// Totally not safe, but whatever. 
static void 
eval_script(struct mg_connection *conn,
      const struct mg_request_info *request_info,
      void *user_data) 
{
  char *code = mg_get_var(conn, "code"), *err_str;
  if (code) {
    
    // thread-safe, blocking eval call
    err_str = eval_buffer_write(code);
    if (err_str) {
        mg_printf(conn, "HTTP/1.1 500 Internal Server Error\r\n"
                  "Content-Type: text/plain\r\n\r\n"
                  "Lua error: %s", err_str);
        free(err_str);
    } else {
        mg_printf(conn, "HTTP/1.1 200 OK\r\n"
                  "Content-Type: text/plain\r\n\r\n");        
    }

    mg_free(code);
  } else {
    mg_printf(conn, "HTTP/1.1 400 Bad Request\r\n"
          "Content-Type: text/plain\r\n\r\n"
          "Parameter 'code' not specified");  
  }
}
Example #4
0
static void
free_slot (MonoGHashTable *hash, Slot *slot)
{
	if (hash->gc_type == MONO_HASH_CONSERVATIVE_GC)
		mono_gc_free_fixed (slot);
	else
		mg_free (slot);
}
Example #5
0
static void
login_page(struct mg_connection *conn,
		const struct mg_request_info *ri, void *data)
{
	char		*name, *pass, uri[100];
	const char	*cookie;

	name = mg_get_var(conn, "name");
	pass = mg_get_var(conn, "pass");
	cookie = mg_get_header(conn, "Cookie");

	/*
	 * Here user name and password must be checked against some
	 * database - this is step 2 from the algorithm described above.
	 * This is an example, so hardcode name and password to be
	 * admin/admin, and if this is so, set "allow=yes" cookie and
	 * redirect back to the page where we have been redirected to login.
	 */
	if (name != NULL && pass != NULL &&
	    strcmp(name, "admin") == 0 && strcmp(pass, "admin") == 0) {
		if (cookie == NULL || sscanf(cookie, "uri=%99s", uri) != 1)
			(void) strcpy(uri, "/");
		/* Set allow=yes cookie, which is expected by authorize() */
		mg_printf(conn, "HTTP/1.1 301 Moved Permanently\r\n"
		    "Location: %s\r\n"
		    "Set-Cookie: allow=yes;\r\n\r\n", uri);
	} else {
		/* Print login page */
		mg_printf(conn, "HTTP/1.1 200 OK\r\n"
		    "content-Type: text/html\r\n\r\n"
		    "Please login (enter admin/admin to pass)<br>"
		    "<form method=post>"
		    "Name: <input type=text name=name></input><br/>"
		    "Password: <input type=password name=pass></input><br/>"
		    "<input type=submit value=Login></input>"
		    "</form>");
	}

	if (name != NULL)
		mg_free(name);
	if (pass != NULL)
		mg_free(pass);
}
Example #6
0
File: main.c Project: undees/cukeup
static void multiply(
    struct mg_connection *conn,
    const struct mg_request_info *request_info,
    void *user_data)
{
    char *multiplier_s   = mg_get_var(conn, "multiplier");
    char *multiplicand_s = mg_get_var(conn, "multiplicand");

    int multiplier   = atol(multiplier_s);
    int multiplicand = atol(multiplicand_s);

    result = multiplier * multiplicand;

    mg_printf(conn,
        "HTTP/1.1 200 OK\r\n\
Content-Type: text/plain\r\n\r\n");

    mg_free(multiplier_s);
    mg_free(multiplicand_s);
}
Example #7
0
static void
login_page(struct mg_connection *conn,
		const struct mg_request_info *ri, void *data)
{
	char		*name, *pass;

	name = mg_get_var(conn, "UserId");
	pass = mg_get_var(conn, "PassWord");

	/*
	 * Here user name and password must be checked against some
	 * database - this is step 2 from the algorithm described above.
	 * This is an example, so hardcode name and password to be
	 * admin/admin, and if this is so, set "allow=yes" cookie and
	 * redirect back to the page where we have been redirected to login.
	 */
	if (name != NULL && pass != NULL ) {
		/* Print login page */
		mg_printf(conn, "HTTP/1.1 200 OK\r\n"
		    "content-Type: text/html\r\n\r\n"
		    "Name is %s<br>"
            "password is %s<br>",name,pass);
	} else {
		/* Print login page */
		mg_printf(conn, "HTTP/1.1 200 OK\r\n"
		    "content-Type: text/html\r\n\r\n"
		    "Please login (enter admin/admin to pass)<br>"
		    "<form method=post>"
		    "Name: <input type=text name=name></input><br/>"
		    "Password: <input type=password name=pass></input><br/>"
		    "<input type=submit value=Login></input>"
		    "</form>");
	}

	if (name != NULL)
		mg_free(name);
	if (pass != NULL)
		mg_free(pass);

	return ;
}
Example #8
0
static void
open_file(struct mg_connection *conn,
      const struct mg_request_info *request_info,
      void *user_data)
{
  FILE  *fp;
  char *file_name, *buf, abs_path[PATH_MAX], *dir;
  long size;
  
  if (!(file_name = get_var_or_err(conn, "file"))
      || !(dir = get_abs_dirtype_path(conn))) {
    return;
  }

  sprintf(abs_path, "%s/%s", dir, file_name);
  
  if (!(fp = fopen(abs_path, "r"))) {
    mg_printf(conn, "HTTP/1.1 500 Internal Server Error\r\n"
          "Content-Type: text/plain\r\n\r\n"
          "Can't open file '%s'", abs_path);
    return;
  }
  
  // obtain file size:
  fseek(fp, 0, SEEK_END);
  size = ftell(fp);
  rewind (fp);
    
  // allocate memory to contain the whole file:
  buf = (char*) malloc (sizeof(char)*size);
  fread(buf, 1, size, fp);
  
  mg_printf(conn, "HTTP/1.1 200 OK\r\n"
        "Content-Type: text/plain\r\n"
        "Content-Length: %ld\r\n"
        "Connection: close\r\n\r\n", size);

  mg_write(conn, buf, size);
  
  mg_free(file_name);
  free(buf);
  fclose(fp);
}
Example #9
0
static void
rehash (MonoGHashTable *hash)
{
	int diff = ABS (hash->last_rehash - hash->in_use);
	RehashData data;
	void *old_table;

	/* These are the factors to play with to change the rehashing strategy */
	/* I played with them with a large range, and could not really get */
	/* something that was too good, maybe the tests are not that great */
	if (!(diff * 0.75 > hash->table_size * 2))
		return;

	data.hash = hash;
	data.new_size = g_spaced_primes_closest (hash->in_use);
	data.table = mg_new0 (Slot *, data.new_size);

	old_table = mono_gc_invoke_with_gc_lock (do_rehash, &data);
	mg_free (old_table);
}
Example #10
0
/* gets the directory folder specified by the 'dirtype' parameter */
static char *
get_abs_dirtype_path(struct mg_connection *conn)
{
  char *dir_type, *dir;

  if (!(dir_type = get_var_or_err(conn, "dirtype"))) {
    return NULL;
  }
  if (strcmp(dir_type, "doc") == 0) {
    dir = doc_root;
  } else if (strcmp(dir_type, "root") == 0){
    dir = web_root;
  } else {
    mg_printf(conn, "HTTP/1.1 400 Bad Request\r\n"
          "Content-Type: text/plain\r\n\r\n"
          "Parameter '%s' not valid", dir_type);
    mg_free(dir_type);
    return NULL;
  }
  return dir;
}
Example #11
0
static void
free_slot (MonoGHashTable *hash, Slot *slot)
{
	mg_free (slot);
}