HCE project C++ developers source code library  1.1.1
HCE project developer library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
AdminClient.cpp
Go to the documentation of this file.
1 #include <AdminClient.h>
2 AdminClient::AdminClient(const char *host, int port, List<BaseHandler*> *handlerList)
3 {
4  int status = 0;
5  this->handlerList = handlerList;
6  listenSock = new(std::nothrow) Socket();
7  if(!listenSock)lastErrNo = ENOMEM;
8  listenSock->ReuseAddr(status);
9  lastErrNo = 0;
10  listenSock->bind(host, port, status);
11  if(status == SOCK_OK)
12  {
13  listenSock->listen(status, 1);
14  if(status != SOCK_OK)
15  {
16  lastErrNo = errno;
17  }
18  }
19  else
20  {
21  lastErrNo = errno;
22  }
23 }
24 
26 {
27  delete listenSock;
28 }
29 
30 char* AdminClient::urlEncode(const char*url)
31 {
32  char *encoded = NULL;
33  register int enc_len = 0;
34  encoded = (char*)malloc( strlen(url) * 3 );
35  for(unsigned int i = 0; i < strlen(url); i++)
36  {
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))
38  {
39  enc_len += sprintf((char*)encoded + enc_len, "%%%02X", url[i]);
40  }
41  else
42  {
43  encoded[enc_len++] = url[i];
44  }
45  }
46  encoded = (char*)realloc(encoded, enc_len + 1);
47  encoded[enc_len] = 0;
48  return encoded;
49 }
50 
51 void* AdminClient::run()
52 {
53  int status = 0;
54  size_t requestLen = 0;
55  char *request;
56  char *buf = NULL;
57  while(!exitFlag)
58  {
59  while(!listenSock->selectRead(status))
60  {
61  if(exitFlag)
62  {
63  return NULL;
64  }
65  usleep(1000);
66  }
67  sock_fd_t sock = listenSock->accept(status);
68  if(status == SOCK_OK)
69  {
70  clientSock = new(std::nothrow) Socket(sock);
71  if(!clientSock)
72  {
73  lastErrNo = ENOMEM;
74  return NULL;
75  }
76  clientSock->setNonBlock(status);
77  request = readRequest(requestLen);
78  //printf("%d\n%s\n", requestLen, request );
79  if(request)
80  {
81  char *response = NULL;
82  httpParser = new(std::nothrow) HttpParser(request, requestLen);
83  if(!httpParser)
84  {
85  lastErrNo = ENOMEM;
86  return NULL;
87  }
88  char *command = httpParser->getByName("c");
89  if(command != NULL)
90  {
91  unsigned int handlerId = atoi(command);
92  while(*command >= '0' && *command <= '9')command++;
93  if(*command)
94  {
95  if(handlerList)
96  {
97  mutex.lock();
98  for(unsigned long i = 0; i < handlerList->getCount(); i++)
99  {
100  if((*handlerList)[i]->getId() == handlerId)
101  {
102  switch(handlerId)
103  {
104  case 121:
105  {
106  if(*command == 'a')
107  {
108  char *ret = (char*)(*handlerList)[i]->handle(*command);
109  if(ret)
110  {
111  buf = urlEncode(ES_OK);
112  response = (char*)malloc(strlen(buf) + 50);
113  sprintf(response, "n=0&e=%d&d=%s", E_OK, buf);
114  free(buf);
115  delete []ret;
116  }
117  }
118  break;
119  }
120  case 122:
121  {
122  if(*command == 'a')
123  {
124  char *p1 = httpParser->getByName("p1");
125  if(p1)
126  {
127  bool *ret = (bool*)(*handlerList)[i]->handle(*command, p1);
128  if(ret)
129  {
130  buf = urlEncode(ES_OK);
131  response = (char*)malloc(strlen(buf) + 50);
132  sprintf(response, "n=0&e=%d&d=%s", E_OK, buf);
133  free(buf);
134  delete ret;
135  }
136  }
137  else
138  {
139  buf = urlEncode(ES_WRONGPARAM);
140  response = (char*)malloc(512);
141  sprintf(response, "n=0&e=%d&d=%s", E_WRONGPARAM, buf);
142  free(buf);
143  }
144 
145  }
146  if(*command == 'c' || *command == 'e')
147  {
148  char *p1 = httpParser->getByName("p1");
149  if(p1)
150  {
151  bool *ret = (bool*)(*handlerList)[i]->handle(*command, atoi(p1));
152  if(ret)
153  {
154  buf = urlEncode(ES_OK);
155  response = (char*)malloc(strlen(buf) + 50);
156  sprintf(response, "n=0&e=%d&d=%s", E_OK, buf);
157  free(buf);
158  delete ret;
159  }
160  }
161  else
162  {
163  buf = urlEncode(ES_WRONGPARAM);
164  response = (char*)malloc(512);
165  sprintf(response, "n=0&e=%d&d=%s", E_WRONGPARAM, buf);
166  free(buf);
167  }
168 
169  }
170  if(*command == 'b' || *command == 'd' || *command == 'f' || *command == 'g')
171  {
172  int *v1 = (int*)(*handlerList)[i]->handle(*command);
173  if(v1)
174  {
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);
178  free(buf);
179  delete v1;
180  }
181  }
182  if(*command == 'h')
183  {
184  char *title = httpParser->getByName("p1");
185  char *h = httpParser->getByName("p2");
186  char *body = httpParser->getByName("p3");
187  char *topic = httpParser->getByName("p4");
188  if(title && h && body && topic)
189  {
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++)
195  {
196  len += sprintf(response + len, "&v%d=%u", i + 1, ret[i]);
197  }
198  free(buf);
199  delete []ret;
200  }
201  }
202  if(*command == 'j' || *command == 'k')
203  {
204  char *content = httpParser->getByName("p1");
205  if(content)
206  {
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';
214  free(buf);
215  if(ret)free(ret);
216  }
217  }
218  if(*command == 'l')
219  {
220  bool *ret = (bool*)(*handlerList)[i]->handle(*command);
221  if(ret)
222  {
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);
226  delete ret;
227  free(buf);
228  }
229  }
230  if(*command == 'm')
231  {
232  char *ret = (char*)(*handlerList)[i]->handle(*command);
233  if(ret)
234  {
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);
238  free(ret);
239  free(buf);
240  }
241  }
242 
243  break;
244  }
245 
246  case 112:
247  {
248  if(*command == 'a')
249  {
250  unsigned long *ret = (unsigned long*)(*handlerList)[i]->handle(*command);
251  if(ret)
252  {
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]);
256  free(buf);
257  delete []ret;
258  }
259  }
260  else
261  {
262  if(*command == 'b')
263  {
264  char *p1 = httpParser->getByName("p1");
265  char *p2 = httpParser->getByName("p2");
266  char *p3 = httpParser->getByName("p3");
267  char *p4 = httpParser->getByName("p4");
268  if(p1 && p2 && p3 && p4)
269  {
270  char *ret = (char*)(*handlerList)[i]->handle(*command, (unsigned long)atol(p1), (unsigned long)atol(p2), (unsigned long)atol(p3), (unsigned long)atol(p4));
271  if(ret)
272  {
273  buf = urlEncode(ES_OK);
274  response = (char*)malloc(strlen(buf) + 30);
275  sprintf(response, "n=0&e=%d&d=%s", E_OK, buf);
276  free(buf);
277  delete ret;
278  }
279  }
280  else
281  {
282  buf = urlEncode(ES_WRONGPARAM);
283  response = (char*)malloc(512);
284  sprintf(response, "n=0&e=%d&d=%s", E_WRONGPARAM, buf);
285  free(buf);
286  }
287  }
288  else
289  {
290  buf = urlEncode(ES_WRONGPARAM);
291  response = (char*)malloc(512);
292  sprintf(response, "n=0&e=%d&d=%s", E_WRONGPARAM, buf);
293  free(buf);
294  }
295  }
296  break;
297  }
298  case 140:
299  {
300  if(*command == 'a')
301  {
302  char *ret = (char*)(*handlerList)[i]->handle(*command);
303  if(ret)
304  {
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);
309  free(buf);
310  memcpy(response + pos, ret + sizeof(int), len);
311  response[pos + len] = '\0';
312  free(ret);
313  }
314  }
315  else
316  {
317  if(*command == 'b')
318  {
319  char *value = httpParser->getByName("p1");
320  int *ret = (int*)(*handlerList)[i]->handle(*command, value);
321  if(ret)
322  {
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);
326  free(ret);
327  free (buf);
328  }
329  }
330  else if (*command == 'c')
331  {
332  char *value = httpParser->getByName("p1");
333  if(value)
334  {
335  int *ret = (int*)(*handlerList)[i]->handle(*command, atoi(value));
336  if(ret)
337  {
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);
341  free(ret);
342  free (buf);
343  }
344  }
345  }
346  else if(*command == 'd')
347  {
348  char *value = httpParser->getByName("p1");
349  if(value)
350  {
351  char *ret = (char*)(*handlerList)[i]->handle(*command, atoi(value));
352  if(ret)
353  {
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);
357  free(ret);
358  free (buf);
359  }
360  }
361  }
362  else if(*command == 'e')
363  {
364  char *value = httpParser->getByName("p1");
365  if(value)
366  {
367  bool *ret = (bool*)(*handlerList)[i]->handle(*command, value);
368  if(ret)
369  {
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);
373  free(ret);
374  free (buf);
375  }
376  }
377  }
378  else if(*command == 'f')
379  {
380  char *value = httpParser->getByName("p1");
381  if(value)
382  {
383  bool *ret = (bool*)(*handlerList)[i]->handle(*command, atoi(value));
384  if(ret)
385  {
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);
389  free(ret);
390  free (buf);
391  }
392  }
393  }
394  else
395  {
396  buf = urlEncode(ES_WRONGPARAM);
397  response = (char*)malloc(512);
398  sprintf(response, "n=0&e=%d&d=%s", E_WRONGPARAM, buf);
399  free(buf);
400  }
401  }
402  break;
403  }
404  case 111:
405  {
406  switch(*command)
407  {
408  case 'a':
409  {
410  char *ret = (char*)(*handlerList)[i]->handle(*command);
411  if(ret)
412  {
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);
416  free(buf);
417  delete ret;
418  }
419  break;
420  }
421  case 'b':
422  {
423  char *p1 = httpParser->getByName("p1");
424  if(p1)
425  {
426  char *ret = (char*)(*handlerList)[i]->handle(*command, atoi(p1));
427  if(ret)
428  {
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);
432  free(buf);
433  delete ret;
434  }
435  }
436  break;
437  }
438  case 'c':
439  {
440  char *p1 = httpParser->getByName("p1");
441  if(p1)
442  {
443  char *ret = (char*)(*handlerList)[i]->handle(*command, atoi(p1));
444  if(ret)
445  {
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);
449  free(buf);
450  delete ret;
451  }
452  }
453  break;
454  }
455  case 'd':
456  {
457  char *ret = (char*)(*handlerList)[i]->handle(*command);
458  if(ret)
459  {
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);
463  free(buf);
464  delete ret;
465  }
466  break;
467  }
468  default:
469  {
470  buf = urlEncode(ES_WRONGPARAM);
471  response = (char*)malloc(512);
472  sprintf(response, "n=0&e=%d&d=%s", E_WRONGPARAM, buf);
473  free(buf);
474  }
475  }
476  break;
477  }
478  case 110:
479  {
480  char *value = httpParser->getByName("p1");
481  if(value && (*command >= 'a' && *command <= 'd'))
482  {
483  unsigned long *ret;
484  char *url = *command == 'a' ? httpParser->getByName("p2") : NULL;
485  ret = (unsigned long*)(*handlerList)[i]->handle(*command, (u_int32_t)atoi(value), url);
486  if(ret)
487  {
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);
491  free(buf);
492  delete ret;
493  }
494  else
495  {
496  char *errbuf;
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);
501  free(buf);
502  }
503  }
504  else
505  {
506  if(*command == 'e' || *command == 'g')
507  {
508  u_int32_t site = 0;
509  if(*command == 'g')
510  {
511  char *p1 = httpParser->getByName("p1");
512  if(p1)site = atoi(p1);
513  }
514  char *ret = (char*)(*handlerList)[i]->handle(*command, site);
515  if(ret)
516  {
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);
521  free(buf);
522  free(ret);
523  }
524  else
525  {
526  char *errbuf;
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);
531  free(buf);
532  }
533  }
534  else
535  {
536  if(*command == 'f')
537  {
538  char *p1 = httpParser->getByName("p1");
539  if(p1)
540  {
541  char *ret = (char*)(*handlerList)[i]->handle(*command, atoi(p1));
542  if(ret)
543  {
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);
548  free(buf);
549  memcpy(response + pos, ret + sizeof(int), len);
550  response[pos + len] = '\0';
551  free(ret);
552  }
553  }
554  }
555  else
556  {
557  if(*command == 'h')
558  {
559  char *p1 = httpParser->getByName("p1");
560  if(p1)
561  {
562  String *ret = (String*)(*handlerList)[i]->handle(*command, p1);
563  if(ret)
564  {
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);
568  free(buf);
569  memcpy(response + pos, ret->c_str(), ret->length());
570  response[pos + ret->length()] = '\0';
571  delete ret;
572  }
573  }
574  }
575  else
576  {
577  if (*command == 'j')
578  {
579  char *p1 = httpParser->getByName("p1");
580  if(p1)
581  {
582  char *ret = (char*)(*handlerList)[i]->handle(*command, p1);
583  if(ret)
584  {
585  response = (char*)malloc(strlen(ret) + 16);
586  sprintf(response, "n=1&e=0&d=&v1=%s", ret);
587  delete ret;
588  }
589  else
590  {
591  response = (char*)malloc(16);
592  strcpy(response, "n=0&e=1&d=&v1=");
593  }
594  }
595  }
596  else
597  {
598  buf = urlEncode(ES_WRONGPARAM);
599  response = (char*)malloc(512);
600  sprintf(response, "n=0&e=%d&d=%s", E_WRONGPARAM, buf);
601  free(buf);
602  }
603  }
604  }
605  }
606  }
607  break;
608  }
609 
610  case 106:
611  {
612  char *value = httpParser->getByName("p1");
613  if(value || *command == 'b' || *command == 'd' || *command == 'f')
614  {
615  unsigned long *ret;
616  if(value)
617  {
618  ret = (unsigned long*)(*handlerList)[i]->handle(*command, (unsigned long)atol(value));
619  }
620  else
621  {
622  ret = (unsigned long*)(*handlerList)[i]->handle(*command);
623  }
624  if(ret)
625  {
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);
629  free(buf);
630  delete ret;
631  }
632  else
633  {
634  char *errbuf;
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);
639  free(buf);
640  }
641  }
642  else
643  {
644  buf = urlEncode(ES_WRONGPARAM);
645  response = (char*)malloc(512);
646  sprintf(response, "n=0&e=%d&d=%s", E_WRONGPARAM, buf);
647  free(buf);
648  }
649  break;
650  }
651  //***************************Dump handler for CDH**********************************************************************
652  case 105:
653  {
654  char *value = httpParser->getByName("p1");
655  if(value || *command == 'f' || *command == 'i' || *command == 'j')
656  {
657  switch(*command)
658  {
659  case 'a':
660  case 'b':
661  case 'c':
662  {
663  bool *ret = (bool*)(*handlerList)[i]->handle(*command, value);
664  if(ret)
665  {
666  if(*ret)
667  {
668  buf = urlEncode(ES_OK);
669  response = (char*)malloc(strlen(buf) + 15);
670  sprintf(response, "n=0&e=%d&d=%s", E_OK, buf);
671  free(buf);
672  }
673  else
674  {
675  char *errbuf;
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);
680  free(buf);
681  }
682  delete ret;
683  }
684  break;
685  }
686  case 'f':
687  {
688  char *ret = (char*)(*handlerList)[i]->handle(*command, value);
689  if(ret)
690  {
691  buf = urlEncode(ES_OK);
692  response = (char*)malloc((buf ? strlen(buf) : 0) + 256);
693  int state = *ret;
694  char *start = ret;
695  ret++;
696  int errNo = *((int*)ret);
697  ret += sizeof(int);
698  char *errorStr = ret + sizeof(int);
699  ret += sizeof(int) + *((int*)ret) + 1;
700  char *fileName = ret + sizeof(int);
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);
705  if(buf)
706  {
707  free(buf);
708  }
709  delete []start;
710  }
711  break;
712  }
713  case 'g':
714  {
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 : ""));
719  if(buf)
720  {
721  free(buf);
722  }
723  if(ret)
724  {
725  free(ret);
726  }
727  break;
728  }
729  case 'h':
730  {
731  bool *ret = (bool*)(*handlerList)[i]->handle(*command, atoi(value));
732  if(ret)
733  {
734  if(*ret)
735  {
736 // Add code herre
737  }
738  else
739  {
740 // Add code here
741  }
742  delete ret;
743  }
744 //--------------------------------------------------------------------------
745  buf = urlEncode(ES_OK);
746  response = (char*)malloc(512);
747  sprintf(response, "n=0&e=%d&d=%s", E_OK, buf);
748  free(buf);
749 //--------------------------------------------------------------------------
750  break;
751  }
752  case 'i':
753  {
754  (*handlerList)[i]->handle(*command);
755 //--------------------------------------------------------------------------
756  buf = urlEncode(ES_OK);
757  response = (char*)malloc(512);
758  sprintf(response, "n=0&e=%d&d=%s", E_OK, buf);
759  free(buf);
760 //--------------------------------------------------------------------------
761  break;
762  }
763  case 'j':
764  {
765  bool *ret = (bool*)(*handlerList)[i]->handle(*command);
766 //--------------------------------------------------------------------------
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'));
770  free(buf);
771  if(ret)
772  {
773  delete ret;
774  }
775 //--------------------------------------------------------------------------
776  break;
777  }
778  case 'k':
779  {
780  (*handlerList)[i]->handle(*command, atoi(value));
781 //--------------------------------------------------------------------------
782  buf = urlEncode(ES_OK);
783  response = (char*)malloc(512);
784  sprintf(response, "n=0&e=%d&d=%s", E_OK, buf);
785  free(buf);
786 //--------------------------------------------------------------------------
787  break;
788  }
789 
790  default:
791  {
792  buf = urlEncode(ES_WRONGPARAM);
793  response = (char*)malloc(512);
794  sprintf(response, "n=0&e=%d&d=%s", E_WRONGPARAM, buf);
795  free(buf);
796  break;
797  }
798  }
799  }
800  else
801  {
802  buf = urlEncode(ES_WRONGPARAM);
803  response = (char*)malloc(512);
804  sprintf(response, "n=0&e=%d&d=%s", E_WRONGPARAM, buf);
805  free(buf);
806  }
807  break;
808  }
809  //***************************Resource Handler***************************************************************************
810  case 104:
811  {
812  char *value = httpParser->getByName("p1");
813  if(value || *command == 'f')
814  {
815  switch(*command)
816  {
817  case 'a':
818  case 'h':
819  {
820  char **ret = (char**)(*handlerList)[i]->handle(*command, strtoull(value, NULL, 10));
821  if(ret)
822  {
823  buf = urlEncode(ES_OK);
824  size_t rLen = 2048;
825  response = (char*)malloc(rLen + 5);
826  size_t rPos = sprintf(response, "n=17&e=%d&d=%s", E_OK, buf);
827  free(buf);
828  buf = (char*)malloc(10);
829  for(unsigned int r = 0; r < 17; r++)
830  {
831  size_t sLen = strlen(ret[r]);
832  if(rLen < rPos + sLen + 4)
833  {
834  rLen += rPos + sLen + 4;
835  response = (char*)realloc(response, rLen + 5);
836  }
837  size_t bufLen = sprintf(buf, "&v%u=", r + 1);
838  memcpy(response + rPos, buf, bufLen);
839  rPos += bufLen;
840  memcpy(response + rPos, ret[r], sLen);
841  rPos += sLen;
842  delete []ret[r];
843  }
844  response[rPos] = '\0';
845  free(buf);
846  delete []ret;
847  }
848  else
849  {
850  char *errbuf;
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);
855  free(buf);
856  }
857  break;
858  }
859  case 'b':
860  case 'c':
861  case 'd':
862  case 'e':
863  {
864  bool *ret = (bool*)(*handlerList)[i]->handle(*command, value);
865  if(ret)
866  {
867  if(*ret)
868  {
869  buf = urlEncode(ES_OK);
870  response = (char*)malloc(strlen(buf) + 15);
871  sprintf(response, "n=0&e=%d&d=%s", E_OK, buf);
872  free(buf);
873  }
874  else
875  {
876  char *errbuf;
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);
881  free(buf);
882  }
883  delete ret;
884  }
885  break;
886  }
887  case 'k':
888  {
889  bool *ret = (bool*)(*handlerList)[i]->handle(*command, strtoull(value, NULL, 10));
890  if(ret)
891  {
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);
895  free(buf);
896  delete ret;
897  }
898  break;
899  }
900  case 'i':
901  case 'j':
902  {
903  char *smId = httpParser->getByName("p2");
904  if(smId)
905  {
906  bool *ret = (bool*)(*handlerList)[i]->handle(*command, value, atoi(smId));
907  if(ret)
908  {
909  if(*ret)
910  {
911  buf = urlEncode(ES_OK);
912  response = (char*)malloc(strlen(buf) + 15);
913  sprintf(response, "n=0&e=%d&d=%s", E_OK, buf);
914  free(buf);
915  }
916  else
917  {
918  char *errbuf;
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);
923  free(buf);
924  }
925  delete ret;
926  }
927  }
928  break;
929  }
930  case 'f':
931  {
932  char *ret = (char*)(*handlerList)[i]->handle(*command, value);
933  if(ret)
934  {
935  buf = urlEncode(ES_OK);
936  response = (char*)malloc(strlen(buf) + 256);
937  int state = *ret;
938  char *start = ret;
939  ret++;
940  int errNo = *((int*)ret);
941  ret += sizeof(int);
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);
948  free(buf);
949  delete []start;
950  }
951  break;
952  }
953  case 'g':
954  {
955  u_int32_t *count = (u_int32_t*)(*handlerList)[i]->handle(*command, atoi(value));
956  if(count)
957  {
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);
961  free(buf);
962  delete count;
963  }
964  break;
965  }
966  }
967 
968  }
969  else
970  {
971  buf = urlEncode(ES_WRONGPARAM);
972  response = (char*)malloc(512);
973  sprintf(response, "n=0&e=%d&d=%s", E_WRONGPARAM, buf);
974  free(buf);
975  }
976  break;
977  }
978  //***************************Log handler**********************************************************************************
979  case 101:
980  {
981  char *value = httpParser->getByName("p1");
982  if(value)
983  {
984  int *ret = (int*)(*handlerList)[i]->handle(*command, atoi(value));
985  if(ret)
986  {
987  if(*command == 'a' || *command == 'b')
988  {
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);
992  free(buf);
993  delete ret;
994  }
995  if(*command == 'c')
996  {
997  int errNo = *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);
1003  free(errText);
1004  free(buf);
1005  delete []ret;
1006  }
1007  }
1008  else
1009  {
1010  char *errbuf;
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);
1015  free(buf);
1016  }
1017  }
1018  else
1019  {
1020  buf = urlEncode(ES_WRONGPARAM);
1021  response = (char*)malloc(512);
1022  sprintf(response, "n=0&e=%d&d=%s", E_WRONGPARAM, buf);
1023  free(buf);
1024  }
1025  break;
1026  }
1027  //************************************Storage**************************************************************************
1028  case 102:
1029  {
1030  char *name = httpParser->getByName("p1");
1031  if(name && *name)
1032  {
1033  char *ret = (char*)(*handlerList)[i]->handle(*command, name);
1034  if(ret)
1035  {
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);
1039  free(buf);
1040  delete []ret;
1041  }
1042  else
1043  {
1044  char *errbuf;
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);
1049  free(buf);
1050  }
1051  }
1052  else
1053  {
1054  buf = urlEncode(ES_WRONGPARAM);
1055  response = (char*)malloc(512);
1056  sprintf(response, "n=0&e=%d&d=%s", E_WRONGPARAM, buf);
1057  free(buf);
1058  }
1059  break;
1060  }
1061  //************************************COMMON***************************************************************************
1062  case 100:
1063  {
1064  if(*command >= 'a' && *command <= 'f')
1065  {
1066  time_t *retval = (time_t*)(*handlerList)[i]->handle(*command);
1067  if(retval)
1068  {
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);
1072  free(buf);
1073  delete retval;
1074  }
1075  else
1076  {
1077  char *errbuf;
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);
1082  free(buf);
1083  }
1084  }
1085  if(*command == 'g')
1086  {
1087  int *retval = (int*)(*handlerList)[i]->handle(*command);
1088  if(retval)
1089  {
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);
1093  free(buf);
1094  delete retval;
1095  }
1096  else
1097  {
1098  char *errbuf;
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);
1103  free(buf);
1104  }
1105 
1106  }
1107  break;
1108  }
1109  //************************************Admin Part***************************************************************************
1110  case 13:
1111  {
1112  bool *retval = NULL;
1113  unsigned long long *intval = NULL;
1114  if(*command == 's')
1115  {
1116  char *p1 = httpParser->getByName("p1");
1117  if(p1)
1118  {
1119  retval = new bool;
1120  *retval = true;
1121  intval = (unsigned long long*)(*handlerList)[i]->handle(*command, atoi(p1));
1122  }
1123  }
1124  else
1125  {
1126  if(*command == 'd' || *command == 'f')
1127  {
1128  char *p1 = httpParser->getByName("p1");
1129  char *p2 = httpParser->getByName("p2");
1130  if(p1 && p2)
1131  {
1132  retval = (bool*)(*handlerList)[i]->handle(*command, atoi(p1), atoi(p2));
1133  }
1134  }
1135  else
1136  {
1137  char *p1 = httpParser->getByName("p1");
1138  if(p1)
1139  {
1140  retval = (bool*)(*handlerList)[i]->handle(*command, atoi(p1));
1141  }
1142  }
1143  }
1144  response = (char*)malloc(512);
1145  if(retval)
1146  {
1147  if(*retval)
1148  {
1149  buf = urlEncode(ES_OK);
1150  if(intval)
1151  sprintf(response, "n=0&e=%d&d=%s&v1=%u&v2=%u", E_OK, buf, (unsigned int)(*intval >> 32), (unsigned int)(*intval & 0xffffffff));
1152  else
1153  sprintf(response, "n=0&e=%d&d=%s", E_OK, buf);
1154  free(buf);
1155  }
1156  else
1157  {
1158  buf = urlEncode(ES_UNKNOWN);
1159  sprintf(response, "n=0&e=%d&d=%s", E_UNKNOWN, buf);
1160  free(buf);
1161  }
1162  delete retval;
1163  }
1164  else
1165  {
1166  buf = urlEncode(ES_WRONGPARAM);
1167  sprintf(response, "n=0&e=%d&d=%s", E_WRONGPARAM, buf);
1168  free(buf);
1169  }
1170  if(intval)delete intval;
1171  break;
1172  }
1173  case 141:
1174  {
1175  if(*command == 'a' || *command == 'b' || *command == 'c')
1176  {
1177  char *p1 = httpParser->getByName("p1");
1178  if(p1)
1179  {
1180  char *ret = (char*)(*handlerList)[i]->handle(*command, p1);
1181  response = (char*)malloc(512);
1182  if(ret)
1183  {
1184  buf = urlEncode(ES_OK);
1185  sprintf(response, "n=0&e=%d&d=%s", E_OK, buf);
1186  free(buf);
1187  delete ret;
1188  }
1189  else
1190  {
1191  char *errbuf;
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);
1196  free(buf);
1197  }
1198  }
1199  }
1200  break;
1201  }
1202  case 142:
1203  {
1204  switch(*command)
1205  {
1206  case 'a':
1207  case 'b':
1208  {
1209  char *p1 = httpParser->getByName("p1");
1210  if(p1)
1211  {
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);
1216  free(buf);
1217  }
1218  break;
1219  }
1220  case 'c':
1221  {
1222  char *p1 = httpParser->getByName("p1");
1223  char *p2 = httpParser->getByName("p2");
1224  char *p3 = httpParser->getByName("p3");
1225  if(p1 && p2 && p3)
1226  {
1227  char *ret = (char*)(*handlerList)[i]->handle(*command, atoi(p1), atoi(p2), p3);
1228  if(ret)
1229  {
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);
1234  free(buf);
1235  memcpy(response + pos, ret, len + 1);
1236  free(ret);
1237  }
1238  }
1239  break;
1240  }
1241  }
1242  break;
1243  }
1244  case 12:
1245  {
1246  switch(*command)
1247  {
1248  case 'c':
1249  case 'a':
1250  {
1251  char *p1 = httpParser->getByName("p1");
1252  if(p1)
1253  {
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);
1258  free(buf);
1259  }
1260  break;
1261  }
1262  case 'e':
1263  case 'd':
1264  case 'b':
1265  {
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);
1270  free(buf);
1271  break;
1272  }
1273  }
1274  break;
1275  }
1276 
1277  //***************************************************************************************************************
1278  default:
1279  response = (char*)malloc(512);
1280  buf = urlEncode(ES_BADCOMMAND);
1281  sprintf(response, "n=0&e=%d&d=%s", E_BADCOMMAND, buf);
1282  free(buf);
1283  }
1284  break;
1285  }
1286  }
1287  mutex.unlock();
1288  }
1289  }
1290  }
1291  if(!response)
1292  {
1293  response = (char*)malloc(512);
1294  buf = urlEncode(ES_BADCOMMAND);
1295  sprintf(response, "n=0&e=%d&d=%s", E_BADCOMMAND, buf);
1296  free(buf);
1297  }
1298  sendResponse(response);
1299  free(response);
1300  delete httpParser;
1301  free(request);
1302  }
1303  delete clientSock;
1304  }
1305  }
1306  return NULL;
1307 }
1308 
1309 void AdminClient::sendResponse(const char *response)
1310 {
1311  size_t contentLen = strlen(response);
1312  size_t headerLen = 0;
1313  char header[512];
1314  int status;
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);
1318  do
1319  {
1320  while(!clientSock->selectWrite(status) && !exitFlag)
1321  {
1322  if(status != SOCK_OK)break;
1323  if(startTime + timeOut < time(NULL))break;
1324  }
1325  if(status == SOCK_OK && !exitFlag)
1326  {
1327  sendSize += clientSock->write(header + sendSize, headerLen - sendSize, status);
1328  }
1329  }while(sendSize < headerLen && status == SOCK_OK && !exitFlag && startTime + timeOut > time(NULL));
1330  sendSize = 0;
1331  while(sendSize < contentLen && status == SOCK_OK && !exitFlag && startTime + timeOut > time(NULL))
1332  {
1333  while(!clientSock->selectWrite(status) && !exitFlag)
1334  {
1335  if(status != SOCK_OK)break;
1336  if(startTime + timeOut < time(NULL))break;
1337  }
1338  if(status == SOCK_OK && !exitFlag)
1339  {
1340  sendSize += clientSock->write(response + sendSize, contentLen - sendSize, status);
1341  }
1342  }
1343 }
1344 
1345 char* AdminClient::readRequest(size_t &size)
1346 {
1347  size_t partSize = 1024;
1348  char *data = (char*)malloc(partSize + 1);
1349  size_t dataSize = partSize;
1350  bool endRequest = false;
1351  int status = 0;
1352  bool isGet = false, isPost = false;
1353  size_t contentLenght = 0;
1354  time_t startTime = time(NULL), timeOut = 5;
1355  size = 0;
1356  do
1357  {
1358  while(!clientSock->selectRead(status))
1359  {
1360  if(exitFlag || status != SOCK_OK || startTime + timeOut < time(NULL))
1361  {
1362  size = 0;
1363  free(data);
1364  return NULL;
1365  }
1366  }
1367  if(status == SOCK_OK)
1368  {
1369  if(dataSize < size + partSize)
1370  {
1371  dataSize += partSize;
1372  data = (char*)realloc(data, dataSize + 1);
1373  }
1374  size += clientSock->readPart(data + size, partSize, status);
1375  if(status != SOCK_OK)
1376  {
1377  free(data);
1378  size = 0;
1379  return NULL;
1380  }
1381  data[size] = '\0';
1382  if(!isGet && !isPost)
1383  {
1384  char *str = strstr(data, "GET ");
1385  isGet = (str == data);
1386  if(!isGet)
1387  {
1388  str = strstr(data, "POST ");
1389  isPost = (str == data);
1390  }
1391  }
1392  if(isGet && size > 4)
1393  {
1394  endRequest = (data[size - 4] == '\r' && data[size - 3] == '\n' && data[size - 2] == '\r' && data[size - 1] == '\n');
1395  }
1396  if(isPost)
1397  {
1398  char *str = NULL;
1399  if(!contentLenght)
1400  {
1401  str = strstr(data, "Content-Length:");
1402  if(str)
1403  {
1404  str += strlen("Content-Lenght:");
1405  while(*str == ' ' || *str == '\t')str++;
1406  contentLenght = atol(str);
1407  }
1408  }
1409  str = strstr(data, "\r\n\r\n");
1410  if(!str)
1411  {
1412  str = strstr(data, "\n\r\n\r");
1413  if(!str)
1414  {
1415  str = strstr(data, "\n\n");
1416  if(!str)
1417  {
1418  str = strstr(data, "\r\r");
1419  }
1420  }
1421  }
1422  if(str != NULL)
1423  {
1424  endRequest = (size - (str - data) >= contentLenght);
1425  }
1426  }
1427  }
1428  }while(size < MAX_REQUEST_SIZE && status == SOCK_OK && !endRequest && startTime + timeOut > time(NULL));
1429  if(size)
1430  {
1431  data = (char*)realloc(data, size + 1);
1432  }
1433  else
1434  {
1435  free(data);
1436  data = NULL;
1437  }
1438  return data;
1439 }