Exemplo n.º 1
0
int main (int argc, char *argv[])
{
    time_t now;
    u_char http_buf[BUFSIZ];
    u_char cookie_buf[BUFSIZ];

    ngx_time_init();

    now = ngx_time();
    ngx_http_time(http_buf, now);
    ngx_http_cookie_time(cookie_buf, now);

    printf("current timestamp:%zu\n", now);
    printf("http   time      :%s\n", http_buf);
    printf("cookie time      :%s\n", cookie_buf);

    printf("\n\nWait 10sec...\n\n");

    sleep(10);
    ngx_time_update();
    now = ngx_time();
    ngx_http_time(http_buf, now);
    ngx_http_cookie_time(cookie_buf, now);
    printf("current timestamp:%zu\n", now);
    printf("http   time      :%s\n", http_buf);
    printf("cookie time      :%s\n", cookie_buf);
    
    return 0;
}
Exemplo n.º 2
0
void time_test001(void)
{
    time_t now;
    u_char http_buf[sizeof("Thu, 14 Aug 2014 23:02:53 GMT") - 1];
    u_char cookie_buf[BUFSIZ];

    // TODO: move setup().
    ngx_time_init();

    now = 1408024973;
    ngx_http_time(http_buf, now);
    ngx_http_cookie_time(cookie_buf, now);

    CU_ASSERT_NSTRING_EQUAL(
        "Thu, 14 Aug 2014 14:02:53 GMT",
        http_buf,
        sizeof("Thu, 14 Aug 2014 23:02:53 GMT") - 1
    );

    CU_ASSERT_NSTRING_EQUAL(
        "Thu, 14-Aug-14 14:02:53 GMT",
        cookie_buf,
        sizeof("Thu, 14-Aug-14 14:02:53 GMT") - 1
    );
}
Exemplo n.º 3
0
Arquivo: sample.c Projeto: qtkmz/dojo
static void sample_time()
{
	u_char *p;
	u_char buf[64];

	p = ngx_http_cookie_time(buf, time(NULL));

	puts("sample_time --------");
	printf("sizeof(time_t) = %d\n", sizeof(time_t));
	printf("ngx_time = %ld\n", ngx_time());
	printf("ngx_http_cookie_time = %.*s\n", p - buf, buf);

	return;
}
Exemplo n.º 4
0
static void
ngx_http_upstream_persistence_set_cookie(ngx_http_request_t *r, 
        ngx_uint_t current,
        ngx_http_upstream_persistence_group_t *group, 
        ngx_str_t *value)
{
    ngx_table_elt_t                 *set_cookie;
    u_char                          *cookie;
    u_char                          *key_value;
    u_char                          *tmp;
    size_t                          len = value->len + 50; 
    //50 is strlen("=655350") + strlen("; expires=Thu, 31-Dec-37 23:55:55 GMT") + 1;
    
    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 
            "http_cookie_refresh_peer\n");

    cookie = ngx_pcalloc(r->pool, len * sizeof (u_char));
    if (cookie == NULL) {
        return;
    }

    key_value = ngx_pcalloc(r->pool, value->len + 1);
    if (key_value == NULL) {
        return;
    }

    ngx_memcpy(key_value, value->data, value->len);

    tmp = ngx_sprintf(cookie, "%s=%d", key_value, current);
    tmp = ngx_cpymem(tmp, "; expires=", sizeof("; expires=") - 1);
    ngx_http_cookie_time(tmp, ngx_time() + group->timeout);

    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 
        "http_cookie_refresh_peer: set cookie %s", cookie);

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

    set_cookie->hash = 1;
    set_cookie->key.len = sizeof("Set-Cookie") - 1;
    set_cookie->key.data = (u_char *)"Set-Cookie";
    set_cookie->value.len = ngx_strlen(cookie);
    set_cookie->value.data = cookie;
}
static ngx_int_t
ngx_http_secure_cookie_set_expires_base64_variable(ngx_http_request_t *r,
    ngx_http_variable_value_t *v, uintptr_t data)
{
    u_char                         *last, t[32], *base64;
    time_t                          expires;
    ngx_str_t                       time, time_base64; 
    ngx_http_secure_cookie_conf_t  *sclcf;

    sclcf = ngx_http_get_module_loc_conf(r, ngx_http_secure_cookie_module);
    if (sclcf == NULL) {
        goto not_found;
    }

    base64 = ngx_palloc(r->pool, 64);
    if ( base64 == NULL) {
        goto not_found;
    }

    expires = ngx_time() + sclcf->expires;

    last = ngx_http_cookie_time(t, expires);

    time.data = t;
    time.len = last - t;

    time_base64.data = base64;
    time_base64.len = 64;

    ngx_encode_base64(&time_base64, &time);

    v->len = time_base64.len;
    v->valid = 1;
    v->no_cacheable = 0;
    v->not_found = 0;
    v->data = time_base64.data;

    return NGX_OK;

not_found:

    v->not_found = 1;

    return NGX_OK;
}
static mrb_value ngx_http_mruby_cookie_time(mrb_state *mrb, mrb_value self)
{
    mrb_value  mrb_time;
    time_t     time;
    u_char    *p;
    u_char     buf[sizeof("Mon, 04 Aug 2013 01:00:00 GMT") - 1];

    mrb_get_args(mrb, "o", &mrb_time);

    if (mrb_type(mrb_time) != MRB_TT_FIXNUM) {
        mrb_time = mrb_funcall(mrb, mrb_time, "to_i", 0, NULL);
    }

    time = mrb_fixnum(mrb_time);
    p    = buf;
    p    = ngx_http_cookie_time(p, time);

    return mrb_str_new(mrb, (char *)buf, p - buf);
}
Exemplo n.º 7
0
int
ngx_http_lua_ngx_cookie_time(lua_State *L)
{
    time_t                               t;
    u_char                              *p;

    u_char   buf[sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1];

    if (lua_gettop(L) != 1) {
        return luaL_error(L, "expecting one argument");
    }

    t = (time_t) luaL_checknumber(L, 1);

    p = buf;
    p = ngx_http_cookie_time(p, t);

    lua_pushlstring(L, (char *) buf, p - buf);

    return 1;
}
static ngx_int_t
ngx_http_secure_cookie_set_expires_variable(ngx_http_request_t *r,
    ngx_http_variable_value_t *v, uintptr_t data)
{
    u_char                         *last, *time;
    time_t                          expires;
    ngx_http_secure_cookie_conf_t  *sclcf;

    sclcf = ngx_http_get_module_loc_conf(r, ngx_http_secure_cookie_module);
    if (sclcf == NULL) {
        goto not_found;
    }

    time = ngx_palloc(r->pool, sizeof("Mon, 28 Sep 1970 06:00:00 GMT"));
    if (time == NULL) {
        goto not_found;
    }

    expires = ngx_time() + sclcf->expires;

    last = ngx_http_cookie_time(time, expires);

    v->len = last - time;
    v->valid = 1;
    v->no_cacheable = 0;
    v->not_found = 0;
    v->data = time;

    return NGX_OK;

not_found:

    v->not_found = 1;

    return NGX_OK;
}
static ngx_int_t
ngx_http_session_sticky_insert(ngx_http_request_t *r)
{
    u_char             *p;
    ngx_uint_t          i;
    ngx_list_part_t    *part;
    ngx_table_elt_t    *set_cookie, *table;
    ngx_http_ss_ctx_t  *ctx;

    ctx = ngx_http_get_module_ctx(r, ngx_http_upstream_session_sticky_module);
    if (ctx->frist != 1 && ctx->sscf->maxidle == NGX_CONF_UNSET) {
        return NGX_OK;
    }

    set_cookie = NULL;
    if (ctx->sscf->flag & NGX_HTTP_SESSION_STICKY_INDIRECT) {
        part = &r->headers_out.headers.part;
        while (part && set_cookie == NULL) {
            table = (ngx_table_elt_t *) part->elts;
            for (i = 0; i < part->nelts; i++) {
                if (table[i].key.len == (sizeof("set-cookie") - 1)
                    && ngx_strncasecmp(table[i].key.data,
                                       (u_char *) "set-cookie",
                                       table[i].key.len) == 0)
                {
                    p = ngx_strlcasestrn(table[i].value.data,
                                         table[i].value.data +
                                         table[i].value.len,
                                         ctx->sscf->cookie.data,
                                         ctx->sscf->cookie.len - 1);
                    if (p != NULL) {
                        set_cookie = &table[i];
                        break;
                    }
                }
            }
            part = part->next;
        }
    }

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

        set_cookie->hash = 1;
        ngx_str_set(&set_cookie->key, "Set-Cookie");
    }

    set_cookie->value.len = ctx->sscf->cookie.len
                          + sizeof("=") - 1
                          + ctx->sid.len
                          + sizeof("; Domain=") - 1
                          + ctx->sscf->domain.len
                          + sizeof("; Path=") - 1
                          + ctx->sscf->path.len;

    if (ctx->sscf->maxidle != NGX_CONF_UNSET) {
        set_cookie->value.len = set_cookie->value.len
                              + ctx->s_lastseen.len
                              + ctx->s_firstseen.len
                              + 2; /* '|' and '|' */
    } else {
        set_cookie->value.len = set_cookie->value.len
                              + sizeof("; Max-Age=") - 1
                              + ctx->sscf->maxage.len
                              + sizeof("; Expires=") - 1
                              + sizeof("Xxx, 00-Xxx-00 00:00:00 GMT") - 1;
    }

    p = ngx_pnalloc(r->pool, set_cookie->value.len);
    if (p == NULL) {
        return NGX_ERROR;
    }

    set_cookie->value.data = p;

    p = ngx_cpymem(p, ctx->sscf->cookie.data, ctx->sscf->cookie.len);
    *p++ = '=';
    p = ngx_cpymem(p, ctx->sid.data, ctx->sid.len);
    if (ctx->sscf->maxidle != NGX_CONF_UNSET) {
        *(p++) = NGX_HTTP_SESSION_STICKY_DELIMITER;
        p = ngx_cpymem(p, ctx->s_lastseen.data, ctx->s_lastseen.len);
        *(p++) = NGX_HTTP_SESSION_STICKY_DELIMITER;
        p = ngx_cpymem(p, ctx->s_firstseen.data, ctx->s_firstseen.len);
    }
    if (ctx->sscf->domain.len) {
        p = ngx_cpymem(p, "; Domain=", sizeof("; Domain=") - 1);
        p = ngx_cpymem(p, ctx->sscf->domain.data, ctx->sscf->domain.len);
    }
    if (ctx->sscf->path.len) {
        p = ngx_cpymem(p, "; Path=", sizeof("; Path=") - 1);
        p = ngx_cpymem(p, ctx->sscf->path.data, ctx->sscf->path.len);
    }
    if (ctx->sscf->maxidle == NGX_CONF_UNSET && ctx->sscf->maxage.len) {
        p = ngx_cpymem(p, "; Max-Age=", sizeof("; Max-Age=") - 1);
        p = ngx_cpymem(p, ctx->sscf->maxage.data, ctx->sscf->maxage.len);
        p = ngx_cpymem(p, "; Expires=", sizeof("; Expires=") - 1);
        ngx_uint_t maxage = ngx_atoi(ctx->sscf->maxage.data,
                                      ctx->sscf->maxage.len);
        p = ngx_http_cookie_time(p, ngx_time() + maxage);
    }

    set_cookie->value.len = p - set_cookie->value.data;

    return NGX_OK;
}