FACT++  1.0
void sendFile ( int  conn_id,
char *  buffer,
int  size 
)

Definition at line 447 of file webServer.c.

References BUFSIZE, ext, extensions, getTime(), i, log_it(), O_BINARY, sendData(), sendSmiData(), size, SORRY, sprintf(), web_write(), and WebDID_Debug.

Referenced by handler().

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 
462 if(WebDID_Debug)
463 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 
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 
479 if(WebDID_Debug)
480 printf("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  {
520 if(WebDID_Debug)
521 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);
560  return;
561  }
562 /*
563  if((!strncmp(&buffer[5],"didData",7)) || (!strncmp(&buffer[5],"didServices",11)))
564  {
565  if(ptr = strchr(&buffer[5],'/'))
566  {
567  *ptr = '\0';
568  }
569  buflen=(int)strlen(buffer);
570  for(i=0;extensions[i].ext != 0;i++)
571  {
572  len = (int)strlen(extensions[i].ext);
573  if( !strncmp(&buffer[buflen-len], extensions[i].ext, len))
574  {
575  fstr =extensions[i].filetype;
576  printf("fstr %s", fstr);
577  break;
578  }
579  }
580  if(!strncmp(&buffer[5],"didData",7))
581  {
582  sendData(conn_id, &buffer[5]);
583  }
584  else if(!strncmp(&buffer[5],"didServices",11))
585  {
586  sendData(conn_id, &buffer[5]);
587  }
588  }
589 */
590  else
591  {
592  log_it(SORRY,"file extension type not supported",buffer,conn_id);
593  return;
594  }
595  }
596 
597  if(( file_fd = open(&buffer[5],O_RDONLY | O_BINARY)) == -1) /* open the file for reading */
598  {
599  log_it(SORRY, "failed to open file",&buffer[5],conn_id);
600  return;
601  }
602 
603  flen = 0;
604  while ( (ret = (int)read(file_fd, snd_buffer, BUFSIZE)) > 0 )
605  {
606  flen += ret;
607  }
608  close(file_fd);
609 
610  if(( file_fd = open(&buffer[5],O_RDONLY | O_BINARY)) == -1) /* open the file for reading */
611  {
612  log_it(SORRY, "failed to open file",&buffer[5],conn_id);
613  return;
614  }
615 
616  getTime(date_buffer);
617  (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",
618  date_buffer, flen, fstr);
619  (void)web_write(conn_id,snd_buffer,(int)strlen(snd_buffer));
620 if(WebDID_Debug)
621  printf("SENDING to conn %d:\n%s\n",conn_id, snd_buffer);
622 
623  /* send file in 8KB block - last block may be smaller */
624  while ( (ret = (int)read(file_fd, snd_buffer, BUFSIZE)) > 0 ) {
625  (void)web_write(conn_id,snd_buffer,ret);
626 if(WebDID_Debug == 2)
627  printf("SENDING data to conn %d: %d bytes\n",conn_id, ret);
628  }
629  close(file_fd);
630 #ifdef LINUX
631  sleep(1); /* to allow socket to drain */
632 #endif
633 }
char * ext
Definition: webServer.c:19
void getTime(char *buffer)
Definition: webServer.c:45
int i
Definition: db_dim_client.c:21
#define BUFSIZE
Definition: webServer.c:9
void web_write()
#define SORRY
Definition: webServer.c:11
struct @0 extensions[]
void sendSmiData(int conn_id, char *buffer, int type)
Definition: webServer.c:344
int buffer[BUFFSIZE]
Definition: db_dim_client.c:14
int size
Definition: db_dim_server.c:17
void sendData(int conn_id, char *buffer, int type)
Definition: webServer.c:240
int WebDID_Debug
Definition: webServer.c:7
sprintf(name1,"NewService%d", i)
#define O_BINARY
Definition: webServer.c:15
void log_it(int type, char *s1, char *s2, int conn_id)
Definition: webServer.c:55

+ Here is the call graph for this function:

+ Here is the caller graph for this function: