1.  
  2. /*
  3. ** parser.c
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #include <sys/stat.h>
  11. #include <errno.h>
  12. #include <string.h>
  13. #include <sys/types.h>
  14. #include <sys/socket.h>
  15. #include <netinet/in.h>
  16. #include <arpa/inet.h>
  17.  
  18. #define MYPORT 3499    // the port users will be connecting to
  19. #define BACKLOG 10     // how many pending connections queue will hold
  20. #define MAXLEN 1024 //upper limit of the length of the string
  21.  
  22.  
  23. int readline(int fd, char *buf, int maxlen);
  24. void sendSomething(int fd, char * msg);
  25.  
  26. int main(void)
  27. {
  28.     char input[MAXLEN]; //the line that is read from the client
  29.     char * token1; //GET request
  30.     char * token2; //filepath
  31.     char tmpstring[MAXLEN]; //filesize
  32.     int sockfd, new_fd;  // listen on sock_fd, new connection on new_fd, file open on file_fd
  33.     struct sockaddr_in my_addr;    // my address information
  34.     struct sockaddr_in their_addr; // connector's address information
  35.     int sin_size;
  36.     int yes=1;
  37.     int n; //the amount of read characters from the client
  38.  
  39.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  40.         perror("socket");
  41.         exit(1);
  42.     }
  43.  
  44.     if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
  45.         perror("setsockopt");
  46.         exit(1);
  47.     }
  48.    
  49.     my_addr.sin_family = AF_INET;         // host byte order
  50.     my_addr.sin_port = htons(MYPORT);     // short, network byte order
  51.     my_addr.sin_addr.s_addr = INADDR_ANY; // automatically fill with my IP
  52.     memset(&(my_addr.sin_zero), '\0', 8); // zero the rest of the struct
  53.  
  54.     if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
  55.         perror("bind");
  56.         exit(1);
  57.     }
  58.  
  59.     if (listen(sockfd, BACKLOG) == -1) {
  60.         perror("listen");
  61.         exit(1);
  62.     }
  63.  
  64.     while(1) {  // main accept() loop
  65.         sin_size = sizeof(struct sockaddr_in);
  66.         if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
  67.             perror("accept");
  68.             continue;
  69.         }
  70.  
  71.         printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));
  72.     n = readline(new_fd, input, MAXLEN); //n is the amount of read characters
  73.     if (n == -1) {
  74.         perror("Unable to read line");
  75.     }
  76.  
  77.     //Check if it is a GET message
  78.     token1 = strtok(input," ");
  79.     if(strcmp(token1, "GET") != 0)
  80.     {
  81.         sendSomething(new_fd, "Bad request\n");
  82.     }
  83.     else
  84.     {
  85.         //Retrieve the file path
  86.         token2 = strtok(NULL, " ");
  87.         if(token2 == NULL)
  88.         {
  89.             sendSomething(new_fd, "File path not specified\n"); //Check if filename is empty
  90.            
  91.         }
  92.         sendSomething(new_fd, token2); //test
  93.         printf("%s", token2);
  94.         if(token2[0] == '/') //remove the initial slash
  95.             memmove(token2, token2 + 1, strlen(token2));
  96.         sendSomething(new_fd, token2); //test
  97.         sendSomething(new_fd, "test.html"); //test to compare      
  98.         //Check if file is on the server
  99.         if(open(token2, O_RDONLY) < 0) //Error opening file
  100.         {
  101.             if(errno == EACCES)        
  102.                 sendSomething(new_fd, "Access error\n");
  103.             else
  104.             {
  105.                 perror("Current problem: ");
  106.                 sendSomething(new_fd, "Not existed\n");
  107.             }
  108.                
  109.         }
  110.         else
  111.         {
  112.  
  113.             FILE * requested_file = fopen(token2, "r");
  114.             if(requested_file == NULL) //
  115.             {
  116.                 sendSomething(new_fd, "Error in fopen\n");
  117.             }
  118.             else
  119.             {
  120.                 sendSomething(new_fd, "File found\n"); //successful
  121.             }
  122.             fseek(requested_file, 0, SEEK_END); // move to the end of the file
  123.             int end=  ftell(requested_file);    // get the position of the end of file
  124.             int stringlen = sprintf(tmpstring, "file size: %d\n", end);
  125.             send(new_fd, tmpstring, stringlen, 0);
  126.         }
  127.        
  128.     }
  129.  
  130.         close(new_fd); //close connection
  131.     }
  132.  
  133.     return 0;
  134. }
  135.  
  136. //helper function for recieving text
  137. int readline(int fd, char *buf, int maxlen)
  138. {
  139.     int n, rc;
  140.     char c;
  141.  
  142.     for (n = 1; n < maxlen; n++) {
  143.     if ((rc = read(fd, &c, 1)) == 1) {
  144.         *buf++ = c;
  145.         if (c == '\n')
  146.     break;
  147.     } else if (rc == 0) {
  148.         if (n == 1)
  149.         return 0; // EOF, no data read
  150.     else
  151.         break; // EOF, read some data
  152.         } else
  153.     return -1; // error
  154.     }
  155.  
  156.     *buf = '\0'; // null-terminate
  157.     return n;
  158. }
  159.  
  160. void sendSomething(int fd, char * msg)
  161. {
  162.     int length = strlen(msg);
  163.     send(fd, msg, length, 0);  
  164. }