- Timestamp:
- Feb 2, 2015, 10:04:56 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PublicaMundi_David-devel/zoo-project/zoo-kernel/zoo_loader.c
r512 r549 30 30 */ 31 31 #ifndef WIN32 32 /* 32 33 #include "fcgio.h" 33 34 #include "fcgi_config.h" 34 35 #include "fcgi_stdio.h" 35 #endif 36 */ 37 #include <unistd.h> 38 #include <fcgiapp.h> 39 #endif 40 #include <sys/wait.h> 41 #include <pthread.h> 36 42 #include <sys/types.h> 37 43 #include <unistd.h> … … 56 62 #include <glib.h> 57 63 #include <sys/stat.h> 64 } 65 58 66 #include "service_zcfg.h" 59 } 60 61 #include "service_internal.h" 67 //#include "service_internal.h" 62 68 63 69 xmlXPathObjectPtr extractFromDoc (xmlDocPtr, const char *); 64 int runRequest (map ** );70 int runRequest (map **,struct cgi_env **,FCGX_Request *); 65 71 66 72 using namespace std; … … 74 80 75 81 76 int 77 main (int argc, char *argv[]) 82 static void PrintEnv(FCGX_Stream *out, char *label, char **envp) 78 83 { 79 maps *m; 80 m = (maps *) malloc (MAP_SIZE); 81 conf_read ("main.cfg", m); 82 char ntmp[1024]; 83 #ifndef WIN32 84 getcwd (ntmp, 1024); 85 #else 86 _getcwd (ntmp, 1024); 87 #endif 88 char *rootDir = "/var/www/zoo-wps/cgi-bin"; 89 init_services_conf (rootDir); 90 91 92 while (FCGI_Accept () >= 0) 93 { 94 cgiMain_init (argc, argv); 95 /** 96 * We'll use cgiOut as the default output (stdout) to produce plain text 97 * response. 98 */ 99 dup2 (fileno (cgiOut), fileno (stdout)); 100 101 #ifdef DEBUG 102 fprintf (cgiOut, 103 "Content-Type: text/plain; charset=utf-8\r\nStatus: 200 OK\r\n\r\n"); 104 fprintf (cgiOut, "Welcome on ZOO verbose debuging mode \r\n\r\n"); 105 fflush (cgiOut); 106 fprintf (stderr, "Addr:%s\n", cgiRemoteAddr); 107 fprintf (stderr, "RequestMethod: (%s) %d %d\n", cgiRequestMethod, 108 strncasecmp (cgiRequestMethod, "post", 4), 109 strncmp (cgiContentType, "text/xml", 8) == 0 110 || strncasecmp (cgiRequestMethod, "post", 4) == 0); 111 fprintf (stderr, "Request: %s\n", cgiQueryString); 112 fprintf (stderr, "ContentType: %s\n", cgiContentType); 113 fprintf (stderr, "ContentLength: %d\n", cgiContentLength); 114 fflush (stderr); 115 #endif 116 117 84 FCGX_FPrintF(out, "%s:<br>\n<pre>\n", label); 85 for( ; *envp != NULL; envp++) { 86 FCGX_FPrintF(out, "%s\n", *envp); 87 } 88 FCGX_FPrintF(out, "</pre><p>\n"); 89 } 90 91 #define PATH_SOCKET "/tmp/zoo.sock" 92 #define THREAD_COUNT 50 93 static int counts[THREAD_COUNT]; 94 95 96 int process(FCGX_Request *request){ 97 98 int pid = getpid(); 99 struct cgi_env *cgi; 100 //PrintEnv(request.err, "Request environment", request.envp); 101 cgi = (struct cgi_env*)malloc(sizeof(struct cgi_env)); 102 cgiMain_init (NULL, NULL,&cgi,request); 118 103 char *strQuery = NULL; 119 if (cgi QueryString != NULL)120 strQuery = zStrdup (cgi QueryString);104 if (cgi->cgiQueryString != NULL) 105 strQuery = zStrdup (cgi->cgiQueryString); 121 106 map *tmpMap = NULL; 122 107 123 if (strncmp (cgi ContentType, "text/xml", 8) == 0 ||124 strncasecmp (cgi RequestMethod, "post", 4) == 0)108 if (strncmp (cgi->cgiContentType, "text/xml", 8) == 0 || 109 strncasecmp (cgi->cgiRequestMethod, "post", 4) == 0) 125 110 { 126 if (cgiContentLength == 0) 127 { 128 char *buffer = new char[2]; 129 char *res = NULL; 130 int r = 0; 131 while ((r = fread (buffer, sizeof (char), 1, cgiIn))) 132 { 133 buffer[1] = 0; 134 cgiContentLength += r; 135 if (res == NULL) 136 { 137 res = (char *) malloc (2 * sizeof (char)); 138 sprintf (res, "%s", buffer); 111 if (cgi->cgiContentLength == 0) 112 { 113 char *post_data = NULL; 114 int i = 0; 115 int ch = FCGX_GetChar(request->in); 116 while (ch != -1){ 117 { 118 i++; 119 if (post_data == NULL) 120 { 121 post_data=(char*)malloc(sizeof(char)); 122 post_data[i-1] = (char) ch; 139 123 } 140 124 else 141 125 { 142 char *tmp = zStrdup (res); 143 res = 144 (char *) realloc (res, 145 (strlen (tmp) + 2) * sizeof (char)); 146 sprintf (res, "%s%s", tmp, buffer); 147 free (tmp); 148 } 149 } 150 delete[]buffer; 151 if (res == NULL && (strQuery == NULL || strlen (strQuery) == 0)) 126 post_data=(char*)realloc(post_data,i*sizeof(char)); 127 post_data[i-1] = (char) ch; 128 } 129 ch = FCGX_GetChar(request->in); 130 if (ch == -1 ){ 131 post_data=(char*)realloc(post_data,(i + 1)*sizeof(char)); 132 post_data[i] = '\0'; 133 } 134 } 135 cgi->cgiContentLength = i; 136 if (post_data == NULL && (strQuery == NULL || strlen (strQuery) == 0)) 152 137 { 153 138 return errorException (NULL, 154 139 "ZOO-Kernel failed to process your request cause the request was emtpty.", 155 "InternalError", NULL );140 "InternalError", NULL,request->out); 156 141 } 157 142 else 158 143 { 159 144 if (strQuery == NULL || strlen (strQuery) == 0) 160 tmpMap = createMap ("request", res); 161 } 162 if (res != NULL) 163 free (res); 145 tmpMap = createMap ("request", post_data); 146 } 147 if (post_data != NULL) 148 free (post_data); 149 } 164 150 } 165 151 else 166 152 { 167 char *buffer = new char[cgiContentLength + 1]; 168 if (fread (buffer, sizeof (char), cgiContentLength, cgiIn) > 0) 169 { 170 buffer[cgiContentLength] = 0; 171 tmpMap = createMap ("request", buffer); 153 char *post_data = new char[cgi->cgiContentLength + 1]; 154 int r = FCGX_GetStr(post_data,cgi->cgiContentLength,request->in); 155 if ( r > 0) 156 { 157 post_data[r] = '\0'; 158 cgi->cgiContentLength = r; 159 tmpMap = createMap ("request", post_data); 172 160 } 173 161 else 174 162 { 175 buffer[0] = 0;163 post_data[0] = '\0'; 176 164 char **array, **arrayStep; 177 if (cgiFormEntries (&array ) != cgiFormSuccess)165 if (cgiFormEntries (&array,&cgi) != cgiFormSuccess) 178 166 { 179 167 return 1; … … 182 170 while (*arrayStep) 183 171 { 184 char *ivalue = new char[cgi ContentLength];172 char *ivalue = new char[cgi->cgiContentLength]; 185 173 cgiFormStringNoNewlines (*arrayStep, ivalue, 186 cgi ContentLength);174 cgi->cgiContentLength,&cgi); 187 175 char *tmpValueFinal = 188 176 (char *) … … 190 178 1) * sizeof (char)); 191 179 sprintf (tmpValueFinal, "%s=%s", *arrayStep, ivalue); 192 if (strlen ( buffer) == 0)180 if (strlen (post_data) == 0) 193 181 { 194 sprintf ( buffer, "%s", tmpValueFinal);182 sprintf (post_data, "%s", tmpValueFinal); 195 183 } 196 184 else 197 185 { 198 char *tmp = zStrdup ( buffer);199 sprintf ( buffer, "%s&%s", tmp, tmpValueFinal);186 char *tmp = zStrdup (post_data); 187 sprintf (post_data, "%s&%s", tmp, tmpValueFinal); 200 188 free (tmp); 201 189 } … … 209 197 } 210 198 if (tmpMap != NULL) 211 addToMap (tmpMap, "request", buffer);199 addToMap (tmpMap, "request", post_data); 212 200 else 213 tmpMap = createMap ("request", buffer);214 } 215 delete[] buffer;201 tmpMap = createMap ("request", post_data); 202 } 203 delete[]post_data; 216 204 } 217 205 } … … 221 209 dumpMap (tmpMap); 222 210 #endif 223 char **array, **arrayStep; 224 if (cgiFormEntries (&array) != cgiFormSuccess) 211 212 char **array, **arrayStep; 213 if (cgiFormEntries (&array,&cgi) != cgiFormSuccess) 225 214 { 226 215 return 1; … … 229 218 while (*arrayStep) 230 219 { 231 char *value = new char[cgi ContentLength];232 cgiFormStringNoNewlines (*arrayStep, value, cgi ContentLength);220 char *value = new char[cgi->cgiContentLength]; 221 cgiFormStringNoNewlines (*arrayStep, value, cgi->cgiContentLength,&cgi); 233 222 #ifdef DEBUG 234 223 fprintf (stderr, "(( \n %s \n %s \n ))", *arrayStep, value); … … 265 254 * one. 266 255 */ 267 if (strncasecmp (cgi RequestMethod, "post", 4) == 0 ||256 if (strncasecmp (cgi->cgiRequestMethod, "post", 4) == 0 || 268 257 (count (tmpMap) == 1 && strncmp (tmpMap->value, "<", 1) == 0) 269 258 #ifdef WIN32 … … 280 269 addToMap (tmpMap, "xrequest", t1->value); 281 270 xmlInitParser (); 282 xmlDocPtr doc = xmlParseMemory (t1->value, cgi ContentLength);271 xmlDocPtr doc = xmlParseMemory (t1->value, cgi->cgiContentLength); 283 272 { 284 273 xmlXPathObjectPtr reqptr = extractFromDoc (doc, … … 384 373 char *identifiers = NULL; 385 374 identifiers = 386 (char *) calloc (cgi ContentLength, sizeof (char));375 (char *) calloc (cgi->cgiContentLength, sizeof (char)); 387 376 identifiers[0] = 0; 388 377 for (int k = 0; k < id->nodeNr; k++) … … 427 416 428 417 char *token, *saveptr; 429 token = strtok_r (cgi QueryString, "&", &saveptr);418 token = strtok_r (cgi->cgiQueryString, "&", &saveptr); 430 419 while (token != NULL) 431 420 { … … 452 441 } 453 442 454 if (strncasecmp (cgi ContentType, "multipart/form-data", 19) == 0)443 if (strncasecmp (cgi->cgiContentType, "multipart/form-data", 19) == 0) 455 444 { 456 445 map *tmp = getMap (tmpMap, "dataInputs"); … … 465 454 free (strQuery); 466 455 467 runRequest (&tmpMap );456 runRequest (&tmpMap,&cgi,request); 468 457 469 458 /** … … 475 464 free (tmpMap); 476 465 } 477 cgiFreeResources (); 466 // a verifier fait planter 467 cgiFreeResources (&cgi); 468 469 FCGX_Finish_r(request); 470 return 0; 471 } 472 473 474 475 int 476 main (int argc, char *argv[]) 477 { 478 maps *main_config; 479 int max_requests, start_servers; 480 start_servers = 10; 481 max_requests= 100; 482 main_config = (maps *) malloc (MAP_SIZE); 483 conf_read ("main.cfg", main_config); 484 char ntmp[1024]; 485 #ifndef WIN32 486 getcwd (ntmp, 1024); 487 #else 488 _getcwd (ntmp, 1024); 489 #endif 490 char *rootDir = "/var/www/zoo-wps/cgi-bin"; 491 int fork_status = fork(); 492 if (fork_status == 0){ 493 //child 494 int forker_pid = getpid(); 495 init_services_conf (rootDir); 496 int sock = FCGX_OpenSocket(PATH_SOCKET, 1000); 497 FCGX_Init(); 498 FCGX_Request request; 499 FCGX_InitRequest(&request, sock, 0); 500 int i; 501 int count_request = 0; 502 for (i = 0; i< start_servers; i++){ 503 fork_status = fork(); 504 if (fork_status == 0){ 505 fprintf(stderr,"child %d \n",i); 506 fflush(stderr); 507 break; 508 } 478 509 } 479 FCGI_Finish (); 510 while(1){ 511 if (forker_pid != getpid()){ 512 while(FCGX_Accept_r(&request) == 0){ 513 process(&request); 514 count_request ++; 515 if (count_request >= max_requests){ 516 fprintf(stderr,"Max request stop process\n"); 517 fflush(stderr); 518 exit(0); 519 } 520 } 521 } 522 else { 523 wait(0); 524 fprintf(stderr,"new child\n"); 525 fflush(stderr); 526 fork(); 527 } 528 } 529 } 530 else { 531 532 while(1); 533 534 535 536 } 537 480 538 return 0; 481 482 539 } 540
Note: See TracChangeset
for help on using the changeset viewer.