// 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; }
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 ); } }
static void StatQuery_CacheResponseRaw( stat_query_t *query ) { size_t respSize; wswcurl_getsize( query->req, &respSize ); if( !respSize ) return; // read the response string query->response_raw = SQALLOC( respSize + 1); respSize = wswcurl_read( query->req, query->response_raw, respSize ); query->response_raw[respSize] = '\0'; }
static void StatQuery_CacheTokenized( stat_query_t *query ) { size_t respSize; char *buffer, *realBuffer, *p, *end, **tokens; int numTokens, startOfs, endToken; if( !query->response_raw ) StatQuery_CacheResponseRaw( query ); if( !query->response_raw ) return; respSize = strlen( query->response_raw ); buffer = query->response_raw; // count the number of tokens (could this be done in 1 pass?) p = buffer; end = buffer + respSize; numTokens = 0; startOfs = 0; while( p < end ) { // skip whitespace while( p < end && *p <= ' ') p++; if( p >= end ) break; if( !numTokens ) startOfs = p - buffer; numTokens++; // skip the token while( p < end && *p > ' ') p++; } // fail if( !numTokens ) { SQFREE( buffer ); return; } tokens = SQALLOC( (numTokens + 1) * sizeof( char* ) ); // allocate the actual buffer that we are going to return if( startOfs > 0 ) { realBuffer = SQALLOC( (respSize - startOfs) + 1 ); memcpy( realBuffer, buffer + startOfs, respSize - startOfs ); SQFREE( buffer ); buffer = realBuffer; respSize -= startOfs; buffer[respSize] = '\0'; } // 2nd pass, mark all tokens p = buffer; end = buffer + respSize; endToken = numTokens; numTokens = 0; while( p < end && numTokens < endToken ) { // we made the buffer point into the first character // so we can shuffle this loop a little tokens[numTokens++] = p; // skip the token while( p < end && *p > ' ') p++; if( p >= end ) break; // skip whitespace while( p < end && *p <= ' ') *p++ = '\0'; } *p = '\0'; // we left room for 1 character tokens[numTokens] = 0; query->response_tokens = tokens; query->response_numtokens = numTokens; }
void *SQ_JSON_Alloc( size_t size ) { return SQALLOC( size ); }