/** * @short Creates the onion structure to fill with the server data, and later do the onion_listen() * @memberof onion_t * * Creates an onion structure that can be used to set the server, port, SSL and similar parameters. It works over * the onion structure, which is the main structure to control the listening of new connections throught TCP/IP. * * A normal usage would be like this: * * @code * * onion *o=onion_new(O_THREADED); * onion_set_root_handler(o, onion_handler_directory(".")); * onion_listen(o); * * @endcode * * @param flags Or'ed flags to use at the listening daemon. Normally one of O_ONE, O_ONE_LOOP or O_THREADED. * * @returns The onion structure. * * @see onion_mode_e onion_t */ onion *onion_new(int flags){ ONION_DEBUG0("Some internal sizes: onion size: %d, request size %d, response size %d",sizeof(onion),sizeof(onion_request),sizeof(onion_response)); if(SOCK_CLOEXEC == 0){ ONION_WARNING("There is no support for SOCK_CLOEXEC compiled in libonion. This may be a SECURITY PROBLEM as connections may leak into executed programs."); } if (!(flags&O_NO_SIGPIPE)){ ONION_DEBUG("Ignoring SIGPIPE"); signal(SIGPIPE, SIG_IGN); } onion *o=onion_low_calloc(1,sizeof(onion)); if (!o){ return NULL; } o->flags=(flags&0x0FF)|O_SSL_AVAILABLE; o->timeout=5000; // 5 seconds of timeout, default. o->poller=onion_poller_new(15); if (!o->poller){ onion_low_free(o); return NULL; } o->sessions=onion_sessions_new(); o->internal_error_handler=onion_handler_new((onion_handler_handler)onion_default_error, NULL, NULL); o->max_post_size=1024*1024; // 1MB o->max_file_size=1024*1024*1024; // 1GB #ifdef HAVE_PTHREADS o->flags|=O_THREADS_AVALIABLE; o->nthreads=8; if (o->flags&O_THREADED) o->flags|=O_THREADS_ENABLED; #endif return o; }
/** * @short Initializes the server data. * @memberof onion_server_t * * This data is independant of communication channel, and its the data that should be alsays available, even shared between the channels. * * All data have sensitive defaults, except write and root_handler, and can be modified with accessor functions. * * These defaults include: * * internal_error_handler * * max_post_size -- 1MB * * max_file_size -- 1GB */ onion_server *onion_server_new(void){ onion_server *ret=malloc(sizeof(onion_server)); ret->root_handler=NULL; ret->write=NULL; ret->internal_error_handler=onion_handler_new((onion_handler_handler)onion_default_error, NULL, NULL); ret->max_post_size=1024*1024; // 1MB ret->max_file_size=1024*1024*1024; // 1GB ret->sessions=onion_sessions_new(); return ret; }
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(); }