<commit_before>/*
 * Copyright (c) Members of the EGEE Collaboration. 2006-2010.
 * See http://www.eu-egee.org/partners/ for details on the copyright holders.
 *
 * 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.
 */

/* $Id$ */

#include <stdarg.h>  /* va_list, va_arg, ... */
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>

/* from ../util */
#include "linkedlist.h"
#include "buffer.h"
#include "base64.h"
#include "log.h"

#include "pep.h"
#include "io.h"
#include "error.h"


#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define PACKAGE_NAME "argus-pep-api"
#define PACKAGE_VERSION "2.1.0"
#endif

/** internal client counter */
static int n_pep_clients= 0;

/** Default constants */
static const long   DEFAULT_CURL_TIMEOUT= 30L;
static const int    DEFAULT_CURL_SSL_VALIDATION= TRUE;
static const int    DEFAULT_LOG_LEVEL= PEP_LOGLEVEL_NONE;
static const FILE * DEFAULT_LOG_FILE= NULL;
static const int    DEFAULT_PIPS_ENABLED= TRUE;
static const int    DEFAULT_OHS_ENABLED= TRUE;
/* default SSL cipher without ECDH: OpenSSL 1.0 bug */
/*
static const char * DEFAULT_SSL_CIPHER_LIST= "DEFAULT:-ECDH";
*/

/** internal functions prototypes */
static void init_pep_defaults(PEP * pep);
static void init_curl_defaults(PEP * pep);
/* static void init_log_defaults(const PEP * pep); */
static int set_curl_endpoint_url(const PEP * pep);
static int set_curl_connection_timeout(const PEP * pep);
static int set_curl_ssl_validation(const PEP * pep);
static int set_curl_ssl_cipher_list(const PEP * pep);
static int set_curl_server_cert(const PEP * pep);
static int set_curl_server_capath(const PEP * pep);
static int set_curl_client_cert(const PEP * pep);
static int set_curl_client_key(const PEP * pep);
static int set_curl_client_keypassword(const PEP * pep);
static int set_curl_verbose(const PEP * pep);
static int set_curl_stderr(const PEP * pep);
static int set_curl_nosignal(const PEP * pep);
static int set_curl_http_headers(PEP * pep);
static int set_curl_ssl_option_allow_beast(PEP * pep);

/** 
* ADT for PEP client handle.
*
* CURL string parameters must be kept as with libcul <= 7.17.0, strings were not copied. 
* Instead the user was forced keep them available until libcurl no longer needed them. 
*/
struct pep_handle {
    int id;
    CURL * curl;
    struct curl_slist * curl_http_headers;
    pep_linkedlist_t * pips;
    pep_linkedlist_t * ohs;
    char * option_endpoint_url; /* current url */
    pep_linkedlist_t * option_endpoint_urls; /* urls list */
    int option_loglevel;
    FILE * option_logout;
    long option_timeout; 
    char * option_server_cert;
    char * option_server_capath;
    char * option_client_cert;
    char * option_client_key;
    char * option_client_keypassword;
    int option_ssl_validation;
    char * option_ssl_cipher_list;
    int option_pips_enabled;
    int option_ohs_enabled;
    // temporary buffers for pep_authorize
    pep_buffer_t * output;
    pep_buffer_t * b64output;
    pep_buffer_t * input;
    pep_buffer_t * b64input;
};

/* GLOBAL NOT THREAD SAFE FUNCTION */
pep_error_t pep_global_init(void) {
    CURLcode curl_rc;
    curl_rc= curl_global_init(CURL_GLOBAL_ALL);
    if (curl_rc != CURLE_OK) {
        pep_log_error("pep_global_init: curl_global_init(CURL_GLOBAL_ALL) failed: %s", curl_easy_strerror(curl_rc));
        return PEP_ERR_CURL + curl_rc;
    }
    return PEP_OK;
}

/* GLOBAL NOT THREAD SAFE FUNCTION */
void pep_global_cleanup(void) {
    curl_global_cleanup();
}


/* create and init */
PEP * pep_initialize(void) {

    /* allocate struct */
    PEP * pep= calloc(1,sizeof(struct pep_handle));
    if (pep == NULL) {
        pep_log_error("pep_initialize: can't allocate struct pep_client: %d", sizeof(struct pep_handle));
        return NULL;
    }
    /* set default PEP values */
    init_pep_defaults(pep);
    
    /* create and init curl handle */
    pep->curl= curl_easy_init();
    if (pep->curl == NULL) {
        pep_log_error("pep_initialize: can't create CURL session handle.");
        free(pep);
        return NULL;
    }
    /* set default CURL options */
    init_curl_defaults(pep);
        
    /* create all required lists */
    pep->pips= pep_llist_create();
    if (pep->pips == NULL) {
        pep_log_error("pep_initialize: PIPs list allocation failed.");
        curl_easy_cleanup(pep->curl);
        free(pep);
        return NULL;
    }
    pep->ohs= pep_llist_create();
    if (pep->ohs == NULL) {
        pep_log_error("pep_initialize: OHs list allocation failed.");
        curl_easy_cleanup(pep->curl);
        pep_llist_delete(pep->pips);
        free(pep);
        return NULL;
    }
    
    pep->option_endpoint_urls= pep_llist_create();
    if (pep->option_endpoint_urls == NULL) {
        pep_log_error("pep_initialize: endpoint URLs list allocation failed.");
        curl_easy_cleanup(pep->curl);
        pep_llist_delete(pep->pips);
        pep_llist_delete(pep->ohs);
        free(pep);
        return NULL;
    }
    
    return pep;
}

