/*
 * Copyright 2014 Cloud Linux Zug GmbH
 *
 * Licensed under CLOUD LINUX LICENSE AGREEMENT
 * http://cloudlinux.com/docs/LICENSE.TXT
 *
 * The part the support library for lsapi & proxy_lsapi Apache modules
 * author Alexander Demeshko <ademeshko@cloudlinux.com>
 *
 */

#ifndef _LSCAPI_UTIL_H_
#define _LSCAPI_UTIL_H_

#include <lscapi.h>

#include <httpd.h>
#include <http_config.h>
#include <http_core.h>
#include <http_log.h>
#include <http_protocol.h>
#include <unixd.h>
#include <apr_strings.h>

#ifdef APACHE2_2

#define APLOGNO(n)              "AH" #n ": "

#define APLOG_USE_MODULE(foo) \
    extern module AP_MODULE_DECLARE_DATA foo##_module

#else

extern const int *lsapilog_module_index;

#ifdef APLOG_MODULE_INDEX
#undef APLOG_MODULE_INDEX
#endif

#define APLOG_MODULE_INDEX  \
    (aplog_module_index ? *aplog_module_index : (lsapilog_module_index ? *lsapilog_module_index : APLOG_NO_MODULE) )
#endif

#define LSCAPI_APLOGNO APLOGNO(01968)

struct lsapi_conn_t {
    request_rec *r;
    const module *m;
    lscapi_rec *lscapi;
    lsphp_conn_t *backend;
};
typedef struct lsapi_conn_t lsapi_conn_t;

struct lsapi_svr_conf_t {
    char *socket_path;
    uint32_t default_uid;
    uint32_t default_gid;
    uint32_t suphp_uid;
    uint32_t suphp_gid;
    uint32_t ruid;
    uint32_t rgid;
    uint32_t itk_uid;
    uint32_t itk_gid;

    lscapi_backend_info_t backend_info;
    
    unsigned lve_enabled:1;
    unsigned debug_enabled:1;
    unsigned terminate_backends_on_exit: 1;

    unsigned debug_enabled_was_set:1;
    unsigned terminate_backends_was_set: 1;

};
typedef struct lsapi_svr_conf_t lsapi_svr_conf_t;

struct php_param_t {
    char *value;
    size_t valueLen;
    int isAdmin;
};
typedef struct php_param_t php_param_t;

struct lsapi_dir_conf_t {
    apr_hash_t *phpParams;
};
typedef struct lsapi_dir_conf_t lsapi_dir_conf_t;

lsapi_conn_t *lscapi_create_connection(request_rec *r, const char *backend_path,
                                       unsigned *flagsPtr, apr_status_t *statusPtr, 
                                       char *errbuf, size_t errlen,
                                       const module *m);
void lscapi_close_connection(lsapi_conn_t *conn);


int lscapi_parse_server_vars(request_rec *r, lscapi_var_t **varsPtr);
int lscapi_parse_special_vars(request_rec *r, apr_hash_t *cfgH, lscapi_var_t **varsPtr);

void lscapi_recreate_http_header(request_rec *r,
                                lsapi_http_header_index_t **hdrIndexPtr,
                                lsapi_header_offset_t **hdrOffsetsPtr, 
                                size_t *hdrOffsetsNumPtr,
                                int *contentLengthPtr,
                                char **bufPtr, size_t *bufLenPtr);

#ifdef lscapi_log
#undef lscapi_log
#endif

#define lscapi_log(level, r, fmt, ...)           \
    lscapi_log_rerror(__FILE__, __LINE__, level, r, fmt, __VA_ARGS__)


void lscapi_log_rerror(const char *file, int line, int level,
                        const request_rec *r, const char *fmt, ...)
                        __attribute__((format(printf,5,6)));

apr_status_t lscapi_do_request(lsapi_conn_t *conn, lsapi_svr_conf_t *svrcfg);

const char* lscapi_php_value_handler(cmd_parms* cmd, void *cfg,
                                    const char *name, const char *value,
                                    int isAdmin);
const char *lscapi_php_flag_handler(cmd_parms *cmd, void *cfg,
                                    const char *name, const char *value, int isAdmin);
void *lscapi_create_dir_config(apr_pool_t *pool, char *x);
void *lscapi_merge_dir_config(apr_pool_t *pool, void *base, void *cur);
void *lscapi_create_svr_config(apr_pool_t *pool, server_rec *s);
void *lscapi_merge_svr_config(apr_pool_t *pool, void *base, void *cur);


//int process_lve_error (request_rec * r, lsapi_dir_conf_t * cfg);


#endif //_LSCAPI_UTIL_H_

