static jobject jni_ngx_http_clojure_shared_map_put_if_absent(JNIEnv *env, jclass cls, jlong jctx,
		jint ktype, jobject key, jlong koff, jlong klen, jint vtype, jobject val, jlong voff, jlong vlen) {
	void *pp[2];
	ngx_int_t rc;
	u_char err[1024]= {0};
	ngx_http_clojure_shared_map_ctx_t *ctx = (ngx_http_clojure_shared_map_ctx_t *)(uintptr_t)jctx;

	pp[0] = (void *)env;
	pp[1] = cls;
	rc = ctx->impl->put_if_absent(ctx,
			(uint8_t)ktype, ngx_http_clojure_abs_off_addr(key, koff), (size_t)klen,
			(uint8_t)vtype, ngx_http_clojure_abs_off_addr(val, voff), (size_t)vlen,
			nji_ngx_http_clojure_shared_map_val_to_jobj_handler,
			pp
			);
	if (rc == NGX_CLOJURE_SHARED_MAP_OUT_OF_MEM) {
		jclass ec = (*env)->FindClass(env, "java/lang/OutOfMemoryError");
		if (ec != NULL) {
			ngx_snprintf(err, sizeof(err)-1, "shared map '%V' outofmemory (size=%ud)!", &ctx->name, ctx->impl->size(ctx));
			(*env)->ThrowNew(env, ec, (char*)err);
		}
		(*env)->DeleteLocalRef(env, ec);
	} else if (rc == NGX_CLOJURE_SHARED_MAP_INVLAID_VALUE_TYPE) {
		jclass ec = (*env)->FindClass(env, "java/lang/RuntimeException");
		if (ec != NULL) {
			ngx_snprintf(err, sizeof(err) - 1, "shared map '%V' value type %d is not matched with existing type!", &ctx->name, vtype);
			(*env)->ThrowNew(env, ec, (char*)err);
		}
		(*env)->DeleteLocalRef(env, ec);
	}
	return !rc ? pp[1] : NULL;
}
u_char *
ngx_tcp_log_error(ngx_log_t *log, u_char *buf, size_t len)
{
    u_char              *p;
    ngx_tcp_session_t   *s;
    ngx_tcp_log_ctx_t   *ctx;

    p = buf;

    if (log->action) {
        p = ngx_snprintf(p, len + (buf - p), " while %s", log->action);
    }

    ctx = log->data;

    p = ngx_snprintf(p, len + (buf - p), ", client: %V", ctx->client);

    s = ctx->session;

    if (s == NULL) {
        return p;
    }

    p = ngx_snprintf(p, len + (buf - p), ", server: %V", s->addr_text);

    /*
    if (s->upstream) {
        if (s->upstream->peer.connection) {
            p = ngx_snprintf(p, len + (buf - p), ", upstream: %V", s->upstream->peer.name);
        }
    }
    */

    return p;
}
static jlong jni_ngx_http_clojure_shared_map_remove_number(JNIEnv *env, jclass cls, jlong jctx, jint ktype, jobject key,
		jlong koff, jlong klen, jint vtype, jlong null_val) {
	ngx_http_clojure_shared_map_ctx_t *ctx = (ngx_http_clojure_shared_map_ctx_t *)(uintptr_t)jctx;
	jlong rt[2];
	u_char err[1024] = {0};
	ngx_int_t rc;

	rt[0] = vtype;
	rt[1] = 0;
	rc = ctx->impl->remove(ctx,
					(uint8_t)ktype, ngx_http_clojure_abs_off_addr(key, koff), (size_t)klen,
					nji_ngx_http_clojure_shared_map_val_to_jpimary_handler,
					rt);

	if (rc != NGX_CLOJURE_SHARED_MAP_OK) {
		ngx_snprintf(err, sizeof(err) - 1, "shared map '%V' key %d not found!", &ctx->name, key);
		goto THROWS_EXCEPTION;
	}else if (rt[0] == NGX_CLOJURE_SHARED_MAP_NOT_FOUND) {
		return null_val;
	}
	return rt[1];

THROWS_EXCEPTION: {
	jclass ec = (*env)->FindClass(env, "java/lang/RuntimeException");
	if (ec != NULL) {
		ngx_snprintf(err, sizeof(err) - 1, "shared map '%V' key %d not found!", &ctx->name, key);
		(*env)->ThrowNew(env, ec, (char*)err);
	}
	(*env)->DeleteLocalRef(env, ec);
	return rc;
  }
}
Example #4
0
    static ngx_int_t
