static char *
ngx_http_geoip2(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
    ngx_http_geoip2_conf_t  *gcf = conf;
    ngx_str_t               *value;
    int                     status, nelts, i;
    ngx_http_geoip2_db_t    *database;
    char                    *rv;
    ngx_conf_t              save;

    value = cf->args->elts;

    if (gcf->databases == NULL) {
        gcf->databases = ngx_array_create(cf->pool, 2,
                                        sizeof(ngx_http_geoip2_db_t));
        if (gcf->databases == NULL) {
            return NGX_CONF_ERROR;
        }
    } else {
        nelts = (int) gcf->databases->nelts;
        database = gcf->databases->elts;

        for (i = 0; i < nelts; i++) {
            if (ngx_strcmp(value[1].data, database[i].mmdb.filename) == 0) {
                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                                   "Duplicate GeoIP2 mmdb - %V", &value[1]);
                return NGX_CONF_ERROR;
            }
        }
    }

    database = ngx_array_push(gcf->databases);
    if (database == NULL) {
        return NGX_CONF_ERROR;
    }

    status = MMDB_open((char *) value[1].data, MMDB_MODE_MMAP, &database->mmdb);

    if (status != MMDB_SUCCESS) {
        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                           "MMDB_open(\"%V\") failed - %s", &value[1],
                           MMDB_strerror(status));
        return NGX_CONF_ERROR;
    }

#if (NGX_HAVE_INET6)
    ngx_memset(&database->address, 0, sizeof(database->address));
#else
    database->address = 0;
