Esempio n. 1
0
static int
send_file(struct http_request *req)
{
	int r;
	off_t file_size = -1;
	int fd;
	struct Stat stat;
	// open the requested url for reading
	// if the file does not exist, send a 404 error using send_error
	// if the file is a directory, send a 404 error using send_error
	// set file_size to the size of the file

	// LAB 6: Your code here.
	fd = open(req->url, O_RDONLY);
	//struct Fd *open_file = INDEX2FD(i);	
	if(fd < 0)
	{
		cprintf("file %s does not exists\n",req->url);
		r = send_error(req, 404);
		if(r<0)
			cprintf("send_file:send_error failed:%e\n",r);
	}
	r = fstat(fd, &stat);
	if(r<0)
	{
		cprintf("send_file:fstat failed:%e\n",r);
		r = send_error(req, 404);
	}
	if(stat.st_isdir == 1)
	{
		cprintf("file %s is a directory\n",req->url);
		 r = send_error(req, 404);
                if(r<0)
                        cprintf("send_file:send_error failed:%e\n",r);
	}
	file_size = stat.st_size;
	
	if ((r = send_header(req, 200)) < 0)
		goto end;

	if ((r = send_size(req, file_size)) < 0)
		goto end;

	if ((r = send_content_type(req)) < 0)
		goto end;

	if ((r = send_header_fin(req)) < 0)
		goto end;

	r = send_data(req, fd);

end:
	close(fd);
	return r;
}
Esempio n. 2
0
static int
send_file(struct http_request *req)
{
	int r;
	off_t file_size = -1;
	int fd;

	// open the requested url for reading
	// if the file does not exist, send a 404 error using send_error
	// if the file is a directory, send a 404 error using send_error
	// set file_size to the size of the file

	// LAB 6: Your code here.

	char path[MAXPATHLEN];                                                 
        struct Stat stat;
        memmove(path, req->url, strlen(req->url));  

	if ((fd = open(path, O_RDONLY)) < 0) {
		send_error(req, 404);  // HTTP page not found
		r = fd;
		goto end;
	}

	if ((r = fstat(fd, &stat)) < 0) {
		goto end;
	}

	if (stat.st_isdir) {
		send_error(req, 404); // HTTP page not found
		r = -1;
		goto end;
	}

	file_size = stat.st_size;

	if ((r = send_header(req, 200)) < 0)
		goto end;

	if ((r = send_size(req, file_size)) < 0)
		goto end;

	if ((r = send_content_type(req)) < 0)
		goto end;

	if ((r = send_header_fin(req)) < 0)
		goto end;

	r = send_data(req, fd);

end:
	close(fd);
	return r;
}
Esempio n. 3
0
File: httpd.c Progetto: MG47/JOS-MG
static int
send_file(struct http_request *req)
{
	int r;
	off_t file_size = -1;
	int fd;

	// open the requested url for reading
	// if the file does not exist, send a 404 error using send_error
	// if the file is a directory, send a 404 error using send_error
	// set file_size to the size of the file

	// code for lab 6-M.G
    //	panic("send_file not implemented");

    if ((fd = open(req->url, O_RDONLY)) < 0) 
    {
		send_error(req, 404);
		goto end;
	}

	struct Stat s;
	if ((r = stat(req->url, &s)) < 0) 
    {
		send_error(req, 404);
		goto end;
	}

	if (s.st_isdir) 
    {
		send_error(req, 404);
		goto end;
	}
	file_size = s.st_size;

	if ((r = send_header(req, 200)) < 0)
		goto end;

	if ((r = send_size(req, file_size)) < 0)
		goto end;

	if ((r = send_content_type(req)) < 0)
		goto end;

	if ((r = send_header_fin(req)) < 0)
		goto end;

	r = send_data(req, fd);

end:
	close(fd);
	return r;
}
Esempio n. 4
0
static int
send_file(struct http_request *req)
{
	int r;
	off_t file_size = -1;
	int fd;
	struct Stat stat;

	// open the requested url for reading
	// if the file does not exist, send a 404 error using send_error
	// if the file is a directory, send a 404 error using send_error
	// set file_size to the size of the file

	// LAB 6: Your code here.
	//panic("send_file not implemented");
	fd = open(req->url, O_RDONLY);
	if (fd < 0) {
		send_error(req, 404);
		r = fd;
		goto end;
	}
	r = fstat(fd, &stat);
	if (r < 0) {
		send_error(req, 404);
		goto end;
	}
	if (stat.st_isdir == 1) {
		send_error(req, 404);
		r = 0;
		goto end;
	}
	file_size = stat.st_size;

	if ((r = send_header(req, 200)) < 0)
		goto end;

	if ((r = send_size(req, file_size)) < 0)
		goto end;

	if ((r = send_content_type(req)) < 0)
		goto end;

	if ((r = send_header_fin(req)) < 0)
		goto end;

	r = send_data(req, fd, file_size);

end:
	close(fd);
	return r;
}
Esempio n. 5
0
File: httpd.c Progetto: ajsbu/cse506
static int
send_file(struct http_request *req)
{
	int r;
	off_t file_size = -1;
	int fd;

	// open the requested url for reading
	// if the file does not exist, send a 404 error using send_error
	// if the file is a directory, send a 404 error using send_error
	// set file_size to the size of the file

	// LAB 6: Your code here.
	char path[MAXPATHLEN];                                                
        struct Stat stat;
        strcpy(path, req->url);  

	cprintf("Reached in send_file...path = %s\n", path);

        if ((fd = open(path, O_RDONLY)) < 0) {
		cprintf("Failed to open so sending 404...\n");
                send_error(req, 404);
                r = fd;
		close(fd);
		return r;
        }

        if ((r = fstat(fd, &stat)) < 0) {
		cprintf("Failed to get stat so sending 404...\n");
		close(fd);
		return r;
        }

        if (stat.st_isdir) {
		cprintf("it is a directory so sending 404...\n");
                send_error(req, 404);
                r = -1;
		close(fd);
		return r;
        }

        file_size = stat.st_size;

	if ((r = send_header(req, 200)) < 0 || (r = send_size(req, file_size)) < 0 || (r = send_content_type(req)) < 0 || (r = send_header_fin(req)) < 0) {
		cprintf("sending failed so sending 404...\n");
		close(fd);
		return r;
	}

	r = send_data(req, fd);
	close(fd);

	cprintf("send_file() successful...\n");
	return r;

	panic("send_file not implemented");
}