ngx_http_auth_spnego_headers(
        ngx_http_request_t *r,
        ngx_http_auth_spnego_ctx_t *ctx,
        ngx_str_t *token,
        ngx_http_auth_spnego_loc_conf_t *alcf)
{
    ngx_str_t value = ngx_null_string;
    /* only use token if authorized as there appears to be a bug in
     * Google Chrome when parsing a 401 Negotiate with a token */
    if (NULL == token || ctx->ret != NGX_OK) {
        value.len = sizeof("Negotiate") - 1;
        value.data = (u_char *) "Negotiate";
    } else {
        value.len = sizeof("Negotiate") + token->len; /* space accounts for \0 */
        value.data = ngx_pcalloc(r->pool, value.len);
        if (NULL == value.data) {
            return NGX_ERROR;
        }
        ngx_snprintf(value.data, value.len, "Negotiate %V", token);
    }

    r->headers_out.www_authenticate =
        ngx_list_push(&r->headers_out.headers);
    if (NULL == r->headers_out.www_authenticate) {
        return NGX_ERROR;
    }

    r->headers_out.www_authenticate->hash = 1;
    r->headers_out.www_authenticate->key.len = sizeof("WWW-Authenticate") - 1;
    r->headers_out.www_authenticate->key.data = (u_char *) "WWW-Authenticate";
    r->headers_out.www_authenticate->value.len = value.len;
    r->headers_out.www_authenticate->value.data = value.data;

    if (alcf->allow_basic) {
        ngx_str_t value2 = ngx_null_string;
        value2.len = sizeof("Basic realm=\"\"") - 1 + alcf->realm.len;
        value2.data = ngx_pcalloc(r->pool, value2.len);
        if (NULL == value2.data) {
            return NGX_ERROR;
        }
        ngx_snprintf(value2.data, value2.len, "Basic realm=\"%V\"",
                &alcf->realm);
        r->headers_out.www_authenticate =
            ngx_list_push(&r->headers_out.headers);
        if (NULL == r->headers_out.www_authenticate) {
            return NGX_ERROR;
        }

        r->headers_out.www_authenticate->hash = 2;
        r->headers_out.www_authenticate->key.len = sizeof("WWW-Authenticate") - 1;
        r->headers_out.www_authenticate->key.data = (u_char *) "WWW-Authenticate";
        r->headers_out.www_authenticate->value.len = value2.len;
        r->headers_out.www_authenticate->value.data = value2.data;
    }

    ctx->head = 1;

    return NGX_OK;
}
Example #5
0
void NPrintBig(NLogCb LogCb,const char* LEVEL, const char* funcName, 
			const char* fileName, int line,  const char* format,  ...){
#define NLOG_BIGBUF_LEN (1024*32)
#define big_buf_rest(buf, p) (buf+NLOG_BIGBUF_LEN-p-1)
	u_char logbuf[NLOG_BIGBUF_LEN];	
	memset(logbuf, 0, NLOG_BIGBUF_LEN);
	u_char* p = logbuf; 
	//不显示日志时间。
	time_t timep;
	struct tm *ptm, mytm;
	timep = ngx_time();
	ptm = localtime_r(&timep, &mytm); 
	
	p = ngx_snprintf(p, big_buf_rest(logbuf, p), NLOG_TF "%s:%s[%s:%d] ", 
			1+ptm->tm_mon, ptm->tm_mday,  
			ptm->tm_hour, ptm->tm_min, ptm->tm_sec, 
			LEVEL, funcName, fileName, line); 

	va_list   args;
	va_start(args,   format); 
	p = ngx_vslprintf(p ,  logbuf+NLOG_BIGBUF_LEN-2, format , args);
	va_end(args); 
	if(big_buf_rest(logbuf, p) > 0){ 
 		p = ngx_snprintf(p, big_buf_rest(logbuf, p), "\n");
	}
	logbuf[NLOG_BIGBUF_LEN-1] = 0;
	if(LogCb == NULL){
		fprintf(stderr, "%.*s", (int)(p-logbuf), logbuf);
	}else{
 		LogCb((const char*)logbuf, p-logbuf);
 	}
}
static void
ngx_rtmp_stat_bw(ngx_http_request_t *r, ngx_chain_t ***lll,
        ngx_rtmp_bandwidth_t *bw_in, ngx_rtmp_bandwidth_t *bw_out)
{
    u_char                          buf[NGX_OFF_T_LEN + 1];

    ngx_rtmp_update_bandwidth(bw_in, 0);
    ngx_rtmp_update_bandwidth(bw_out, 0);

    NGX_RTMP_STAT_L("<in>");
    NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf), 
                "%uz", bw_in->bytes) - buf);
    NGX_RTMP_STAT_L("</in>\r\n");

    NGX_RTMP_STAT_L("<out>");
    NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf), 
                "%uz", bw_out->bytes) - buf);
    NGX_RTMP_STAT_L("</out>\r\n");

    NGX_RTMP_STAT_L("<bwin>");
    NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf), 
                "%uz", bw_in->bandwidth * 8) - buf);
    NGX_RTMP_STAT_L("</bwin>\r\n");

    NGX_RTMP_STAT_L("<bwout>");
    NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf), 
                "%uz", bw_out->bandwidth * 8) - buf);
    NGX_RTMP_STAT_L("</bwout>\r\n");
}
static void
ngx_rtmp_stat_bw(ngx_http_request_t *r, ngx_chain_t ***lll,
                 ngx_rtmp_bandwidth_t *bw, char *name,
                 ngx_uint_t flags)
{
    u_char  buf[NGX_INT64_LEN + 9];

    ngx_rtmp_update_bandwidth(bw, 0);

    if (flags & NGX_RTMP_STAT_BW) {
        NGX_RTMP_STAT_L("<bw_");
        NGX_RTMP_STAT_CS(name);
        NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf), ">%uL</bw_",
                                        bw->bandwidth * 8)
                           - buf);
        NGX_RTMP_STAT_CS(name);
        NGX_RTMP_STAT_L(">\r\n");
    }

    if (flags & NGX_RTMP_STAT_BYTES) {
        NGX_RTMP_STAT_L("<bytes_");
        NGX_RTMP_STAT_CS(name);
        NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf), ">%uL</bytes_",
                                        bw->bytes)
                           - buf);
        NGX_RTMP_STAT_CS(name);
        NGX_RTMP_STAT_L(">\r\n");
    }
}
static u_char *
ngx_http_lua_log_timer_error(ngx_log_t *log, u_char *buf, size_t len)
{
    u_char              *p;
    ngx_connection_t    *c;

    if (log->action) {
        p = ngx_snprintf(buf, len, " while %s", log->action);
        len -= p - buf;
        buf = p;
    }

    c = log->data;

    dd("ctx = %p", ctx);

    p = ngx_snprintf(buf, len, ", context: ngx.timer");
    len -= p - buf;
    buf = p;

    if (c->addr_text.len) {
        p = ngx_snprintf(buf, len, ", client: %V", &c->addr_text);
        len -= p - buf;
        buf = p;
    }

    if (c && c->listening && c->listening->addr_text.len) {
        p = ngx_snprintf(buf, len, ", server: %V", &c->listening->addr_text);
        /* len -= p - buf; */
        buf = p;
    }

    return buf;
}
Example #9
0
static passenger_app_type_t
detect_application_type(const ngx_str_t *public_dir) {
    u_char filename[NGX_MAX_PATH];
    
    ngx_memzero(filename, sizeof(filename));
    ngx_snprintf(filename, sizeof(filename), "%s/%s",
                 public_dir->data, "../config/environment.rb");
    if (file_exists(filename, 1)) {
        return AP_RAILS;
    }
    
    ngx_memzero(filename, sizeof(filename));
    ngx_snprintf(filename, sizeof(filename), "%s/%s",
                 public_dir->data, "../config.ru");
    if (file_exists(filename, 1)) {
        return AP_RACK;
    }
    
    ngx_memzero(filename, sizeof(filename));
    ngx_snprintf(filename, sizeof(filename), "%s/%s",
                 public_dir->data, "../passenger_wsgi.py");
    if (file_exists(filename, 1)) {
        return AP_WSGI;
    }
    
    return AP_NONE;
}
static u_char *
ngx_http_lua_log_ssl_sess_store_error(ngx_log_t *log, u_char *buf, size_t len)
{
    u_char              *p;
    ngx_connection_t    *c;

    if (log->action) {
        p = ngx_snprintf(buf, len, " while %s", log->action);
        len -= p - buf;
        buf = p;
    }

    p = ngx_snprintf(buf, len, ", context: ssl_session_store_by_lua*");
    len -= p - buf;
    buf = p;

    c = log->data;

    if (c->addr_text.len) {
        p = ngx_snprintf(buf, len, ", client: %V", &c->addr_text);
        len -= p - buf;
        buf = p;
    }

    if (c && c->listening && c->listening->addr_text.len) {
        p = ngx_snprintf(buf, len, ", server: %V", &c->listening->addr_text);
        buf = p;
    }

    return buf;
}
static u_char *
ngx_rtmp_log_error(ngx_log_t *log, u_char *buf, size_t len)
{
    u_char                     *p;
    ngx_rtmp_session_t         *s;
    ngx_rtmp_error_log_ctx_t   *ctx;

    if (log->action) {
        p = ngx_snprintf(buf, len, " while %s", log->action);
        len -= p - buf;
        buf = p;
    }

    ctx = log->data;

    p = ngx_snprintf(buf, len, ", client: %V", ctx->client);
    len -= p - buf;
    buf = p;

    s = ctx->session;

    if (s == NULL) {
        return p;
    }

    p = ngx_snprintf(buf, len, ", server: %V", s->addr_text);
    len -= p - buf;
    buf = p;

    return p;
}
Example #12
0
static void
ngx_syslog_prebuild_header(ngx_syslog_t *task)
{
    size_t        len;
    u_char       *p, pid[NGX_INT64_LEN], *appname;
    ngx_int_t     ident_len;

    appname = (u_char *) NGINX_VAR;

    p = ngx_snprintf(pid, NGX_INT64_LEN, "%P", ngx_log_pid);

    len = sizeof(" ") - 1
        + ngx_syslog_hostname.len
        + (task->ident.len == 0
            ? (ident_len = sizeof(NGINX_VAR) - 1)
            : (ident_len = task->ident.len))
        + sizeof(" [") - 1
        + p - pid
        + sizeof("]: ") - 1;

    task->header.len = ngx_min(NGX_SYSLOG_HEADER_LEN, len);
    ident_len -= ngx_max((ngx_int_t) (len - task->header.len), 0);

    ngx_snprintf(task->header.data,
                 task->header.len,
                 " %V %*s[%*s]: ",
                 &ngx_syslog_hostname,
                 ident_len,
                 (task->ident.len == 0 ? appname : task->ident.data),
                 p - pid,
                 pid);
}
static u_char *
ngx_stream_log_error(ngx_log_t *log, u_char *buf, size_t len)
{
    u_char                *p;
    ngx_stream_session_t  *s;

    if (log->action) {
        p = ngx_snprintf(buf, len, " while %s", log->action);
        len -= p - buf;
        buf = p;
    }

    s = log->data;

    p = ngx_snprintf(buf, len, ", client: %V, server: %V",
                     &s->connection->addr_text,
                     &s->connection->listening->addr_text);
    len -= p - buf;
    buf = p;

    if (s->log_handler) {
        p = s->log_handler(log, buf, len);
    }

    return p;
}
static u_char *
ngx_tcp2http_log_error(ngx_log_t *log, u_char *buf, size_t len)
{
    u_char              *p;
    ngx_http_request_t  *r;
    ngx_http_log_ctx_t  *ctx;

    if (log->action) {
        p = ngx_snprintf(buf, len, " while %s", log->action);
        len -= p - buf;
        buf = p;
    }

    ctx = log->data;

    p = ngx_snprintf(buf, len, ", client: %V", &ctx->connection->addr_text);
    len -= p - buf;

    r = ctx->request;

    if (r) {
        return r->log_handler(r, ctx->current_request, p, len);

    } else {
        p = ngx_snprintf(p, len, ", server: %V",
                         &ctx->connection->listening->addr_text);
    }

    return p;
}
Example #15
0
static u_char *
ngx_stream_proxy_log_error(ngx_log_t *log, u_char *buf, size_t len)
{
    u_char                 *p;
    ngx_connection_t       *pc;
    ngx_stream_session_t   *s;
    ngx_stream_upstream_t  *u;

    s = log->data;

    u = s->upstream;

    p = buf;

    if (u->peer.name) {
        p = ngx_snprintf(p, len, ", upstream: \"%V\"", u->peer.name);
        len -= p - buf;
    }

    pc = u->peer.connection;

    p = ngx_snprintf(p, len,
                     ", bytes from/to client:%O/%O"
                     ", bytes from/to upstream:%O/%O",
                     s->received, s->connection->sent,
                     u->received, pc ? pc->sent : 0);

    return p;
}
void ngx_http_monitor_send(ngx_http_monitor_sender_t* sender, ngx_http_request_t *r) {
    ngx_http_monitor_str_t* str = (ngx_http_monitor_str_t*)malloc(sizeof(ngx_http_monitor_str_t));
    if(str == NULL) {
        ngx_log_error(NGX_LOG_ERR,r->connection->log,0,"Failed to malloc for buf in ngx_http_monitor_send!");
        return;
    }

    struct Message msg = MESSAGE_INTIALIZER;

    if(r->upstream != NULL) {
        msg.totalLen = msg.HEADER_LENGTH + sizeof(NGX_HTTP_MONITOR_FORMATER_US) +
                       r->headers_in.server.len - 2 + r->unparsed_uri.len - 2 + r->upstream->peer.name->len - 2 + 3 - 3 -1;
    } else {
        msg.totalLen = msg.HEADER_LENGTH + sizeof(NGX_HTTP_MONITOR_FORMATER) +
                       r->headers_in.server.len - 2 + r->unparsed_uri.len - 2 + 3 - 3 -1;
    }

    struct hostent *he = gethostbyname((const char*)ngx_cycle->hostname.data);
//  struct hostent *he = gethostbyname((const char*)r->headers_in.host->value.data);

    struct in_addr *addr = (struct in_addr*)he->h_addr;

    msg.warnlevel = 1;
    msg.ip = revertByteL(ntohl(addr->s_addr));
    msg.timestamp = (int64_t) ngx_time();

    toByteWithoutCopyBody(&msg, str);

    if (str->buf == NULL) {
        ngx_log_error(NGX_LOG_ERR,r->connection->log,0,"Failed to malloc for buf in ngx_http_monitor_send!");

        free(str);
        return;
    }

    // %V                        ngx_str_t *
    // %[0][width][u][x|X]d      int/u_int
    if (r->upstream == NULL) {
        ngx_snprintf(str->buf + msg.HEADER_LENGTH, str->buflen - msg.HEADER_LENGTH, NGX_HTTP_MONITOR_FORMATER,
                     &r->headers_in.server, &r->unparsed_uri, r->headers_out.status);
    } else {
        ngx_snprintf(str->buf + msg.HEADER_LENGTH, str->buflen - msg.HEADER_LENGTH, NGX_HTTP_MONITOR_FORMATER_US,
                     &r->headers_in.server, &r->unparsed_uri, r->upstream->peer.name, r->headers_out.status);
    }

    // no need
    // str->buf[str->buflen -1] = '\0';

    if (sender->sendthr == NULL) {
        ngx_http_monitor_send_startloop(sender, r);
    }

    ngx_http_monitor_push_queue(sender->queue, str);
}
static u_char *
ngx_http_lua_log_timer_error(ngx_log_t *log, u_char *buf, size_t len)
{
    u_char              *p;

    if (log->action) {
        p = ngx_snprintf(buf, len, " while %s", log->action);
        len -= p - buf;
        buf = p;
    }

    return ngx_snprintf(buf, len, ", context: ngx.timer");
}
static ngx_int_t
ngx_lua_regex_compile(ngx_lua_regex_compile_t *rc)
{
    int           n, erroff;
    char         *p;
    const char   *errstr;
    pcre         *re;
    ngx_pool_t   *old_pool;

    old_pool = ngx_http_lua_pcre_malloc_init(rc->pool);

    re = pcre_compile((const char *) rc->pattern.data, (int) rc->options,
                      &errstr, &erroff, NULL);

    ngx_http_lua_pcre_malloc_done(old_pool);

    if (re == NULL) {
        if ((size_t) erroff == rc->pattern.len) {
           rc->err.len = ngx_snprintf(rc->err.data, rc->err.len,
                              "pcre_compile() failed: %s in \"%V\"",
                               errstr, &rc->pattern)
                      - rc->err.data;

        } else {
           rc->err.len = ngx_snprintf(rc->err.data, rc->err.len,
                              "pcre_compile() failed: %s in \"%V\" at \"%s\"",
                               errstr, &rc->pattern, rc->pattern.data + erroff)
                      - rc->err.data;
        }

        return NGX_ERROR;
    }

    rc->regex = re;

#if 1
    n = pcre_fullinfo(re, NULL, PCRE_INFO_CAPTURECOUNT, &rc->captures);
    if (n < 0) {
        p = "pcre_fullinfo(\"%V\", PCRE_INFO_CAPTURECOUNT) failed: %d";
        goto failed;
    }
#endif

    return NGX_OK;

failed:

    rc->err.len = ngx_snprintf(rc->err.data, rc->err.len, p, &rc->pattern, n)
                  - rc->err.data;
    return NGX_OK;
}
static void
ngx_rtmp_stat_dump_pool(ngx_http_request_t *r, ngx_chain_t ***lll, 
        ngx_pool_t *pool)
{
    ngx_uint_t  nlarge, size;
    u_char      buf[NGX_INT_T_LEN];

    size = 0;
    nlarge = 0;
    ngx_rtmp_stat_get_pool_size(pool, &nlarge, &size);
    NGX_RTMP_STAT_L("<pool><nlarge>");
    NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf), "%ui", nlarge) - buf);
    NGX_RTMP_STAT_L("</nlarge><size>");
    NGX_RTMP_STAT(buf, ngx_snprintf(buf, sizeof(buf), "%ui", size) - buf);
    NGX_RTMP_STAT_L("</size></pool>\r\n");
}
static ngx_int_t
zupstream_upstream_create_request(ngx_http_request_t *r)
{
    static ngx_str_t backendQueryLine =
        ngx_string("GET /%V HTTP/1.1\r\nHost: www.iciba.com\r\nConnection: close\r\n\r\n");
    ngx_int_t queryLineLen = backendQueryLine.len + r->args.len - 2;

    ngx_buf_t* b = ngx_create_temp_buf(r->pool, queryLineLen);
    if (b == NULL)
        return NGX_ERROR;

    b->last = b->pos + queryLineLen;

    ngx_snprintf(b->pos, queryLineLen ,
                 (char*)backendQueryLine.data, &r->args);

    r->upstream->request_bufs = ngx_alloc_chain_link(r->pool);
    if (r->upstream->request_bufs == NULL)
        return NGX_ERROR;

    r->upstream->request_bufs->buf = b;
    r->upstream->request_bufs->next = NULL;

    r->upstream->request_sent = 0;
    r->upstream->header_sent = 0;

    r->header_hash = 1;
    return NGX_OK;
}
Example #21
0
/*
 * Because 'remote_user' is assumed to be provided by basic authorization
 * (see ngx_http_variable_remote_user) we are forced to create bogus
 * non-Negotiate authorization header. This may possibly clobber Negotiate
 * token too soon.
 */
    ngx_int_t
