void t01_server_one(){ INIT_LOCAL(); o=onion_new(O_ONE); onion_set_root_handler(o,onion_handler_new((void*)process_request,NULL,NULL)); do_petition_set(1,0.1,1,0); onion_free(o); o=onion_new(O_ONE_LOOP); onion_set_root_handler(o,onion_handler_new((void*)process_request,NULL,NULL)); do_petition_set(1,0.1,1,1); onion_free(o); o=onion_new(O_ONE_LOOP); // change poller queue size onion_poller *p=onion_get_poller(o); onion_poller_set_queue_size_per_thread(p, 1); onion_set_root_handler(o,onion_handler_new((void*)process_request,NULL,NULL)); do_petition_set(1,0.001,100,1); onion_free(o); END_LOCAL(); }
void t06_empty(){ INIT_LOCAL(); onion *server=onion_new(0); onion_add_listen_point(server,NULL,NULL,onion_buffer_listen_point_new()); onion_request *request; char buffer[4096]; memset(buffer,0,sizeof(buffer)); request=onion_request_new(server->listen_points[0]); onion_response *response=onion_response_new(request); // onion_response_write_headers(response); onion_response_flush(response); onion_response_free(response); buffer[sizeof(buffer)-1]=0; strncpy(buffer,onion_buffer_listen_point_get_buffer_data(request),sizeof(buffer)-1); onion_request_free(request); onion_free(server); FAIL_IF_NOT_STRSTR(buffer, "Server:"); FAIL_IF_NOT_STRSTR(buffer, "Content-Type:"); // ONION_DEBUG(buffer); END_LOCAL(); }
void t05_printf(){ INIT_LOCAL(); onion *server=onion_new(0); onion_add_listen_point(server,NULL,NULL,onion_buffer_listen_point_new()); onion_request *request; char buffer[4096]; memset(buffer,0,sizeof(buffer)); request=onion_request_new(server->listen_points[0]); onion_response *response=onion_response_new(request); onion_response_printf(response, "%s %d %p", "Hello world", 123, NULL); onion_response_flush(response); onion_response_free(response); buffer[sizeof(buffer)-1]=0; strncpy(buffer,onion_buffer_listen_point_get_buffer_data(request),sizeof(buffer)-1); onion_request_free(request); onion_free(server); FAIL_IF_NOT_STRSTR(buffer, "Hello world 123 (nil)"); END_LOCAL(); }
void t05_post_content_json(){ INIT_LOCAL(); onion *server=onion_new(0); onion_listen_point *lp=onion_buffer_listen_point_new(); json_response post_json = { 0 }; onion_add_listen_point(server,NULL,NULL,lp); onion_set_root_handler(server, onion_handler_new((void*)&post_json_check,&post_json,NULL)); onion_request *req=onion_request_new(lp); #define POST_HEADER "POST / HTTP/1.1\nContent-Type: application/json\nContent-Length: %d\n\n" char tmp[1024]; int json_length=sizeof(JSON_EXAMPLE); ONION_DEBUG("Post size is about %d",json_length); snprintf(tmp, sizeof(tmp), POST_HEADER, json_length); // ONION_DEBUG("%s",tmp); onion_request_write(req,tmp,strlen(tmp)); onion_request_write(req,JSON_EXAMPLE,json_length); // ONION_DEBUG("%s",JSON_EXAMPLE); FAIL_IF_NOT_EQUAL_INT(post_json.processed, 2); onion_request_free(req); onion_free(server); END_LOCAL(); }
void t04_server_timeout_threaded(){ INIT_LOCAL(); CURL *curl=prepare_curl("http://localhost:8082"); o=onion_new(O_THREADED | O_DETACH_LISTEN); onion_set_root_handler(o,onion_handler_new((void*)process_request,NULL,NULL)); onion_set_port(o,"8082"); onion_set_timeout(o,2000); onion_listen(o); sleep(1); int fd=connect_to("localhost","8082"); sleep(3); // Should have closed the connection int w=write(fd,"GET /\n\n",7); FAIL_IF_NOT_EQUAL_INT(w,7); char data[256]; FAIL_IF(read(fd, data,sizeof(data))>0); close(fd); FAIL_IF_NOT(curl_get(curl, "http://localhost:8082")); onion_free(o); curl_easy_cleanup(curl); END_LOCAL(); }
void t02_stop_listening_some_petitions(){ INIT_LOCAL(); signal(SIGTERM, shutdown_server); o=onion_new(O_POOL); pthread_t th; pthread_create(&th, NULL, listen_thread_f, NULL); sleep(2); ONION_INFO("Connecting to server"); int connfd=connect_to("localhost","8080"); FAIL_IF( connfd < 0 ); FAIL_IF_NOT(ok_listening); kill(getpid(), SIGTERM); sleep(2); FAIL_IF(ok_listening); pthread_join(th, NULL); onion_free(o); END_LOCAL(); }
void t04_websocket_server_close_handshake() { INIT_LOCAL(); int length = 0; unsigned char *buffer = NULL, buffer2[8]; onion *o = websocket_server_new(); onion_request *req = websocket_start_handshake(o); req->connection.listen_point->read = (lpreader_sig_t *) websocket_data_buffer_read; req->connection.listen_point->write = (lpwriter_sig_t *) websocket_data_buffer_write; onion_response *res = onion_response_new(req); onion_websocket *ws = onion_websocket_new(req, res); length = websocket_forge_close_packet((char **)&buffer); websocket_data_buffer_write(req, (char *)buffer, length); onion_connection_status ret = onion_websocket_call(ws); FAIL_IF(ret != -2); websocket_data_buffer_read(req, (char *)&buffer2, 8); FAIL_IF_NOT(buffer2[0] == 0x88); FAIL_IF_NOT(buffer2[1] == 0x02); FAIL_IF_NOT(buffer2[2] == 0x03); FAIL_IF_NOT(buffer2[3] == 0xE8); onion_websocket_free(ws); onion_request_free(req); onion_free(o); END_LOCAL(); }
void t06_timeouts(){ INIT_LOCAL(); o=onion_new(O_POOL | O_DETACH_LISTEN); onion_set_timeout(o, 100); onion_set_root_handler(o, onion_handler_new((void*)wait_random, NULL, NULL)); onion_set_port(o, "8081"); onion_listen(o); sleep(1); int nthreads=10; pthread_t *thread=malloc(sizeof(pthread_t*)*nthreads); int i; for (i=0;i<nthreads;i++){ pthread_create(&thread[i], NULL, (void*)do_timeout_request, NULL); } for (i=0;i<nthreads;i++){ pthread_join(thread[i], NULL); } free(thread); onion_free(o); END_LOCAL(); }
void t02_full_cycle_http10(){ INIT_LOCAL(); onion *server=onion_new(0); onion_add_listen_point(server,NULL,NULL,onion_buffer_listen_point_new()); onion_request *request; char buffer[4096]; memset(buffer,0,sizeof(buffer)); request=onion_request_new(server->listen_points[0]); onion_response *response=onion_response_new(request); onion_response_set_length(response, 30); FAIL_IF_NOT_EQUAL(response->length,30); onion_response_write_headers(response); onion_response_write0(response,"123456789012345678901234567890"); onion_response_flush(response); FAIL_IF_NOT_EQUAL(response->sent_bytes,30); onion_response_free(response); strncpy(buffer,onion_buffer_listen_point_get_buffer_data(request),sizeof(buffer)); onion_request_free(request); onion_free(server); FAIL_IF_NOT_STRSTR(buffer, "HTTP/1.0 200 OK\r\n"); FAIL_IF_NOT_STRSTR(buffer, "Connection: Keep-Alive\r\n"); FAIL_IF_NOT_STRSTR(buffer, "Content-Length: 30\r\n"); FAIL_IF_NOT_STRSTR(buffer, "Server: libonion"); FAIL_IF_NOT_STRSTR(buffer, "coralbits"); FAIL_IF_NOT_STRSTR(buffer, "\r\n\r\n123456789012345678901234567890"); END_LOCAL(); }
int OnionServer::stop_server() { onion_listen_stop(m_ponion);//stop loop int i = pthread_join( m_pthread, NULL);//wait till loop ends onion_free(m_ponion); return i; }
void t05_server_timeout_threaded_ssl(){ INIT_LOCAL(); CURL *curl=prepare_curl("https://localhost:8081"); ONION_DEBUG("%s",__FUNCTION__); o=onion_new(O_THREADED | O_DETACH_LISTEN); onion_set_root_handler(o,onion_handler_new((void*)process_request,NULL,NULL)); FAIL_IF_NOT_EQUAL_INT(onion_set_certificate(o, O_SSL_CERTIFICATE_KEY, "mycert.pem", "mycert.pem"),0); onion_set_port(o,"8081"); onion_set_timeout(o,3000); onion_listen(o); sleep(1); int fd=connect_to("localhost","8081"); sleep(4); // Should have closed the connection int w=write(fd,"GET /\n\n",7); FAIL_IF_NOT_EQUAL_INT(w,7); char data[256]; FAIL_IF(read(fd, data,sizeof(data))>0); close(fd); FAIL_IF_NOT(curl_get(curl, "https://localhost:8081")); onion_free(o); curl_easy_cleanup(curl); END_LOCAL(); }
int main() { request_list_t request_list; request_list.reqres = onion_ptr_list_new(); request_list.running = true; pthread_mutex_init(&request_list.lock, NULL); pthread_t long_process_thread; o = onion_new(O_POOL); pthread_create(&long_process_thread, NULL, (void *)&long_process, &request_list); onion_set_root_handler(o, onion_handler_new(&handler, &request_list, NULL)); ONION_INFO("Listening at http://localhost:8080"); onion_listen(o); // Close request_list.running = false; onion_ptr_list_foreach(request_list.reqres, (void *)free_reqres); onion_ptr_list_free(request_list.reqres); pthread_join(long_process_thread, NULL); onion_free(o); return 0; }
void t01_handle_static_request(){ INIT_LOCAL(); char ok; onion *server=onion_new(0); onion_listen_point *lp=onion_buffer_listen_point_new(); onion_add_listen_point(server, NULL, NULL, lp); onion_request *request=onion_request_new(lp); FILL(request,"GET / HTTP/1.1\n"); onion_handler *handler=onion_handler_static("Not ready",302); FAIL_IF_NOT(handler); onion_set_root_handler(server, handler); onion_response *response=onion_response_new(request); ok=onion_handler_handle(handler, request, response); FAIL_IF_NOT_EQUAL(ok, OCS_PROCESSED); onion_response_free(response); const char *buffer=onion_buffer_listen_point_get_buffer_data(request); FAIL_IF_EQUAL_STR(buffer,""); FAIL_IF_NOT_STRSTR(buffer, "HTTP/1.1 302 REDIRECT\r\n"); FAIL_IF_NOT_STRSTR(buffer, "\r\nContent-Length: 9\r\n"); FAIL_IF_NOT_STRSTR(buffer, "libonion"); FAIL_IF_STRSTR(buffer, "License: AGPL"); // License is now LGPL, no need to advertise FAIL_IF_STRSTR(buffer, "License"); FAIL_IF_NOT_STRSTR(buffer, "\r\n\r\nNot ready"); onion_request_free(request); onion_free(server); END_LOCAL(); }
void t02_server_full(){ 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_set_root_handler(server, onion_handler_static("Succedded", 200)); onion_request *req=onion_request_new(lp); #define S "GET / HTTP/1.1\r\nHeader-1: This is header1\r\nHeader-2: This is header 2\r\n" 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. buffer=onion_buffer_listen_point_get_buffer_data(req); FAIL_IF_EQUAL_STR(buffer,""); FAIL_IF_NOT_STRSTR(buffer, "HTTP/1.1 200 OK\r\n"); FAIL_IF_NOT_STRSTR(buffer, "\r\nContent-Length: 9\r\n"); FAIL_IF_NOT_STRSTR(buffer, "libonion"); FAIL_IF_NOT_STRSTR(buffer, "\r\n\r\nSuccedded"); onion_request_free(req); onion_free(server); END_LOCAL(); }
void t02_long_template(){ INIT_LOCAL(); int count=0; onion *s=onion_new(0); onion_set_root_handler(s, onion_handler_new((void*)AGPL_txt_handler_page, NULL, NULL)); onion_listen_point *lp=onion_buffer_listen_point_new(); onion_add_listen_point(s,NULL,NULL,lp); lp->write=count_bytes; onion_request *req=onion_request_new(lp); req->connection.listen_point->close(req); req->connection.user_data=&count; req->connection.listen_point->close=NULL; FAIL_IF_NOT_EQUAL_INT(onion_request_write0(req, "GET /\n\n"), OCS_REQUEST_READY); FAIL_IF_NOT_EQUAL_INT(onion_request_process(req), OCS_CLOSE_CONNECTION); FAIL_IF(count<30000); onion_request_free(req); onion_free(s); END_LOCAL(); }
void t01_server_min(){ 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_set_root_handler(server, onion_handler_static("Succedded", 200)); onion_request *req=onion_request_new(lp); onion_request_write(req, "GET ",4); onion_request_write(req, "/",1); onion_request_write(req, " HTTP/1.1\r\n",11); onion_request_write(req, "\r\n",2); const char *buffer=onion_buffer_listen_point_get_buffer_data(req); FAIL_IF_EQUAL_STR(buffer,""); FAIL_IF_NOT_STRSTR(buffer, "HTTP/1.1 200 OK\r\n"); FAIL_IF_NOT_STRSTR(buffer, "\r\nContent-Length: 9\r\n"); FAIL_IF_NOT_STRSTR(buffer, "libonion"); FAIL_IF_NOT_STRSTR(buffer, "\r\n\r\nSuccedded"); onion_request_free(req); onion_free(server); END_LOCAL(); }
void t08_server_with_error_404(){ 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_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 404 NOT FOUND\r\n"); FAIL_IF_NOT_STRSTR(buffer, "libonion"); onion_request_free(req); onion_free(server); END_LOCAL(); }
void t04_server_overflow(){ 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_set_root_handler(server, onion_handler_static("Succedded", 200)); onion_block *long_req=onion_block_new(); onion_block_add_str(long_req,"GET / HTTP/1.1\n"); int i; for(i=0;i<1000;i++){ onion_block_add_str(long_req,"Header-1: This is header1 Header-2: This is header 2 "); } onion_request *req=onion_request_new(lp); onion_request_write(req, onion_block_data(long_req),onion_block_size(long_req)-1); // send it all, but the final 0. const char *buffer=onion_buffer_listen_point_get_buffer_data(req); FAIL_IF_NOT_EQUAL_STR(buffer,""); onion_request_write(req, "\n\n",2); // finish this request. no \n\n before to check possible bugs. buffer=onion_buffer_listen_point_get_buffer_data(req); FAIL_IF_EQUAL_STR(buffer,""); FAIL_IF_NOT_STRSTR(buffer, "HTTP/1.1 200 OK\r\n"); FAIL_IF_NOT_STRSTR(buffer, "\r\nContent-Length: 9\r\n"); FAIL_IF_NOT_STRSTR(buffer, "libonion"); FAIL_IF_NOT_STRSTR(buffer, "\r\n\r\nSuccedded"); onion_block_free(long_req); onion_request_free(req); onion_free(server); END_LOCAL(); }
void t03_websocket_server_receive_small_packet() { INIT_LOCAL(); int length = 0; char *buffer = NULL, buffer2[115]; memset(&ws_status, 0, sizeof(ws_status)); onion *o = websocket_server_new(); onion_request *req = websocket_start_handshake(o); req->connection.listen_point->read = (lpreader_sig_t *) websocket_data_buffer_read; onion_response *res = onion_response_new(req); onion_websocket *ws = onion_websocket_new(req, res); length = websocket_forge_small_packet((char **)&buffer); websocket_data_buffer_write(req, buffer, length); onion_websocket_read(ws, (char *)&buffer2, 120); buffer2[114] = '\0'; FAIL_IF_NOT_EQUAL_STR(buffer2, "Some UTF-8-encoded chars which will be cut at the 117th char so I write some gap-filling text with no meaning unti"); onion_websocket_free(ws); onion_request_free(req); onion_free(o); END_LOCAL(); }
int main(int argc, char *argv[]) { printf("Datagram hash client example\n\n"); //Check if arguments of sufficient size if (argc != 4) { fprintf(stderr,"Usage: udpClient serverName serverPort ownPort\n"); exit(EXIT_FAILURE); } //build sockets buildSocket(argv); //Init favicon initializeFavicon(argv); //TODO: MAIN LOOP onion_listen(o); onion_free(o); //Close socket close(sockfd); return EXIT_SUCCESS; }
void free_onion(int unused){ static int already_closing=0; if (!already_closing){ ONION_INFO("Closing connections."); onion_free(o); already_closing=1; } exit(0); }
void t02_handle_generic_request(){ 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 *url=onion_url_new(); int error; error=onion_url_add_handler(url, "^$", onion_handler_static("Not ready",302)); FAIL_IF(error); error=onion_url_add_handler(url, "^a.*$", onion_handler_static("any",200)); FAIL_IF(error); error=onion_url_add_static(url, "^.*", "Internal error", 500); FAIL_IF(error); onion_set_root_handler(server, onion_url_to_handler(url)); onion_request *request; request=onion_request_new(lp); FILL(request,"GET / HTTP/1.1\n"); onion_request_process(request); const char *buffer=onion_buffer_listen_point_get_buffer_data(request); FAIL_IF_NOT_STRSTR(buffer, "HTTP/1.1 302 REDIRECT\r\n"); FAIL_IF_NOT_STRSTR(buffer, "Content-Length: 9\r\n"); FAIL_IF_NOT_STRSTR(buffer, "\r\n\r\nNot ready"); onion_request_free(request); // gives error, as such url does not exist. request=onion_request_new(lp); FILL(request,"GET /error HTTP/1.1\n"); onion_request_process(request); buffer=onion_buffer_listen_point_get_buffer_data(request); ONION_DEBUG("<%s>", buffer); FAIL_IF_NOT_STRSTR(buffer, "HTTP/1.1 500 INTERNAL ERROR\r\n"); FAIL_IF_NOT_STRSTR(buffer, "Content-Length: 14\r\n"); FAIL_IF_NOT_STRSTR(buffer, "\r\n\r\nInternal error"); onion_request_free(request); request=onion_request_new(lp); FILL(request,"GET /any HTTP/1.1\n"); onion_request_process(request); buffer=onion_buffer_listen_point_get_buffer_data(request); FAIL_IF_NOT_STRSTR(buffer, "HTTP/1.1 200 OK\r\n"); FAIL_IF_NOT_STRSTR(buffer, "Content-Length: 3\r\n"); FAIL_IF_NOT_STRSTR(buffer, "\r\n\r\nany"); onion_request_free(request); onion_free(server); 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; }
void t02_server_epoll(){ INIT_LOCAL(); o=onion_new(O_POLL); onion_set_root_handler(o,onion_handler_new((void*)process_request,NULL,NULL)); do_petition_set(1,0.1,1,1); onion_free(o); o=onion_new(O_POLL); onion_set_root_handler(o,onion_handler_new((void*)process_request,NULL,NULL)); do_petition_set(1,0.001,100,1); onion_free(o); o=onion_new(O_POOL); onion_set_root_handler(o,onion_handler_new((void*)process_request,NULL,NULL)); do_petition_set(1,0.001,100,1); onion_free(o); o=onion_new(O_POOL); onion_set_root_handler(o,onion_handler_new((void*)process_request,NULL,NULL)); do_petition_set_threaded(1,0.001,100,2,10); onion_free(o); o=onion_new(O_POOL); onion_set_root_handler(o,onion_handler_new((void*)process_request,NULL,NULL)); do_petition_set_threaded(1,0.001,100,2,15); onion_free(o); o=onion_new(O_POOL); onion_set_root_handler(o,onion_handler_new((void*)process_request,NULL,NULL)); do_petition_set_threaded(1,0.001,100,2,100); onion_free(o); END_LOCAL(); }
/** * @short In a simple loop asks the user for the answer of the query. */ int main(int argc, char **argv){ onion *server; server=onion_new(O_ONE_LOOP); onion_set_hostname(server, "0.0.0.0"); onion_set_root_handler(server, onion_handler_new((void*)ask_handler, NULL, NULL)); onion_listen(server); onion_free(server); 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 trivia(void) { printf("creating web-server\n"); o=onion_new(O_ONE_LOOP); printf("obtaining root url\n"); onion_url *urls=onion_root_url(o); printf("adding css skeleton\n"); if(add_css_page(urls)) { printf("error: add_questions(): %s\n", strerror(errno)); exit(1); } printf("adding questions\n"); if(add_questions(urls)) { printf("error: add_questions(): %s\n", strerror(errno)); exit(1); } printf("adding landing page\n"); if(add_start_page(urls)) { printf("error: add_landing_page(): %s\n", strerror(errno)); exit(1); } printf("adding done page\n"); if(add_done_page(urls)) { printf("error: add_done_page(): %s\n", strerror(errno)); exit(1); } printf("adding reset page\n"); if(add_reset_page(urls)) { printf("error: add_landing_page(): %s\n", strerror(errno)); exit(1); } printf("adding signal handlers\n"); signal(SIGTERM, onexit); signal(SIGINT, onexit); printf("adding listening point\n"); onion_add_listen_point(o, NULL, "80", onion_http_new()); printf("listening\n"); onion_listen(o); printf("freeing\n"); onion_free(o); return 0; }
void t01_websocket_server_no_ws() { INIT_LOCAL(); memset(&ws_status, 0, sizeof(ws_status)); onion *o = websocket_server_new(); onion_request *req = onion_request_new(onion_get_listen_point(o, 0)); onion_request_write0(req, "GET /\n\n"); onion_request_process(req); FAIL_IF(ws_status.is_connected); FAIL_IF_NOT_EQUAL_INT(ws_status.connected, 1); onion_request_free(req); onion_free(o); END_LOCAL(); }
void t02_post_a_lot() { INIT_LOCAL(); onion *o = onion_new(O_ONE); onion_set_root_handler(o, onion_handler_new((void *)return_length, NULL, NULL)); pthread_t pt; pthread_create(&pt, NULL, (void *)POST_a_lot, NULL); onion_listen(o); pthread_join(pt, NULL); onion_free(o); END_LOCAL(); }
void t02_websocket_server_w_ws() { INIT_LOCAL(); memset(&ws_status, 0, sizeof(ws_status)); onion *o = websocket_server_new(); onion_request *req = onion_request_new(onion_get_listen_point(o, 0)); onion_request_write0(req, "GET /\nUpgrade: websocket\nSec-Websocket-Version: 13\nSec-Websocket-Key: My-key\n\n"); onion_request_process(req); FAIL_IF_NOT(ws_status.is_connected); FAIL_IF_NOT_EQUAL_INT(ws_status.connected, 1); onion_request_free(req); onion_free(o); END_LOCAL(); }