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

Last change on this file since 14937 was 14937, checked in by tbretz, 12 years ago
Added in v20r5
File size: 13.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,strlen(snd_buffer));
71 (void)web_write(conn_id,logbuffer,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+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 = strlen(buffer) + 1;
209 if(buffer_size > conv_buffer_size )
210 {
211 if(conv_buffer_size)
212 free(conv_buffer);
213 conv_buffer = malloc(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,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,strlen(ptr));
339if(WebDID_Debug == 2)
340 printf("SENDING DATA to conn %d:\n%s\n",conn_id, ptr);
341}
342
343void sendFile(int conn_id, char *buffer, int size)
344{
345 int j, file_fd, buflen, len;
346 int i, ret;
347 char * fstr;
348 int flen;
349 static char snd_buffer[BUFSIZE+1]; /* static so zero filled */
350 static char date_buffer[128];
351
352
353 ret = size;
354 if(ret > 0 && ret < BUFSIZE) /* return code is valid chars */
355 buffer[ret]=0; /* terminate the buffer */
356 else buffer[0]=0;
357
358 if( strncmp(buffer,"GET ",4) && strncmp(buffer,"get ",4) )
359 {
360 log_it(SORRY,"Only simple GET operation supported",buffer,conn_id);
361 return;
362 }
363
364 for(i=4;i<BUFSIZE;i++)
365 { /* null terminate after the second space to ignore extra stuff */
366 if(buffer[i] == ' ')
367 { /* string is "GET URL " +lots of other stuff */
368 buffer[i] = 0;
369 break;
370 }
371 }
372
373 for(j=0;j<i-1;j++) /* check for illegal parent directory use .. */
374 {
375 if(buffer[j] == '.' && buffer[j+1] == '.')
376 {
377 log_it(SORRY,"Parent directory (..) path names not supported",buffer,conn_id);
378 return;
379 }
380 }
381 if( !strncmp(&buffer[0],"GET /\0",6) || !strncmp(&buffer[0],"get /\0",6) )
382 /* convert no filename to index file */
383 (void)strcpy(buffer,"GET /index.html");
384
385 /* work out the file type and check we support it */
386 buflen=strlen(buffer);
387 fstr = (char *)0;
388 for(i=0;extensions[i].ext != 0;i++)
389 {
390 len = strlen(extensions[i].ext);
391 if( !strncmp(&buffer[buflen-len], extensions[i].ext, len))
392 {
393 fstr =extensions[i].filetype;
394 break;
395 }
396 }
397/*
398 (void)sprintf(snd_buffer,"HTTP/1.1 100 Continue\r\n\r\n");
399 (void)web_write(conn_id,snd_buffer,strlen(snd_buffer));
400 printf("SENDING to conn %d:\n%s\n",conn_id, snd_buffer);
401*/
402 if(fstr == 0)
403 {
404if(WebDID_Debug)
405printf("Got %s\n", buffer);
406 if(!strncmp(&buffer[5],"didHeader",9))
407 {
408 sendData(conn_id, &buffer[5], 0);
409 return;
410 }
411 else if(!strncmp(&buffer[5],"didData",7))
412 {
413 sendData(conn_id, &buffer[5], 1);
414 return;
415 }
416 else if(!strncmp(&buffer[5],"didServices",11))
417 {
418 sendData(conn_id, &buffer[5], 2);
419 return;
420 }
421 else if(!strncmp(&buffer[5],"didServiceData",14))
422 {
423 sendData(conn_id, &buffer[5], 3);
424 return;
425 }
426 else if(!strncmp(&buffer[5],"didPoll",7))
427 {
428 sendData(conn_id, &buffer[5], 5);
429 return;
430 }
431 else if(!strncmp(&buffer[5],"didQuery",8))
432 {
433 sendData(conn_id, &buffer[5], 6);
434 return;
435 }
436/*
437 if((!strncmp(&buffer[5],"didData",7)) || (!strncmp(&buffer[5],"didServices",11)))
438 {
439 if(ptr = strchr(&buffer[5],'/'))
440 {
441 *ptr = '\0';
442 }
443 buflen=strlen(buffer);
444 for(i=0;extensions[i].ext != 0;i++)
445 {
446 len = strlen(extensions[i].ext);
447 if( !strncmp(&buffer[buflen-len], extensions[i].ext, len))
448 {
449 fstr =extensions[i].filetype;
450 printf("fstr %s", fstr);
451 break;
452 }
453 }
454 if(!strncmp(&buffer[5],"didData",7))
455 {
456 sendData(conn_id, &buffer[5]);
457 }
458 else if(!strncmp(&buffer[5],"didServices",11))
459 {
460 sendData(conn_id, &buffer[5]);
461 }
462 }
463*/
464 else
465 {
466 log_it(SORRY,"file extension type not supported",buffer,conn_id);
467 return;
468 }
469 }
470
471 if(( file_fd = open(&buffer[5],O_RDONLY | O_BINARY)) == -1) /* open the file for reading */
472 {
473 log_it(SORRY, "failed to open file",&buffer[5],conn_id);
474 return;
475 }
476
477 flen = 0;
478 while ( (ret = read(file_fd, snd_buffer, BUFSIZE)) > 0 )
479 {
480 flen += ret;
481 }
482 close(file_fd);
483
484 if(( file_fd = open(&buffer[5],O_RDONLY | O_BINARY)) == -1) /* open the file for reading */
485 {
486 log_it(SORRY, "failed to open file",&buffer[5],conn_id);
487 return;
488 }
489
490 getTime(date_buffer);
491 (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",
492 date_buffer, flen, fstr);
493 (void)web_write(conn_id,snd_buffer,strlen(snd_buffer));
494if(WebDID_Debug)
495 printf("SENDING to conn %d:\n%s\n",conn_id, snd_buffer);
496
497 /* send file in 8KB block - last block may be smaller */
498 while ( (ret = read(file_fd, snd_buffer, BUFSIZE)) > 0 ) {
499 (void)web_write(conn_id,snd_buffer,ret);
500if(WebDID_Debug == 2)
501 printf("SENDING data to conn %d: %d bytes\n",conn_id, ret);
502 }
503 close(file_fd);
504#ifdef LINUX
505 sleep(1); /* to allow socket to drain */
506#endif
507}
508
509static void handler( int conn_id, char *packet, int size, int status )
510{
511 switch(status)
512 {
513 case STA_DISC: /* connection broken */
514if(WebDID_Debug)
515{
516 dim_print_date_time();
517 printf(" Disconnect received - conn: %d to %s@%s\n", conn_id,
518 Net_conns[conn_id].task,Net_conns[conn_id].node );
519}
520 web_close(conn_id);
521 break;
522 case STA_CONN: /* connection received */
523if(WebDID_Debug)
524{
525 dim_print_date_time();
526 printf(" Connection request received - conn: %d\n", conn_id);
527}
528 break;
529 case STA_DATA: /* normal packet */
530/*
531 dim_print_date_time();
532 printf(" conn %d packet received:\n", conn_id);
533 printf("packet size = %d\n", size);
534 printf("%s\n",packet);
535 fflush(stdout);
536*/
537 sendFile(conn_id, packet, size);
538 break;
539 default:
540 dim_print_date_time();
541 printf( " - DIM panic: recv_rout(): Bad switch, exiting...\n");
542 abort();
543 }
544}
545
546static void error_handler(int conn_id, int severity, int errcode, char *reason)
547{
548 if(conn_id){}
549 if(errcode){}
550 dim_print_msg(reason, severity);
551/*
552 if(severity == 3)
553 {
554 printf("Exiting!\n");
555 exit(2);
556 }
557*/
558}
559
560int main(int argc, char **argv)
561{
562 int port;
563 int proto;
564 char dns_node[128];
565 int web_get_port();
566 char *ptr;
567 char currwd[256];
568
569 if(argc){}
570
571 strcpy(currwd, argv[0]);
572 printf("arg %s\n",currwd);
573 ptr = strrchr(currwd,'/');
574 if(ptr)
575 {
576 *ptr = '\0';
577 }
578 ptr = strrchr(currwd,'\\');
579 if(ptr)
580 {
581 *ptr = '\0';
582 }
583 chdir(currwd);
584 log_it(LOG,"webDid starting",argv[1],getpid());
585 /* setup the network socket */
586 proto = 1;
587 port = web_get_port();
588 get_node_name(dns_node);
589 did_init(dns_node, DNS_PORT);
590 if(!web_open_server("DID",handler, &proto, &port, error_handler))
591 return(0);
592/*
593 ret = matchString("hello world","*ll*");
594 printf("%s %s %d\n", "hello world","*ll*",ret);
595 ret = matchString("hello world","ll*");
596 printf("%s %s %d\n", "hello world","ll*",ret);
597*/
598 while(1)
599 sleep(10);
600 return(0);
601}
Note: See TracBrowser for help on using the repository browser.