Changeset 18920 for trunk/FACT++/dim/src/webDid/webServer.c
- Timestamp:
- 10/18/17 13:54:41 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/dim/src/webDid/webServer.c
r18058 r18920 5 5 #include <fcntl.h> 6 6 7 int WebDID_Debug = 0; 7 extern int WebDID_Debug; 8 extern int IsWebDID; 8 9 9 10 #define BUFSIZE 8096 … … 83 84 } 84 85 85 int getParameters(char *buffer, char (*pars)[], char *ptrs[] )86 int getParameters(char *buffer, char (*pars)[], char *ptrs[], int nmandatory) 86 87 { 87 88 char *ptr, *parptr; … … 106 107 for(i = 0; ptrs[i]; i++) 107 108 { 108 109 if((ptr = strchr(ptrs[i],'&'))) 109 110 *ptr = '\0'; 110 111 while((ptr = strchr(ptrs[i],'%'))) … … 121 122 } 122 123 } 123 if(found == n) 124 return 1; 125 else 126 return 0; 124 if(nmandatory == -1) 125 { 126 if(found == n) 127 return 1; 128 } 129 else if(found >= nmandatory) 130 { 131 return found; 132 } 133 return 0; 127 134 } 128 135 … … 136 143 strcpy(pars[1],"browser="); 137 144 pars[2][0] = '\0'; 138 ret = getParameters(buffer, pars, ptrs );145 ret = getParameters(buffer, pars, ptrs, -1); 139 146 if(!ret) 140 147 return 0; … … 158 165 strcpy(pars[3],"browser="); 159 166 pars[4][0] = '\0'; 160 ret = getParameters(buffer, pars, ptrs );167 ret = getParameters(buffer, pars, ptrs, -1); 161 168 if(!ret) 162 169 return 0; … … 181 188 strcpy(pars[3],"force="); 182 189 pars[4][0] = '\0'; 183 ret = getParameters(buffer, pars, ptrs );190 ret = getParameters(buffer, pars, ptrs, -1); 184 191 if(!ret) 185 192 return 0; … … 190 197 if(WebDID_Debug) 191 198 printf("\nparse service pars - service %s %d %d %d\n\n",service, *req, *browser, *force); 199 return 1; 200 } 201 202 int getServiceParametersDim(char *buffer, char *service, void *nolink, int *nolinksize, int *update) 203 { 204 char pars[10][32]; 205 char *ptrs[10]; 206 int ret; 207 char nolinkstr[MAX_NAME] = { '\0' }, updatestr[10] = { '\0' }; 208 char servicestr[MAX_NAME]; 209 210 strcpy(pars[0], "service="); 211 strcpy(pars[1], "nolink="); 212 strcpy(pars[2], "update="); 213 pars[3][0] = '\0'; 214 ret = getParameters(buffer, pars, ptrs, 1); 215 if (!ret) 216 return 0; 217 strcpy(servicestr, ptrs[0]); 218 if (ptrs[1]) 219 strcpy(nolinkstr, ptrs[1]); 220 if (ptrs[2]) 221 strcpy(updatestr, ptrs[2]); 222 // sscanf(ptrs[1], "%d", req); 223 // sscanf(ptrs[2], "%d", browser); 224 // sscanf(ptrs[3], "%d", force); 225 if (servicestr[0] == '"') 226 { 227 strcpy((char *)service, &servicestr[1]); 228 ((char *)service)[strlen(servicestr) - 2] = '\0'; 229 } 230 else 231 { 232 strcpy(service, ptrs[0]); 233 } 234 if ((nolink) && (nolinksize)) 235 { 236 if(nolinkstr[0] != '\0') 237 { 238 if (nolinkstr[0] == '"') 239 { 240 strcpy((char *)nolink, &nolinkstr[1]); 241 ((char *)nolink)[strlen(nolinkstr) - 2] = '\0'; 242 *nolinksize = strlen(nolinkstr) - 2 + 1; 243 } 244 else 245 { 246 sscanf(nolinkstr, "%d", (int *)nolink); 247 *nolinksize = sizeof(int); 248 } 249 } 250 else 251 { 252 *((int *)nolink) = -1; 253 *nolinksize = sizeof(int); 254 } 255 } 256 if (update) 257 { 258 if (updatestr[0] != '\0') 259 { 260 sscanf(updatestr, "%d", (int *)update); 261 } 262 } 263 if (WebDID_Debug) 264 printf("\nparse service pars - service %s %s %s\n\n", service, nolinkstr, updatestr); 192 265 return 1; 193 266 } … … 238 311 } 239 312 240 void sendData(int conn_id, char *buffer, int type )313 void sendData(int conn_id, char *buffer, int type, int oper) 241 314 { 242 315 static char date_buffer[128]; … … 250 323 extern char *getJSONHeader(); 251 324 extern char *getJSONBuffer(); 325 extern char *getJSONDimBuffer(); 252 326 char datatype[128]; 253 327 char *conv_buffer; 328 int conv_size = 0; 329 char nolink[MAX_NAME]; 330 int nolinksize; 331 int update; 254 332 255 333 conv_buffer = buffer; … … 287 365 else 288 366 { 289 update_service_data(service, conn_id, 0, req, browser, force );367 update_service_data(service, conn_id, 0, req, browser, force, 1, 0, 0); 290 368 return; 291 369 } … … 296 374 if(WebDID_Debug) 297 375 printf("%s\n",ptr); 376 strcpy(datatype,"text/html"); 377 } 378 else if(type == 10) 379 { 380 ret = getServiceParametersDim(conv_buffer, service, nolink, &nolinksize, &update); 381 if(!ret) 382 { 383 strcpy(snd_data_buffer,"{}"); 384 ptr = snd_data_buffer; 385 } 386 else 387 { 388 if (oper == 0) 389 update = -1; 390 else if (oper == -1) 391 update = -2; 392 update_service_data(service, conn_id, update, 0, conn_id, 1, 0, nolink, nolinksize); 393 return; 394 } 395 } 396 else if(type == 11) 397 { 398 ptr = conv_buffer; 399 if(WebDID_Debug) 400 printf("%s\n",ptr); 401 // strcpy(datatype,"application/octet-stream"); 298 402 strcpy(datatype,"text/html"); 299 403 } … … 330 434 } 331 435 } 332 getTime(date_buffer); 333 (void)sprintf(snd_buffer,"HTTP/1.1 200 OK\r\nDate: %s\r\nServer: DID/19.7\r\nContent-Length: %d\r\nContent-Type: %s\r\nConnection: close\r\n\r\n", 334 date_buffer, (int)strlen(ptr), datatype); 335 (void)web_write(conn_id,snd_buffer,(int)strlen(snd_buffer)); 436 else if (type == 12) 437 { 438 ret = getServiceParametersDim(conv_buffer, service, 0, 0, 0); 439 if (!ret) 440 { 441 strcpy(snd_data_buffer, "{}"); 442 ptr = snd_data_buffer; 443 } 444 else 445 { 446 find_services(service, conn_id, conn_id, 1); 447 ptr = getJSONDimBuffer("src", conn_id); 448 } 449 } 450 if (IsWebDID) 451 { 452 getTime(date_buffer); 453 /* 454 (void)sprintf(snd_buffer, "HTTP/1.1 200 OK\r\nDate: %s\r\nServer: DID/19.7\r\nContent-Length: %d\r\nContent-Type: %s\r\nKeep-Alive: timeout=1000\r\nConnection: keep-alive\r\n\r\n", 455 date_buffer, (int)strlen(ptr), datatype); 456 */ 457 (void)sprintf(snd_buffer, "HTTP/1.1 200 OK\r\nDate: %s\r\nServer: DID/19.7\r\nContent-Length: %d\r\nContent-Type: %s\r\nConnection: close\r\n\r\n", 458 date_buffer, (int)strlen(ptr), datatype); 459 (void)web_write(conn_id, snd_buffer, (int)strlen(snd_buffer)); 460 } 336 461 if(WebDID_Debug) 337 462 printf("SENDING DATA to conn %d:\n%s\n",conn_id, snd_buffer); … … 391 516 else 392 517 { 393 update_service_data(service, conn_id, 0, req, browser, force );518 update_service_data(service, conn_id, 0, req, browser, force, 0, 0); 394 519 return; 395 520 } … … 434 559 } 435 560 } 436 getTime(date_buffer); 437 (void)sprintf(snd_buffer,"HTTP/1.1 200 OK\r\nDate: %s\r\nServer: DID/19.7\r\nContent-Length: %d\r\nContent-Type: %s\r\nConnection: close\r\n\r\n", 438 date_buffer, (int)strlen(ptr), datatype); 439 (void)web_write(conn_id,snd_buffer,(int)strlen(snd_buffer)); 561 if (IsWebDID) 562 { 563 getTime(date_buffer); 564 (void)sprintf(snd_buffer, "HTTP/1.1 200 OK\r\nDate: %s\r\nServer: DID/19.7\r\nContent-Length: %d\r\nContent-Type: %s\r\nConnection: close\r\n\r\n", 565 date_buffer, (int)strlen(ptr), datatype); 566 (void)web_write(conn_id, snd_buffer, (int)strlen(snd_buffer)); 567 } 440 568 if(WebDID_Debug) 441 569 printf("SENDING DATA to conn %d:\n%s\n",conn_id, snd_buffer); … … 453 581 static char snd_buffer[BUFSIZE+1]; /* static so zero filled */ 454 582 static char date_buffer[128]; 583 char *ptr; 584 int operation = 0; 455 585 456 586 … … 462 592 if(WebDID_Debug) 463 593 printf("Got %s\n", buffer); 464 if( strncmp(buffer,"GET ",4) && strncmp(buffer,"get ",4) ) 465 { 466 log_it(SORRY,"Only simple GET operation supported",buffer,conn_id); 467 return; 468 } 469 594 if (IsWebDID) 595 { 596 if (strncmp(buffer, "GET ", 4) && strncmp(buffer, "get ", 4)) 597 { 598 log_it(SORRY, "Only simple GET operation supported", buffer, conn_id); 599 return; 600 } 601 } 470 602 for(i=4;i<BUFSIZE;i++) 471 603 { /* null terminate after the second space to ignore extra stuff */ … … 479 611 if(WebDID_Debug) 480 612 printf("Got 1 %s\n", buffer); 481 for(j=0;j<i-1;j++) /* check for illegal parent directory use .. */613 for(j=0;j<i-1;j++) /* check for illegal parent directory use .. */ 482 614 { 483 615 if(buffer[j] == '.' && buffer[j+1] == '.') … … 516 648 printf("SENDING to conn %d:\n%s\n",conn_id, snd_buffer); 517 649 */ 650 ptr = &buffer[5]; 651 if (!IsWebDID) 652 { 653 if (!strncmp(&buffer[0], "GET", 3) || !strncmp(&buffer[0], "get", 3)) 654 { 655 operation = 0; 656 } 657 if (!strncmp(&buffer[0], "SUBSCRIBE", 9) || !strncmp(&buffer[0], "subscribe", 9)) 658 { 659 operation = 1; 660 ptr = &buffer[11]; 661 } 662 if (!strncmp(&buffer[0], "UNSUBSCRIBE", 11) || !strncmp(&buffer[0], "unsubscribe", 11)) 663 { 664 operation = -1; 665 ptr = &buffer[13]; 666 } 667 } 518 668 if(fstr == 0) 519 669 { 520 670 if(WebDID_Debug) 521 671 printf("Got %s\n", buffer); 522 if(!strncmp(&buffer[5],"didHeader",9)) 523 { 524 sendData(conn_id, &buffer[5], 0); 525 return; 526 } 527 else if(!strncmp(&buffer[5],"didData",7)) 528 { 529 sendData(conn_id, &buffer[5], 1); 530 return; 531 } 532 else if(!strncmp(&buffer[5],"didServices",11)) 533 { 534 sendData(conn_id, &buffer[5], 2); 535 return; 536 } 537 else if(!strncmp(&buffer[5],"didServiceData",14)) 538 { 539 sendData(conn_id, &buffer[5], 3); 540 return; 541 } 542 else if(!strncmp(&buffer[5],"didPoll",7)) 543 { 544 sendData(conn_id, &buffer[5], 5); 545 return; 546 } 547 else if(!strncmp(&buffer[5],"didQuery",8)) 548 { 549 sendData(conn_id, &buffer[5], 6); 550 return; 551 } 552 else if(!strncmp(&buffer[5],"smiData",7)) 553 { 554 sendSmiData(conn_id, &buffer[5], 1); 555 return; 556 } 557 else if(!strncmp(&buffer[5],"smiObjects",10)) 558 { 559 sendSmiData(conn_id, &buffer[5], 2); 672 if (!strncmp(ptr, "didHeader", 9)) 673 { 674 sendData(conn_id, ptr, 0, 0); 675 return; 676 } 677 else if (!strncmp(ptr, "didData", 7)) 678 { 679 sendData(conn_id, ptr, 1, 0); 680 return; 681 } 682 else if (!strncmp(ptr, "didServices", 11)) 683 { 684 sendData(conn_id, ptr, 2, 0); 685 return; 686 } 687 else if (!strncmp(ptr, "didServiceData", 14)) 688 { 689 sendData(conn_id, ptr, 3, 0); 690 return; 691 } 692 else if (!strncmp(ptr, "dimService", 10)) 693 { 694 sendData(conn_id, ptr, 10, operation); 695 return; 696 } 697 else if (!strncmp(ptr, "didPoll", 7)) 698 { 699 sendData(conn_id, ptr, 5, 0); 700 return; 701 } 702 else if (!strncmp(ptr, "didQuery", 8)) 703 { 704 sendData(conn_id, ptr, 6, 0); 705 return; 706 } 707 else if (!strncmp(ptr, "dimBrowser", 10)) 708 { 709 sendData(conn_id, ptr, 12, 0); 710 return; 711 } 712 else if (!strncmp(ptr, "smiData", 7)) 713 { 714 sendSmiData(conn_id, ptr, 1); 715 return; 716 } 717 else if (!strncmp(ptr, "smiObjects", 10)) 718 { 719 sendSmiData(conn_id, ptr, 2); 560 720 return; 561 721 } … … 635 795 static void handler( int conn_id, char *packet, int size, int status ) 636 796 { 797 int close_browser(); 798 637 799 switch(status) 638 800 { … … 644 806 Net_conns[conn_id].task,Net_conns[conn_id].node ); 645 807 } 646 web_close(conn_id); 808 close_browser(conn_id); 809 web_close(conn_id); 647 810 break; 648 811 case STA_CONN: /* connection received */ … … 707 870 *ptr = '\0'; 708 871 } 709 chdir(currwd);710 log_it(LOG,"webDi dstarting",argv[1],getpid());872 // chdir(currwd); 873 log_it(LOG,"webDim starting",argv[1],getpid()); 711 874 /* setup the network socket */ 712 875 proto = 1; … … 714 877 get_node_name(dns_node); 715 878 did_init(dns_node, DNS_PORT); 716 if(!web_open_server("DID",handler, &proto, &port, error_handler)) 717 return(0); 879 if(IsWebDID) 880 { 881 if (!web_open_server("DID", handler, &proto, &port, error_handler)) 882 return(0); 883 } 884 else 885 { 886 if (!web_open_server("DimClient", handler, &proto, &port, error_handler)) 887 return(0); 888 } 718 889 /* 719 890 ret = matchString("hello world","*ll*");
Note:
See TracChangeset
for help on using the changeset viewer.