//Initialize favicon int initializeFavicon(char *argv[]) { //What does this do? signal(SIGINT,shutdown_server); signal(SIGTERM,shutdown_server); //Initialize address and port o=onion_new(O_POOL); onion_set_timeout(o, 5000); onion_set_hostname(o,"0.0.0.0"); onion_set_port(o, argv[3]); onion_url *urls=onion_root_url(o); //Add handlers onion_url_add(urls, "", forward); onion_url_add(urls, "^(.*)$", forward); }
/*+++++++++++++ OnionServer-Class ++++++++++++++++++ */ int OnionServer::start_server() { onion_url *url=onion_root_url(m_ponion); const char *host, *port; if( m_psettingKinectGrid != NULL){ host = m_psettingKinectGrid->getString("host"); port = m_psettingKinectGrid->getString("port"); }else{ host = "0.0.0.0"; port = "8080"; } onion_set_hostname(m_ponion, host); // Force ipv4. onion_set_port(m_ponion, port); onion_url_add_with_data(url, "kinectgrid_settings.js", (void*)insert_json, m_psettingKinect, NULL); onion_url_add_with_data(url, "index.html", (void*)index_html, m_psettingKinectGrid, NULL); onion_url_add_with_data(url, "", (void*)index_html, m_psettingKinectGrid, NULL); //onion_url_add_with_data(url, "index.html", (void*)checkFormularValues, this, NULL); //onion_url_add_with_data(url, "", (void*)checkFormularValues, this, NULL); onion_url_add_with_data(url, "json", (void*)checkFormularValues, this, NULL); onion_url_add(url, "^.*$", (void*)search_file); /* Now, m_ponion get the O_DETACH_LISTEN flag on creation and the Extra thread is omitable. */ //start loop as thread //return pthread_create( &m_pthread, NULL, &start_myonion_server, m_ponion); onion_listen(m_ponion);//loop return 0; }
void t07_server_with_error_501(){ INIT_LOCAL(); onion *server=onion_new(0); onion_listen_point *lp=onion_buffer_listen_point_new(); onion_add_listen_point(server,NULL,NULL,lp); onion_url *urls=onion_url_new(); onion_set_root_handler(server, onion_url_to_handler(urls)); onion_url_add(urls, "", error_501); onion_request *req=onion_request_new(lp); #define S "GET / HTTP/1.1" onion_request_write(req, S,sizeof(S)-1); // send it all, but the final 0. #undef S const char *buffer=onion_buffer_listen_point_get_buffer_data(req); FAIL_IF_NOT_EQUAL_STR(buffer,""); onion_request_write(req, "\n",1); // finish this request. no \n\n before to check possible bugs. onion_request_write(req, "\n",1); // finish this request. no \n\n before to check possible bugs. The last \n was not processed, as was overflowing. buffer=onion_buffer_listen_point_get_buffer_data(req); FAIL_IF_EQUAL_STR(buffer,""); FAIL_IF_NOT_STRSTR(buffer, "HTTP/1.1 501 NOT IMPLEMENTED"); FAIL_IF_NOT_STRSTR(buffer, "libonion"); onion_request_free(req); onion_free(server); END_LOCAL(); }
onion *websocket_server_new() { onion *o = onion_new(0); onion_url *urls = onion_root_url(o); onion_url_add(urls, "", ws_handler); onion_listen_point *lp = onion_buffer_listen_point_new(); onion_add_listen_point(o, NULL, NULL, lp); return o; }
int main(int argc, char **argv) { signal(SIGINT, shutdown_server); signal(SIGTERM, shutdown_server); ONION_VERSION_IS_COMPATIBLE_OR_ABORT(); o = onion_new(O_POOL); onion_set_timeout(o, 5000); onion_set_hostname(o, "0.0.0.0"); onion_url *urls = onion_root_url(o); onion_url_add_static(urls, "static", "Hello static world", HTTP_OK); onion_url_add(urls, "timeout", random_timeout); onion_url_add(urls, "", hello); onion_url_add(urls, "^(.*)$", hello); onion_listen(o); onion_free(o); return 0; }
void t01_url() { INIT_LOCAL(); onion_url *url = onion_url_new(); onion_url_add_handler(url, "^handler1.$", onion_handler_new((onion_handler_handler) handler1, NULL, NULL)); onion_url_add(url, "handler2/", handler2); onion_url_add_with_data(url, "^handler(3|4)/", handler3, NULL, NULL); onion_set_root_handler(server, onion_url_to_handler(url)); onion_request *req = onion_request_new(onion_get_listen_point(server, 0)); #define R "GET /handler1/ HTTP/1.1\n\n" onion_request_write(req, R, sizeof(R)); onion_request_process(req); FAIL_IF_NOT_EQUAL_INT(handler_called, 1); FAIL_IF_NOT_EQUAL_STR(urltxt, ""); free(urltxt); urltxt = NULL; onion_request_clean(req); onion_request_write(req, "GET /handler2/ HTTP/1.1\n\n", sizeof(R)); onion_request_process(req); FAIL_IF_NOT_EQUAL_INT(handler_called, 2); ONION_DEBUG("%s", urltxt); FAIL_IF_NOT_EQUAL_STR(urltxt, ""); free(urltxt); urltxt = NULL; onion_request_clean(req); onion_request_write(req, "GET /handler3/hello HTTP/1.1\n\n", sizeof(R) + 5); onion_request_process(req); FAIL_IF_NOT_EQUAL_INT(handler_called, 3); FAIL_IF_NOT_EQUAL_STR(urltxt, "hello"); free(urltxt); urltxt = NULL; handler_called = 0; onion_request_clean(req); onion_request_write(req, "GET /handler2/hello HTTP/1.1\n\n", sizeof(R) + 5); onion_request_process(req); FAIL_IF_NOT_EQUAL_INT(handler_called, 0); FAIL_IF_EQUAL_STR(urltxt, ""); free(urltxt); urltxt = NULL; onion_request_free(req); onion_url_free(url); onion_set_root_handler(server, NULL); END_LOCAL(); }
int main(){ onion *o=onion_new(O_THREADED); onion_url *urls=onion_root_url(o); onion_url_add(urls, "", websocket_example); onion_listen(o); onion_free(o); return 0; }
// This fixes that whenever a session is created, but no new data is added, it does not set the proper cookie void t04_lot_of_sessionid(){ INIT_LOCAL(); onion *o=onion_new(O_ONE_LOOP); onion_server_set_write(o->server, empty_write); onion_url *url=onion_root_url(o); onion_url_add(url, "^.*", ask_session); char sessionid[256]; char tmp[1024]; char tmp2[4096]; onion_request *req; int i; set_data_on_session=1; req=onion_request_new(o->server, NULL, NULL); req->fullpath="/"; onion_request_process(req); FAIL_IF_NOT_EQUAL_INT(onion_dict_count(o->server->sessions->sessions), 1); FAIL_IF_EQUAL_STR(lastsessionid,""); strcpy(sessionid, lastsessionid); req->fullpath=NULL; onion_request_free(req); req=onion_request_new(o->server, NULL, NULL); req->fullpath="/"; snprintf(tmp,sizeof(tmp)," sessionid=xx%sxx;",lastsessionid); strcpy(tmp2,"Cookie:"); for(i=0;i<64;i++) strncat(tmp2, tmp, sizeof(tmp2)); snprintf(tmp,sizeof(tmp)," sessionid=%s\n",lastsessionid); strncat(tmp2, tmp, sizeof(tmp2)); ONION_DEBUG("Set cookies (%d bytes): %s",strlen(tmp2),tmp2); strcpy(tmp,"GET /\n"); onion_request_write(req,tmp,strlen(tmp)); // Here is the problem, at parsing too long headers onion_request_write(req,tmp2,strlen(tmp2)); // Here is the problem, at parsing too long headers onion_request_write(req,"\n",1); //onion_dict_add(req->headers, "Cookie", tmp2, 0); onion_request_process(req); FAIL_IF_NOT_EQUAL_INT(onion_dict_count(o->server->sessions->sessions), 1); FAIL_IF_EQUAL_STR(lastsessionid,""); FAIL_IF_NOT_EQUAL_STR(lastsessionid, sessionid); FAIL_IF_NOT(has_set_cookie); onion_request_free(req); onion_free(o); END_LOCAL(); }
int main(int argc, char **argv){ char *port="8080"; char *serverip="::"; const char *command="/bin/bash"; const char *certificatefile="/etc/pki/tls/certs/pound.pem"; const char *keyfile="/etc/pki/tls/certs/pound.key"; int error; int i; int ssl=1; #ifdef HAVE_PAM int use_pam=1; #endif for (i=1;i<argc;i++){ if (strcmp(argv[i],"--help")==0){ show_help(); exit(0); } else if(strcmp(argv[i],"-p")==0 || strcmp(argv[i],"--port")==0){ if (i+1>argc){ ONION_ERROR("Need to set the port number."); show_help(); exit(1); } port=argv[++i]; fprintf(stderr, "Using port %s\n",port); } else if(strcmp(argv[i],"-i")==0 || strcmp(argv[i],"--ip")==0){ if (i+1>argc){ ONION_ERROR("Need to set the ip address or hostname."); show_help(); exit(1); } serverip=argv[++i]; fprintf(stderr, "Using ip %s\n",serverip); } else if(strcmp(argv[i],"-c")==0 || strcmp(argv[i],"--cert")==0){ if (i+1>argc){ ONION_ERROR("Need to set the certificate filename"); show_help(); exit(1); } certificatefile=argv[++i]; ONION_INFO("Using certificate %s",certificatefile); } else if(strcmp(argv[i],"-k")==0 || strcmp(argv[i],"--key")==0){ if (i+1>argc){ ONION_ERROR("Need to set the certificate key filename."); show_help(); exit(1); } keyfile=argv[++i]; ONION_INFO("Using certificate key %s",keyfile); } else if(strcmp(argv[i],"-x")==0 || strcmp(argv[i],"--exec")==0){ if (i+1>argc){ ONION_ERROR("Need the command to execute."); show_help(); exit(1); } command=argv[++i]; ONION_INFO("New terminal execute the command %s",command); } else if(strcmp(argv[i],"--no-ssl")==0){ ssl=0; ONION_INFO("Disabling SSL!"); } #ifdef HAVE_PAM else if(strcmp(argv[i],"--no-pam")==0){ use_pam=0; ONION_INFO("Disabling PAM!"); } #endif } o=onion_new(O_POOL|O_SYSTEMD); // I prepare the url handler, with static, uuid and term. Also added the empty rule that redirects to static/index.html onion_url *url=onion_url_new(); onion_handler *term_handler=oterm_handler(o,command); #ifdef HAVE_PAM if (use_pam){ onion_url_add_handler(url, "^term/", onion_handler_auth_pam("Onion Terminal", "login", term_handler)); } else #endif { onion_url_add_with_data(url, "^term/", oterm_nopam, term_handler, NULL); } onion_url_add_with_data(url, "^uuid/", oterm_uuid, onion_handler_get_private_data(term_handler), NULL); #ifdef __DEBUG__ if (getenv("OTERM_DEBUG")) onion_url_add_handler(url, "^static/", onion_handler_export_local_new("static")); else #endif { onion_url_add(url, "^static/", opack_static); } onion_url_add_with_data(url, "", onion_shortcut_internal_redirect, "static/index.html", NULL); srand(time(NULL)); onion_set_root_handler(o, onion_url_to_handler(url)); if (!(onion_flags(o)&O_SSL_AVAILABLE)){ ONION_WARNING("SSL support is not available. Oterm is in unsecure mode!"); } else if (ssl){ // Not necesary the else, as onion_use_certificate would just return an error. But then it will exit. error=onion_set_certificate(o, O_SSL_CERTIFICATE_KEY, certificatefile, keyfile); if (error){ ONION_ERROR("Cant set certificate and key files (%s, %s)",certificatefile, keyfile); show_help(); exit(1); } } onion_set_port(o, port); onion_set_hostname(o, serverip); onion_set_timeout(o,5000); signal(SIGINT, free_onion); signal(SIGPIPE, SIG_IGN); fprintf(stderr, "Listening at %s\n",port); error=onion_listen(o); if (error){ ONION_ERROR("Cant create the server: %s", strerror(errno)); } onion_free(o); return 0; }
int add_reset_page(onion_url* urls) { return onion_url_add(urls, "reset", handle_reset); }
int add_done_page(onion_url* urls) { return onion_url_add(urls, trivia_end.uri, handle_done); }
int add_start_page(onion_url* urls) { return onion_url_add(urls, "", handle_start); }
// This fixes that whenever a session is created, but no new data is added, it does not set the proper cookie, and should not create a new session. void t03_bug_empty_session_is_new_session(){ INIT_LOCAL(); onion *o=onion_new(O_ONE_LOOP); onion_server_set_write(o->server, empty_write); onion_url *url=onion_root_url(o); onion_url_add(url, "^.*", ask_session); char sessionid[256]; char tmp[256]; set_data_on_session=1; onion_request *req=onion_request_new(o->server, NULL, NULL); req->fullpath="/"; onion_request_process(req); FAIL_IF_EQUAL_STR(lastsessionid,""); strcpy(sessionid, lastsessionid); req->fullpath=NULL; onion_request_free(req); FAIL_IF_NOT_EQUAL_INT(onion_dict_count(o->server->sessions->sessions), 1); req=onion_request_new(o->server, NULL, NULL); req->fullpath="/"; onion_dict_add(req->headers, "Cookie", "sessionid=xxx", 0); onion_request_process(req); FAIL_IF_EQUAL_STR(lastsessionid,""); FAIL_IF_EQUAL_STR(lastsessionid, sessionid); FAIL_IF_NOT(has_set_cookie); req->fullpath=NULL; onion_request_free(req); FAIL_IF_NOT_EQUAL_INT(onion_dict_count(o->server->sessions->sessions), 2); req=onion_request_new(o->server, NULL, NULL); req->fullpath="/"; snprintf(tmp,sizeof(tmp),"sessionid=%s",lastsessionid); onion_dict_add(req->headers, "Cookie", tmp, 0); onion_request_process(req); FAIL_IF_EQUAL_STR(lastsessionid,""); FAIL_IF_EQUAL_STR(lastsessionid, sessionid); FAIL_IF_NOT(has_set_cookie); strcpy(sessionid, lastsessionid); req->fullpath=NULL; onion_request_free(req); FAIL_IF_NOT_EQUAL_INT(onion_dict_count(o->server->sessions->sessions), 2); req=onion_request_new(o->server, NULL, NULL); req->fullpath="/"; snprintf(tmp,sizeof(tmp),"sessionid=%sxx",lastsessionid); onion_dict_add(req->headers, "Cookie", tmp, 0); onion_request_process(req); FAIL_IF_EQUAL_STR(lastsessionid,""); FAIL_IF_EQUAL_STR(lastsessionid, sessionid); FAIL_IF_NOT(has_set_cookie); req->fullpath=NULL; onion_request_free(req); FAIL_IF_NOT_EQUAL_INT(onion_dict_count(o->server->sessions->sessions), 3); // Ask for new, without session data, but I will not set data on session, so session is not created. set_data_on_session=0; req=onion_request_new(o->server, NULL, NULL); req->fullpath="/"; onion_request_process(req); FAIL_IF_EQUAL_STR(lastsessionid,""); strcpy(sessionid, lastsessionid); req->fullpath=NULL; FAIL_IF_NOT_EQUAL_INT(onion_dict_count(o->server->sessions->sessions), 4); // For a moment it exists, until onion realizes is not necesary. onion_request_free(req); FAIL_IF_NOT_EQUAL_INT(onion_dict_count(o->server->sessions->sessions), 3); onion_free(o); END_LOCAL(); }