コード例 #1
0
// TODO; prefer GET/POST
stat_query_t *StatQuery_CreateQuery( const char *str, qboolean get )
{
	stat_query_t *query;

	assert( str != NULL );
	if( str == NULL ) {
		return NULL;
	}

	query = SQALLOC( sizeof( *query ) );
	memset( query, 0, sizeof( *query ) );

	query->json_out = cJSON_CreateObject();

	if( str[0] == '/' ) {
		str += 1;
	}

	if( !get )
		query->req = wswcurl_create( "%s/%s", mm_url->string, str );
	else
	{
		// add in '/', '?' and '\0' = 3
		query->url = SQALLOC( strlen( mm_url->string ) + strlen( str ) + 3 );
		// ch : lazy code :(
		strcpy( query->url, mm_url->string );
		strcat( query->url, "/" );
		strcat( query->url, str );
		strcat( query->url, "?" );
	}

	return query;
}
コード例 #2
0
ファイル: mm_query.c プロジェクト: Picmip/qfusion
static void StatQuery_Prepare( stat_query_t *query ) {
	if( !query->req && query->url ) {
		// GET request, finish the url and create the object
		query->req = wswcurl_create( query->iface, query->url );
	}
	// only allow json for POST requests
	else if( query->has_json ) {
		const char *json_text;
		size_t jsonSize, b64Size;
		unsigned long compSize;
		void *compData, *b64Data;
		int z_result;

		if( query->url ) {
			Com_Printf( "StatQuery: Tried to add JSON field to GET request\n" );
			return;
		}

		json_text = cJSON_Print( query->json_out );
		jsonSize = strlen( json_text );

		// compress
		compSize = ( jsonSize * 1.1 ) + 12;
		compData = SQALLOC( compSize );
		if( compData == NULL ) {
			Com_Printf( "StatQuery: Failed to allocate space for compressed JSON\n" );
			return;
		}
		z_result = qzcompress( compData, &compSize, (unsigned char*)json_text, jsonSize );
		if( z_result != Z_OK ) {
			Com_Printf( "StatQuery: Failed to compress JSON\n" );
			SQFREE( compData );
			return;
		}

		// base64
		b64Data = base64_encode( compData, compSize, &b64Size );
		if( b64Data == NULL ) {
			Com_Printf( "StatQuery: Failed to base64_encode JSON\n" );
			SQFREE( compData );
			return;
		}

		// Com_Printf("Match report size: %u, compressed: %u, base64'd: %u\n", reportSize, compSize, b64Size );

		// we dont need this anymore
		SQFREE( compData );
		compData = NULL;

		// set the json field to POST request
		wswcurl_formadd_raw( query->req, "data", b64Data, b64Size );

		free( b64Data );
	}
}