#endif

    save = *cf;
    cf->handler = ngx_http_geoip2_add_variable;
    cf->handler_conf = (void *) database;

    rv = ngx_conf_parse(cf, NULL);
    *cf = save;
    return rv;
}
static ngx_int_t
ngx_http_sub_shm_init_res_cache(ngx_http_memcache_server_t **cache,
                                ngx_pool_t *pool)
{
    *cache = ngx_pcalloc(pool, sizeof(ngx_http_memcache_server_t));
    if (*cache == NULL) {
        return NGX_ERROR;

    }

    ngx_memset((*cache)->host, '\0', NAME_LEN);
    ngx_memset((*cache)->status, '\0', 8);

    (*cache)->port = NGX_CONF_UNSET_UINT;

    return NGX_OK;
}
static char *
ngx_http_traffic_status_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
{
	ngx_http_traffic_status_rule_t	*conf = child;
	ngx_memset(conf, 0, sizeof(ngx_http_traffic_status_rule_t));
	
    return NGX_CONF_OK;
}
Exemplo n.º 4
0
void
ngx_setproctitle(char *title)
{
    u_char     *p;

#if (NGX_SOLARIS)

    ngx_int_t   i;
    size_t      size;

#endif

    ngx_os_argv[1] = NULL;

    p = ngx_cpystrn((u_char *) ngx_os_argv[0], (u_char *) "nginx: ",
                    ngx_os_argv_last - ngx_os_argv[0]);

    p = ngx_cpystrn(p, (u_char *) title, ngx_os_argv_last - (char *) p);

#if (NGX_SOLARIS)

    size = 0;

    for (i = 0; i < ngx_argc; i++) {
        size += ngx_strlen(ngx_argv[i]) + 1;
    }

    if (size > (size_t) ((char *) p - ngx_os_argv[0])) {

        /*
         * ngx_setproctitle() is too rare operation so we use
         * the non-optimized copies
         */

        p = ngx_cpystrn(p, (u_char *) " (", ngx_os_argv_last - (char *) p);

        for (i = 0; i < ngx_argc; i++) {
            p = ngx_cpystrn(p, (u_char *) ngx_argv[i],
                            ngx_os_argv_last - (char *) p);
            p = ngx_cpystrn(p, (u_char *) " ", ngx_os_argv_last - (char *) p);
        }

        if (*(p - 1) == ' ') {
            *(p - 1) = ')';
        }
    }

#endif

    if (ngx_os_argv_last - (char *) p) {
        ngx_memset(p, NGX_SETPROCTITLE_PAD, ngx_os_argv_last - (char *) p);
    }

    ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
                   "setproctitle: \"%s\"", ngx_os_argv[0]);
}
Exemplo n.º 5
0
/*----------------------------------------------------------------------------*/
ngx_int_t ngx_http_rp_init_module(ngx_cycle_t *cycle)
{
    rp_notice(cycle->log, "Initialized ngx_http_rp_module version %s-%s",
              VERSION_STR, REVISION_STR);
    ngx_memset(&rp_module_ctx, 0, sizeof(ngx_http_rp_module_ctx_t));

    rp_module_ctx.log = cycle->log;

    return NGX_OK;
}
Exemplo n.º 6
0
ngx_int_t rbtree_destroy_node(rbtree_seed_t *seed, ngx_rbtree_node_t *node) {
  seed->allocd_nodes--;
#if NCHAN_RBTREE_DBG
  ngx_memset(node, 0x67, sizeof(*node));
#endif
  DBG("Destroyed node %p", node);
  ngx_free(node);
  
  return NGX_OK;
}
static ngx_str_t *
ngx_http_video_thumbextractor_create_str(ngx_pool_t *pool, uint len)
{
    ngx_str_t *aux = (ngx_str_t *) ngx_pcalloc(pool, sizeof(ngx_str_t) + len + 1);
    if (aux != NULL) {
        aux->data = (u_char *) (aux + 1);
        aux->len = len;
        ngx_memset(aux->data, '\0', len + 1);
    }
    return aux;
}
ngx_str_t *
ngx_selective_cache_purge_alloc_str(ngx_pool_t *pool, uint len)
{
    ngx_str_t *aux = (ngx_str_t *) ngx_pcalloc(pool, sizeof(ngx_str_t) + len + 1);
    if (aux != NULL) {
        aux->data = (u_char *) (aux + 1);
        aux->len = len;
        ngx_memset(aux->data, '\0', len + 1);
    }
    return aux;
}
static ngx_int_t
ngx_http_sub_shm_init_res_http(ngx_http_http_server_t **http,
                               ngx_pool_t *pool)
{
    *http = ngx_pcalloc(pool, sizeof(ngx_http_http_server_t));
    if (*http == NULL) {
        return NGX_ERROR;

    }

    ngx_memset((*http)->host, '\0', NAME_LEN);
    ngx_memset((*http)->url, '\0', NAME_LEN);
    ngx_memset((*http)->status, '\0', 8);

    ngx_memcpy((*http)->host, sub_ctx->http_host.data, sub_ctx->http_host.len);

    (*http)->port = sub_ctx->http_port;

    return NGX_OK;
}
static ngx_int_t nchan_ws_llink_destroy(nchan_llist_timed_t *cur) {
  nchan_llist_timed_t    *prev, *next;
  prev = cur->prev;
  next = cur->next;
  
  prev->next = next;
  next->prev = prev;
  
  ngx_memset(cur, 0xC4, sizeof(*cur)); //debugstuffs
  ngx_free(cur);
  return NGX_OK;
}
Exemplo n.º 11
0
/*----------------------------------------------------------------------------*/
void *ngx_http_rp_create_loc_conf(ngx_conf_t *cf)
{
    ngx_http_rp_loc_conf_t *conf;

    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_rp_loc_conf_t));
    if(conf == NULL) {
        return NGX_CONF_ERROR;
    }

    ngx_memset(conf, 0, sizeof(ngx_http_rp_loc_conf_t));

    return conf;
}
Exemplo n.º 12
0
// this function adapted from push stream module. thanks Wandenberg Peixoto <*****@*****.**> and Rogério Carvalho Schneider <*****@*****.**>
static ngx_buf_t * nchan_request_body_to_single_buffer(ngx_http_request_t *r) {
  ngx_buf_t *buf = NULL;
  ngx_chain_t *chain;
  ssize_t n;
  off_t len;

  chain = r->request_body->bufs;
  if (chain->next == NULL) {
    return chain->buf;
  }
  //ngx_log_error(NGX_LOG_WARN, ngx_cycle->log, 0, "nchan: multiple buffers in request, need memcpy :(");
  if (chain->buf->in_file) {
    if (ngx_buf_in_memory(chain->buf)) {
      ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "nchan: can't handle a buffer in a temp file and in memory ");
    }
    if (chain->next != NULL) {
      ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "nchan: error reading request body with multiple ");
    }
    return chain->buf;
  }
  buf = ngx_create_temp_buf(r->pool, r->headers_in.content_length_n + 1);
  if (buf != NULL) {
    ngx_memset(buf->start, '\0', r->headers_in.content_length_n + 1);
    while ((chain != NULL) && (chain->buf != NULL)) {
      len = ngx_buf_size(chain->buf);
      // if buffer is equal to content length all the content is in this buffer
      if (len >= r->headers_in.content_length_n) {
        buf->start = buf->pos;
        buf->last = buf->pos;
        len = r->headers_in.content_length_n;
      }
      if (chain->buf->in_file) {
        n = ngx_read_file(chain->buf->file, buf->start, len, 0);
        if (n == NGX_FILE_ERROR) {
          ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "nchan: cannot read file with request body");
          return NULL;
        }
        buf->last = buf->last + len;
        ngx_delete_file(chain->buf->file->name.data);
        chain->buf->file->fd = NGX_INVALID_FILE;
      } else {
        buf->last = ngx_copy(buf->start, chain->buf->pos, len);
      }

      chain = chain->next;
      buf->start = buf->last;
    }
    buf->last_buf = 1;
  }
  return buf;
}
Exemplo n.º 13
0
void ngx_setproctitle(char *title)
{
   u_char *p;

   ngx_os_argv[1] = NULL;

   p = ngx_cpystrn((u_char *) ngx_os_argv[0], (u_char *) "nginx: ", ngx_os_argv_last - ngx_os_argv[0]);

   p = ngx_cpystrn(p, (u_char *) title, ngx_os_argv_last - (char *) p);

   if (ngx_os_argv_last - (char *) p) {
      ngx_memset(p, '\0', ngx_os_argv_last - (char *) p);
   }
}
Exemplo n.º 14
0
/* Returns 1 if application controller within the "app_id" 
 * application directory is OK.
 * Returns 0 otherwise.
 */
