5 this->handlerList = handlerList;
6 listenSock =
new(std::nothrow) Socket();
7 if(!listenSock)lastErrNo = ENOMEM;
8 listenSock->ReuseAddr(status);
10 listenSock->bind(host, port, status);
13 listenSock->listen(status, 1);
30 char* AdminClient::urlEncode(
const char*url)
33 register int enc_len = 0;
34 encoded = (
char*)malloc( strlen(url) * 3 );
35 for(
unsigned int i = 0; i < strlen(url); i++)
37 if( url[i] < 45 || (url[i] == 47) || (url[i] > 57 && url[i] < 65) || (url[i] > 90 && url[i] < 95) || (url[i] == 96) || (url[i] > 122))
39 enc_len += sprintf((
char*)encoded + enc_len,
"%%%02X", url[i]);
43 encoded[enc_len++] = url[i];
46 encoded = (
char*)realloc(encoded, enc_len + 1);
51 void* AdminClient::run()
54 size_t requestLen = 0;
59 while(!listenSock->selectRead(status))
67 sock_fd_t sock = listenSock->accept(status);
70 clientSock =
new(std::nothrow) Socket(sock);
76 clientSock->setNonBlock(status);
77 request = readRequest(requestLen);
81 char *response = NULL;
82 httpParser =
new(std::nothrow)
HttpParser(request, requestLen);
91 unsigned int handlerId = atoi(command);
92 while(*command >=
'0' && *command <=
'9')command++;
98 for(
unsigned long i = 0; i < handlerList->getCount(); i++)
100 if((*handlerList)[i]->getId() == handlerId)
108 char *ret = (
char*)(*handlerList)[i]->handle(*command);
111 buf = urlEncode(
ES_OK);
112 response = (
char*)malloc(strlen(buf) + 50);
113 sprintf(response,
"n=0&e=%d&d=%s", E_OK, buf);
127 bool *ret = (
bool*)(*handlerList)[i]->handle(*command, p1);
130 buf = urlEncode(
ES_OK);
131 response = (
char*)malloc(strlen(buf) + 50);
132 sprintf(response,
"n=0&e=%d&d=%s", E_OK, buf);
140 response = (
char*)malloc(512);
146 if(*command ==
'c' || *command ==
'e')
151 bool *ret = (
bool*)(*handlerList)[i]->handle(*command, atoi(p1));
154 buf = urlEncode(
ES_OK);
155 response = (
char*)malloc(strlen(buf) + 50);
156 sprintf(response,
"n=0&e=%d&d=%s", E_OK, buf);
164 response = (
char*)malloc(512);
170 if(*command ==
'b' || *command ==
'd' || *command ==
'f' || *command ==
'g')
172 int *v1 = (
int*)(*handlerList)[i]->handle(*command);
175 buf = urlEncode(
ES_OK);
176 response = (
char*)malloc(strlen(buf) + 50);
177 sprintf(response,
"n=1&e=%d&d=%s&v1=%d", E_OK, buf, *v1);
184 char *title = httpParser->
getByName(
"p1");
186 char *body = httpParser->
getByName(
"p3");
187 char *topic = httpParser->
getByName(
"p4");
188 if(title && h && body && topic)
190 unsigned int *ret = (
unsigned int*)(*handlerList)[i]->handle(*command, atoi(topic), title, h, body);
191 buf = urlEncode(
ES_OK);
192 response = (
char*)malloc(strlen(buf) + 50 + 16 * 12);
193 unsigned int len = sprintf(response,
"n=19&e=%d&d=%s", E_OK, buf);
194 for(
int i = 0; i < 19; i++)
196 len += sprintf(response + len,
"&v%d=%u", i + 1, ret[i]);
202 if(*command ==
'j' || *command ==
'k')
204 char *content = httpParser->
getByName(
"p1");
207 char *ret = (
char*)(*handlerList)[i]->handle(*command, content);
208 buf = urlEncode(
ES_OK);
209 size_t rLen = (ret != NULL ? strlen(ret): 0);
210 response = (
char*)malloc(strlen(buf) + 50 + rLen);
211 unsigned int len = sprintf(response,
"n=1&e=%d&d=%s&v1=", E_OK, buf);
212 memcpy(response + len, ret, rLen);
213 response[len + rLen] =
'\0';
220 bool *ret = (
bool*)(*handlerList)[i]->handle(*command);
223 buf = urlEncode(
ES_OK);
224 response = (
char*)malloc(50);
225 sprintf(response,
"n=1&e=%d&d=%s&v1=%d", E_OK, buf, *ret ? 1:0);
232 char *ret = (
char*)(*handlerList)[i]->handle(*command);
235 buf = urlEncode(
ES_OK);
236 response = (
char*)malloc(50 + strlen(ret));
237 sprintf(response,
"n=1&e=%d&d=%s&v1=%s", E_OK, buf, ret);
250 unsigned long *ret = (
unsigned long*)(*handlerList)[i]->handle(*command);
253 buf = urlEncode(
ES_OK);
254 response = (
char*)malloc(strlen(buf) + 50);
255 sprintf(response,
"n=4&e=%d&d=%s&v1=%lu&v2=%lu&v3=%lu&v4=%lu", E_OK, buf, ret[0], ret[1], ret[2], ret[3]);
268 if(p1 && p2 && p3 && p4)
270 char *ret = (
char*)(*handlerList)[i]->handle(*command, (
unsigned long)atol(p1), (
unsigned long)atol(p2), (
unsigned long)atol(p3), (
unsigned long)atol(p4));
273 buf = urlEncode(
ES_OK);
274 response = (
char*)malloc(strlen(buf) + 30);
275 sprintf(response,
"n=0&e=%d&d=%s", E_OK, buf);
283 response = (
char*)malloc(512);
291 response = (
char*)malloc(512);
302 char *ret = (
char*)(*handlerList)[i]->handle(*command);
305 int len = *((
int*)ret);
306 buf = urlEncode(
ES_OK);
307 response = (
char*)malloc(strlen(buf) + 30 + len);
308 int pos = sprintf(response,
"n=1&e=%d&d=%s&v1=", E_OK, buf);
310 memcpy(response + pos, ret +
sizeof(
int), len);
311 response[pos + len] =
'\0';
319 char *value = httpParser->
getByName(
"p1");
320 int *ret = (
int*)(*handlerList)[i]->handle(*command, value);
323 buf = urlEncode(
ES_OK);
324 response = (
char*)malloc(strlen(buf) + 30);
325 sprintf(response,
"n=1&e=%d&d=%s&v1=%d", E_OK, buf, *ret);
330 else if (*command ==
'c')
332 char *value = httpParser->
getByName(
"p1");
335 int *ret = (
int*)(*handlerList)[i]->handle(*command, atoi(value));
338 buf = urlEncode(
ES_OK);
339 response = (
char*)malloc(strlen(buf) + 64);
340 sprintf(response,
"n=1&e=%d&d=%s&v1=%d", E_OK, buf, *ret);
346 else if(*command ==
'd')
348 char *value = httpParser->
getByName(
"p1");
351 char *ret = (
char*)(*handlerList)[i]->handle(*command, atoi(value));
354 buf = urlEncode(
ES_OK);
355 response = (
char*)malloc(strlen(buf) + 30 + strlen(ret));
356 sprintf(response,
"n=3&e=%d&d=%s&%s", E_OK, buf, ret);
362 else if(*command ==
'e')
364 char *value = httpParser->
getByName(
"p1");
367 bool *ret = (
bool*)(*handlerList)[i]->handle(*command, value);
370 buf = urlEncode(
ES_OK);
371 response = (
char*)malloc(strlen(buf) + 30 +
sizeof (u_int32_t));
372 sprintf(response,
"n=1&e=%d&d=%s&v1=%u", E_OK,
"", *ret ? 0 : 1);
378 else if(*command ==
'f')
380 char *value = httpParser->
getByName(
"p1");
383 bool *ret = (
bool*)(*handlerList)[i]->handle(*command, atoi(value));
386 buf = urlEncode(
ES_OK);
387 response = (
char*)malloc(strlen(buf) + 30 +
sizeof (u_int32_t));
388 sprintf(response,
"n=1&e=%d&d=%s&v1=%u", E_OK,
"", *ret ? 0 : 1);
397 response = (
char*)malloc(512);
410 char *ret = (
char*)(*handlerList)[i]->handle(*command);
413 buf = urlEncode(
ES_OK);
414 response = (
char*)malloc(strlen(buf) + 30);
415 sprintf(response,
"n=1&e=%d&d=%s&v1=%c", E_OK, buf, *ret);
426 char *ret = (
char*)(*handlerList)[i]->handle(*command, atoi(p1));
429 buf = urlEncode(
ES_OK);
430 response = (
char*)malloc(strlen(buf) + 30);
431 sprintf(response,
"n=1&e=%d&d=%s&v1=%c", E_OK, buf, *ret);
443 char *ret = (
char*)(*handlerList)[i]->handle(*command, atoi(p1));
446 buf = urlEncode(
ES_OK);
447 response = (
char*)malloc(strlen(buf) + 30);
448 sprintf(response,
"n=1&e=%d&d=%s&v1=%d", E_OK, buf, *ret);
457 char *ret = (
char*)(*handlerList)[i]->handle(*command);
460 buf = urlEncode(
ES_OK);
461 response = (
char*)malloc(strlen(buf) + 30);
462 sprintf(response,
"n=1&e=%d&d=%s&v1=%d", E_OK, buf, *ret);
471 response = (
char*)malloc(512);
480 char *value = httpParser->
getByName(
"p1");
481 if(value && (*command >=
'a' && *command <=
'd'))
484 char *url = *command ==
'a' ? httpParser->
getByName(
"p2") : NULL;
485 ret = (
unsigned long*)(*handlerList)[i]->handle(*command, (u_int32_t)atoi(value), url);
488 buf = urlEncode(
ES_OK);
489 response = (
char*)malloc(strlen(buf) + 30);
490 sprintf(response,
"n=1&e=%d&d=%s&v1=%lu", E_OK, buf, *ret);
497 int code = (*handlerList)[i]->getError(&errbuf);
498 buf = urlEncode(errbuf);
499 response = (
char*)malloc(512);
500 sprintf(response,
"n=0&e=%d&d=%s", code, buf);
506 if(*command ==
'e' || *command ==
'g')
512 if(p1)site = atoi(p1);
514 char *ret = (
char*)(*handlerList)[i]->handle(*command, site);
517 buf = urlEncode(
ES_OK);
518 unsigned int len = strlen(ret);
519 response = (
char*)malloc(strlen(buf) + 30 + len);
520 sprintf(response,
"n=1&e=%d&d=%s&v1=%s", E_OK, buf, ret);
527 int code = (*handlerList)[i]->getError(&errbuf);
528 buf = urlEncode(errbuf);
529 response = (
char*)malloc(512);
530 sprintf(response,
"n=0&e=%d&d=%s", code, buf);
541 char *ret = (
char*)(*handlerList)[i]->handle(*command, atoi(p1));
544 int len = *((
int*)ret);
545 buf = urlEncode(
ES_OK);
546 response = (
char*)malloc(strlen(buf) + 30 + len);
547 int pos = sprintf(response,
"n=1&e=%d&d=%s&v1=", E_OK, buf);
549 memcpy(response + pos, ret +
sizeof(
int), len);
550 response[pos + len] =
'\0';
562 String *ret = (
String*)(*handlerList)[i]->handle(*command, p1);
565 buf = urlEncode(
ES_OK);
566 response = (
char*)malloc(strlen(buf) + 30 + ret->
length());
567 int pos = sprintf(response,
"n=1&e=%d&d=%s&v1=", E_OK, buf);
569 memcpy(response + pos, ret->
c_str(), ret->
length());
570 response[pos + ret->
length()] =
'\0';
582 char *ret = (
char*)(*handlerList)[i]->handle(*command, p1);
585 response = (
char*)malloc(strlen(ret) + 16);
586 sprintf(response,
"n=1&e=0&d=&v1=%s", ret);
591 response = (
char*)malloc(16);
592 strcpy(response,
"n=0&e=1&d=&v1=");
599 response = (
char*)malloc(512);
612 char *value = httpParser->
getByName(
"p1");
613 if(value || *command ==
'b' || *command ==
'd' || *command ==
'f')
618 ret = (
unsigned long*)(*handlerList)[i]->handle(*command, (
unsigned long)atol(value));
622 ret = (
unsigned long*)(*handlerList)[i]->handle(*command);
626 buf = urlEncode(
ES_OK);
627 response = (
char*)malloc(strlen(buf) + 30);
628 sprintf(response,
"n=1&e=%d&d=%s&v1=%lu", E_OK, buf, *ret);
635 int code = (*handlerList)[i]->getError(&errbuf);
636 buf = urlEncode(errbuf);
637 response = (
char*)malloc(512);
638 sprintf(response,
"n=0&e=%d&d=%s", code, buf);
645 response = (
char*)malloc(512);
654 char *value = httpParser->
getByName(
"p1");
655 if(value || *command ==
'f' || *command ==
'i' || *command ==
'j')
663 bool *ret = (
bool*)(*handlerList)[i]->handle(*command, value);
668 buf = urlEncode(
ES_OK);
669 response = (
char*)malloc(strlen(buf) + 15);
670 sprintf(response,
"n=0&e=%d&d=%s", E_OK, buf);
676 int code = (*handlerList)[i]->getError(&errbuf);
677 buf = urlEncode(errbuf);
678 response = (
char*)malloc(512);
679 sprintf(response,
"n=0&e=%d&d=%s", code, buf);
688 char *ret = (
char*)(*handlerList)[i]->handle(*command, value);
691 buf = urlEncode(
ES_OK);
692 response = (
char*)malloc((buf ? strlen(buf) : 0) + 256);
696 int errNo = *((
int*)ret);
698 char *errorStr = ret +
sizeof(int);
699 ret +=
sizeof(int) + *((
int*)ret) + 1;
701 ret +=
sizeof(int) + *((
int*)ret) + 1;
702 time_t t = *((time_t*)ret);
703 sprintf(response,
"n=5&e=%d&d=%s&v1=%u&v2=%d&v3=%s&v4=%s&v5=%u", E_OK, (buf ? buf :
""), state,
704 errNo, errorStr, fileName, (
unsigned int)t);
715 char *ret = (
char*)(*handlerList)[i]->handle(*command, atoi(value));
716 buf = urlEncode(
ES_OK);
717 response = (
char*)malloc((buf ? strlen(buf) : 0) + (ret ? strlen(ret) : 0) + 256);
718 sprintf(response,
"n=1&e=%d&d=%s&v1=%s", E_OK, (buf ? buf :
""), (ret ? ret :
""));
731 bool *ret = (
bool*)(*handlerList)[i]->handle(*command, atoi(value));
745 buf = urlEncode(
ES_OK);
746 response = (
char*)malloc(512);
747 sprintf(response,
"n=0&e=%d&d=%s", E_OK, buf);
754 (*handlerList)[i]->handle(*command);
756 buf = urlEncode(
ES_OK);
757 response = (
char*)malloc(512);
758 sprintf(response,
"n=0&e=%d&d=%s", E_OK, buf);
765 bool *ret = (
bool*)(*handlerList)[i]->handle(*command);
767 buf = urlEncode(
ES_OK);
768 response = (
char*)malloc(512);
769 sprintf(response,
"n=0&e=%d&d=%s&v1=%c", E_OK, buf, ((ret && *ret) ?
'1' :
'0'));
780 (*handlerList)[i]->handle(*command, atoi(value));
782 buf = urlEncode(
ES_OK);
783 response = (
char*)malloc(512);
784 sprintf(response,
"n=0&e=%d&d=%s", E_OK, buf);
793 response = (
char*)malloc(512);
803 response = (
char*)malloc(512);
812 char *value = httpParser->
getByName(
"p1");
813 if(value || *command ==
'f')
820 char **ret = (
char**)(*handlerList)[i]->handle(*command, strtoull(value, NULL, 10));
823 buf = urlEncode(
ES_OK);
825 response = (
char*)malloc(rLen + 5);
826 size_t rPos = sprintf(response,
"n=17&e=%d&d=%s", E_OK, buf);
828 buf = (
char*)malloc(10);
829 for(
unsigned int r = 0; r < 17; r++)
831 size_t sLen = strlen(ret[r]);
832 if(rLen < rPos + sLen + 4)
834 rLen += rPos + sLen + 4;
835 response = (
char*)realloc(response, rLen + 5);
837 size_t bufLen = sprintf(buf,
"&v%u=", r + 1);
838 memcpy(response + rPos, buf, bufLen);
840 memcpy(response + rPos, ret[r], sLen);
844 response[rPos] =
'\0';
851 int code = (*handlerList)[i]->getError(&errbuf);
852 buf = urlEncode(errbuf);
853 response = (
char*)malloc(512);
854 sprintf(response,
"n=0&e=%d&d=%s", code, buf);
864 bool *ret = (
bool*)(*handlerList)[i]->handle(*command, value);
869 buf = urlEncode(
ES_OK);
870 response = (
char*)malloc(strlen(buf) + 15);
871 sprintf(response,
"n=0&e=%d&d=%s", E_OK, buf);
877 int code = (*handlerList)[i]->getError(&errbuf);
878 buf = urlEncode(errbuf);
879 response = (
char*)malloc(512);
880 sprintf(response,
"n=0&e=%d&d=%s", code, buf);
889 bool *ret = (
bool*)(*handlerList)[i]->handle(*command, strtoull(value, NULL, 10));
892 buf = urlEncode(
ES_OK);
893 response = (
char*)malloc(strlen(buf) + 15);
894 sprintf(response,
"n=1&e=%d&d=%s&v1=%d", E_OK, buf, *ret ? 1:0);
903 char *smId = httpParser->
getByName(
"p2");
906 bool *ret = (
bool*)(*handlerList)[i]->handle(*command, value, atoi(smId));
911 buf = urlEncode(
ES_OK);
912 response = (
char*)malloc(strlen(buf) + 15);
913 sprintf(response,
"n=0&e=%d&d=%s", E_OK, buf);
919 int code = (*handlerList)[i]->getError(&errbuf);
920 buf = urlEncode(errbuf);
921 response = (
char*)malloc(512);
922 sprintf(response,
"n=0&e=%d&d=%s", code, buf);
932 char *ret = (
char*)(*handlerList)[i]->handle(*command, value);
935 buf = urlEncode(
ES_OK);
936 response = (
char*)malloc(strlen(buf) + 256);
940 int errNo = *((
int*)ret);
942 char *errorStr = ret +
sizeof(int);
943 ret +=
sizeof(int) + *((
int*)ret) + 1;
944 char *fileName = ret +
sizeof(int);
945 ret +=
sizeof(int) + *((
int*)ret) + 1;
946 time_t t = *((time_t*)ret);
947 sprintf(response,
"n=5&e=%d&d=%s&v1=%u&v2=%d&v3=%s&v4=%s&v5=%u", E_OK, buf, state, errNo, errorStr, fileName, (
unsigned int)t);
955 u_int32_t *count = (u_int32_t*)(*handlerList)[i]->handle(*command, atoi(value));
958 buf = urlEncode(
ES_OK);
959 response = (
char*)malloc(strlen(buf) + 256);
960 sprintf(response,
"n=1&e=%d&d=%s&v1=%u", E_OK, buf, *count);
972 response = (
char*)malloc(512);
981 char *value = httpParser->
getByName(
"p1");
984 int *ret = (
int*)(*handlerList)[i]->handle(*command, atoi(value));
987 if(*command ==
'a' || *command ==
'b')
989 buf = urlEncode(
ES_OK);
990 response = (
char*)malloc(512);
991 sprintf(response,
"n=1&e=%d&d=%s&v1=%d", E_OK, buf, *ret);
998 char *retData = (
char*)(ret + 1);
999 buf = urlEncode(
ES_OK);
1000 response = (
char*)malloc(strlen(retData) + 128);
1001 char *errText = urlEncode(retData);
1002 sprintf(response,
"n=2&e=%d&d=%s&v1=%d&v2=%s", E_OK, buf, errNo, errText);
1011 int code = (*handlerList)[i]->getError(&errbuf);
1012 buf = urlEncode(errbuf);
1013 response = (
char*)malloc(512);
1014 sprintf(response,
"n=0&e=%d&d=%s", code, buf);
1021 response = (
char*)malloc(512);
1030 char *name = httpParser->
getByName(
"p1");
1033 char *ret = (
char*)(*handlerList)[i]->handle(*command, name);
1036 buf = urlEncode(
ES_OK);
1037 response = (
char*)malloc(512 + strlen(ret));
1038 sprintf(response,
"n=1&e=%d&d=%s&v1=%s", E_OK, buf, ret);
1045 int code = (*handlerList)[i]->getError(&errbuf);
1046 buf = urlEncode(errbuf);
1047 response = (
char*)malloc(512);
1048 sprintf(response,
"n=0&e=%d&d=%s", code, buf);
1055 response = (
char*)malloc(512);
1064 if(*command >=
'a' && *command <=
'f')
1066 time_t *retval = (time_t*)(*handlerList)[i]->handle(*command);
1069 buf = urlEncode(
ES_OK);
1070 response = (
char*)malloc(512);
1071 sprintf(response,
"n=1&e=%d&d=%s&v1=%ld", E_OK, buf, *retval);
1078 int code = (*handlerList)[i]->getError(&errbuf);
1079 buf = urlEncode(errbuf);
1080 response = (
char*)malloc(512);
1081 sprintf(response,
"n=0&e=%d&d=%s", code, buf);
1087 int *retval = (
int*)(*handlerList)[i]->handle(*command);
1090 buf = urlEncode(
ES_OK);
1091 response = (
char*)malloc(512);
1092 sprintf(response,
"n=1&e=%d&d=%s&v1=%d", E_OK, buf, *retval);
1099 int code = (*handlerList)[i]->getError(&errbuf);
1100 buf = urlEncode(errbuf);
1101 response = (
char*)malloc(512);
1102 sprintf(response,
"n=0&e=%d&d=%s", code, buf);
1112 bool *retval = NULL;
1113 unsigned long long *intval = NULL;
1121 intval = (
unsigned long long*)(*handlerList)[i]->handle(*command, atoi(p1));
1126 if(*command ==
'd' || *command ==
'f')
1132 retval = (
bool*)(*handlerList)[i]->handle(*command, atoi(p1), atoi(p2));
1140 retval = (
bool*)(*handlerList)[i]->handle(*command, atoi(p1));
1144 response = (
char*)malloc(512);
1149 buf = urlEncode(
ES_OK);
1151 sprintf(response,
"n=0&e=%d&d=%s&v1=%u&v2=%u", E_OK, buf, (
unsigned int)(*intval >> 32), (
unsigned int)(*intval & 0xffffffff));
1153 sprintf(response,
"n=0&e=%d&d=%s", E_OK, buf);
1159 sprintf(response,
"n=0&e=%d&d=%s",
E_UNKNOWN, buf);
1170 if(intval)
delete intval;
1175 if(*command ==
'a' || *command ==
'b' || *command ==
'c')
1180 char *ret = (
char*)(*handlerList)[i]->handle(*command, p1);
1181 response = (
char*)malloc(512);
1184 buf = urlEncode(
ES_OK);
1185 sprintf(response,
"n=0&e=%d&d=%s", E_OK, buf);
1192 int code = (*handlerList)[i]->getError(&errbuf);
1193 buf = urlEncode(errbuf);
1194 response = (
char*)malloc(512);
1195 sprintf(response,
"n=0&e=%d&d=%s", code, buf);
1212 response = (
char*)malloc(512);
1213 (*handlerList)[i]->handle(*command, atoi(p1));
1214 buf = urlEncode(
ES_OK);
1215 sprintf(response,
"n=0&e=%d&d=%s", E_OK, buf);
1227 char *ret = (
char*)(*handlerList)[i]->handle(*command, atoi(p1), atoi(p2), p3);
1230 size_t len = strlen(ret);
1231 response = (
char *)malloc(len + 512);
1232 buf = urlEncode(
ES_OK);
1233 int pos = sprintf(response,
"n=1&e=%d&d=%s&v1=", E_OK, buf);
1235 memcpy(response + pos, ret, len + 1);
1254 (*handlerList)[i]->handle(*command, atoi(p1));
1255 response = (
char*)malloc(512);
1256 buf = urlEncode(
ES_OK);
1257 sprintf(response,
"n=0&e=%d&d=%s", E_OK, buf);
1266 (*handlerList)[i]->handle(*command);
1267 response = (
char*)malloc(512);
1268 buf = urlEncode(
ES_OK);
1269 sprintf(response,
"n=0&e=%d&d=%s", E_OK, buf);
1279 response = (
char*)malloc(512);
1293 response = (
char*)malloc(512);
1298 sendResponse(response);
1309 void AdminClient::sendResponse(
const char *response)
1311 size_t contentLen = strlen(response);
1312 size_t headerLen = 0;
1315 size_t sendSize = 0;
1316 time_t startTime =
time(NULL), timeOut = 30;
1317 headerLen = sprintf(header,
"HTTP/1.0 200 OK\r\nConnection: close\r\nContent-Type: text/html; charset=utf-8\r\nAccept-Ranges: bytes\r\nServer: hceAdminPart\r\nContent-Length: %lu\r\n\r\n",(
unsigned long)contentLen);
1320 while(!clientSock->selectWrite(status) && !exitFlag)
1322 if(status != SOCK_OK)
break;
1323 if(startTime + timeOut <
time(NULL))
break;
1325 if(status == SOCK_OK && !exitFlag)
1327 sendSize += clientSock->write(header + sendSize, headerLen - sendSize, status);
1329 }
while(sendSize < headerLen && status == SOCK_OK && !exitFlag && startTime + timeOut >
time(NULL));
1331 while(sendSize < contentLen && status == SOCK_OK && !exitFlag && startTime + timeOut >
time(NULL))
1333 while(!clientSock->selectWrite(status) && !exitFlag)
1335 if(status != SOCK_OK)
break;
1336 if(startTime + timeOut <
time(NULL))
break;
1338 if(status == SOCK_OK && !exitFlag)
1340 sendSize += clientSock->write(response + sendSize, contentLen - sendSize, status);
1345 char* AdminClient::readRequest(
size_t &size)
1347 size_t partSize = 1024;
1348 char *
data = (
char*)malloc(partSize + 1);
1349 size_t dataSize = partSize;
1350 bool endRequest =
false;
1352 bool isGet =
false, isPost =
false;
1353 size_t contentLenght = 0;
1354 time_t startTime =
time(NULL), timeOut = 5;
1358 while(!clientSock->selectRead(status))
1360 if(exitFlag || status != SOCK_OK || startTime + timeOut <
time(NULL))
1367 if(status == SOCK_OK)
1369 if(dataSize < size + partSize)
1371 dataSize += partSize;
1372 data = (
char*)realloc(data, dataSize + 1);
1374 size += clientSock->readPart(data + size, partSize, status);
1375 if(status != SOCK_OK)
1382 if(!isGet && !isPost)
1384 char *str = strstr(data,
"GET ");
1385 isGet = (str ==
data);
1388 str = strstr(data,
"POST ");
1389 isPost = (str ==
data);
1392 if(isGet && size > 4)
1394 endRequest = (data[size - 4] ==
'\r' && data[size - 3] ==
'\n' && data[size - 2] ==
'\r' && data[size - 1] ==
'\n');
1401 str = strstr(data,
"Content-Length:");
1404 str += strlen(
"Content-Lenght:");
1405 while(*str ==
' ' || *str ==
'\t')str++;
1406 contentLenght = atol(str);
1409 str = strstr(data,
"\r\n\r\n");
1412 str = strstr(data,
"\n\r\n\r");
1415 str = strstr(data,
"\n\n");
1418 str = strstr(data,
"\r\r");
1424 endRequest = (size - (str -
data) >= contentLenght);
1428 }
while(size < MAX_REQUEST_SIZE && status == SOCK_OK && !endRequest && startTime + timeOut >
time(NULL));
1431 data = (
char*)realloc(data, size + 1);