-
Notifications
You must be signed in to change notification settings - Fork 5
/
balancer.cpp
executable file
·145 lines (120 loc) · 3.66 KB
/
balancer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "balancer.h"
#include "common.h"
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, (char*)"var SOCKET_SERVER = \"");
cluster = _ortc_remove(temp, (char*)"\";");
free(s->ptr);
free(s);
free(temp);
*response = cluster;
return 0;
}
int _ortc_parseEndPoint(char* url, char** host, int* port, int* useSSL, int isSSL){
char* temp;
char* sPort = NULL;
int idx = 0;
if (isSSL != 1) {
*useSSL = 0;
idx = 7;
} else {
*useSSL = 2;
idx = 8;
}
size_t len = strlen(url);
int idxOfColon = -1;
int index = idx;
int i = 0;
for(i = index; i < len; i++){
if(url[i]==':'){
idxOfColon = i;
}
}
if(idxOfColon == -1){
if (isSSL != 1) {
*port = 80;
} else {
*port = 443;
}
temp = (char*)malloc(len - idx + 1);
if(temp == NULL){
return -1;
}
memcpy(temp, &url[idx], len - idx);
temp[len - idx] = '\0';
*host = temp;
return 0;
} else {
temp = (char*)malloc(idxOfColon - idx + 1);
if(temp == NULL){
return -1;
}
memcpy(temp, &url[idx], idxOfColon - idx);
temp[idxOfColon - idx] = '\0';
*host = temp;
sPort = (char*)malloc(len - idxOfColon);
if(sPort == NULL){
free(temp);
return -1;
}
memcpy(sPort, &url[idxOfColon+1], len - idxOfColon);
sPort[len - idxOfColon -1] = '\0';
*port = atoi(sPort);
free(sPort);
return 0;
}
}
int _ortc_parseUrl(char* url, char** host, int* port, int* useSSL){
int ret = -1;
if(url[0]=='h' && url[1]=='t' && url[2]=='t' && url[3]=='p' && url[4]==':' && url[5]=='/' && url[6]=='/') { //url starts with http://
ret = _ortc_parseEndPoint(url, host, port, useSSL, 0);
}
if(url[0]=='h' && url[1]=='t' && url[2]=='t' && url[3]=='p' && url[4]=='s' && url[5]==':' && url[6]=='/' && url[7]=='/') { //url starts with https://
ret = _ortc_parseEndPoint(url, host, port, useSSL, 1);
}
return ret;
}