inline int is_registered(const char *dir,
                            const char *app_id,
                            const char *fname)
{
    int status;
    char file [strlen(dir) + strlen(app_id) + strlen(fname) + 3];
    struct stat stat_buf;
    const mode_t perms = S_IRUSR | S_IXUSR;
    
    sprintf(file, "%s/%s/%s", dir, app_id, fname);

    if(stat((const char *)file, &stat_buf) < 0) {
        /* File does not exist */
        fprintf(stderr, "%s does not exist.\n", file);
        return 0;
    }
    if((stat_buf.st_mode & perms) != perms) {
        /* Permissions wrong */
        fprintf(stderr, "%s exists but has wrong permissions.\n", file);
        return 0;
    }
    
    rp_bazaar_app_t app;
    ngx_memset(&app, 0, sizeof(rp_bazaar_app_t));

    /* Open and check init, exit and desc symbols - if they exist,
     * controller is OK.
     */

    status = rp_bazaar_app_load_module(file, &app);
    if(status < 0) {
        fprintf(stderr, "Problem loading app (return %d): %s\n", status, dlerror());
        rp_bazaar_app_unload_module(&app);
        return 0;
    }

    int is_reg = 1;
    if(app.verify_app_license_func)
        is_reg = !app.verify_app_license_func(app_id); // 1 - is registered

    rp_bazaar_app_unload_module(&app);

    if(is_reg)    
        fprintf(stderr, "App '%s' is registered\n", app_id);
    else
        fprintf(stderr, "App '%s' is not registered\n", app_id);

    return is_reg;
}
static char *
ngx_tcp_cmd_concat_filename(const char *cmdso_path, const char *fname)
{
    int new_path_len;
    char *new_path;

    new_path_len = ngx_strlen(cmdso_path) + ngx_strlen(fname) + 2;
    new_path = (char *) malloc(new_path_len);
    ngx_memset(new_path, 0, new_path_len);
    if (new_path == NULL)
        return NULL;
    ngx_sprintf((u_char *)new_path, "%s/%s", cmdso_path, fname);

    return new_path;
}
Exemplo n.º 16
0
static uintptr_t
ngx_http_log_escape(ngx_http_log_loc_conf_t *lcf, u_char *dst, u_char *src,
    size_t size)
{
    ngx_uint_t                   n;
    static u_char                hex[] = "0123456789ABCDEF";

    static uint32_t   escape[] = {
        0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */

                    /* ?>=< ;:98 7654 3210  /.-, +*)( '&%$ #"!  */
        0x00000004, /* 0000 0000 0000 0000  0000 0000 0000 0100 */

                    /* _^]\ [ZYX WVUT SRQP  ONML KJIH GFED CBA@ */
        0x10000000, /* 0001 0000 0000 0000  0000 0000 0000 0000 */

                    /*  ~}| {zyx wvut srqp  onml kjih gfed cba` */
        0x80000000, /* 1000 0000 0000 0000  0000 0000 0000 0000 */

        0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
        0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
        0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
        0xffffffff, /* 1111 1111 1111 1111  1111 1111 1111 1111 */
    };

    if (lcf->escape_non_ascii) {
        ngx_memset(&escape[4], 0xff, sizeof(uint32_t) * 4);

    } else {
        ngx_memzero(&escape[4], sizeof(uint32_t) * 4);
    }

    if (dst == NULL) {

        /* find the number of the characters to be escaped */

        n = 0;

        while (size) {
            if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
                n++;
            }
            src++;
            size--;
        }

        return (uintptr_t) n;
    }
