<commit_before>#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <sass_interface.h>


typedef struct {
    ngx_flag_t  enable;
    ngx_uint_t  error_log;
    ngx_str_t   image_path;
    ngx_str_t   include_paths;
    ngx_uint_t  output_style;
    ngx_uint_t  source_comments;
} ngx_http_sass_loc_conf_t;


static ngx_int_t ngx_http_sass_init(ngx_conf_t *cf);
static void *ngx_http_sass_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_sass_merge_loc_conf(ngx_conf_t *cf,void *parent, void *child);
static char *ngx_http_sass_comments_value(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_sass_error_log_value(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_sass_output_value(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);


static ngx_command_t  ngx_http_sass_commands[] = {
    { ngx_string("sass_comments"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
      ngx_http_sass_comments_value,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_sass_loc_conf_t, source_comments),
      NULL },

    { ngx_string("sass_compile"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
      ngx_conf_set_flag_slot,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_sass_loc_conf_t, enable),
      NULL },

    { ngx_string("sass_error_log"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
      ngx_http_sass_error_log_value,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_sass_loc_conf_t, error_log),
      NULL },

    { ngx_string("sass_image_path"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_str_slot,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_sass_loc_conf_t, image_path),
      NULL },

    { ngx_string("sass_include_paths"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_str_slot,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_sass_loc_conf_t, include_paths),
      NULL },

    { ngx_string("sass_output"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
      ngx_http_sass_output_value,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_sass_loc_conf_t, output_style),
      NULL },

    ngx_null_command
};


static ngx_str_t  ngx_http_sass_type = ngx_string("text/css");


static ngx_str_t  err_levels[] = {
    ngx_null_string,
    ngx_string("emerg"),
    ngx_string("alert"),
    ngx_string("crit"),
    ngx_string("error"),
    ngx_string("warn"),
    ngx_string("notice"),
    ngx_string("info"),
    ngx_string("debug")
};


static ngx_http_module_t  ngx_http_sass_module_ctx = {
    NULL,                          /* preconfiguration */
    ngx_http_sass_init,            /* postconfiguration */

    NULL,                          /* create main configuration */
    NULL,                          /* init main configuration */

    NULL,                          /* create server configuration */
    NULL,                          /* merge server configuration */

    ngx_http_sass_create_loc_conf, /* create location configuration */
    ngx_http_sass_merge_loc_conf   /* merge location configuration */
};


ngx_module_t  ngx_http_sass_module = {
    NGX_MODULE_V1,
    &ngx_http_sass_module_ctx, /* module context */
    ngx_http_sass_commands,    /* module directives */
    NGX_HTTP_MODULE,           /* module type */

    NULL,                      /* init master */
    NULL,                      /* init module */
    NULL,                      /* init process */
    NULL,                      /* init thread */

    NULL,                      /* exit thread */
    NULL,                      /* exit process */
    NULL,                      /* exit master */
    NGX_MODULE_V1_PADDING
};


static ngx_int_t
ngx_http_sass_handler(ngx_http_request_t *r)
{
    size_t                     root;
    u_char                    *last;
    ngx_buf_t*                 b;
    ngx_chain_t                out;
    ngx_file_t                 file;
    ngx_str_t                  path;
    ngx_http_sass_loc_conf_t  *clcf;
    struct sass_file_context  *ctx;
    struct sass_options        options;

    if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
        return NGX_DECLINED;
    }

    clcf = ngx_http_get_module_loc_conf(r, ngx_http_sass_module);

    if (!clcf->enable) {
        return NGX_DECLINED;
    }

    last = ngx_http_map_uri_to_path(r, &path, &root, 0);

    if (NULL == last) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    path.len = last - path.data;

    ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "sass compile file: \"%V\"", &path);
    ngx_memzero(&file, sizeof(ngx_file_t));

    options.output_style    = clcf->output_style;
    options.source_comments = clcf->source_comments;
    options.image_path      = (char*) clcf->image_path.data;
    options.include_paths   = (char*) clcf->include_paths.data;

    ctx             = sass_new_file_context();
    ctx->options    = options;
    ctx->input_path = (char*) path.data;

    sass_compile_file(ctx);

    if (ctx->error_status && ctx->error_message) {
        ngx_log_error(clcf->error_log, r->connection->log, 0, "sass compilation error: %s", ctx->error_message);
        sass_free_file_context(ctx);

        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    } else if (ctx->error_status) {
        ngx_log_error(clcf->error_log, r->connection->log, 0, "sass compilation error");
        sass_free_file_context(ctx);

        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    b        = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
    out.buf  = b;
    out.next = NULL;

    b->pos      = (u_char*) ctx->output_string;
    b->last     = (u_char*) ctx->output_string + strlen(ctx->output_string);
    b->memory   = 1;
    b->last_buf = 1;

    r->headers_out.status           = NGX_HTTP_OK;
    r->headers_out.content_type     = ngx_http_sass_type;
    r->headers_out.content_length_n = strlen(ctx->output_string);

    sass_free_file_context(ctx);

    ngx_http_send_header(r);
    return ngx_http_output_filter(r, &out);
}


static void *
ngx_http_sass_create_loc_conf(ngx_conf_t *cf)
{
    ngx_http_sass_loc_conf_t  *conf;

    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_sass_loc_conf_t));

    if (NULL == conf) {
        return NULL;
    }<commit_msg>fixes issues with unfreed buffer<commit_after>#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <sass_interface.h>


typedef struct {
    ngx_flag_t  enable;
    ngx_uint_t  error_log;
    ngx_str_t   image_path;
    ngx_str_t   include_paths;
    ngx_uint_t  output_style;
    ngx_uint_t  source_comments;
} ngx_http_sass_loc_conf_t;


