void reply_cat(char *fileName, int fd, struct logInformation *logInfo)
{
    const TIME_STRING_LENGTH = 30;
    char *type;
    char *extension = getFileNameExtension(fileName);
    FILE *fpSocket;
    FILE *fpLocal;
    int character;
    int bytes = 0;
    char timeString[TIME_STRING_LENGTH];
    
    if (strcmp(extension, "html") == 0)
        type = "text/html";
    else if (strcmp(extension, "gif") == 0)
        type = "image/gif";
    else if (strcmp(extension, "jpg") == 0)
        type = "image/jpeg";
    else if (strcmp(extension, "jpeg") == 0)
        type = "image/jpeg";
    else
        type = "text/plain";
        
    fpSocket = fdopen(fd, "w");
    fpLocal = fopen(fileName, "r");
    if (fpSocket != NULL && fpLocal != NULL)
    {
        // get the last modified time for the file
        struct stat info;
        stat(fileName, &info);
        strncpy(timeString, asctime(gmtime(&(info.st_mtime))), TIME_STRING_LENGTH - 1); // get the Greenwich Mean Time
        char *timeStringPointer = timeString;
        // step through the time string until we find the newline character and 
        // change it to a null character to remove it
        while (*timeStringPointer != '\n')
        {
            timeStringPointer++;
        }
        *timeStringPointer = '\0'; 
        // count the number of bytes that will be sent
        while ((character = getc(fpLocal)) != EOF)
        {
            bytes++;
        }         
        rewind(fpLocal);    
        bytes += httpReply(fd, &fpSocket, 200, "OK", type, NULL, timeString, bytes, logInfo);
        while ((character = getc(fpLocal)) != EOF)
        {
            putc(character, fpSocket);
        }
        fclose(fpSocket);
        fclose(fpLocal);
    }
    
    // update the bytes sent statistics    
    updateBytesSent(bytes);
}
void processRequest(char *request, int fd, struct logInformation *logInfo)
{
    char command[BUFFERLENGTH];
    char argument[BUFFERLENGTH];
    
    // parse the command and argument from the request
    if (sscanf(request, "%s%s", command, argument) != 2)
        return;
    
    sanitize(argument);
    //printf("sanitized version is %s\n", argument);
    
    // get the quoted first line of the request and store it in the log structure
    int size = 0;
    char *requestPointer = request;
    while (*requestPointer != '\r' && *requestPointer != '\n')
    {
        size++;
        requestPointer++;
    }
    logInfo->quotedFirstLineOfRequest = malloc((size+3) * sizeof(char));
    strcpy(logInfo->quotedFirstLineOfRequest, "\"");
    strncat(logInfo->quotedFirstLineOfRequest, request, size);
    strcat(logInfo->quotedFirstLineOfRequest, "\"");
    
    if (strcmp(command, "GET") == 0 || strcmp(command, "POST") == 0)
    {
        if (strcmp(argument, "status") == 0)
        {
            reply_status(fd, logInfo);
        }
        else if (strcmp(getFileNameExtension(argument), "cgi") == 0) // cgi script request
        {
            reply_cgi(argument, fd, logInfo);
        }
        else if (doesNotExist(argument))
        {    
            reply_404(argument, fd, logInfo);
        }
        else if (isDirectory(argument))
        {
            // check if the directory contains an index.html file
            char *path = malloc(strlen(argument) + strlen("index.html") + 2);
            strcpy(path, argument);
            strcat(path, "/");
            strcat(path, "index.html");
            if (doesNotExist(path))
            {
                reply_ls(argument, fd, logInfo); // if index.html does not exist then list the contents of the directory
            }    
            else // otherwise cat index.html
            {
                reply_cat(path, fd, logInfo);
            }
            free(path);
        }
        else
        {
            reply_cat(argument, fd, logInfo);
        }
    }
    else
    {
        reply_notImplemented(fd, logInfo);
    }
}
Ejemplo n.º 3
0
int loadFromZipByName(unsigned char *buffer, char *archive, char *filename, int *filesize)
{
    char name[_MAX_PATH];
    //unsigned char *buffer;
	int i;
	const char *recognizedExtensions[] = {
		".ngp",
		".npc",
		".ngc"
	};

    int zerror = UNZ_OK;
    unzFile zhandle;
    unz_file_info zinfo;

    zhandle = unzOpen(archive);
    if(!zhandle) return (0);

    /* Seek to first file in archive */
    zerror = unzGoToFirstFile(zhandle);
    if(zerror != UNZ_OK)
    {
        unzClose(zhandle);
        return (0);
    }

	//On scanne tous les fichiers de l'archive et ne prend que ceux qui ont une extension valable, sinon on prend le dernier fichier trouvé...
	while (zerror == UNZ_OK) {
		if (unzGetCurrentFileInfo(zhandle, &zinfo, name, 0xff, NULL, 0, NULL, 0) != UNZ_OK) {
			unzClose(zhandle);
			return 0;
		}

		//Vérifions que c'est la bonne extension
		char *extension = getFileNameExtension(name);

		for (i=0;i<numberof(recognizedExtensions);i++)		{
			if (!strcmp(extension, recognizedExtensions[i]))
				break;
		}
		if (i < numberof(recognizedExtensions))
			break;

		zerror = unzGoToNextFile(zhandle);
	}

    /* Get information about the file */
//    unzGetCurrentFileInfo(zhandle, &zinfo, &name[0], 0xff, NULL, 0, NULL, 0);
    *filesize = zinfo.uncompressed_size;

    /* Error: file size is zero */
    if(*filesize <= 0 || *filesize > (4*1024*1024))
    {
        unzClose(zhandle);
        return (0);
    }

    /* Open current file */
    zerror = unzOpenCurrentFile(zhandle);
    if(zerror != UNZ_OK)
    {
        unzClose(zhandle);
        return (0);
    }

    /* Allocate buffer and read in file */
    //buffer = malloc(*filesize);
    //if(!buffer) return (NULL);
    zerror = unzReadCurrentFile(zhandle, buffer, *filesize);

    /* Internal error: free buffer and close file */
    if(zerror < 0 || zerror != *filesize)
    {
        //free(buffer);
        //buffer = NULL;
        unzCloseCurrentFile(zhandle);
        unzClose(zhandle);
        return (0);
    }

    /* Close current file and archive file */
    unzCloseCurrentFile(zhandle);
    unzClose(zhandle);

    memcpy(filename, name, _MAX_PATH);
    return 1;
}