source: trunk/FACT++/dim/src/webDid/webServer.c@ 16336

Last change on this file since 16336 was 15282, checked in by tbretz, 12 years ago
Updated to v20r7.
File size: 16.9 KB
Line 
1#include <dim.h>
2
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6
7int WebDID_Debug = 0;
8
9#define BUFSIZE 8096
10#define WERROR 42
11#define SORRY 43
12#define LOG 44
13
14#ifndef WIN32
15#define O_BINARY 0
16#endif
17
18struct {
19 char *ext;
20 char *filetype;
21} extensions [] = {
22 {"gif", "image/gif" },
23 {"jpg", "image/jpeg"},
24 {"jpeg","image/jpeg"},
25 {"png", "image/png" },
26 {"zip", "image/zip" },
27 {"gz", "image/gz" },
28 {"tar", "image/tar" },
29 {"htm", "text/html" },
30 {"html","text/html" },
31 {"js","text/javascript" },
32 {"css","text/css" },
33 {"php","text/php" },
34 {"json","application/json" },
35 {"ico","image/x-icon" },
36 {0,0} };
37
38extern int web_open_server();
39extern void web_write();
40extern int web_close();
41extern int did_init();
42extern int check_browser_changes();
43extern int find_services();
44
45void getTime(char *buffer)
46{
47 time_t nowtime;
48 struct tm *nowtm;
49
50 nowtime = time((time_t *)0);
51 nowtm = (struct tm *)gmtime(&nowtime);
52 strftime(buffer, 128, "%a, %d %b %Y %H:%M:%S GMT", nowtm);
53}
54
55void log_it(int type, char *s1, char *s2, int conn_id)
56{
57 char logbuffer[BUFSIZE*2];
58 static char date_buffer[128];
59 static char snd_buffer[BUFSIZE+1]; /* static so zero filled */
60
61 switch (type) {
62 case WERROR: (void)printf("ERROR: %s:%s exiting pid=%d\n",s1, s2, getpid()); break;
63 case SORRY:
64 (void)sprintf(logbuffer, "<HTML><BODY><H1>webDid: %s %s</H1></BODY></HTML>\r\n", s1, s2);
65 dim_print_date_time();
66 (void)printf("webDid: %s %s\n",s1, s2);
67 getTime(date_buffer);
68 (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",
69 date_buffer, (int)strlen(logbuffer), "text/html");
70 (void)web_write(conn_id,snd_buffer,(int)strlen(snd_buffer));
71 (void)web_write(conn_id,logbuffer,(int)strlen(logbuffer));
72 break;
73 case LOG: (void)printf("INFO: %s:%s:%d\n",s1, s2,conn_id);
74 break;
75 }
76/*
77 if(type == WERROR || type == SORRY)
78 {
79 sleep(60);
80 exit(3);
81 }
82*/
83}
84
85int getParameters(char *buffer, char (*pars)[], char *ptrs[])
86{
87 char *ptr, *parptr;
88 int i, j, n = 0, found = 0;
89 int code;
90
91 if(!strchr(buffer,'?'))
92 return 0;
93 parptr = (char *)pars;
94 for(i = 0; *parptr; i++)
95 {
96 n++;
97 ptrs[i] = 0;
98 if((ptr = strstr(buffer, parptr)))
99 {
100 ptrs[i] = ptr+(int)strlen(parptr);
101 found++;
102 }
103 parptr += 32;
104 }
105 ptrs[i] = 0;
106 for(i = 0; ptrs[i]; i++)
107 {
108 if((ptr = strchr(ptrs[i],'&')))
109 *ptr = '\0';
110 while((ptr = strchr(ptrs[i],'%')))
111 {
112 sscanf((ptr + 1),"%2X",&code);
113 sprintf(ptr,"%c",(char)code);
114 ptr++;
115 for(j = 0; *(ptr + 2); j++)
116 {
117 *ptr = *(ptr+2);
118 ptr++;
119 }
120 *ptr = '\0';
121 }
122 }
123 if(found == n)
124 return 1;
125 else
126 return 0;
127}
128
129int getNodeParameters(char *buffer, char *node, int *browser)
130{
131 char pars[4][32];
132 char *ptrs[4];
133 int ret;
134
135 strcpy(pars[0],"node=");
136 strcpy(pars[1],"browser=");
137 pars[2][0] = '\0';
138 ret = getParameters(buffer, pars, ptrs);
139 if(!ret)
140 return 0;
141 strcpy(node, ptrs[0]);
142 sscanf(ptrs[1],"%d",browser);
143if(WebDID_Debug)
144 printf("parse pars - node %s, browser id %d\n", node, *browser);
145 return 1;
146}
147
148
149int getServerParameters(char *buffer, char *node, char *server, int *pid, int *browser)
150{
151 char pars[10][32];
152 char *ptrs[10];
153 int ret;
154
155 strcpy(pars[0],"dimnode=");
156 strcpy(pars[1],"dimserver=");
157 strcpy(pars[2],"dimserverid=");
158 strcpy(pars[3],"browser=");
159 pars[4][0] = '\0';
160 ret = getParameters(buffer, pars, ptrs);
161 if(!ret)
162 return 0;
163 strcpy(node, ptrs[0]);
164 strcpy(server, ptrs[1]);
165 sscanf(ptrs[2],"%d",pid);
166 sscanf(ptrs[3],"%d",browser);
167if(WebDID_Debug)
168printf("parse pars - node %s, server %s, pid %d, browser %d\n",node, server, *pid, *browser);
169 return 1;
170}
171
172int getServiceParameters(char *buffer, char *service, int *req, int* browser, int *force)
173{
174 char pars[10][32];
175 char *ptrs[10];
176 int ret;
177
178 strcpy(pars[0],"dimservice=");
179 strcpy(pars[1],"reqNr=");
180 strcpy(pars[2],"reqId=");
181 strcpy(pars[3],"force=");
182 pars[4][0] = '\0';
183 ret = getParameters(buffer, pars, ptrs);
184 if(!ret)
185 return 0;
186 strcpy(service, ptrs[0]);
187 sscanf(ptrs[1],"%d",req);
188 sscanf(ptrs[2],"%d",browser);
189 sscanf(ptrs[3],"%d",force);
190if(WebDID_Debug)
191printf("\nparse service pars - service %s %d %d %d\n\n",service, *req, *browser, *force);
192 return 1;
193}
194
195
196extern char JSONHeader[];
197extern char *JSONBuffer;
198
199static char *conv_buffer = 0;
200static int conv_buffer_size = 0;
201
202char *unescape(char *buffer)
203{
204 int buffer_size;
205 char *ptr, *ptr1;
206 int code;
207
208 buffer_size = (int)strlen(buffer) + 1;
209 if(buffer_size > conv_buffer_size )
210 {
211 if(conv_buffer_size)
212 free(conv_buffer);
213 conv_buffer = malloc((size_t)buffer_size);
214 conv_buffer_size = buffer_size;
215 }
216 ptr = buffer;
217 ptr1 = conv_buffer;
218 while(*ptr)
219 {
220 if(*ptr != '%')
221 {
222 *ptr1 = *ptr;
223 ptr++;
224 ptr1++;
225 }
226 else
227 {
228 ptr++;
229 sscanf(ptr,"%2X",&code);
230 sprintf(ptr1,"%c",code);
231 ptr += 2;
232 ptr1++;
233 *ptr1 = '\0';
234 }
235 }
236 *ptr1 = '\0';
237 return conv_buffer;
238}
239
240void sendData(int conn_id, char *buffer, int type)
241{
242 static char date_buffer[128];
243 static char snd_buffer[BUFSIZE+1]; /* static so zero filled */
244 static char snd_data_buffer[BUFSIZE+1]; /* static so zero filled */
245 char *ptr = 0;
246 char node[128], server[256], service[256];
247 int pid, ret, req, browser, force;
248 extern char *update_services();
249 extern char *update_service_data();
250 extern char *getJSONHeader();
251 extern char *getJSONBuffer();
252 char datatype[128];
253 char *conv_buffer;
254
255 conv_buffer = buffer;
256 strcpy(datatype,"application/json");
257 if(type == 0)
258 {
259 ptr = getJSONHeader();
260 }
261 else if(type == 1)
262 {
263 ret = getNodeParameters(conv_buffer, node, &browser);
264 ptr = getJSONBuffer(node, browser);
265 }
266 else if(type == 2)
267 {
268 ret = getServerParameters(conv_buffer, node, server, &pid, &browser);
269 if(!ret)
270 {
271 strcpy(snd_data_buffer,"{}");
272 ptr = snd_data_buffer;
273 }
274 else
275 {
276 ptr = update_services(node, server, pid, browser);
277 }
278 }
279 else if(type == 3)
280 {
281 ret = getServiceParameters(conv_buffer, service, &req, &browser, &force);
282 if(!ret)
283 {
284 strcpy(snd_data_buffer,"{}");
285 ptr = snd_data_buffer;
286 }
287 else
288 {
289 update_service_data(service, conn_id, 0, req, browser, force);
290 return;
291 }
292 }
293 else if(type == 4)
294 {
295 ptr = conv_buffer;
296if(WebDID_Debug)
297 printf("%s\n",ptr);
298 strcpy(datatype,"text/html");
299 }
300 else if(type == 5)
301 {
302 ret = getServiceParameters(conv_buffer, service, &req, &browser, &force);
303 if(!ret)
304 {
305 strcpy(snd_data_buffer,"{}");
306 ptr = snd_data_buffer;
307 }
308 else
309 {
310 check_browser_changes(service, conn_id, 0, req, browser, force);
311 return;
312 }
313 }
314 else if(type == 6)
315 {
316 ret = getServiceParameters(conv_buffer, service, &req, &browser, &force);
317 if(!ret)
318 {
319 strcpy(snd_data_buffer,"{}");
320 ptr = snd_data_buffer;
321 }
322 else
323 {
324 find_services(service, conn_id, browser, force);
325 if(force == -1)
326 strcpy(snd_data_buffer,"");
327 else
328 strcpy(snd_data_buffer,"load");
329 ptr = snd_data_buffer;
330 }
331 }
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));
336if(WebDID_Debug)
337 printf("SENDING DATA to conn %d:\n%s\n",conn_id, snd_buffer);
338 (void)web_write(conn_id,ptr,(int)strlen(ptr));
339if(WebDID_Debug == 2)
340 printf("SENDING DATA to conn %d:\n%s\n",conn_id, ptr);
341}
342
343
344void sendSmiData(int conn_id, char *buffer, int type)
345{
346 static char date_buffer[128];
347 static char snd_buffer[BUFSIZE+1]; /* static so zero filled */
348 static char snd_data_buffer[BUFSIZE+1]; /* static so zero filled */
349 char *ptr = 0;
350 char node[128], server[256], service[256];
351 int pid, ret, req, browser, force;
352 extern char *update_services();
353 extern char *update_service_data();
354 extern char *getJSONHeader();
355 extern char *getJSONBuffer();
356 char datatype[128];
357 char *conv_buffer;
358
359 conv_buffer = buffer;
360 strcpy(datatype,"application/json");
361 if(type == 0)
362 {
363 ptr = getJSONHeader();
364 }
365 else if(type == 1)
366 {
367 ret = getNodeParameters(conv_buffer, node, &browser);
368 ptr = getJSONBuffer(node, browser);
369 }
370 else if(type == 2)
371 {
372 ret = getServerParameters(conv_buffer, node, server, &pid, &browser);
373 if(!ret)
374 {
375 strcpy(snd_data_buffer,"{}");
376 ptr = snd_data_buffer;
377 }
378 else
379 {
380 ptr = update_services(node, server, pid, browser);
381 }
382 }
383 else if(type == 3)
384 {
385 ret = getServiceParameters(conv_buffer, service, &req, &browser, &force);
386 if(!ret)
387 {
388 strcpy(snd_data_buffer,"{}");
389 ptr = snd_data_buffer;
390 }
391 else
392 {
393 update_service_data(service, conn_id, 0, req, browser, force);
394 return;
395 }
396 }
397 else if(type == 4)
398 {
399 ptr = conv_buffer;
400if(WebDID_Debug)
401 printf("%s\n",ptr);
402 strcpy(datatype,"text/html");
403 }
404 else if(type == 5)
405 {
406 ret = getServiceParameters(conv_buffer, service, &req, &browser, &force);
407 if(!ret)
408 {
409 strcpy(snd_data_buffer,"{}");
410 ptr = snd_data_buffer;
411 }
412 else
413 {
414 check_browser_changes(service, conn_id, 0, req, browser, force);
415 return;
416 }
417 }
418 else if(type == 6)
419 {
420 ret = getServiceParameters(conv_buffer, service, &req, &browser, &force);
421 if(!ret)
422 {
423 strcpy(snd_data_buffer,"{}");
424 ptr = snd_data_buffer;
425 }
426 else
427 {
428 find_services(service, conn_id, browser, force);
429 if(force == -1)
430 strcpy(snd_data_buffer,"");
431 else
432 strcpy(snd_data_buffer,"load");
433 ptr = snd_data_buffer;
434 }
435 }
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));
440if(WebDID_Debug)
441 printf("SENDING DATA to conn %d:\n%s\n",conn_id, snd_buffer);
442 (void)web_write(conn_id,ptr,(int)strlen(ptr));
443if(WebDID_Debug == 2)
444 printf("SENDING DATA to conn %d:\n%s\n",conn_id, ptr);
445}
446
447void sendFile(int conn_id, char *buffer, int size)
448{
449 int j, file_fd, buflen, len;
450 int i, ret;
451 char * fstr;
452 int flen;
453 static char snd_buffer[BUFSIZE+1]; /* static so zero filled */
454 static char date_buffer[128];
455
456
457 ret = size;
458 if(ret > 0 && ret < BUFSIZE) /* return code is valid chars */
459 buffer[ret]=0; /* terminate the buffer */
460 else buffer[0]=0;
461
462if(WebDID_Debug)
463printf("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
470 for(i=4;i<BUFSIZE;i++)
471 { /* null terminate after the second space to ignore extra stuff */
472 if(buffer[i] == ' ')
473 { /* string is "GET URL " +lots of other stuff */
474 buffer[i] = 0;
475 break;
476 }
477 }
478
479if(WebDID_Debug)
480printf("Got 1 %s\n", buffer);
481 for(j=0;j<i-1;j++) /* check for illegal parent directory use .. */
482 {
483 if(buffer[j] == '.' && buffer[j+1] == '.')
484 {
485 log_it(SORRY,"Parent directory (..) path names not supported",buffer,conn_id);
486 return;
487 }
488 }
489 if((int)strlen(buffer) == 5)
490 {
491 if( !strncmp(&buffer[0],"GET /",5) || !strncmp(&buffer[0],"get /",5) )
492 /* convert no filename to index file */
493 (void)strcpy(buffer,"GET /index.html");
494 }
495 if((int)strlen(buffer) == 8)
496 {
497 if( !strncmp(&buffer[0],"GET /smi",8) || !strncmp(&buffer[0],"get /smi",8) )
498 /* convert no filename to index file */
499 (void)strcpy(buffer,"GET /smi/index.html");
500 }
501 /* work out the file type and check we support it */
502 buflen=(int)strlen(buffer);
503 fstr = (char *)0;
504 for(i=0;extensions[i].ext != 0;i++)
505 {
506 len = (int)strlen(extensions[i].ext);
507 if( !strncmp(&buffer[buflen-len], extensions[i].ext, (size_t)len))
508 {
509 fstr =extensions[i].filetype;
510 break;
511 }
512 }
513/*
514 (void)sprintf(snd_buffer,"HTTP/1.1 100 Continue\r\n\r\n");
515 (void)web_write(conn_id,snd_buffer,(int)strlen(snd_buffer));
516 printf("SENDING to conn %d:\n%s\n",conn_id, snd_buffer);
517*/
518 if(fstr == 0)
519 {
520if(WebDID_Debug)
521printf("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],"smiObjects",11))
553 {
554 sendSmiData(conn_id, &buffer[5], 2);
555 return;
556 }
557/*
558 if((!strncmp(&buffer[5],"didData",7)) || (!strncmp(&buffer[5],"didServices",11)))
559 {
560 if(ptr = strchr(&buffer[5],'/'))
561 {
562 *ptr = '\0';
563 }
564 buflen=(int)strlen(buffer);
565 for(i=0;extensions[i].ext != 0;i++)
566 {
567 len = (int)strlen(extensions[i].ext);
568 if( !strncmp(&buffer[buflen-len], extensions[i].ext, len))
569 {
570 fstr =extensions[i].filetype;
571 printf("fstr %s", fstr);
572 break;
573 }
574 }
575 if(!strncmp(&buffer[5],"didData",7))
576 {
577 sendData(conn_id, &buffer[5]);
578 }
579 else if(!strncmp(&buffer[5],"didServices",11))
580 {
581 sendData(conn_id, &buffer[5]);
582 }
583 }
584*/
585 else
586 {
587 log_it(SORRY,"file extension type not supported",buffer,conn_id);
588 return;
589 }
590 }
591
592 if(( file_fd = open(&buffer[5],O_RDONLY | O_BINARY)) == -1) /* open the file for reading */
593 {
594 log_it(SORRY, "failed to open file",&buffer[5],conn_id);
595 return;
596 }
597
598 flen = 0;
599 while ( (ret = (int)read(file_fd, snd_buffer, BUFSIZE)) > 0 )
600 {
601 flen += ret;
602 }
603 close(file_fd);
604
605 if(( file_fd = open(&buffer[5],O_RDONLY | O_BINARY)) == -1) /* open the file for reading */
606 {
607 log_it(SORRY, "failed to open file",&buffer[5],conn_id);
608 return;
609 }
610
611 getTime(date_buffer);
612 (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",
613 date_buffer, flen, fstr);
614 (void)web_write(conn_id,snd_buffer,(int)strlen(snd_buffer));
615if(WebDID_Debug)
616 printf("SENDING to conn %d:\n%s\n",conn_id, snd_buffer);
617
618 /* send file in 8KB block - last block may be smaller */
619 while ( (ret = (int)read(file_fd, snd_buffer, BUFSIZE)) > 0 ) {
620 (void)web_write(conn_id,snd_buffer,ret);
621if(WebDID_Debug == 2)
622 printf("SENDING data to conn %d: %d bytes\n",conn_id, ret);
623 }
624 close(file_fd);
625#ifdef LINUX
626 sleep(1); /* to allow socket to drain */
627#endif
628}
629
630static void handler( int conn_id, char *packet, int size, int status )
631{
632 switch(status)
633 {
634 case STA_DISC: /* connection broken */
635if(WebDID_Debug)
636{
637 dim_print_date_time();
638 printf(" Disconnect received - conn: %d to %s@%s\n", conn_id,
639 Net_conns[conn_id].task,Net_conns[conn_id].node );
640}
641 web_close(conn_id);
642 break;
643 case STA_CONN: /* connection received */
644if(WebDID_Debug)
645{
646 dim_print_date_time();
647 printf(" Connection request received - conn: %d\n", conn_id);
648}
649 break;
650 case STA_DATA: /* normal packet */
651/*
652 dim_print_date_time();
653 printf(" conn %d packet received:\n", conn_id);
654 printf("packet size = %d\n", size);
655 printf("%s\n",packet);
656 fflush(stdout);
657*/
658 sendFile(conn_id, packet, size);
659 break;
660 default:
661 dim_print_date_time();
662 printf( " - DIM panic: recv_rout(): Bad switch, exiting...\n");
663 abort();
664 }
665}
666
667static void error_handler(int conn_id, int severity, int errcode, char *reason)
668{
669 if(conn_id){}
670 if(errcode){}
671 dim_print_msg(reason, severity);
672/*
673 if(severity == 3)
674 {
675 printf("Exiting!\n");
676 exit(2);
677 }
678*/
679}
680
681int main(int argc, char **argv)
682{
683 int port;
684 int proto;
685 char dns_node[128];
686 int web_get_port();
687 char *ptr;
688 char currwd[256];
689
690 if(argc){}
691
692 strcpy(currwd, argv[0]);
693 printf("arg %s\n",currwd);
694 ptr = strrchr(currwd,'/');
695 if(ptr)
696 {
697 *ptr = '\0';
698 }
699 ptr = strrchr(currwd,'\\');
700 if(ptr)
701 {
702 *ptr = '\0';
703 }
704 chdir(currwd);
705 log_it(LOG,"webDid starting",argv[1],getpid());
706 /* setup the network socket */
707 proto = 1;
708 port = web_get_port();
709 get_node_name(dns_node);
710 did_init(dns_node, DNS_PORT);
711 if(!web_open_server("DID",handler, &proto, &port, error_handler))
712 return(0);
713/*
714 ret = matchString("hello world","*ll*");
715 printf("%s %s %d\n", "hello world","*ll*",ret);
716 ret = matchString("hello world","ll*");
717 printf("%s %s %d\n", "hello world","ll*",ret);
718*/
719 while(1)
720 sleep(10);
721 return(0);
722}
Note: See TracBrowser for help on using the repository browser.