<commit_before>/*
** Copyright 2014-2016 The Earlham Institute
** 
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
** 
**     http://www.apache.org/licenses/LICENSE-2.0
** 
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
/*
 * This code is based on the simplessl.c example code from http://curl.haxx.se/libcurl/c/simplessl.html
 */

/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2012, <NAME>, <<EMAIL>>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at http://curl.haxx.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 */
/* some requirements for this to work:
   1.   set pCertFile to the file with the client certificate
   2.   if the key is passphrase protected, set pPassphrase to the
        passphrase you use
   3.   if you are using a crypto engine:
   3.1. set a #define USE_ENGINE
   3.2. set pEngine to the name of the crypto engine you use
   3.3. set pKeyName to the key identifier you want to use
   4.   if you don't use a crypto engine:
   4.1. set pKeyName to the file name of your client key
   4.2. if the format of the key file is DER, set pKeyType to "DER"

   !! verify of the server certificate is not implemented here !!

   **** This example only works with libcurl 7.9.3 and later! ****

*/
 
/***************************************************************************/

#include <string.h>

#include <curl/curl.h>
#include <curl/easy.h>

#include "jansson.h"

#include "curl_tools.h"
#include "streams.h"
#include "memory_allocations.h"

#include "string_utils.h"
#include "streams.h"


#ifdef _DEBUG
	#define CURL_TOOLS_DEBUG	(STM_LEVEL_FINE)
#else
	#define CURL_TOOLS_DEBUG	(STM_LEVEL_NONE)
#endif


typedef struct CURLParam
{
	CURLoption cp_opt;
	const char *cp_value_s;
} CURLParam;


static size_t WriteMemoryCallback (char *response_data_p, size_t block_size, size_t num_blocks, void *store_p);

static size_t WriteFileCallback (char *response_data_p, size_t block_size, size_t num_blocks, void *store_p);

static bool SetCurlToolJSONRequestData (CurlTool *tool_p, json_t *json_p);


CurlTool *AllocateCurlTool (CurlMode mode)
{
	ByteBuffer *buffer_p = AllocateByteBuffer (1024);
	
	if (buffer_p)
		{
			CurlTool *curl_tool_p = (CurlTool *) AllocMemory (sizeof (CurlTool));

			if (curl_tool_p)
				{
					if (SetupCurl (curl_tool_p, buffer_p))
						{
							curl_tool_p -> ct_buffer_p = buffer_p;
							curl_tool_p -> ct_form_p = NULL;
							curl_tool_p -> ct_last_field_p = NULL;
							curl_tool_p -> ct_headers_list_p = NULL;
							curl_tool_p -> ct_temp_f = NULL;
							curl_tool_p -> ct_mode = mode;

							return curl_tool_p;
						}

					FreeMemory (curl_tool_p);
				}		/* if (curl_tool_p) */

			FreeByteBuffer (buffer_p);
		}		/* if (buffer_p) */
		
	return NULL;
}


void FreeCurlTool (CurlTool *curl_tool_p)
{
	FreeCurl (curl_tool_p -> ct_curl_p);

	if (curl_tool_p -> ct_headers_list_p)
		{
			curl_slist_free_all (curl_tool_p -> ct_headers_list_p);
		}

	FreeByteBuffer (curl_tool_p -> ct_buffer_p);
	FreeMemory (curl_tool_p);
}