ngx_http_auth_spnego_set_bogus_authorization(
        ngx_http_request_t *r)
{
    const char *bogus_passwd = "bogus_auth_gss_passwd";
    ngx_str_t plain, encoded, final;

    if (r->headers_in.user.len == 0) {
        spnego_debug0("ngx_http_auth_spnego_set_bogus_authorization: no user NGX_DECLINED");
        return NGX_DECLINED;
    }

    /* +1 because of the ":" in "user:password" */
    plain.len = r->headers_in.user.len + ngx_strlen(bogus_passwd) + 1;
    plain.data = ngx_pnalloc(r->pool, plain.len);
    if (NULL == plain.data) {
        return NGX_ERROR;
    }

    ngx_snprintf(plain.data, plain.len, "%V:%s",
            &r->headers_in.user, bogus_passwd);

    encoded.len = ngx_base64_encoded_length(plain.len);
    encoded.data = ngx_pnalloc(r->pool, encoded.len);
    if (NULL == encoded.data) {
        return NGX_ERROR;
    }

    ngx_encode_base64(&encoded, &plain);

    final.len = sizeof("Basic ") + encoded.len - 1;
Example #22
0
subscriber_t *http_multipart_subscriber_create(ngx_http_request_t *r, nchan_msg_id_t *msg_id) {
  subscriber_t         *sub = longpoll_subscriber_create(r, msg_id);
  full_subscriber_t    *fsub = (full_subscriber_t *)sub;
  multipart_privdata_t *multipart_data;
  nchan_request_ctx_t  *ctx = ngx_http_get_module_ctx(fsub->sub.request, nchan_module);
  
  if(multipart_fn == NULL) {
    multipart_fn = &multipart_fn_data;
    *multipart_fn = *sub->fn;
    multipart_fn->enqueue = multipart_enqueue;
    multipart_fn->respond_message = multipart_respond_message;
    multipart_fn->respond_status = multipart_respond_status;
  }
  
  fsub->data.shook_hands = 0;
  
  fsub->privdata = ngx_palloc(sub->request->pool, sizeof(multipart_privdata_t));
  multipart_data = (multipart_privdata_t *)fsub->privdata;
  multipart_data->boundary_end = ngx_snprintf(multipart_data->boundary, 50, "\r\n--%V", nchan_request_multipart_boundary(fsub->sub.request, ctx));
  
  //header bufs -- unique per response
  ctx->output_str_queue = ngx_palloc(r->pool, sizeof(*ctx->output_str_queue));
  nchan_reuse_queue_init(ctx->output_str_queue, offsetof(headerbuf_t, prev), offsetof(headerbuf_t, next), headerbuf_alloc, NULL, sub->request->pool);
  
  ctx->bcp = ngx_palloc(r->pool, sizeof(nchan_bufchain_pool_t));
  nchan_bufchain_pool_init(ctx->bcp, r->pool);
  
  nchan_subscriber_common_setup(sub, HTTP_MULTIPART, &sub_name, multipart_fn, 0);
  return sub;
}
static int ngx_copy_header_in_to_response(ngx_http_request_t *r, u_char* buff, size_t size)
{
    ngx_list_part_t *part = &r->headers_in.headers.part;
    ngx_table_elt_t *header = part->elts;
    //ngx_table_elt_t* content_length = headers_in->content_length;

    size_t i = 0;
    size_t len = 0;
    for(i=0; len < size; i++)
    {
        //char temp[1024]= {0};
        if(i >= part->nelts)
        {
            if (part->next == NULL)
            {
                break;
            }
            part = part->next;
            header = part->elts;
            i = 0;
        }

        if(header[i].hash == 0)
        {
            continue;
        }
        //ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "conf param enbale: on\n");
        ngx_snprintf(buff+len, size-len-1, "%V:%V\n", &header[i].key, &header[i].value);
        len += header[i].key.len + header[i].value.len + 2;
    }
    return size;
}
static ngx_int_t
mytest_upstream_create_request(ngx_http_request_t *r){
	static ngx_str_t backendQueryLine =
			ngx_string("GET HTTP/1.1\r\nHost: www.mmkuaipai.com\r\nConnection: close\r\n\r\n");

	//ngx_string("GET /search?q=%V HTTP/1.1\r\nHost: www.baidu.com\r\nConnection: close\r\n\r\n");

	ngx_int_t queryLineLen = backendQueryLine.len + r->args.len - 2;
	/* 必须在内存池中申请内存,这有以下两点好处,一个好处是,在网络情况不佳的情况下,向上游服务器发送请求时
	 * 可能需要epoll多次调度send才能发送完成,这时必须保证这段内存不会被释放;另一个各好处是,在请求结束
	 * 时,这段内存会被自动释放,降低内存泄露的可能*/
	ngx_buf_t *b = ngx_create_temp_buf(r->pool,queryLineLen);
	if(b == NULL) return NGX_ERROR;
	b->last = b->pos + queryLineLen;
	ngx_snprintf(b->pos,queryLineLen,(char*)backendQueryLine.data,&r->args);
	//request_bufs是一个ngx_chain_t结构,它包含着要发送给上游服务器的请求
	r->upstream->request_bufs = ngx_alloc_chain_link(r->pool);
	if(r->upstream->request_bufs == NULL)
		return NGX_ERROR;

	r->upstream->request_bufs->buf = b;
	r->upstream->request_bufs->next = NULL;

	r->upstream->request_sent = 0;
	r->upstream->header_sent = 0;
	r->header_hash = 1;
	return NGX_OK;
}
Example #25
0
static void
ngx_tcl_set_error_code(Tcl_Interp *interp, int rc)
{
    char rcstr[10];
    ngx_snprintf((u_char*)rcstr, sizeof(rcstr), "%i", rc);
    Tcl_SetErrorCode(interp, "NGX", rcstr, NULL);
}
ngx_chain_t *
ngx_live_relay_static_state(ngx_http_request_t *r)
{
    ngx_live_relay_static_main_conf_t  *rsmcf;
    ngx_chain_t                        *cl;
    ngx_buf_t                          *b;
    size_t                              len;

    rsmcf = ngx_rtmp_cycle_get_module_main_conf(ngx_cycle,
                                                ngx_live_relay_static_module);

    len = sizeof("##########rtmp live relay static##########\n") - 1
        + sizeof("relay_static alloc frame: \n") - 1 + NGX_OFF_T_LEN
        + sizeof("relay_static free frame: \n") - 1 + NGX_OFF_T_LEN;

    cl = ngx_alloc_chain_link(r->pool);
    if (cl == NULL) {
        return NULL;
    }
    cl->next = NULL;

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

    b->last = ngx_snprintf(b->last, len,
            "##########rtmp live relay static##########\n"
            "relay_static alloc frame: %ui\n"
            "relay_static free frame: %ui\n",
            rsmcf->nalloc, rsmcf->nfree);

    return cl;
}
static u_char *
ngx_stream_app_log_error(ngx_log_t *log, u_char *buf, size_t len)
{
    u_char                 *p;
//    ngx_connection_t       *pc;
    ngx_stream_session_t   *s;
//    ngx_stream_upstream_t  *u;

    s = log->data;

//    u = s->upstream;

    p = buf;

//    if (u->peer.name) {
//        p = ngx_snprintf(p, len, ", app: \"%V\"", u->peer.name);
//        len -= p - buf;
//    }

//   pc = s->connection;
//	if(pc->buffer){
//		p = ngx_snprintf(p, len, ", revd: \"%V\"", pc->buffer->start);
//		len -= p - buf;
//	}

    p = ngx_snprintf(p, len,
                     ", bytes from/to client:%O/%O",
//                     ", bytes from/to upstream:%O/%O",
                     s->received, s->connection->sent);
//                     u->received, pc ? pc->sent : 0);

    return p;
}
ngx_int_t nchan_set_msgid_http_response_headers(ngx_http_request_t *r, nchan_request_ctx_t *ctx, nchan_msg_id_t *msgid) {
    ngx_str_t                 *etag, *tmp_etag;
    nchan_loc_conf_t          *cf = ngx_http_get_module_loc_conf(r, ngx_nchan_module);
    int8_t                     output_etag = 1, cross_origin;

    if(!ctx) {
        ctx = ngx_http_get_module_ctx(r, ngx_nchan_module);
    }
    cross_origin = ctx && ctx->request_origin_header.data;

    if(!cf->msg_in_etag_only) {
        //last-modified
        if(msgid->time > 0) {
            r->headers_out.last_modified_time = msgid->time;
        }
        else {
            output_etag = 0;
        }
        tmp_etag = msgtag_to_str(msgid);
    }
    else {
        tmp_etag = msgid_to_str(msgid);
    }

    if((etag = ngx_palloc(r->pool, sizeof(*etag) + tmp_etag->len))==NULL) {
        return NGX_ERROR;
    }
    etag->data = (u_char *)(etag+1);
    etag->len = tmp_etag->len;
    ngx_memcpy(etag->data, tmp_etag->data, tmp_etag->len);

    if(cf->custom_msgtag_header.len == 0) {
        if(output_etag) {
            nchan_add_response_header(r, &NCHAN_HEADER_ETAG, etag);
        }
        if(cross_origin) {
            nchan_add_response_header(r, &NCHAN_HEADER_ACCESS_CONTROL_EXPOSE_HEADERS, &NCHAN_MSG_RESPONSE_ALLOWED_HEADERS);
        }
    }
    else {
        if(output_etag) {
            nchan_add_response_header(r, &cf->custom_msgtag_header, etag);
        }
        if(cross_origin) {
            u_char        *cur = ngx_palloc(r->pool, 255);
            if(cur == NULL) {
                return NGX_ERROR;
            }
            ngx_str_t      allowed;
            allowed.data = cur;
            cur = ngx_snprintf(cur, 255, NCHAN_MSG_RESPONSE_ALLOWED_CUSTOM_ETAG_HEADERS_STRF, &cf->custom_msgtag_header);
            allowed.len = cur - allowed.data;
            nchan_add_response_header(r, &NCHAN_HEADER_ACCESS_CONTROL_EXPOSE_HEADERS, &allowed);
        }
    }

    //Vary header needed for proper HTTP caching.
    nchan_add_response_header(r, &NCHAN_HEADER_VARY, &NCHAN_VARY_HEADER_VALUE);
    return NGX_OK;
}
Example #29
0
void ngx_cdecl
ngx_conf_log_error(ngx_uint_t level, ngx_conf_t *cf, ngx_err_t err,
    char *fmt, ...)
{
    u_char   errstr[NGX_MAX_CONF_ERRSTR], *buf, *last;
    va_list  args;

    last = errstr + NGX_MAX_CONF_ERRSTR;

    va_start(args, fmt);
    buf = ngx_vsnprintf(errstr, last - errstr, fmt, args);
    va_end(args);

    *buf = '\0';

    if (err) {
        buf = ngx_snprintf(buf, last - buf - 1, " (%d: ", err);
        buf = ngx_strerror_r(err, buf, last - buf - 1);
        *buf++ = ')';
        *buf = '\0';
    }

    if (cf->conf_file == NULL) {
        ngx_log_error(level, cf->log, 0, "%s", errstr);
        return;
    }

    ngx_log_error(level, cf->log, 0, "%s in %s:%ui",
                  errstr, cf->conf_file->file.name.data, cf->conf_file->line);
}
int
ngx_stream_lua_ffi_exit(ngx_stream_session_t *s, int status, u_char *err,
    size_t *errlen)
{
    ngx_stream_lua_ctx_t       *ctx;

    ctx = ngx_stream_get_module_ctx(s, ngx_stream_lua_module);
    if (ctx == NULL) {
        *errlen = ngx_snprintf(err, *errlen, "no session ctx found") - err;
        return NGX_ERROR;
    }

    if (ngx_stream_lua_ffi_check_context(ctx, NGX_STREAM_LUA_CONTEXT_CONTENT
                                         | NGX_STREAM_LUA_CONTEXT_TIMER,
                                         err, errlen)
        != NGX_OK)
    {
        return NGX_ERROR;
    }

    ctx->exit_code = status;
    ctx->exited = 1;

    ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0,
                   "stream lua exit with code %i", ctx->exit_code);

    return NGX_OK;
}