int allow_content_type(const char *uri, int urilen)
{
    const char *ctype;
    int ctype_len;

    if (!http_allowed_content_types_head) {
        return 1; // Allowed 
    }

    ctype = get_http_content_type(uri, urilen);
    if (!ctype) {
    	return 0; // Not allowed
    }
    ctype_len = strlen(ctype);

    name_descriptor_t *nd;

    pthread_mutex_lock(&cfg_mutex);
    nd = http_allowed_content_types_head;
    while (nd) {
        if ((nd->namelen == ctype_len) && 
		(strncasecmp(nd->name, ctype, ctype_len) == 0)) {
    	    pthread_mutex_unlock(&cfg_mutex);
	    return 1; // Match, allow
	}
	nd = nd->next;
    }
    pthread_mutex_unlock(&cfg_mutex);
    return 0; // No Match
}
static int setup_http_build_200(http_cb_t * phttp, mime_header_t * presponse_hdr)
{
	char buf[128];
	const char *data;
	const char * type;
        const cooked_data_types_t *pckd;
        int dlen;
	mime_hdr_datatype_t dtype;
	int len;

	// Setup content length
	if (!get_cooked_known_header_data(presponse_hdr,
                                        MIME_HDR_CONTENT_LENGTH, &pckd, &dlen,
                                        &dtype)) {
		if (dtype == DT_SIZE) {
			phttp->content_length = pckd->u.dt_size.ll;
		}
		if( !CHECK_HTTP_FLAG(phttp, HRF_TRANSCODE_CHUNKED) ) {
			len = snprintf(buf, sizeof(buf), "%ld", 
				CHECK_HTTP_FLAG(phttp, HRF_MULTIPART_RESPONSE) ?
					phttp->pseudo_content_length : 
					phttp->content_length);
			add_known_header(presponse_hdr, MIME_HDR_CONTENT_LENGTH, buf, len);
		}
	}


	glob_http_tot_content_length ++;

	if (!is_known_header_present(presponse_hdr, MIME_HDR_CONTENT_TYPE)) {
		const namespace_config_t *nsc;
		/* Only fill up content_type when it is NFS OM */
		nsc = phttp->nsconf;
		if(nsc && nsc->http_config) {
		    if((nsc->http_config->origin.select_method == OSS_NFS_CONFIG) ||
		       (nsc->http_config->origin.select_method == OSS_NFS_SERVER_MAP)) {
			get_uri(phttp, &data, &len);
			type = get_http_content_type(data, len);
			add_known_header(presponse_hdr, MIME_HDR_CONTENT_TYPE, type, strlen(type));
		    }
		}
	}
        delete_known_header(presponse_hdr, MIME_HDR_CONNECTION) ;
	if( CHECK_HTTP_FLAG(phttp, HRF_CONNECTION_KEEP_ALIVE) ) {
		add_known_header(presponse_hdr, MIME_HDR_CONNECTION, "Keep-Alive", 10);
		SET_HTTP_FLAG(phttp, HRF_CONNECTION_KEEP_ALIVE);
	}
	else {
		add_known_header(presponse_hdr, MIME_HDR_CONNECTION, "Close", 5);
		CLEAR_HTTP_FLAG(phttp, HRF_CONNECTION_KEEP_ALIVE);
	}

#if 0
	add_known_header(presponse_hdr, MIME_HDR_CACHE_CONTROL, "no-cache", 8);
#endif

        return 1;
}
Esempio n. 3
0
char* create_HTTP_response_header(const char *filename, char buffer[])
{
	//printf("................\n");
	char* response_header = (char*) malloc(BUFFER_SIZE);
	int range_end;
	int range_start = 0;
	int content_length;
	char *ptr;
	char *lineRange = NULL;
	//char *lineModified = NULL;



	if(response_header == NULL)
	{
		error("Error allocating response_header");
	}
	char status_text[100] = "HTTP/1.1 %i Partial Content\r\n";
	char date_text[100] = "DATUM Funktion einbauen\r\n";
	char server_text[100] = "Server: TinyWeb (Build Jun 12 2014)\r\n";
	char accept_range_text[100] = "Accept-Ranges: bytes\r\n";
	char last_modified_text[100] = "Last-Modified: Thu, 12 Jun 2014\r\n";
	char content_type_text[100] = "Content-Type: text/html\r\n";
	char content_length_text[100] = "Content-Length: 1004\r\n";
	char content_range_text[100] = "Content-Range: bytes 6764-7767/7768\r\n";
	char connection_text[100] = "Connection: Close\r\n\r\n";

	//file length calculating
	struct stat buf;
	if(stat(filename, &buf) != 0)
	{
		printf("Error in file length calculating.\n");
	}

   	//set_http_status(HTTP_STATUS_PARTIAL_CONTENT);
   	time_t t;
   	struct tm *ts;

   	t = time(NULL);
   	ts = localtime(&t);

   	struct stat file_Info;





	sprintf(status_text, "HTTP/1.1 %i %s\r\n", http_status_list[get_http_status()].code, http_status_list[get_http_status()].text ); //TODO: status dynamisch uebergeben
	sprintf(date_text, "Date: %s, %i %s %i %02i:%02i:%02i GMT\r\n", get_weekday(ts->tm_wday), ts->tm_mday, get_month(ts->tm_mon), ts->tm_year + 1900, ts->tm_hour, ts->tm_min, ts->tm_sec); //TODO: Reutemann fragen ob das Format so passt
	//sprintf(server_text, "Server: TinyWeb (Build Jun 12 2014)", ); //TODO: Buildzeit dynamisch einfuegen


	ptr = strtok(NULL, "\n");
	while (ptr != NULL) {
		// extract line with the range if existing
		if (strncmp(ptr,"Range",5) == 0) {
			lineRange = ptr;
		}
		else if ((strncmp(ptr,"If-Modified-Since",strlen("If-Modified-Since"))) == 0) {
			//lineModified = ptr;
		}
		ptr = strtok(NULL, "\n");
	}

	if (lineRange != NULL) {
		char *range = malloc(strlen(lineRange)+1);
		if (range == NULL) {
			perror("Malloc():");
		}
		strtok(lineRange,"=");
		range = strtok(NULL,"="); // after "="
		range_start = atoi(strtok(range,"-"));
		range_end = atoi(strtok(NULL,"-"));

		//printf("Start: %i End: %i", range_start, range_end);
	}



		int check;
		check = stat(filename, &file_Info);

		if (check < 0) {
			printf("NOT FOUND ACCESS CHECK ");
			set_http_status(HTTP_STATUS_NOT_FOUND);
		}

	   	// get last modified
		char* last_modified = malloc(32);
	   	struct tm * timeinfo;
	   	timeinfo = localtime(&file_Info.st_mtim.tv_sec);

	   	strftime (last_modified,32,"%a, %d %b %Y %H:%M:%S %Z",timeinfo);

	sprintf(last_modified_text, "Last-Modified: %s\n", last_modified); //TODO: Dateidatum einfuegen
	sprintf(content_type_text, "Content-Type: %s\r\n",  get_http_content_type_str(get_http_content_type(filename)));



	range_end = file_Info.st_size;

	if(range_end < 0)
	{
		error("Error with range");
	}
	else
	{
		content_length = range_end - range_start;
	}

	sprintf(content_length_text, "Content-Length: %i\r\n", content_length);
	sprintf(content_range_text, "Content-Range: bytes %i-%i/%i\n", range_start, range_end, content_length ); //TODO: Frage was das ist und wie dynamisch abgefragt wird

	strcat(response_header, status_text);
	strcat(response_header, date_text);
	strcat(response_header, server_text);
	strcat(response_header, accept_range_text);
	strcat(response_header, last_modified_text);
	strcat(response_header, content_type_text);
	strcat(response_header, content_length_text);
	strcat(response_header, content_range_text);
	strcat(response_header, connection_text);
	printf("\n------------Response-----------------\n%s------------Response-----------------\n", response_header);

	return response_header;
}
static int setup_http_build_206(http_cb_t * phttp, mime_header_t * presponse_hdr)
{
	char buf[128];
	const char *data;
	const char * type;
	const cooked_data_types_t *pckd;
	int dlen;
	mime_hdr_datatype_t dtype;
	int len;
	off_t start, stop;

	// Setup content length
	if (!get_cooked_known_header_data(presponse_hdr,
                                        MIME_HDR_CONTENT_LENGTH, &pckd, &dlen,
                                        &dtype)) {
		if (dtype == DT_SIZE) {
			phttp->content_length = pckd->u.dt_size.ll;
		}
	}

	glob_http_tot_byte_range ++;

        phttp->respcode = CHECK_HTTP_FLAG(phttp, HRF_BYTE_SEEK) ? 200 : 206;
        phttp->subcode = 0;

	if (!is_known_header_present(presponse_hdr, MIME_HDR_CONTENT_TYPE)) {
		const namespace_config_t *nsc;
		/* Only fill up content_type when it is NFS OM */
		nsc = phttp->nsconf;
		if(nsc && nsc->http_config) {
		    if((nsc->http_config->origin.select_method == OSS_NFS_CONFIG) ||
		       (nsc->http_config->origin.select_method == OSS_NFS_SERVER_MAP)) {
			get_uri(phttp, &data, &len);
			type = get_http_content_type(data, len);
			add_known_header(presponse_hdr, MIME_HDR_CONTENT_TYPE, type, strlen(type));
		    }
		}
	}
        delete_known_header(presponse_hdr, MIME_HDR_CONNECTION) ;
	if( CHECK_HTTP_FLAG(phttp, HRF_CONNECTION_KEEP_ALIVE) ) {
		add_known_header(presponse_hdr, MIME_HDR_CONNECTION, "Keep-Alive", 10);
		SET_HTTP_FLAG(phttp, HRF_CONNECTION_KEEP_ALIVE);
	}
	else {
		add_known_header(presponse_hdr, MIME_HDR_CONNECTION, "Close", 5);
		CLEAR_HTTP_FLAG(phttp, HRF_CONNECTION_KEEP_ALIVE);
	}

	if (CHECK_HTTP_FLAG(phttp, HRF_MULTI_BYTE_RANGE)) {
		/* 
		 * bug 5301 - MFD remembers only the first byte range in the multi 
		 * byte range request. So we should not modify the content_length
		 * and add content_range based on brstart and brstop values. If MFD
		 * remembers all brstart adn brstop, we can think of doing.
		 */
		return 1;
	} else if (CHECK_HTTP_FLAG(phttp, HRF_BYTE_RANGE)) {
		start = phttp->brstart;
		stop = phttp->brstop;
	} else if (CHECK_HTTP_FLAG(phttp, HRF_BYTE_SEEK)) {
		start = phttp->seekstart;
		stop = phttp->seekstop;
	} else {
		// Should never happen
		start = 0;
		stop = 0;
	}

	if (!is_known_header_present(presponse_hdr, MIME_HDR_CONTENT_RANGE)) {
           if (!CHECK_HTTP_FLAG(phttp, HRF_BYTE_SEEK)) {
               len = snprintf(buf, sizeof(buf), "bytes %ld-%ld/%ld", start,
			stop, phttp->content_length);
		add_known_header(presponse_hdr, MIME_HDR_CONTENT_RANGE, buf, len);
           }
	}

	len = snprintf(buf, sizeof(buf), "%ld", 
		stop - start + 1 + 
		(CHECK_HTTP_FLAG(phttp, HRF_BYTE_SEEK) ? 
			phttp->prepend_content_size +
			phttp->append_content_size  : 0));
	add_known_header(presponse_hdr, MIME_HDR_CONTENT_LENGTH, buf, len);

#if 0
	add_known_header(presponse_hdr, MIME_HDR_CACHE_CONTROL, "no-cache", 8);
#endif

        return 1;
}