int pep_getid(PEP * pep) {<commit_msg>do not allocate the option_endpoint_urls list<commit_after>/*
 * Copyright (c) Members of the EGEE Collaboration. 2006-2010.
 * See http://www.eu-egee.org/partners/ for details on the copyright holders.
 *
 * 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.
 */

/* $Id$ */

#include <stdarg.h>  /* va_list, va_arg, ... */
#include <string.h>
#include <stdlib.h>
#include <curl/curl.h>

/* from ../util */
#include "linkedlist.h"
#include "buffer.h"
#include "base64.h"
#include "log.h"

#include "pep.h"
#include "io.h"
#include "error.h"


#ifdef HAVE_CONFIG_H
#include "config.h"
#else
#define PACKAGE_NAME "argus-pep-api"
#define PACKAGE_VERSION "2.1.0"
#endif

/** internal client counter */
static int n_pep_clients= 0;

/** Default constants */
static const long   DEFAULT_CURL_TIMEOUT= 30L;
static const int    DEFAULT_CURL_SSL_VALIDATION= TRUE;
static const int    DEFAULT_LOG_LEVEL= PEP_LOGLEVEL_NONE;
static const FILE * DEFAULT_LOG_FILE= NULL;
static const int    DEFAULT_PIPS_ENABLED= TRUE;
static const int    DEFAULT_OHS_ENABLED= TRUE;
/* default SSL cipher without ECDH: OpenSSL 1.0 bug */
/*
static const char * DEFAULT_SSL_CIPHER_LIST= "DEFAULT:-ECDH";
*/

/** internal functions prototypes */
static void init_pep_defaults(PEP * pep);
static void init_curl_defaults(PEP * pep);
/* static void init_log_defaults(const PEP * pep); */
static int set_curl_endpoint_url(const PEP * pep);
static int set_curl_connection_timeout(const PEP * pep);
static int set_curl_ssl_validation(const PEP * pep);
static int set_curl_ssl_cipher_list(const PEP * pep);
static int set_curl_server_cert(const PEP * pep);
static int set_curl_server_capath(const PEP * pep);
static int set_curl_client_cert(const PEP * pep);
static int set_curl_client_key(const PEP * pep);
static int set_curl_client_keypassword(const PEP * pep);
static int set_curl_verbose(const PEP * pep);
static int set_curl_stderr(const PEP * pep);
static int set_curl_nosignal(const PEP * pep);
static int set_curl_http_headers(PEP * pep);
static int set_curl_ssl_option_allow_beast(PEP * pep);

/** 
* ADT for PEP client handle.
*
* CURL string parameters must be kept as with libcul <= 7.17.0, strings were not copied. 
* Instead the user was forced keep them available until libcurl no longer needed them. 
*/
struct pep_handle {
    int id;
    CURL * curl;
    struct curl_slist * curl_http_headers;
    pep_linkedlist_t * pips;
    pep_linkedlist_t * ohs;
    char * option_endpoint_url; /* current url */
    pep_linkedlist_t * option_endpoint_urls; /* urls list */
    int option_loglevel;
    FILE * option_logout;
    long option_timeout; 
    char * option_server_cert;
    char * option_server_capath;
    char * option_client_cert;
    char * option_client_key;
    char * option_client_keypassword;
    int option_ssl_validation;
    char * option_ssl_cipher_list;
    int option_pips_enabled;
    int option_ohs_enabled;
    // temporary buffers for pep_authorize
    pep_buffer_t * output;
    pep_buffer_t * b64output;
    pep_buffer_t * input;
    pep_buffer_t * b64input;
};

/* GLOBAL NOT THREAD SAFE FUNCTION */
pep_error_t pep_global_init(void) {
    CURLcode curl_rc;
    curl_rc= curl_global_init(CURL_GLOBAL_ALL);
    if (curl_rc != CURLE_OK) {
        pep_log_error("pep_global_init: curl_global_init(CURL_GLOBAL_ALL) failed: %s", curl_easy_strerror(curl_rc));
        return PEP_ERR_CURL + curl_rc;
    }
    return PEP_OK;
}

/* GLOBAL NOT THREAD SAFE FUNCTION */
void pep_global_cleanup(void) {
    curl_global_cleanup();
}


/* create and init */
PEP * pep_initialize(void) {

    /* allocate struct */
    PEP * pep= calloc(1,sizeof(struct pep_handle));
    if (pep == NULL) {
        pep_log_error("pep_initialize: can't allocate struct pep_client: %d", sizeof(struct pep_handle));
        return NULL;
    }
    /* set default PEP values */
    init_pep_defaults(pep);
    
    /* create and init curl handle */
    pep->curl= curl_easy_init();
    if (pep->curl == NULL) {
        pep_log_error("pep_initialize: can't create CURL session handle.");
        free(pep);
        return NULL;
    }
    /* set default CURL options */
    init_curl_defaults(pep);
        
    /* create all required lists */
    pep->pips= pep_llist_create();
    if (pep->pips == NULL) {
        pep_log_error("pep_initialize: PIPs list allocation failed.");
        curl_easy_cleanup(pep->curl);
        free(pep);
        return NULL;
    }
    pep->ohs= pep_llist_create();
    if (pep->ohs == NULL) {
        pep_log_error("pep_initialize: OHs list allocation failed.");
        curl_easy_cleanup(pep->curl);
        pep_llist_delete(pep->pips);
        free(pep);
        return NULL;
    }
    
    pep->option_endpoint_urls= NULL; // not used
    
    return pep;
}

int pep_getid(PEP * pep) {
{'fraction_non_alphanumeric': 0.1033271719038817, 'fraction_numerical': 0.005730129390018484, 'mean_word_length': 3.6046808510638297, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5474651', 'n_tokens_mistral': 3820, 'n_tokens_neox': 3698, 'n_words': 992}