コード例 #1
0
void writeFileToPackage(char *f, char *relpath) {
	char *filename = relpath;
	uint16_t crcsum;
	// Uncompressed then compressed size
	int usize = 0, csize = 0, c;
	FILE *in = fopen(f, "rb");
	int plen = strlen(filename);
	
	fputc(plen, packager.output);
	fputs(filename, packager.output);
	fputc(packager.compressionType, packager.output);
	
	fseek(in, 0, SEEK_END);
	usize = ftell(in);
	fseek(in, 0, SEEK_SET);
	// 24-bits uncompressed file's size
	fputc(usize & 0xff, packager.output);
	fputc((usize >> 8) & 0xff, packager.output);
	fputc((usize >> 16) & 0xff, packager.output);
	
	if (packager.compressionType == COMPRESSION_NONE) {
		csize = usize;
	} else if (packager.compressionType == COMPRESSION_RLE) {
		//
		// TODO
		//
		// RLE compression
		// put compressed size in variable 'csize'
	} else if (packager.compressionType == COMPRESSION_PUCRUNCH) {
		//
		// TODO
		//
		// pucrunch compression
		// put compressed size in variable 'csize'
	}
	// 24-bits compressed file's size
	fputc(csize & 0xff, packager.output);
	fputc((csize >> 8) & 0xff, packager.output);
	fputc((csize >> 16) & 0xff, packager.output);
	
	while ((c = fgetc(in)) != EOF) {
		fputc(c, packager.output);
	}
	
	if (packager.sumType == SUM_NONE) {
		// No checksum
		fputc(SUM_NONE, packager.output);
	} else if (packager.sumType == SUM_CRC16) {
		// CRC-16 checksum
		// See checksums.c for implementation
		fputc(SUM_CRC16, packager.output);
		crcsum = calculateCRC16(in);
		fputc(crcsum & 0xff, packager.output);
		fputc(crcsum >> 8, packager.output);
	} else if (packager.sumType == SUM_SHA1) {
コード例 #2
0
void Parse_Request(char *buffer, struct Element *crcTable, int new_fd, char* PATH, unsigned int *crcPageNotFound) {
    if (!strncmp(buffer, "GET ", 4)) {
        char *startUriPos, *endUriPos;
        printf("input: GET request\n");
        buffer += 4;
        startUriPos = buffer;
        endUriPos = strchr(buffer, ' '); // get position of the ending URI blank space: GET -url- HTTP/1.0
        printf("startPos: character=%c, %p, endPos: character=%c, %p\n", *startUriPos, startUriPos, *endUriPos, endUriPos);
        int url1Length;
        char *url1;
        url1Length = endUriPos - startUriPos - 1;
        url1 = (char*) calloc(url1Length, sizeof (char));
        strncpy(url1, buffer + 1, url1Length);
        printf("URL: %s\n", url1);

        int urlLength = url1Length;
        char *url = (char *) calloc(urlLength + 1, sizeof (char));
        strcat(url, url1);
        printf("PATH, URL: %s, %s\n", PATH, url1);
        printf("combined: %s, urlLength=%d\n", url, urlLength);
        // exit(0);

        unsigned int crcValue = calculateCRC16(url, urlLength);
        printf("crcValue of URL: %d\n", crcValue);
        printf("crcTable[crcValue=%d].pointer=%d\n", crcValue, crcTable[crcValue].pointer);
        printf("crcTable[crcValue=%d].sizeBytes=%d\n", crcValue, crcTable[crcValue].sizeBytes);
        
        // exit(0);

        // check if resource exist on the server
        if (crcTable[crcValue].pointer != 0) {
            // get the resource in the RAM and send it back to a client
            printf("The resource '%s' EXISTS in the RAM\n", url);
            // ssize_t send(int sockfd, const void *buf, size_t len, int flags);
            int bytesSent = send(new_fd, crcTable[crcValue].pointer, crcTable[crcValue].sizeBytes, 0);
            printf("bytesSent: %d\n", bytesSent);
        } else {
            printf("The resource '%s' does not exist in the RAM\nSending default page to the client\n", url);
            int bytesSent = send(new_fd, crcTable[*crcPageNotFound].pointer, crcTable[*crcPageNotFound].sizeBytes, 0);
            exit(-3);
        }

    };
}