Beispiel #1
0
void requestServeStatic(int fd, char *filename, int filesize) 
{
   int srcfd;
   char *srcp, filetype[MAXLINE], buf[MAXBUF];

   requestGetFiletype(filename, filetype);

   srcfd = Open(filename, O_RDONLY, 0);

   // Rather than call read() to read the file into memory, 
   // which would require that we allocate a buffer, we memory-map the file
   srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0);
   Close(srcfd);

   // put together response
   sprintf(buf, "HTTP/1.0 200 OK\r\n");
   sprintf(buf, "%sServer: CS537 Web Server\r\n", buf);
   sprintf(buf, "%sContent-Length: %d\r\n", buf, filesize);
   sprintf(buf, "%sContent-Type: %s\r\n\r\n", buf, filetype);

   Rio_writen(fd, buf, strlen(buf));

   //  Writes out to the client socket the memory-mapped file 
   Rio_writen(fd, srcp, filesize);
   Munmap(srcp, filesize);

}
Beispiel #2
0
void requestServeStatic(request_t request, char *filename, int filesize, thread_t * thread)
{
    int fd = request.connfd;
    int srcfd;
    char *srcp, filetype[MAXLINE], buf[MAXBUF];
    char tmp = 0;
    int i;
    
    requestGetFiletype(filename, filetype);
    
    srcfd = Open(filename, O_RDONLY, 0);
    
	double time_start_read = get_time();
    // Rather than call read() to read the file into memory, 
    // which would require that we allocate a buffer, we memory-map the file
    srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0);
    Close(srcfd);
    
    // The following code is only needed to help you time the "read" given 
    // that the file is memory-mapped.  
    // This code ensures that the memory-mapped file is brought into memory 
    // from disk.
    
    // When you time this, you will see that the first time a client 
    //requests a file, the read is much slower than subsequent requests.
    for (i = 0; i < filesize; i++) {
	tmp += *(srcp + i);
    }
    
    double time_end_read = get_time();
	request.Stat_req_read = time_end_read - time_start_read;
	
	double time_start_write = get_time();
	request.Stat_req_complete = time_start_write - request.Stat_req_arrival;
	
    sprintf(buf, "HTTP/1.0 200 OK\r\n");
    sprintf(buf, "%s Server: CS537 Web Server\r\n", buf);
    
    // CS537: Your statistics go here -- fill in the 0's with something useful!
    sprintf(buf, "%s Stat-req-arrival: %d\r\n", buf, request.Stat_req_arrival);
    sprintf(buf, "%s Stat-req-dispatch: %d\r\n", buf, request.Stat_req_dispatch);
    sprintf(buf, "%s Stat-req-read: %d\r\n", buf, request.Stat_req_read);
    sprintf(buf, "%s Stat-req-complete: %d\r\n", buf, request.Stat_req_complete);
    sprintf(buf, "%s Stat-req-age: %d\r\n", buf, request.Stat_req_age);
    sprintf(buf, "%s Stat-thread-id: %d\r\n", buf, thread->Stat_thread_id);
    sprintf(buf, "%s Stat-thread-count: %d\r\n", buf, thread->Stat_thread_count);
    sprintf(buf, "%s Stat-thread-static: %d\r\n", buf, thread->Stat_thread_static);
    sprintf(buf, "%s Stat-thread-dynamic: %d\r\n", buf, thread->Stat_thread_dynamic);
    
    sprintf(buf, "%s Content-Length: %d\r\n", buf, filesize);
    sprintf(buf, "%s Content-Type: %s\r\n\r\n", buf, filetype);
    
    Rio_writen(fd, buf, strlen(buf));
    
    //  Writes out to the client socket the memory-mapped file 
    Rio_writen(fd, srcp, filesize);
    Munmap(srcp, filesize);
    
}
Beispiel #3
0
void requestServeStatic(int fd, char *filename, int filesize, request* req)
{
	int srcfd;
	char *srcp;
	char filetype[MAXLINE];
	char buf[MAXBUF];
	char tmp = 0;
	int i;
	long t, read_start, read_end;

	requestGetFiletype(filename, filetype);

	srcfd = Open(filename, O_RDONLY, 0);

	/* Rather than call read() to read the file into memory, which would
	 * require that we allocate a buffer, we memory-map the file
	 */
	srcp = Mmap(0, filesize, PROT_READ, MAP_PRIVATE, srcfd, 0);

	Close(srcfd);

	req->req_age = *(req->req_disp_count)-req->old_disp_count;

	(*(req->req_disp_count))++;

	/* The following code is only needed to help you time the "read" given
	 * that the file is memory-mapped.
	 * This code ensures that the memory-mapped file is brought into memory
	 * from disk.
	 */

	read_start = getTime();

	/* When you time this, you will see that the first time a client
	 * requests a file, the read is much slower than subsequent requests.
	 */
	for (i = 0; i < filesize; i++)
	{
		tmp += *(srcp+i);
	}

	read_end = getTime();

	req->req_read = read_end - read_start;

	/* the request is complete and we store that time */
	t = getTime();
	req->req_complete = t - req->req_arrival; 

	sprintf(buf, "HTTP/1.0 200 OK\r\n");
	sprintf(buf, "%s Server: CS537 Web Server\r\n", buf);

	/* CS537: Your statistics go here -- fill in the 0's with something
	 * useful!
	 */
	sprintf(buf, "%s Stat-req-arrival: %f\r\n", buf, toMS(req->req_arrival));
	sprintf(buf, "%s Stat-req-dispatch: %f\r\n", buf, toMS(req->req_dispatch));
	sprintf(buf, "%s Stat-req-read: %f\r\n", buf, toMS(req->req_read));
	sprintf(buf, "%s Stat-req-complete: %f\r\n", buf, toMS(req->req_complete));
	sprintf(buf, "%s Stat-req-age: %d\r\n", buf, req->req_age);
	sprintf(buf, "%s Stat-thread-id: %d\r\n", buf, req->t_stats.thread_id);
	sprintf(buf, "%s Stat-thread-count: %d\r\n", buf, req->t_stats.thread_count);
	sprintf(buf, "%s Stat-thread-static: %d\r\n", buf, req->t_stats.thread_static);
	sprintf(buf, "%s Stat-thread-dynamic: %d\r\n", buf, req->t_stats.thread_dynamic);

	sprintf(buf, "%s Content-Length: %d\r\n", buf, filesize);
	sprintf(buf, "%s Content-Type: %s\r\n\r\n", buf, filetype);

	Rio_writen(fd, buf, strlen(buf));

	/* Writes out to the client socket the memory-mapped file */
	Rio_writen(fd, srcp, filesize);
	Munmap(srcp, filesize);
}