void send_poll_request(State * state, unsigned long id) { pthread_mutex_lock(&state->mState); state->message = perform_curl(state->message, POLL_CALL, PORT, id, NULL); if (!state->message->data) { printf("Poll request got null response\n"); pthread_mutex_unlock(&state->mState); return; } struct json_object * poll_data = json_tokener_parse(state->message->data); if (!poll_data) { printf("The response string could not be parsed: %s\n", state->message->data); pthread_mutex_unlock(&state->mState); return; } pthread_mutex_unlock(&state->mState); int i; struct json_object * price_array; if (json_object_object_get_ex(poll_data, "prices", &price_array) && json_object_get_array(price_array)) { clear_state_changed(state); for (i = 0; i < json_object_array_length(price_array); ++i) { struct json_object * instrument = json_object_array_get_idx(price_array, i); struct json_object * name_obj; if (json_object_object_get_ex(instrument, "instrument", &name_obj)) { setup_instrument(state, json_object_get_string(name_obj), instrument); } } mark_ready(state); } json_object_put(poll_data); }
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; }
int login_hduoj(void) { char url[] = "http://acm.hdu.edu.cn/userloginex.php?action=login"; // 设置提交地址 curl_easy_setopt(curl, CURLOPT_URL, url); // 设置参数 char post_str[BUFSIZE]; char filename[BUFSIZE]; char *html = (char *)malloc(BUFSIZE * BUFSIZE); if (html == NULL) { write_log("alloc login_hduoj html buf memory error.\n"); return -1; } sprintf(filename, "%dlogin.txt", solution->solution_id); sprintf(post_str, "username=%s&userpass=%s", vjudge_user, vjudge_passwd); 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); // modified form bnuoj if (strstr(html, "No such user or wrong password.") != NULL || strstr(html, "<b>One or more following ERROR(s) occurred.") != NULL || strstr(html, "<h2>The requested URL could not be retrieved</h2>") != NULL || strstr(html, "<H1 style=\"COLOR: #1A5CC8\" align=center>Sign In Your Account</H1>") != NULL || strstr(html, "PHP: Maximum execution time of") != NULL) { write_log("login_hduoj remote server error.\n"); free(html); return -1; } free(html); return 0; }
//----------------------"MAIN"----------------- pthread_t setup_state_and_poll_thread(State * state, int argc, char ** argv) { if (!state) return 0; unsigned long id = ID; if (id) { setup_state(state, argc, argv, NULL); } else { struct json_object * request_config = create_json_request(argc, argv); struct String * message = perform_curl(NULL, POLL_CALL, PORT, 0, request_config); json_object_put(request_config); setup_state(state, argc, argv, message); if (!message) { printf("Could not obtain message, exiting\n"); return 0; } id = parse_setup_response(message->data); pthread_mutex_init(&mID, NULL); setID(id); } pthread_t thread = 0; if (!id) { printf("No ID was retrieved from the response\n"); } else { if (pthread_create(&thread, NULL, poll_t, state)) { thread = 0; } } return thread; }
int get_status_cf(void) { char url[BUFSIZE]; sprintf(url, "http://codeforces.com/api/contest.status?contestId=%d&from=1&count=1&handle=%s", solution->problem_info.origin_id / 10, vjudge_user); if (DEBUG) { write_log("perform url is %s.\n", url); } // 设置提交地址 curl_easy_setopt(curl, CURLOPT_URL, url); char filename[BUFSIZE]; sprintf(filename, "%dstatus.txt", solution->solution_id); time_t begin_time = time(NULL); int rid = 0; int usedtime = 0; int memory = 0; char result[BUFSIZE]; while (1) { if (time(NULL) - begin_time > vj_max_wait_time) { write_log("judge time out.\n"); return OJ_JE; } perform_curl(filename); json_object *obj = json_object_from_file(filename); if (obj == NULL) { write_log("load json object from file %s error.\n", filename); return OJ_JE; } json_object *submission; if (strcmp(json_get_str(obj, "status"), "OK") != 0) { json_object_put(obj); return OJ_JE; } else { submission = json_get_obj(obj, "result[0]"); } if (submission == NULL) { write_log("get status error, maybe not submit.\n"); json_object_put(obj); return OJ_JE; } rid = json_get_int(submission, "id"); strcpy(result, json_get_str(submission, "verdict")); usedtime = json_get_int(submission, "timeConsumedMillis"); memory = json_get_int(submission, "memoryConsumedBytes"); memory = memory / 1024 + ((memory % 1024) ? 1: 0); write_log("rid = %d\n", rid); write_log("usedtime = %d\n", usedtime); write_log("memory = %d\n", memory); write_log("result = %s\n", result); if (is_final_result(result)) { solution->remote_rid = rid; solution->time = usedtime; solution->memory = memory; int ret = convert_result(result); if (ret == OJ_RE) { strcpy(solution->runtimeinfo, result); } write_log("get solution %d status over" ": %d.\n", solution->solution_id, ret); json_object_put(obj); return ret; } } return OJ_JE; }
// modified from bnuoj int get_csrf(const char *url, char *csrf) { char err[BUFSIZE]; char filename[] = "csrf"; char *html = (char *)malloc(BUFSIZE * BUFSIZE); if (html == NULL) { write_log("alloc get_csrf html buf memory error.\n"); return -1; } // 设置提交地址 curl_easy_setopt(curl, CURLOPT_URL, url); if (DEBUG) { write_log("perform url is %s.\n", url); } perform_curl(filename); load_file(filename, html); if (strstr(html, "Codeforces is temporary unavailable") != NULL) { write_log("get_csrf remote server error.\n"); free(html); return -1; } int status, i, j; regmatch_t pmatch[2]; const int nmatch = 2; regex_t reg; const char *pattern = "<meta name=\"X-Csrf-Token\" " "content=\"([^/]*)\"/>"; char match_str[BUFSIZE]; int ret = regcomp(®, pattern, REG_EXTENDED); if (ret) { regerror(ret, ®, err, BUFSIZE); free(html); write_log("compile regex error: %s.\n", err); return -1; } status = regexec(®, html, nmatch, pmatch, 0); if (status == REG_NOMATCH) { write_log("no match regex: %s.\n", pattern); write_log("get %s csrf error.\n", url); free(html); regfree(®); return -1; } else if (status == 0) { char buf[BUFSIZE]; for (i = 0; i < nmatch; ++i) { int cnt = 0; for (j = pmatch[i].rm_so; j < pmatch[i].rm_eo; ++j) { buf[cnt++] = html[j]; } buf[cnt] = '\0'; switch (i) { case 0: strcpy(match_str, buf); break; case 1: strcpy(csrf, buf); break; } } write_log("match_str = %s\n", match_str); write_log("csrf = %s\n", csrf); } free(html); regfree(®); return 0; }
// modified from bnuoj int get_ceinfo_cf(void) { int status, i; regmatch_t pmatch[2]; const int nmatch = 2; regex_t reg; const char *pattern = "\"(.*)\""; char url[BUFSIZE]; char csrf[BUFSIZE]; char post_str[BUFSIZE]; sprintf(url, "http://codeforces.com/problemset/submit"); if (get_csrf(url, csrf) < 0) { write_log("get_ceinfo_cf get csrf error.\n"); return -1; } sprintf(url, "http://codeforces.com/data/judgeProtocol"); // 设置提交地址 curl_easy_setopt(curl, CURLOPT_URL, url); sprintf(post_str, "submissionId=%d&csrf_token=%s", solution->remote_rid, csrf); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_str); char err[BUFSIZE]; int ret = regcomp(®, pattern, REG_EXTENDED); if (ret) { regerror(ret, ®, err, BUFSIZE); write_log("compile regex error: %s.\n", err); return -1; } char *html = (char *)malloc(BUFSIZE * BUFSIZE); if (html == NULL) { write_log("alloc get_ceinfo_cf html buf memory error.\n"); regfree(®); return -1; } perform_curl(cefname); load_file(cefname, html); if (strstr(html, "Codeforces is temporary unavailable") != NULL) { write_log("get_ceinfo_cf remote server error.\n"); write_log("get solution %d compile error info error.\n", solution->solution_id); free(html); regfree(®); return -1; } else { status = regexec(®, html, nmatch, pmatch, 0); if (status == REG_NOMATCH) { write_log("get_ceinfo_cf regex no match.\n", solution->solution_id); write_log("get solution %d compile error info error.\n", solution->solution_id); regfree(®); free(html); return -1; } else if (status == 0) { int cnt = 0; for (i = pmatch[1].rm_so; i < pmatch[1].rm_eo; ++i) { solution->compileinfo[cnt++] = html[i]; } solution->compileinfo[cnt] = '\0'; write_log("match_str = %s\n", solution->compileinfo); save_file(cefname, solution->compileinfo); } } write_log("get compile error info: %s.\n", solution->compileinfo); free(html); regfree(®); return 0; }
// modified from bnuoj int submit_cf(void) { char url[BUFSIZE] = "http://codeforces.com/problemset/submit"; char csrf[BUFSIZE]; char buf[BUFSIZE]; if (get_csrf(url, csrf) < 0) { write_log("submit_cf get csrf error.\n"); return -1; } // add random extra spaces in the end to avoid same code error srand(time(NULL)); int len = strlen(solution->src); solution->src[len] = '\n'; while (rand() % 120) { solution->src[len++] = ' '; } solution->src[len] = '\0'; // copy from bnuoj // prepare form for post struct curl_httppost *formpost = NULL; struct curl_httppost *lastptr = NULL; curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "action", CURLFORM_COPYCONTENTS, "submitSolutionFormSubmitted", CURLFORM_END); sprintf(buf, "%d%c", solution->problem_info.origin_id / 10, solution->problem_info.origin_id % 10 + 'A'); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submittedProblemCode", CURLFORM_COPYCONTENTS, buf, CURLFORM_END); sprintf(buf, "%d", lang_table[solution->problem_info.ojtype][solution->language]); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "programTypeId", CURLFORM_COPYCONTENTS, buf, CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "source", CURLFORM_COPYCONTENTS, solution->src, CURLFORM_END); curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "sourceCodeConfirmed", CURLFORM_COPYCONTENTS, "true", CURLFORM_END); memset(buf, 0, sizeof(buf)); if (get_tta(buf) < 0) { write_log("submit_cf get tta error.\n"); curl_formfree(formpost); return -1; } curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "_tta", CURLFORM_COPYCONTENTS, buf, CURLFORM_END); sprintf(url, "http://codeforces.com/problemset/submit?csrf_token=%s", csrf); curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); char filename[BUFSIZE]; sprintf(filename, "%dsubmit.txt", solution->solution_id); if (DEBUG) { write_log("perform url is %s.\n", url); } perform_curl(filename); curl_formfree(formpost); char *html = (char *)malloc(BUFSIZE * BUFSIZE); if (html == NULL) { write_log("alloc submit_cf html buf memory error.\n"); return -1; } load_file(filename, html); // modified form bnuoj if (strstr(html, "You have submitted exactly the same code before") != NULL || strstr(html, "Choose valid language") != NULL || strstr(html, "<span class=\"error for__source\">") != NULL || strstr(html, "<a href=\"/enter\">Enter</a>") != NULL) { write_log("submit_cf remote server error.\n"); free(html); return -1; } free(html); return 0; }
int submit_hduoj(void) { char *post_str = (char *)malloc(BUFSIZE * BUFSIZE); if (post_str == NULL) { write_log("alloc submit_hduoj post_str buf memory error.\n"); return -1; } char url[] = "http://acm.hdu.edu.cn/submit.php?action=submit"; memset(post_str, 0, BUFSIZE * BUFSIZE); sprintf(post_str, "check=0&problemid=%d&language=%d&usercode=", solution->problem_info.origin_id, lang_table[solution->problem_info.ojtype][solution->language]); // 对其进行url编码 utf2gbk(solution->src, solution->code_length); char *str = url_encode(solution->src); if (str == NULL) { write_log("encode url error.\n"); free(str); free(post_str); return -1; } strcat(post_str, str); if (DEBUG) { write_log("perform url is %s.\n", url); write_log("post data is %s.\n", post_str); } // 设置提交地址 curl_easy_setopt(curl, CURLOPT_URL, url); // 设置参数 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_str); char filename[BUFSIZE]; sprintf(filename, "%dsubmit.txt", solution->solution_id); perform_curl(filename); char *html = (char *)malloc(BUFSIZE * BUFSIZE); if (html == NULL) { write_log("alloc submit_hduoj html buf memory error.\n"); free(str); free(post_str); return -1; } load_file(filename, html); // modified form bnuoj if (strstr(html, "Connect(0) to MySQL Server failed.") != NULL || strstr(html, "<b>One or more following ERROR(s) occurred.") != NULL || strstr(html, "<h2>The requested URL could not be retrieved</h2>") != NULL || strstr(html, "<H1 style=\"COLOR: #1A5CC8\" align=center>Sign In Your Account</H1>") != NULL || strstr(html, "PHP: Maximum execution time of") != NULL || strstr(html, "<DIV>Exercise Is Closed Now!</DIV>") != NULL) { write_log("submit_hduoj remote server error.\n"); free(str); free(html); free(post_str); return -1; } free(str); free(html); free(post_str); return 0; }
int get_status_hduoj(void) { int status, i, j; regmatch_t pmatch[5]; const int nmatch = 5; regex_t reg; const char *pattern = "<td height=22px>([0-9]*)</td>" "<td>[: 0-9-]*</td>" "<td><font color=[ a-zA-Z]*>([^/]*)</font></td>" "<td><a[ 0-9a-zA-Z\\./\\?\\\"=]*>[0-9]*</a></td>" "<td>([0-9]*)MS</td>" "<td>([0-9]*)K</td>" ; char url[BUFSIZE]; sprintf(url, "http://acm.hdu.edu.cn/status.php?first=&pid=%d&user=%s" "&lang=0&status=0", solution->problem_info.origin_id, vjudge_user); if (DEBUG) { write_log("perform url is %s.\n", url); } // 设置提交地址 curl_easy_setopt(curl, CURLOPT_URL, url); char filename[BUFSIZE]; sprintf(filename, "%dstatus.txt", solution->solution_id); time_t begin_time = time(NULL); int rid = 0; int usedtime = 0; int memory = 0; char result[BUFSIZE]; char err[BUFSIZE]; char match_str[BUFSIZE]; int ret = regcomp(®, pattern, REG_EXTENDED); if (ret) { regerror(ret, ®, err, BUFSIZE); write_log("compile regex error: %s.\n", err); return OJ_JE; } char *html = (char *)malloc(BUFSIZE * BUFSIZE); if (html == NULL) { write_log("alloc get_status_hduoj html buf memory error.\n"); regfree(®); return OJ_JE; } while (1) { if (time(NULL) - begin_time > vj_max_wait_time) { write_log("judge time out.\n"); free(html); return OJ_JE; } perform_curl(filename); load_file(filename, html); gbk2utf8(html, strlen(html)); // modified form bnuoj if (strstr(html, "Connect(0) to MySQL Server failed.") != NULL || strstr(html, "<b>One or more following ERROR(s) occurred.") != NULL || strstr(html, "<h2>The requested URL could not be retrieved</h2>") != NULL || strstr(html, "<H1 style=\"COLOR: #1A5CC8\" align=center>Sign In Your Account</H1>") != NULL || strstr(html, "PHP: Maximum execution time of") != NULL || strstr(html, "<DIV>Exercise Is Closed Now!</DIV>") != NULL) { write_log("get_status_hduoj remote server error.\n"); write_log("get solution %d status error.\n", solution->solution_id); free(html); regfree(®); return OJ_JE; } else { status = regexec(®, html, nmatch, pmatch, 0); if (status == REG_NOMATCH) { write_log("no match regex: %s.\n", pattern); write_log("get solution %d status error.\n", solution->solution_id); free(html); regfree(®); return OJ_JE; } else if (status == 0) { char buf[BUFSIZE]; for (i = 0; i < nmatch; ++i) { int cnt = 0; for (j = pmatch[i].rm_so; j < pmatch[i].rm_eo; ++j) { buf[cnt++] = html[j]; } buf[cnt] = '\0'; switch (i) { case 0: strcpy(match_str, buf); break; case 1: rid = atoi(buf); break; case 2: strcpy(result, buf); break; case 3: usedtime = atoi(buf); break; case 4: memory = atoi(buf); break; } } write_log("match_str = %s\n", match_str); write_log("rid = %d\n", rid); write_log("usedtime = %d\n", usedtime); write_log("memory = %d\n", memory); write_log("result = %s\n", result); if (is_final_result(result)) { solution->remote_rid = rid; solution->time = usedtime; solution->memory = memory; int ret = convert_result(result); if (ret == OJ_RE) { strcpy(solution->runtimeinfo, result); } write_log("get solution %d status over" ": %d.\n", solution->solution_id, ret); free(html); regfree(®); return ret; } } } } free(html); regfree(®); return OJ_JE; }
int get_ceinfo_hduoj(void) { int status, i; regmatch_t pmatch[2]; const int nmatch = 2; regex_t reg; const char *pattern = "<pre>(.*)</pre>"; char url[BUFSIZE]; sprintf(url, "http://acm.hdu.edu.cn/viewerror.php?rid=%d", solution->remote_rid); if (DEBUG) { write_log("perform url is %s.\n", url); } // 设置提交地址 curl_easy_setopt(curl, CURLOPT_URL, url); char err[BUFSIZE]; int ret = regcomp(®, pattern, REG_EXTENDED); if (ret) { regerror(ret, ®, err, BUFSIZE); write_log("compile regex error: %s.\n", err); return -1; } char *html = (char *)malloc(BUFSIZE * BUFSIZE); if (html == NULL) { write_log("alloc get_ceinfo_hduoj html buf memory error.\n"); regfree(®); return -1; } perform_curl(cefname); load_file(cefname, html); gbk2utf8(html, strlen(html)); // modified form bnuoj if (strstr(html, "Connect(0) to MySQL Server failed.") != NULL || strstr(html, "<b>One or more following ERROR(s) occurred.") != NULL || strstr(html, "<h2>The requested URL could not be retrieved</h2>") != NULL || strstr(html, "<H1 style=\"COLOR: #1A5CC8\" align=center>Sign In Your Account</H1>") != NULL || strstr(html, "PHP: Maximum execution time of") != NULL || strstr(html, "<DIV>Exercise Is Closed Now!</DIV>") != NULL) { write_log("get_ceinfo_hduoj remote server error.\n"); write_log("get solution %d compile error info error.\n", solution->solution_id); free(html); regfree(®); return -1; } else { status = regexec(®, html, nmatch, pmatch, 0); if (status == REG_NOMATCH) { write_log("get_ceinfo_hduoj regex no match.\n", solution->solution_id); write_log("get solution %d compile error info error.\n", solution->solution_id); regfree(®); free(html); return -1; } else if (status == 0) { int cnt = 0; for (i = pmatch[1].rm_so; i < pmatch[1].rm_eo; ++i) { solution->compileinfo[cnt++] = html[i]; } solution->compileinfo[cnt] = '\0'; write_log("match_str = %s\n", solution->compileinfo); save_file(cefname, solution->compileinfo); } } write_log("get compile error info: %s.\n", solution->compileinfo); free(html); regfree(®); return 0; }
struct buffer buf; buffer_init(&buf); CURL *c = curl_easy_init(); curl_easy_setopt(c, CURLOPT_URL, String_val(sUrl)); curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, write_callback); curl_easy_setopt(c, CURLOPT_WRITEDATA, (void*)&buf); struct curl_slist *hdr_list = NULL; CAML_LIST_FOREACH(header, slHeaders) hdr_list = curl_slist_append(hdr_list, String_val(header)); } curl_easy_setopt(c, CURLOPT_HTTPHEADER, hdr_list); int httpCode; CURLcode curlCode = perform_curl(c, &httpCode); if (curlCode != 0) { free(buf.buf); caml_raise_with_arg("caml_http_exception", Val_int(curlCode)); } else { CAML_CREATE_TUPLE(ret, Val_int(httpCode), caml_copy_string(buf.buf)); free(buf.buf); CAMLreturn(ret); } } CAMLprim value caml_post(value sUrl, value slHeaders, value sBody) { struct buffer buf; buffer_init(&buf);