Example #1
0
/** 
 * @short Frees the session dictionary.
 * @memberof onion_request_t
 * 
 * If data is under onion_dict scope (just dicts into dicts and strings), all data is freed.
 * If the user has set some custom data, THAT MEMORY IS LEAKED.
 */
void onion_request_session_free(onion_request *req){
	if (!req->session_id)
		onion_request_guess_session_id(req);
	if (req->session_id){
		onion_sessions_remove(req->server->sessions, req->session_id);
		onion_dict_free(req->session);
		req->session=NULL;
		free(req->session_id);
		req->session_id=NULL;
	}
}
Example #2
0
/**
 * @short Frees the session dictionary.
 * @memberof onion_request_t
 *
 * It removes the session from the sessions dictionary, so this session does not exist anymore.
 *
 * If data is under onion_dict scope (just dicts into dicts and strings), all data is freed.
 * If the user has set some custom data, THAT MEMORY IS LEAKED.
 */
void onion_request_session_free(onion_request *req) {
    if (!req->session_id)
        onion_request_guess_session_id(req);
    if (req->session_id) {
        ONION_DEBUG("Removing from session storage session id: %s",req->session_id);
        onion_sessions_remove(req->connection.listen_point->server->sessions, req->session_id);
        onion_dict_free(req->session);
        req->session=NULL;
        onion_low_free(req->session_id);
        req->session_id=NULL;
    }
}
Example #3
0
void t01_test_session(){
	INIT_LOCAL();
	
	onion_sessions *sessions=onion_sessions_new();
	
	// create a session
	onion_dict *ses=onion_sessions_get(sessions, "s01");
	FAIL_IF_NOT_EQUAL(ses, NULL); // It does not auto create the sessions, to avoid problems
	char *s01=onion_sessions_create(sessions);
	ses=onion_sessions_get(sessions, s01);
	FAIL_IF_EQUAL(ses, NULL); // It does not auto create the sessions, to avoid problems

	// get another pointer to the same session
	onion_dict *ses2=onion_sessions_get(sessions, s01);
	FAIL_IF_NOT_EQUAL(ses, ses2);
	
	//Check it is really the same
	onion_dict_add(ses, "foo", "bar", 0);
	
	FAIL_IF_NOT_EQUAL_STR("bar", onion_dict_get(ses2, "foo"));
	
	// When removed, it should refcount--
	onion_dict_free(ses2);
	ses2=onion_sessions_get(sessions, s01);
	FAIL_IF_NOT_EQUAL(ses, ses2);

	// also all removal, should stay at the sessions
	onion_dict_free(ses);
	onion_dict_free(ses2);
	ses2=onion_sessions_get(sessions, s01);
	FAIL_IF_NOT_EQUAL_STR("bar", onion_dict_get(ses2, "foo"));
	onion_dict_free(ses2);
	
	// Create a second session
	char *s02=onion_sessions_create(sessions);
	ses2=onion_sessions_get(sessions, s02);
	onion_dict_add(ses2, "hello", "world", 0);
	ses=onion_sessions_get(sessions, s01);
	
	FAIL_IF_EQUAL_STR(onion_dict_get(ses, "hello"), onion_dict_get(ses2, "hello"));
	FAIL_IF_EQUAL_STR(onion_dict_get(ses, "foo"), onion_dict_get(ses2, "foo"));
	
	onion_dict_free(ses);
	onion_dict_free(ses2);

	// And finally really remove it
	onion_sessions_remove(sessions, s01);
	ses2=onion_sessions_get(sessions, s01); 
	FAIL_IF_NOT_EQUAL(ses2, NULL);
	
	// this created a new one
	free(s01);
	s01=onion_sessions_create(sessions);
	ses2=onion_sessions_get(sessions, s01); 
	FAIL_IF_EQUAL_STR("bar", onion_dict_get(ses2, "foo"));
	onion_dict_free(ses2);
	
	// This should remove the sessions, and still hanging s02
	onion_sessions_free(sessions);
	
	free(s01);
	free(s02);
	
	END_LOCAL();
}