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(); }
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(); }
int login_cf(void) { char post_str[BUFSIZE]; char csrf[BUFSIZE]; char tta[BUFSIZE]; char filename[BUFSIZE]; char url[] = "http://codeforces.com/enter"; char *html = (char *)malloc(BUFSIZE * BUFSIZE); if (html == NULL) { write_log("alloc login_cf html buf memory error.\n"); return -1; } if (get_csrf(url, csrf) < 0) { write_log("login_cf get csrf error.\n"); free(html); return -1; } // important cleanup_curl(); curl = prepare_curl(); // 设置提交地址 curl_easy_setopt(curl, CURLOPT_REFERER, url); curl_easy_setopt(curl, CURLOPT_URL, url); // 设置参数 sprintf(filename, "%dlogin.txt", solution->solution_id); memset(tta, 0, sizeof(tta)); if (get_tta(tta) < 0) { free(html); write_log("login_cf get tta error.\n"); return -1; } sprintf(post_str, "csrf_token=%s&action=enter&handle=%s&password=%s" "&_tta=%s", csrf, vjudge_user, vjudge_passwd, tta); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_str); if (DEBUG) { write_log("perform url is %s.\n", url); write_log("post data is %s.\n", post_str); } perform_curl(filename); load_file(filename, html); if (strstr(html, "Codeforces is temporary unavailable") != NULL || strstr(html, "Invalid handle or password") != NULL || strstr(html, "Fill in the form to login into Codeforces.") != NULL) { write_log("login_cf remote server error.\n"); free(html); return -1; } free(html); return 0; }
void t03_server_https(){ INIT_LOCAL(); CURL *curl=prepare_curl("https://localhost:8080"); o=onion_new(O_ONE_LOOP | 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); FAIL_IF_NOT_EQUAL_INT(onion_listen(o),0); //do_petition_set(1,1,1,1); sleep(1); //FAIL_IF_EQUAL_INT( curl_get_to_fail("http://localhost:8080"), HTTP_OK); sleep(1); FAIL_IF_NOT_EQUAL_INT( curl_get(curl, "https://localhost:8080"), HTTP_OK); sleep(1); onion_free(o); curl_easy_cleanup(curl); END_LOCAL(); }
void *do_requests(params_t *t){ const char *url="http://localhost:8080/"; CURL *curl=prepare_curl(url); ONION_DEBUG("Do %d petitions",t->n_requests); int i; usleep(t->wait_s*1000000); for(i=0;i<t->n_requests;i++){ curl_get(curl, url); usleep(t->wait_t*1000000); } if (t->close_at_n==1){ usleep(t->wait_s*1000000); onion_listen_stop(o); } curl_easy_cleanup(curl); return NULL; }
/* * Usage: ./get_problem oj_name from_problem_id(or index) to_problem_id(or index) [debug] */ int main(int argc, char *argv[]) { // read config file init_conf(); if (argc < 4 || argc > 5) { fprintf(stderr, "Usage: %s oj_name from_problem_id(or index) " "to_problem_id(or indx) [debug]\n", argv[0]); fprintf(stderr, "Support oj is: "); int i = 0; for (i = 0; i < oj_cnt; ++i) { fprintf(stderr, "%s%c", oj_str[i], (i == oj_cnt - 1) ? '\n' : ' '); } exit(EXIT_SUCCESS); } DEBUG = (argc == 5); int i = 0; int from = atoi(argv[2]); int to = atoi(argv[3]); strcpy(oj_name, argv[1]); oj_type = -1; for (i = 0; i < oj_cnt; ++i) { if (strcmp(oj_name, oj_str[i]) == 0) { oj_type = i; break; } } if (oj_type < 0) { write_log("%s: unsupported oj\n", oj_name); exit(EXIT_FAILURE); } write_log("ojname = %s\n", oj_name); write_log("ojurl = %s\n", oj_url[oj_type]); write_log("type = %d\n", oj_type); write_log("from = %d\n", from); write_log("to = %d\n", to); curl = prepare_curl(); if (curl == NULL) { write_log("prepare curl handle error.\n"); exit(EXIT_FAILURE); } conn = prepare_mysql(); if (conn == NULL) { write_log("prepare mysql handle error.\n"); cleanup_curl(); exit(EXIT_FAILURE); } problem_info = (struct problem_info_t *)malloc(sizeof(struct problem_info_t)); if (problem_info == NULL) { write_log("alloc problem_info memory error.\n"); cleanup_mysql(); cleanup_curl(); exit(EXIT_FAILURE); } if (oj_type == 2) { // codeforces if (get_cf_problem_id() < 0) { write_log("get codeforces problem id error.\n"); exit(EXIT_FAILURE); } if (to > cf_pid_len - 1) { to = cf_pid_len - 1; write_log("index is so large. set it to max index %d.\n", cf_pid_len - 1); } } for (pid = from; pid <= to; ++pid) { memset(problem_info, 0, sizeof(struct problem_info_t)); int ret = get_problem(); if (ret < 0) { write_log("get %s problem %d error.\n", oj_name, pid); } else if (ret == 0) { write_log("get %s problem %d success.\n", oj_name, pid); ret = add_problem(); if (ret < 0) { write_log("add %s problem %d to mysql database error.\n", oj_name, pid); } else { if (!ret) { write_log("add %s problem %d to mysql database success.\n", oj_name, pid); } else { write_log("%s problem %d already exists, update local problem %d.\n", oj_name, pid, ret); } } } else { write_log("%s no problem %d.\n", oj_name, pid); } if (!DEBUG) { execute_cmd("rm -f %d", pid); } if (pid != to) { write_log("get next problem after %d seconds.\n", sleep_time); } sleep(sleep_time); } free(problem_info); cleanup_curl(); cleanup_mysql(); return 0; }