Exemplo n.º 17
0
ngx_int_t longpoll_subscriber_destroy(subscriber_t *sub) {
  full_subscriber_t   *fsub = (full_subscriber_t  *)sub;
  
  if(sub->reserved > 0) {
    DBG("%p not ready to destroy (reserved for %i) for req %p", sub, sub->reserved, fsub->sub.request);
    fsub->data.awaiting_destruction = 1;
  }
  else {
    DBG("%p destroy for req %p", sub, fsub->sub.request);
    nchan_free_msg_id(&fsub->sub.last_msgid);
#if NCHAN_SUBSCRIBER_LEAK_DEBUG
    subscriber_debug_remove(sub);
    ngx_free(sub->lbl);
    ngx_memset(fsub, 0xB9, sizeof(*fsub)); //debug
#endif
    ngx_free(fsub);
  }
  return NGX_OK;
}
Exemplo n.º 18
0
static void
ngx_http_sub_init_tables(ngx_http_sub_tables_t *tables,
                         ngx_http_sub_match_t *match, ngx_uint_t n)
{
    u_char      c;
    ngx_uint_t  i, j, min, max, ch;

    min = match[0].match.len;
    max = match[0].match.len;

    for (i = 1; i < n; i++) {
        min = ngx_min(min, match[i].match.len);
        max = ngx_max(max, match[i].match.len);
    }

    tables->min_match_len = min;
    tables->max_match_len = max;

    ngx_http_sub_cmp_index = tables->min_match_len - 1;
    ngx_sort(match, n, sizeof(ngx_http_sub_match_t), ngx_http_sub_cmp_matches);

    min = ngx_min(min, 255);
    ngx_memset(tables->shift, min, 256);

    ch = 0;

    for (i = 0; i < n; i++) {

        for (j = 0; j < min; j++) {
            c = match[i].match.data[tables->min_match_len - 1 - j];
            tables->shift[c] = ngx_min(tables->shift[c], (u_char) j);
        }

        c = match[i].match.data[tables->min_match_len - 1];
        while (ch <= (ngx_uint_t) c) {
            tables->index[ch++] = (u_char) i;
        }
    }

    while (ch < 257) {
        tables->index[ch++] = (u_char) n;
    }
}
static ngx_int_t
ngx_http_sub_shm_init_meta(ngx_http_meta_t **meta,
                           ngx_pool_t *pool)
{
    *meta = ngx_pcalloc(pool, sizeof(ngx_http_meta_t));
    if (*meta == NULL) {
        return NGX_ERROR;
    }

    (*meta)->etag.data = ngx_pcalloc(pool, ETAG_LEN);
    (*meta)->etag.len = ETAG_LEN;

    ngx_memset((*meta)->etag.data, '\0', ETAG_LEN);

    (*meta)->expire  = NGX_CONF_UNSET_MSEC;
    (*meta)->ctime   = NGX_CONF_UNSET_MSEC;

    return NGX_OK;
}
static ngx_buf_t *
ngx_http_push_stream_read_request_body_to_buffer(ngx_http_request_t *r)
{
    ngx_buf_t                              *buf = NULL;
    ngx_chain_t                            *chain;
    ssize_t                                 n;
    off_t                                   len;

    buf = ngx_create_temp_buf(r->pool, r->headers_in.content_length_n + 1);
    if (buf != NULL) {
        buf->memory = 1;
        buf->temporary = 0;
        ngx_memset(buf->start, '\0', r->headers_in.content_length_n + 1);

        chain = r->request_body->bufs;
        while ((chain != NULL) && (chain->buf != NULL)) {
            len = ngx_buf_size(chain->buf);
            // if buffer is equal to content length all the content is in this buffer
            if (len >= r->headers_in.content_length_n) {
                buf->start = buf->pos;
                buf->last = buf->pos;
                len = r->headers_in.content_length_n;
            }

            if (chain->buf->in_file) {
                n = ngx_read_file(chain->buf->file, buf->start, len, 0);
                if (n == NGX_FILE_ERROR) {
                    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "push stream module: cannot read file with request body");
                    return NULL;
                }
                buf->last = buf->last + len;
                ngx_delete_file(chain->buf->file->name.data);
                chain->buf->file->fd = NGX_INVALID_FILE;
            } else {
                buf->last = ngx_copy(buf->start, chain->buf->pos, len);
            }

            chain = chain->next;
            buf->start = buf->last;
        }
    }
    return buf;
}
Exemplo n.º 21
0
int rp_bazaar_app_unload_module(rp_bazaar_app_t *app)
{
    stop_ws_server();
    if(app->handle) {
        if(app->initialized && app->exit_func) {
            app->exit_func();
        }
        if(app->file_name) {
            free(app->file_name);
        }
        if (app->id) {
            free(app->id);
        }
        dlclose(app->handle);


        ngx_memset(app, 0, sizeof(rp_bazaar_app_t));
    }
    return 0;
}
static ngx_int_t
ngx_http_upstream_init_cookie_hash_peer(ngx_http_request_t *r,
                                        ngx_http_upstream_srv_conf_t *us)
{
    ngx_table_elt_t** cookies;
    ngx_http_upstream_cookie_hash_peer_data_t  *ckhp;

    ngx_uint_t i;

    ckhp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_cookie_hash_peer_data_t));
    if (ckhp == NULL) {
        return NGX_ERROR;
    }
    ngx_memset(ckhp->sess_id, 0, sizeof(ckhp->sess_id));


    r->upstream->peer.data = &ckhp->rrp;

    if (ngx_http_upstream_init_round_robin_peer(r, us) != NGX_OK) {
        return NGX_ERROR;
    }
    r->upstream->peer.get = ngx_http_upstream_get_cookie_hash_peer;

    cookies = r->headers_in.cookies.elts;
    for(i=0; i<r->headers_in.cookies.nelts; i++) {
        if (0 == ngx_strcmp((u_char*)"Cookie", cookies[i]->key.data)) {
            if(0 == get_session_id(r, (char*)cookies[i]->value.data, "PHPSESSID", (char*)ckhp->sess_id)) {
                if (strlen((char*)ckhp->sess_id)>0) {
                    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "session id: %s", ckhp->sess_id);
                    break;
                }
            }
        }
    }

    ckhp->hash = 89;
    ckhp->tries = 0;
    ckhp->get_rr_peer = ngx_http_upstream_get_round_robin_peer;

    return NGX_OK;
}
static ngx_int_t
ngx_http_push_stream_send_response_all_channels_info_summarized(ngx_http_request_t *r) {

    ngx_buf_t                                   *b;
    ngx_uint_t                                   len;
    ngx_str_t                                   *currenttime, *hostname, *format;
    u_char                                      *subscribers_by_workers, *start;
    int                                          i;
    ngx_http_push_stream_shm_data_t             *shm_data;
    ngx_http_push_stream_worker_data_t          *worker_data;
    ngx_http_push_stream_content_subtype_t      *subtype;

    subtype = ngx_http_push_stream_match_channel_info_format_and_content_type(r, 1);
    currenttime = ngx_http_push_stream_get_formatted_current_time(r->pool);
    hostname = ngx_http_push_stream_get_formatted_hostname(r->pool);

    shm_data = (ngx_http_push_stream_shm_data_t *) ngx_http_push_stream_shm_zone->data;

    len = (subtype->format_summarized_worker_item->len > subtype->format_summarized_worker_last_item->len) ? subtype->format_summarized_worker_item->len : subtype->format_summarized_worker_last_item->len;
    len = ngx_http_push_stream_worker_processes * (2*NGX_INT_T_LEN + len - 5); //minus 5 sprintf
    subscribers_by_workers = ngx_pcalloc(r->pool, len);
    start = subscribers_by_workers;
    for (i = 0; i < ngx_http_push_stream_worker_processes; i++) {
        format = (i < ngx_http_push_stream_worker_processes - 1) ? subtype->format_summarized_worker_item : subtype->format_summarized_worker_last_item;
        worker_data = shm_data->ipc + i;
        start = ngx_sprintf(start, (char *) format->data, worker_data->pid, worker_data->subscribers);
    }

    len = 3*NGX_INT_T_LEN + subtype->format_summarized->len + hostname->len + currenttime->len + ngx_strlen(subscribers_by_workers) - 18;// minus 18 sprintf

    if ((b = ngx_create_temp_buf(r->pool, len)) == NULL) {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate response buffer.");
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }

    ngx_memset(b->start, '\0', len);
    b->last = ngx_sprintf(b->start, (char *) subtype->format_summarized->data, hostname->data, currenttime->data, shm_data->channels, shm_data->broadcast_channels, shm_data->published_messages, shm_data->subscribers, subscribers_by_workers);

    return ngx_http_push_stream_send_buf_response(r, b, subtype->content_type, NGX_HTTP_OK);
}
static ngx_buf_t *
ngx_http_push_stream_channel_info_formatted(ngx_pool_t *pool, const ngx_str_t *format, ngx_str_t *id, ngx_uint_t published_messages, ngx_uint_t stored_messages, ngx_uint_t subscribers)
{
    ngx_buf_t      *b;
    ngx_uint_t      len;

    if ((format == NULL) || (id == NULL)) {
        return NULL;
    }

    len = 3*NGX_INT_T_LEN + format->len + id->len - 11;// minus 11 sprintf

    if ((b = ngx_create_temp_buf(pool, len)) == NULL) {
        return NULL;
    }

    ngx_memset(b->start, '\0', len);
    b->last = ngx_sprintf(b->start, (char *) format->data, id->data, published_messages, stored_messages, subscribers);
    b->memory = 1;

    return b;
}
Exemplo n.º 25
0
static ngx_int_t
ngx_http_xrlt_transform_body(ngx_http_request_t *r, ngx_http_xrlt_ctx_t *ctx,
                             size_t id, xrltString *val, xrltBool last,
                             xrltBool error)
{
    xrltTransformValue   v;
    int                  result;
    ngx_int_t            rc;

    dd("Transform body (r: %p, id: %zd, val: %p, last: %d, error: %d)",
       r, id, val, last, error);

    v.type = error ? XRLT_TRANSFORM_VALUE_ERROR : XRLT_TRANSFORM_VALUE_BODY;

    if (val == NULL) {
        ngx_memset(&v.bodyval.val, 0, sizeof(xrltString));
    } else {
        ngx_memcpy(&v.bodyval.val, val, sizeof(xrltString));
    }

    v.bodyval.last = last;

    result = xrltTransform(ctx->xctx, id, &v);

    rc = ngx_http_xrlt_process_transform_result(r, ctx, result);

    if (rc == NGX_OK) {
        v.type = XRLT_TRANSFORM_VALUE_EMPTY;

        while (rc == NGX_OK) {
            result = xrltTransform(ctx->xctx, id, &v);

            rc = ngx_http_xrlt_process_transform_result(r, ctx, result);
        }
    }

    return rc == NGX_ERROR ? NGX_ERROR : NGX_OK;
}
Exemplo n.º 26
0
static ngx_int_t destroy_spool(subscriber_pool_t *spool) {
  rbtree_seed_t         *seed = &spool->spooler->spoolseed;
  spooled_subscriber_t  *ssub;
  subscriber_t          *sub;
  ngx_rbtree_node_t     *node = rbtree_node_from_data(spool);
  
  remove_spool(spool);
  
  DBG("destroy spool node %p", node);
  
  for(ssub = spool->first; ssub!=NULL; ssub=ssub->next) {
    sub = ssub->sub;
    //DBG("dequeue sub %p in spool %p", sub, spool);
    sub->fn->dequeue(sub);
  }
  assert(spool->sub_count == 0);
  assert(spool->first == NULL);
  
  ngx_memset(spool, 0x42, sizeof(*spool)); //debug
  
  rbtree_destroy_node(seed, node);
  return NGX_OK;
}
Exemplo n.º 27
0
static char *
ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
#if (NGX_DEBUG)
    ngx_event_conf_t  *ecf = conf;

    ngx_int_t             rc;
    ngx_str_t            *value;
    ngx_url_t             u;
    ngx_cidr_t            c, *cidr;
    ngx_uint_t            i;
    struct sockaddr_in   *sin;
