Exemplo n.º 1
0
/**
 * Read Directory
 * Read a directory (list or index) from disk into a Resource object
 *
 * @param path Full disk path of the file
 * @param sb Filled in stat struct
 * @return Return's the resource object upon successful load
 */
Resource* ResourceHost::readDirectory(std::string path, struct stat sb) {
	Resource* res = NULL;
	// Make the path end with a / (for consistency) if it doesnt already
	if(path.empty() || path[path.length()-1] != '/')
		path += "/";
	
	// Probe for valid indexes
	int numIndexes = sizeof(validIndexes) / sizeof(*validIndexes);
	std::string loadIndex;
	struct stat sidx;
	for(int i = 0; i < numIndexes; i++) {
		loadIndex = path + validIndexes[i];
		// Found a suitable index file to load and return to the client
		if(stat(loadIndex.c_str(), &sidx) != -1)
			return readFile(loadIndex.c_str(), sidx);
	}
	
	// Make sure the webserver USER owns the directory
	if(!(sb.st_mode & S_IRWXU))
		return NULL;
	
	// Generate an HTML directory listing
	std::string listing = generateDirList(path);
	
	unsigned int slen = listing.length();
	char* sdata = new char[slen];
	strncpy(sdata, listing.c_str(), slen);
	
	res = new Resource(path, true);
	res->setData((byte*)sdata, slen);
	
	return res;
}
Exemplo n.º 2
0
    struct basic_epoll_event *handle_request()
    {
        if (0 == SSTRNCMP(URI_TEST1, URI))
        {
           return response(HTTP_STATUS_200, HTTP_CONTENT_TYPE_TEXT_PLAIN);
        }
        
        URI::decode(URI);
        const char *file = (URI[1] == 0 ? "." : URI + 1);
        int ffd = open(file, O_RDONLY);
        if (0 > ffd)
            return response(HTTP_STATUS_404, HTTP_CONTENT_TYPE_TEXT_PLAIN);

        http_server *h = (http_server *)events_array.get(ffd);
        if (0 > h->sendFile(this))
        {
            struct stat st;
            int res = fstat(ffd, &st);
            ::close(ffd);
            if (res == 0 && S_ISDIR(st.st_mode) && 0 == generateDirList(file))
            {
                return response(HTTP_STATUS_200, HTTP_CONTENT_TYPE_TEXT_HTML);
            } else
                return response(HTTP_STATUS_500, HTTP_CONTENT_TYPE_TEXT_PLAIN);
        }
        headerStartMime(HTTP_STATUS_200, file);
        headerContentLength();
        return headerClose();
    }
Exemplo n.º 3
0
    struct basic_epoll_event *handle_request()
    {
        /*
        sockaddr_in addr;
        socklen_t len = sizeof(addr);
        char addr_str[INET_ADDRSTRLEN];
        const char *addrp = "-";
        if (0 == getpeername(fd, (struct sockaddr*)&addr, &len) &&
            NULL != inet_ntop(AF_INET, &addr.sin_addr, addr_str, INET_ADDRSTRLEN))
        {
            addrp = addr_str;
        }
        LOGGER_INFO("%s %s%s%s", addrp, URI, *query ? "?" : "", query);
        */
        URI::decode(URI);
        const char *file = (URI[1] == 0 ? "." : URI + 1);
        int ffd = open(file, O_RDONLY);
        if (0 > ffd)
            return response(HTTP_STATUS_404, HTTP_CONTENT_TYPE_TEXT_PLAIN);

        /*
        http_server *h = (http_server *)pool.get();
        h->fd = ffd;
        */
        http_server *h = (http_server *)events_array.get(ffd);
        if (0 > h->sendFile(this))
        {
            struct stat st;
            int res = fstat(ffd, &st);
            //pool.put(h);
            ::close(ffd);
            if (res == 0 && S_ISDIR(st.st_mode) && 0 == generateDirList(file))
            {
                return response(HTTP_STATUS_200, HTTP_CONTENT_TYPE_TEXT_HTML);
            } else
                return response(HTTP_STATUS_500, HTTP_CONTENT_TYPE_TEXT_PLAIN);
        }
        headerStartMime(HTTP_STATUS_200, file);
        headerContentLength();
        return headerClose();
    }