int _ortc_getBalancer(char* url, char** response){ CURL *curl; CURLcode res; long httpCode; char *temp, *cluster; ortc_RestString *s = (ortc_RestString *)malloc(sizeof(ortc_RestString)); if(s==NULL){ *response = strdup("malloc() failed!"); return -4; } curl = curl_easy_init(); if(!curl){ *response = strdup("Can not init curl!"); free(s); return -1; } if(_ortc_initRestString(s)<0){ *response = strdup("malloc() failed!"); free(s); return -4; } curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _ortc_writeRestString); curl_easy_setopt(curl, CURLOPT_WRITEDATA, s); res = curl_easy_perform(curl); if(res != CURLE_OK){ *response = strdup("Can not connect with balancer!"); curl_easy_cleanup(curl); free(s->ptr); free(s); return -2; } curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode); curl_easy_cleanup(curl); if(httpCode != 200){ *response = strdup(s->ptr); free(s->ptr); free(s); return -3; } temp = _ortc_remove(s->ptr, "var SOCKET_SERVER = \""); cluster = _ortc_remove(temp, "\";"); free(s->ptr); free(s); free(temp); *response = cluster; return 0; }
int _ortc_getBalancer(char* url, char* appKey, int verifyCA, char** response){ CURL *curl; CURLcode res; long httpCode; char *temp, *cluster; char uUrl[512]; ortc_RestString *s = (ortc_RestString *)malloc(sizeof(ortc_RestString)); if(s==NULL){ *response = strdup("malloc() failed!"); return -4; } curl = curl_easy_init(); if(!curl){ *response = strdup("Can not init curl!"); free(s); return -1; } if(_ortc_initRestString(s)<0){ *response = strdup("malloc() failed!"); free(s); return -4; } snprintf(uUrl, sizeof uUrl, "%s?appkey=%s", url, appKey); curl_easy_setopt(curl, CURLOPT_URL, uUrl); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _ortc_writeRestString); curl_easy_setopt(curl, CURLOPT_WRITEDATA, s); if(verifyCA==0) curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); res = curl_easy_perform(curl); if(res != CURLE_OK){ const char* curlErr = curl_easy_strerror(res); *response = (char*)malloc( strlen(curlErr) + strlen("Can not connect with balancer! ") + 1); sprintf(*response, "Can not connect with balancer! %s", curlErr); curl_easy_cleanup(curl); free(s->ptr); free(s); return -2; } curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode); curl_easy_cleanup(curl); if(httpCode != 200){ *response = strdup(s->ptr); free(s->ptr); free(s); return -3; } temp = _ortc_remove(s->ptr, "var SOCKET_SERVER = \""); cluster = _ortc_remove(temp, "\";"); free(s->ptr); free(s); free(temp); *response = cluster; return 0; }