#if (NGX_HAVE_INET6)
    struct sockaddr_in6  *sin6;
#endif

    value = cf->args->elts;

#if (NGX_HAVE_UNIX_DOMAIN)

    if (ngx_strcmp(value[1].data, "unix:") == 0) {
        cidr = ngx_array_push(&ecf->debug_connection);
        if (cidr == NULL) {
            return NGX_CONF_ERROR;
        }

        cidr->family = AF_UNIX;
        return NGX_CONF_OK;
    }

#endif

    rc = ngx_ptocidr(&value[1], &c);

    if (rc != NGX_ERROR) {
        if (rc == NGX_DONE) {
            ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
                               "low address bits of %V are meaningless",
                               &value[1]);
        }

        cidr = ngx_array_push(&ecf->debug_connection);
        if (cidr == NULL) {
            return NGX_CONF_ERROR;
        }

        *cidr = c;

        return NGX_CONF_OK;
    }

    ngx_memzero(&u, sizeof(ngx_url_t));
    u.host = value[1];

    if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) {
        if (u.err) {
            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                               "%s in debug_connection \"%V\"",
                               u.err, &u.host);
        }

        return NGX_CONF_ERROR;
    }

    cidr = ngx_array_push_n(&ecf->debug_connection, u.naddrs);
    if (cidr == NULL) {
        return NGX_CONF_ERROR;
    }

    ngx_memzero(cidr, u.naddrs * sizeof(ngx_cidr_t));

    for (i = 0; i < u.naddrs; i++) {
        cidr[i].family = u.addrs[i].sockaddr->sa_family;

        switch (cidr[i].family) {

#if (NGX_HAVE_INET6)
        case AF_INET6:
            sin6 = (struct sockaddr_in6 *) u.addrs[i].sockaddr;
            cidr[i].u.in6.addr = sin6->sin6_addr;
            ngx_memset(cidr[i].u.in6.mask.s6_addr, 0xff, 16);
            break;
#endif

        default: /* AF_INET */
            sin = (struct sockaddr_in *) u.addrs[i].sockaddr;
            cidr[i].u.in.addr = sin->sin_addr.s_addr;
            cidr[i].u.in.mask = 0xffffffff;
            break;
        }
    }

#else

    ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
                       "\"debug_connection\" is ignored, you need to rebuild "
                       "nginx using --with-debug option to enable it");