bool SetupCurl (CurlTool *tool_p, ByteBuffer *buffer_p)
{
	tool_p -> ct_curl_p = curl_easy_init ();
	
	if (tool_p -> ct_curl_p)
		{
			if (buffer_p)
				{
					if (AddCurlCallback (tool_p, buffer_p))
						{
							#if CURL_TOOLS_DEBUG >= STM_LEVEL_FINER
							curl_easy_setopt (curl_p, CURLOPT_VERBOSE, 1L);
							#endif

							return true;
						}
					else
						{
							PrintErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, "Failed to add buffer callback for curl object\n");
						}
				}

			FreeCurl (tool_p -> ct_curl_p);<commit_msg>fixed compiplation error when CURL_TOOLS_DEBUG >= STM_LEVEL_FINER<commit_after>/*
** Copyright 2014-2016 The Earlham Institute
** 
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
** 
**     http://www.apache.org/licenses/LICENSE-2.0
** 
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/
/*
 * This code is based on the simplessl.c example code from http://curl.haxx.se/libcurl/c/simplessl.html
 */

/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2012, <NAME>, <<EMAIL>>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at http://curl.haxx.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 */
/* some requirements for this to work:
   1.   set pCertFile to the file with the client certificate
   2.   if the key is passphrase protected, set pPassphrase to the
        passphrase you use
   3.   if you are using a crypto engine:
   3.1. set a #define USE_ENGINE
   3.2. set pEngine to the name of the crypto engine you use
   3.3. set pKeyName to the key identifier you want to use
   4.   if you don't use a crypto engine:
   4.1. set pKeyName to the file name of your client key
   4.2. if the format of the key file is DER, set pKeyType to "DER"

   !! verify of the server certificate is not implemented here !!

   **** This example only works with libcurl 7.9.3 and later! ****

*/
 
/***************************************************************************/

#include <string.h>

#include <curl/curl.h>
#include <curl/easy.h>

#include "jansson.h"

#include "curl_tools.h"
#include "streams.h"
#include "memory_allocations.h"

#include "string_utils.h"
#include "streams.h"


#ifdef _DEBUG
	#define CURL_TOOLS_DEBUG	(STM_LEVEL_FINE)
#else
	#define CURL_TOOLS_DEBUG	(STM_LEVEL_NONE)
#endif


typedef struct CURLParam
{
	CURLoption cp_opt;
	const char *cp_value_s;
} CURLParam;


static size_t WriteMemoryCallback (char *response_data_p, size_t block_size, size_t num_blocks, void *store_p);

static size_t WriteFileCallback (char *response_data_p, size_t block_size, size_t num_blocks, void *store_p);

static bool SetCurlToolJSONRequestData (CurlTool *tool_p, json_t *json_p);


CurlTool *AllocateCurlTool (CurlMode mode)
{
	ByteBuffer *buffer_p = AllocateByteBuffer (1024);
	
	if (buffer_p)
		{
			CurlTool *curl_tool_p = (CurlTool *) AllocMemory (sizeof (CurlTool));

			if (curl_tool_p)
				{
					if (SetupCurl (curl_tool_p, buffer_p))
						{
							curl_tool_p -> ct_buffer_p = buffer_p;
							curl_tool_p -> ct_form_p = NULL;
							curl_tool_p -> ct_last_field_p = NULL;
							curl_tool_p -> ct_headers_list_p = NULL;
							curl_tool_p -> ct_temp_f = NULL;
							curl_tool_p -> ct_mode = mode;

							return curl_tool_p;
						}

					FreeMemory (curl_tool_p);
				}		/* if (curl_tool_p) */

			FreeByteBuffer (buffer_p);
		}		/* if (buffer_p) */
		
	return NULL;
}


void FreeCurlTool (CurlTool *curl_tool_p)
{
	FreeCurl (curl_tool_p -> ct_curl_p);

	if (curl_tool_p -> ct_headers_list_p)
		{
			curl_slist_free_all (curl_tool_p -> ct_headers_list_p);
		}

	FreeByteBuffer (curl_tool_p -> ct_buffer_p);
	FreeMemory (curl_tool_p);
}


bool SetupCurl (CurlTool *tool_p, ByteBuffer *buffer_p)
{
	tool_p -> ct_curl_p = curl_easy_init ();
	
	if (tool_p -> ct_curl_p)
		{
			if (buffer_p)
				{
					if (AddCurlCallback (tool_p, buffer_p))
						{
							#if CURL_TOOLS_DEBUG >= STM_LEVEL_FINER
							curl_easy_setopt (tool_p -> ct_curl_p, CURLOPT_VERBOSE, 1L);
							#endif

							return true;
						}
					else
						{
							PrintErrors (STM_LEVEL_SEVERE, __FILE__, __LINE__, "Failed to add buffer callback for curl object\n");
						}
				}

			FreeCurl (tool_p -> ct_curl_p);
{'fraction_non_alphanumeric': 0.12726694241170855, 'fraction_numerical': 0.008908685968819599, 'mean_word_length': 3.040274207369323, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '102673', 'n_tokens_mistral': 3602, 'n_tokens_neox': 3066, 'n_words': 967}