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 ); } }
void StatQuery_DestroyQuery( stat_query_t *query ) { // close wswcurl and json_in json_out if( query->req ) wswcurl_delete( query->req ); // cached responses if( query->response_tokens ) { if( query->response_tokens[0] && query->response_tokens[0] != query->response_raw ) SQFREE( query->response_tokens[0] ); SQFREE( query->response_tokens ); } if( query->response_raw ) SQFREE( query->response_raw ); // actual query SQFREE( query ); }
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_Free( void *ptr ) { SQFREE( ptr ); }