static ngx_int_t ngx_http_sass_init(ngx_conf_t *cf);
static void *ngx_http_sass_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_sass_merge_loc_conf(ngx_conf_t *cf,void *parent, void *child);
static char *ngx_http_sass_comments_value(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_sass_error_log_value(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_sass_output_value(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);


static ngx_command_t  ngx_http_sass_commands[] = {
    { ngx_string("sass_comments"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
      ngx_http_sass_comments_value,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_sass_loc_conf_t, source_comments),
      NULL },

    { ngx_string("sass_compile"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
      ngx_conf_set_flag_slot,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_sass_loc_conf_t, enable),
      NULL },

    { ngx_string("sass_error_log"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
      ngx_http_sass_error_log_value,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_sass_loc_conf_t, error_log),
      NULL },

    { ngx_string("sass_image_path"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_str_slot,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_sass_loc_conf_t, image_path),
      NULL },

    { ngx_string("sass_include_paths"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
      ngx_conf_set_str_slot,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_sass_loc_conf_t, include_paths),
      NULL },

    { ngx_string("sass_output"),
      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
      ngx_http_sass_output_value,
      NGX_HTTP_LOC_CONF_OFFSET,
      offsetof(ngx_http_sass_loc_conf_t, output_style),
      NULL },

    ngx_null_command
};


static ngx_str_t  ngx_http_sass_type = ngx_string("text/css");


static ngx_str_t  err_levels[] = {
    ngx_null_string,
    ngx_string("emerg"),
    ngx_string("alert"),
    ngx_string("crit"),
    ngx_string("error"),
    ngx_string("warn"),
    ngx_string("notice"),
    ngx_string("info"),
    ngx_string("debug")
};


static ngx_http_module_t  ngx_http_sass_module_ctx = {
    NULL,                          /* preconfiguration */
    ngx_http_sass_init,            /* postconfiguration */

    NULL,                          /* create main configuration */
    NULL,                          /* init main configuration */

    NULL,                          /* create server configuration */
    NULL,                          /* merge server configuration */

    ngx_http_sass_create_loc_conf, /* create location configuration */
    ngx_http_sass_merge_loc_conf   /* merge location configuration */
};


ngx_module_t  ngx_http_sass_module = {
    NGX_MODULE_V1,
    &ngx_http_sass_module_ctx, /* module context */
    ngx_http_sass_commands,    /* module directives */
    NGX_HTTP_MODULE,           /* module type */

    NULL,                      /* init master */
    NULL,                      /* init module */
    NULL,                      /* init process */
    NULL,                      /* init thread */

    NULL,                      /* exit thread */
    NULL,                      /* exit process */
    NULL,                      /* exit master */
    NGX_MODULE_V1_PADDING
};


static ngx_int_t
ngx_http_sass_handler(ngx_http_request_t *r)
{
    size_t                     root;
    u_char                    *last;
    ngx_buf_t*                 b;
    ngx_chain_t                out;
    ngx_file_t                 file;
    ngx_int_t                  rc;
    ngx_str_t                  path;
    ngx_http_sass_loc_conf_t  *clcf;
    struct sass_file_context  *ctx;
    struct sass_options        options;

    if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {
        return NGX_DECLINED;
    }

    clcf = ngx_http_get_module_loc_conf(r, ngx_http_sass_module);

    if (!clcf->enable) {
        return NGX_DECLINED;
    }

    last = ngx_http_map_uri_to_path(r, &path, &root, 0);

    if (NULL == last) {
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    path.len = last - path.data;

    ngx_log_debug(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "sass compile file: \"%V\"", &path);
    ngx_memzero(&file, sizeof(ngx_file_t));

    options.output_style    = clcf->output_style;
    options.source_comments = clcf->source_comments;
    options.image_path      = (char*) clcf->image_path.data;
    options.include_paths   = (char*) clcf->include_paths.data;

    ctx             = sass_new_file_context();
    ctx->options    = options;
    ctx->input_path = (char*) path.data;

    sass_compile_file(ctx);

    if (ctx->error_status && ctx->error_message) {
        ngx_log_error(clcf->error_log, r->connection->log, 0, "sass compilation error: %s", ctx->error_message);
        sass_free_file_context(ctx);

        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    } else if (ctx->error_status) {
        ngx_log_error(clcf->error_log, r->connection->log, 0, "sass compilation error");
        sass_free_file_context(ctx);

        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));

    if (NULL == b) {
        ngx_log_error(clcf->error_log, r->connection->log, 0, "sass compilation error: %s", ctx->error_message);
        sass_free_file_context(ctx);

        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    out.buf  = b;
    out.next = NULL;

    ngx_str_t content = ngx_string(ctx->output_string);

    b->start    = b->pos = content.data;
    b->last     = b->end = content.data + strlen(content.data);
    b->memory   = 1;
    b->last_buf = 1;

    r->headers_out.status           = NGX_HTTP_OK;
    r->headers_out.content_type     = ngx_http_sass_type;
    r->headers_out.content_length_n = strlen(content.data);

    sass_free_file_context(ctx);
    ngx_http_send_header(r);

    rc = ngx_http_output_filter(r, &out);

    free(b);

    return rc;
}


static void *
ngx_http_sass_create_loc_conf(ngx_conf_t *cf)
{
    ngx_http_sass_loc_conf_t  *conf;

    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_sass_loc_conf_t));

    if (NULL == conf) {
        return NULL;
    }
{'fraction_non_alphanumeric': 0.099187979054413, 'fraction_numerical': 0.001897245199969644, 'mean_word_length': 2.8009806749351025, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 80, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6688781', 'n_tokens_mistral': 5076, 'n_tokens_neox': 4899, 'n_words': 696}