Example #1
0
void
connection(int fd)
{
	char request[1000], resource[1000], *ptr;
	int ret;
	int page_fd;

	ret = recv_new(fd, request);
	
	ptr = strstr(request, " HTTP/");
	if(ptr == NULL)
	{
		perror("Not HTTP request");
	}
	else
	{
		*ptr = 0;
		ptr = NULL;

		if(strncmp(request, "GET ", 4) == 0)
		{
			ptr = request + 4;
		}
		if(strncmp(request, "HEAD ", 5) == 0)
		{
			ptr = request + 5;
		}
		if(ptr == NULL)
		{
			perror("Unknown Request. \n");
		}
		else
		{
			memset(resource, 0, sizeof(resource));
			strncpy(resource, WEB_ROOT, sizeof(resource));
			strncat(resource, ptr, sizeof(resource) - sizeof(WEB_ROOT) -1);
			if(ptr[strlen(ptr) -1] == '/' )
			{
				strncat(resource, DEFAULT_PAGE, sizeof(resource)
						- strlen(resource)
						- sizeof(DEFAULT_PAGE)
						- 1);
			}

			if((page_fd = open(resource, O_RDONLY)) == -1)
			{
				perror("open failed, file not found");
				send_new(fd, "HTTP/1.1 404 NOT FOUND\r\n");
				send_new(fd, "<html><head>404 NOT FOUND</head>");
				send_new(fd, "<body>Sorry, NOT FOUND</body></html>");
				close(fd);
			}
			else
			{
				int file_size;
				
				memset(request, 0, sizeof(request));
				file_size = get_file_size(page_fd);
				read(page_fd, request, file_size);
				
				send_new(fd, "HTTP/1.1 200 OK\r\n");
				send_new(fd, request);
				close(fd);
			}
		}
	}
}/* connection() */
/*
 This function parses the HTTP requests,
 arrange resource locations,
 check for supported media types,
 serves files in a web root,
 sends the HTTP error codes.
 */
int connection(int fd) {
 char request[500], resource[500], *ptr;
 int fd1, length;
 if (recv_new(fd, request) == 0) {
  printf("Recieve Failed\n");
 }
 printf("%s\n", request);
 // Check for a valid browser request
 ptr = strstr(request, " HTTP/");
 if (ptr == NULL) {
  printf("NOT HTTP !\n");
 } else {
  *ptr = 0;
  ptr = NULL;

  if (strncmp(request, "GET ", 4) == 0) {
   ptr = request + 4;
  }
  if (ptr == NULL) {
   printf("Unknown Request ! \n");
  } else {
   if (ptr[strlen(ptr) - 1] == '/') {
    strcat(ptr, "index.html");
   }
   strcpy(resource, webroot());
   strcat(resource, ptr);
   char* s = strchr(ptr, '.');
   int i;
   for (i = 0; extensions[i].ext != NULL; i++) {
    if (strcmp(s + 1, extensions[i].ext) == 0) {
     fd1 = open(resource, O_RDONLY, 0);
     printf("Openning \"%s\"\n", resource);
     printf("Extention \"%s\"\n", extensions[i].ext);
     if (fd1 == -1) {
      printf("404 File not found Error\n");
      send_new(fd, "HTTP/1.1 404 Not Found\r\n");
      send_new(fd, "Server : Web Server in C\r\n\r\n");
      send_new(fd, "<html><head><title>404 Not Found</head></title>");
      send_new(fd, "<body><p>404 Not Found: The requested resource could not be found!</p></body></html>");
      //Handling php requests
     } else if (strcmp(extensions[i].ext, "php") == 0) {
		 printf("php\n");
      php_cgi(resource, fd);
      sleep(1);
      close(fd);
      exit(1);
     } else {
      printf("200 OK, Content-Type: %s\n\n",
        extensions[i].mediatype);
      send_new(fd, "HTTP/1.1 200 OK\r\n");
      send_new(fd, "Server : Web Server in C\r\n\r\n");
      if (ptr == request + 4) // if it is a GET request
        {
       if ((length = get_file_size(fd1)) == -1)
        printf("Error in getting size !\n");
       size_t total_bytes_sent = 0;
       ssize_t bytes_sent;
       while (total_bytes_sent < length) {
        //Zero copy optimization
        if ((bytes_sent = sendfile(fd, fd1, 0,
          length - total_bytes_sent)) <= 0) {
         if (errno == EINTR || errno == EAGAIN) {
          continue;
         }
         perror("sendfile");
         return -1;
        }
        total_bytes_sent += bytes_sent;
       }

      }
     }
     break;
    }
    int size = sizeof(extensions) / sizeof(extensions[0]);
    if (i == size - 2) {
     printf("415 Unsupported Media Type\n");
     send_new(fd, "HTTP/1.1 415 Unsupported Media Type\r\n");
     send_new(fd, "Server : Web Server in C\r\n\r\n");
     send_new(fd, "<html><head><title>415 Unsupported Media Type</head></title>");
     send_new(fd, "<body><p>415 Unsupported Media Type!</p></body></html>");
    }
   }

   close(fd);
  }
 }
 shutdown(fd, SHUT_RDWR);
}