#endif

    return NGX_CONF_OK;
}
static ngx_buf_t *
ngx_http_autoindex_html(ngx_http_request_t *r, ngx_array_t *entries)
{
    u_char                         *last, scale;
    off_t                           length;
    size_t                          len, char_len, escape_html;
    ngx_tm_t                        tm;
    ngx_buf_t                      *b;
    ngx_int_t                       size;
    ngx_uint_t                      i, utf8;
    ngx_time_t                     *tp;
    ngx_http_autoindex_entry_t     *entry;
    ngx_http_autoindex_loc_conf_t  *alcf;

    static u_char  title[] =
        "<html>" CRLF
        "<head><title>Index of "
    ;

    static u_char  header[] =
        "</title></head>" CRLF
        "<body bgcolor=\"white\">" CRLF
        "<h1>Index of "
    ;

    static u_char  tail[] =
        "</body>" CRLF
        "</html>" CRLF
    ;

    static char  *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                               "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

    if (r->headers_out.charset.len == 5
        && ngx_strncasecmp(r->headers_out.charset.data, (u_char *) "utf-8", 5)
           == 0)
    {
        utf8 = 1;

    } else {
        utf8 = 0;
    }

    escape_html = ngx_escape_html(NULL, r->uri.data, r->uri.len);

    len = sizeof(title) - 1
          + r->uri.len + escape_html
          + sizeof(header) - 1
          + r->uri.len + escape_html
          + sizeof("</h1>") - 1
          + sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1
          + sizeof("</pre><hr>") - 1
          + sizeof(tail) - 1;

    entry = entries->elts;
    for (i = 0; i < entries->nelts; i++) {
        entry[i].escape = 2 * ngx_escape_uri(NULL, entry[i].name.data,
                                             entry[i].name.len,
                                             NGX_ESCAPE_URI_COMPONENT);

        entry[i].escape_html = ngx_escape_html(NULL, entry[i].name.data,
                                               entry[i].name.len);

        if (utf8) {
            entry[i].utf_len = ngx_utf8_length(entry[i].name.data,
                                               entry[i].name.len);
        } else {
            entry[i].utf_len = entry[i].name.len;
        }

        len += sizeof("<a href=\"") - 1
            + entry[i].name.len + entry[i].escape
            + 1                                          /* 1 is for "/" */
            + sizeof("\">") - 1
            + entry[i].name.len - entry[i].utf_len
            + entry[i].escape_html
            + NGX_HTTP_AUTOINDEX_NAME_LEN + sizeof("&gt;") - 2
            + sizeof("</a>") - 1
            + sizeof(" 28-Sep-1970 12:00 ") - 1
            + 20                                         /* the file size */
            + 2;
    }

    b = ngx_create_temp_buf(r->pool, len);
    if (b == NULL) {
        return NULL;
    }

    b->last = ngx_cpymem(b->last, title, sizeof(title) - 1);

    if (escape_html) {
        b->last = (u_char *) ngx_escape_html(b->last, r->uri.data, r->uri.len);
        b->last = ngx_cpymem(b->last, header, sizeof(header) - 1);
        b->last = (u_char *) ngx_escape_html(b->last, r->uri.data, r->uri.len);

    } else {
        b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len);
        b->last = ngx_cpymem(b->last, header, sizeof(header) - 1);
        b->last = ngx_cpymem(b->last, r->uri.data, r->uri.len);
    }

    b->last = ngx_cpymem(b->last, "</h1>", sizeof("</h1>") - 1);

    b->last = ngx_cpymem(b->last, "<hr><pre><a href=\"../\">../</a>" CRLF,
                         sizeof("<hr><pre><a href=\"../\">../</a>" CRLF) - 1);

    alcf = ngx_http_get_module_loc_conf(r, ngx_http_autoindex_module);
    tp = ngx_timeofday();

    for (i = 0; i < entries->nelts; i++) {
        b->last = ngx_cpymem(b->last, "<a href=\"", sizeof("<a href=\"") - 1);

        if (entry[i].escape) {
            ngx_escape_uri(b->last, entry[i].name.data, entry[i].name.len,
                           NGX_ESCAPE_URI_COMPONENT);

            b->last += entry[i].name.len + entry[i].escape;

        } else {
            b->last = ngx_cpymem(b->last, entry[i].name.data,
                                 entry[i].name.len);
        }

        if (entry[i].dir) {
            *b->last++ = '/';
        }

        *b->last++ = '"';
        *b->last++ = '>';

        len = entry[i].utf_len;

        if (entry[i].name.len != len) {
            if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) {
                char_len = NGX_HTTP_AUTOINDEX_NAME_LEN - 3 + 1;

            } else {
                char_len = NGX_HTTP_AUTOINDEX_NAME_LEN + 1;
            }

            last = b->last;
            b->last = ngx_utf8_cpystrn(b->last, entry[i].name.data,
                                       char_len, entry[i].name.len + 1);

            if (entry[i].escape_html) {
                b->last = (u_char *) ngx_escape_html(last, entry[i].name.data,
                                                     b->last - last);
            }

            last = b->last;

        } else {
            if (entry[i].escape_html) {
                if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) {
                    char_len = NGX_HTTP_AUTOINDEX_NAME_LEN - 3;

                } else {
                    char_len = len;
                }

                b->last = (u_char *) ngx_escape_html(b->last,
                                                  entry[i].name.data, char_len);
                last = b->last;

            } else {
                b->last = ngx_cpystrn(b->last, entry[i].name.data,
                                      NGX_HTTP_AUTOINDEX_NAME_LEN + 1);
                last = b->last - 3;
            }
        }

        if (len > NGX_HTTP_AUTOINDEX_NAME_LEN) {
            b->last = ngx_cpymem(last, "..&gt;</a>", sizeof("..&gt;</a>") - 1);

        } else {
            if (entry[i].dir && NGX_HTTP_AUTOINDEX_NAME_LEN - len > 0) {
                *b->last++ = '/';
                len++;
            }

            b->last = ngx_cpymem(b->last, "</a>", sizeof("</a>") - 1);

            if (NGX_HTTP_AUTOINDEX_NAME_LEN - len > 0) {
                ngx_memset(b->last, ' ', NGX_HTTP_AUTOINDEX_NAME_LEN - len);
                b->last += NGX_HTTP_AUTOINDEX_NAME_LEN - len;
            }
        }

        *b->last++ = ' ';

        ngx_gmtime(entry[i].mtime + tp->gmtoff * 60 * alcf->localtime, &tm);

        b->last = ngx_sprintf(b->last, "%02d-%s-%d %02d:%02d ",
                              tm.ngx_tm_mday,
                              months[tm.ngx_tm_mon - 1],
                              tm.ngx_tm_year,
                              tm.ngx_tm_hour,
                              tm.ngx_tm_min);

        if (alcf->exact_size) {
            if (entry[i].dir) {
                b->last = ngx_cpymem(b->last,  "                  -",
                                     sizeof("                  -") - 1);
            } else {
                b->last = ngx_sprintf(b->last, "%19O", entry[i].size);
            }

        } else {
            if (entry[i].dir) {
                b->last = ngx_cpymem(b->last,  "      -",
                                     sizeof("      -") - 1);

            } else {
                length = entry[i].size;

                if (length > 1024 * 1024 * 1024 - 1) {
                    size = (ngx_int_t) (length / (1024 * 1024 * 1024));
                    if ((length % (1024 * 1024 * 1024))
                                                > (1024 * 1024 * 1024 / 2 - 1))
                    {
                        size++;
                    }
                    scale = 'G';

                } else if (length > 1024 * 1024 - 1) {
                    size = (ngx_int_t) (length / (1024 * 1024));
                    if ((length % (1024 * 1024)) > (1024 * 1024 / 2 - 1)) {
                        size++;
                    }
                    scale = 'M';

                } else if (length > 9999) {
                    size = (ngx_int_t) (length / 1024);
                    if (length % 1024 > 511) {
                        size++;
                    }
                    scale = 'K';

                } else {
                    size = (ngx_int_t) length;
                    scale = '\0';
                }

                if (scale) {
                    b->last = ngx_sprintf(b->last, "%6i%c", size, scale);

                } else {
                    b->last = ngx_sprintf(b->last, " %6i", size);
                }
            }
        }

        *b->last++ = CR;
        *b->last++ = LF;
    }

    b->last = ngx_cpymem(b->last, "</pre><hr>", sizeof("</pre><hr>") - 1);

    b->last = ngx_cpymem(b->last, tail, sizeof(tail) - 1);

    return b;
}
Exemplo n.º 29
0
ngx_int_t
ngx_log_set_syslog(ngx_pool_t *pool, ngx_str_t *value, ngx_log_t *log)
{
    size_t                 len;
    u_char                *p, *p_bak, pri[5];
    ngx_int_t              rc, port, facility, loglevel;
    ngx_str_t              ident;
    ngx_url_t              u;
    ngx_addr_t             addr;
    ngx_uint_t             i;
    enum {
        sw_facility = 0,
        sw_loglevel,
        sw_address,
        sw_port,
        sw_ident,
        sw_done
    } state;

    p = value->data;
    facility = -1;
    loglevel = -1;
    ident.len = 0;
    ident.data = NULL;
    state = sw_facility;
    ngx_memset(&addr, 0, sizeof(ngx_addr_t));

    /**
     * format example:
     *     syslog:user:info:127.0.0.1:514:ident
     *     syslog:user:info:/dev/log:ident
     *     syslog:user:info:127.0.0.1::ident
     *         is short for syslog:user:info:127.0.0.1:514:ident
     *     syslog:user:info:/dev/log
     *         is short for syslog:user:info:/dev/log:NGINX
     *     syslog:user:info:127.0.0.1
     *         is short for syslog:user:info:127.0.0.1:514:NGINX
     *     syslog:user::/dev/log:ident
     *         is short for syslog:user:info:/dev/log:ident
     *     syslog:user:info::ident
     *         is short for syslog:user:info:/dev/log:ident
     *     syslog:user:info
     *         is short for syslog:user:info:/dev/log:NGINX
     *     syslog:user
     *         is short for syslog:user:info:/dev/log:NGINX
     */
    while (state != sw_done) {
        p_bak = p;
        while (*p != ':' && (size_t) (p - value->data) < value->len) p++;

        switch (state) {
        case sw_facility:
            len = p - p_bak;

            for (i = 0; ngx_syslog_facilities[i].name != NULL; i++) {
                if (len == strlen(ngx_syslog_facilities[i].name)
                    && ngx_strncmp(ngx_syslog_facilities[i].name, p_bak, len)
                    == 0)
                {
                    facility = ngx_syslog_facilities[i].val;
                    break;
                }
            }

            if (facility == -1) {
                return NGX_ERROR;
            }

            state = sw_loglevel;

            break;

        case sw_loglevel:
            len = p - p_bak;

            if (len == 0) {
                loglevel = NGX_SYSLOG_INFO;
            } else {
                for (i = 0; ngx_syslog_priorities[i].name != NULL; i++) {
                    if (len == strlen(ngx_syslog_priorities[i].name)
                        && ngx_strncmp(ngx_syslog_priorities[i].name,
                                       p_bak, len)
                        == 0)
                    {
                        loglevel = ngx_syslog_priorities[i].val;
                        break;
                    }
                }

                if (loglevel == -1) {
                    return NGX_ERROR;
                }
            }

            state = sw_address;

            break;

        case sw_address:
            len = p - p_bak;

            if (len == 0) {
                addr.name.data = (u_char *) "/dev/log";
                addr.name.len = sizeof("/dev/log") - 1;

                rc = ngx_set_unix_domain(pool, &addr,
                         (u_char *) "/dev/log", sizeof("/dev/log") - 1);

                state = sw_ident;

            } else {
                addr.name.data = p_bak;
                addr.name.len = len;

                ngx_memzero(&u, sizeof(ngx_url_t));
                u.url.data = p_bak;
                u.url.len = len;
                u.one_addr = 1;

                rc = ngx_parse_url(pool, &u);
                if (rc != NGX_OK) {
                    rc = ngx_set_unix_domain(pool, &addr, p_bak, len);
                    state = sw_ident;
                } else {
                    state = sw_port;
                    addr.socklen = u.addrs[0].socklen;
                    addr.sockaddr = u.addrs[0].sockaddr;
                }
            }

            if (rc != NGX_OK) {
                return NGX_ERROR;
            }

            break;

        case sw_port:
            len = p - p_bak;

            port = ngx_atoi(p_bak, len);
            if (port < 1) {
                port = 514;
            } else if (port > 65535) {
                return NGX_ERROR;
            } else {
                addr.name.len += 1 + len;
            }

            switch (addr.sockaddr->sa_family) {

#if (NGX_HAVE_INET6)
            case AF_INET6:
                ((struct sockaddr_in6 *) addr.sockaddr)->sin6_port =
                                         htons((in_port_t) port);
                break;
#endif

            case AF_INET:
                ((struct sockaddr_in *) addr.sockaddr)->sin_port =
                                        htons((in_port_t) port);
                break;

            default: /* AF_UNIX */
                break;
            }

            state = sw_ident;

            break;

        case sw_ident:
            len = p - p_bak;

            ident.len = len;
            ident.data = p_bak;

            state = sw_done;

            break;

        default:

            break;
        }

        if (p < value->data + value->len) {
            p++;
        }
    }

    log->syslog = ngx_pcalloc(pool, sizeof(ngx_syslog_t));
    if (log->syslog == NULL) {
        return NGX_ERROR;
    }

    p = ngx_snprintf(pri, 5, "<%i>", facility + loglevel);
    log->syslog->syslog_pri.len = p - pri;
    log->syslog->syslog_pri.data = ngx_pcalloc(pool, p - pri);
    if (log->syslog->syslog_pri.data == NULL) {
        return NGX_ERROR;
    }
    ngx_memcpy(log->syslog->syslog_pri.data, pri, p - pri);

    log->syslog->addr = addr;
    log->syslog->ident = ident;
    log->syslog->fd = -1;
    log->syslog->header.data = log->syslog->header_buf;

    return NGX_OK;
}
Exemplo n.º 30
0
char *
ngx_log_set_log(ngx_conf_t *cf, ngx_log_t **head)
{
    ngx_log_t          *new_log;
    ngx_str_t          *value, name;
    ngx_syslog_peer_t  *peer;

    if (*head != NULL && (*head)->log_level == 0) {
        new_log = *head;

    } else {

        new_log = ngx_pcalloc(cf->pool, sizeof(ngx_log_t));
        if (new_log == NULL) {
            return NGX_CONF_ERROR;
        }

        if (*head == NULL) {
            *head = new_log;
        }
    }

    value = cf->args->elts;

    if (ngx_strcmp(value[1].data, "stderr") == 0) {
        ngx_str_null(&name);
        cf->cycle->log_use_stderr = 1;

        new_log->file = ngx_conf_open_file(cf->cycle, &name);
        if (new_log->file == NULL) {
            return NGX_CONF_ERROR;
        }

     } else if (ngx_strncmp(value[1].data, "memory:", 7) == 0) {

#if (NGX_DEBUG)
        size_t                 size, needed;
        ngx_pool_cleanup_t    *cln;
        ngx_log_memory_buf_t  *buf;

        value[1].len -= 7;
        value[1].data += 7;

        needed = sizeof("MEMLOG  :" NGX_LINEFEED)
                 + cf->conf_file->file.name.len
                 + NGX_SIZE_T_LEN
                 + NGX_INT_T_LEN
                 + NGX_MAX_ERROR_STR;

        size = ngx_parse_size(&value[1]);

        if (size == (size_t) NGX_ERROR || size < needed) {
            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                               "invalid buffer size \"%V\"", &value[1]);
            return NGX_CONF_ERROR;
        }

        buf = ngx_pcalloc(cf->pool, sizeof(ngx_log_memory_buf_t));
        if (buf == NULL) {
            return NGX_CONF_ERROR;
        }

        buf->start = ngx_pnalloc(cf->pool, size);
        if (buf->start == NULL) {
            return NGX_CONF_ERROR;
        }

        buf->end = buf->start + size;

        buf->pos = ngx_slprintf(buf->start, buf->end, "MEMLOG %uz %V:%ui%N",
                                size, &cf->conf_file->file.name,
                                cf->conf_file->line);

        ngx_memset(buf->pos, ' ', buf->end - buf->pos);

        cln = ngx_pool_cleanup_add(cf->pool, 0);
        if (cln == NULL) {
            return NGX_CONF_ERROR;
        }

        cln->data = new_log;
        cln->handler = ngx_log_memory_cleanup;

        new_log->writer = ngx_log_memory_writer;
        new_log->wdata = buf;

#else
        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                           "nginx was built without debug support");
        return NGX_CONF_ERROR;
#endif

     } else if (ngx_strncmp(value[1].data, "syslog:", 7) == 0) {
        peer = ngx_pcalloc(cf->pool, sizeof(ngx_syslog_peer_t));
        if (peer == NULL) {
            return NGX_CONF_ERROR;
        }

        if (ngx_syslog_process_conf(cf, peer) != NGX_CONF_OK) {
            return NGX_CONF_ERROR;
        }

        new_log->writer = ngx_syslog_writer;
        new_log->wdata = peer;

    } else {
        new_log->file = ngx_conf_open_file(cf->cycle, &value[1]);
        if (new_log->file == NULL) {
            return NGX_CONF_ERROR;
        }
    }

    if (ngx_log_set_levels(cf, new_log) != NGX_CONF_OK) {
        return NGX_CONF_ERROR;
    }

    if (*head != new_log) {
        ngx_log_insert(*head, new_log);
    }

    return NGX_CONF_OK;
}