⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 session_sdbm.c

📁 开源的OpenId的一个java实现
💻 C
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */#include <stdio.h>#include <stdlib.h>#include <time.h>#include <string.h>#include "apr_general.h"#include "apr_sdbm.h"#include "apr_dbm.h"#include "httpd.h"#include "mod_cspace.h"#include "session.h"/*#define SUCC 1#define FAIL !SUCC */#define CSPACE_SDBM_FILE "/tmp/cspace_sessions"#define CSPACE_SDBM_OPEN(sdbm, file, pool) \            apr_sdbm_open((sdbm), \                (file)?(file):CSPACE_SDBM_FILE, \                APR_WRITE | APR_CREATE | APR_SHARELOCK, \                APR_OS_DEFAULT, (pool))#define CSPACE_SDBM_CLOSE(sdbm) apr_sdbm_close(sdbm)const char* ctable = "1234567890QWERTYUIOPASDFGHJKLZXCVBNM"                     "qwertyuiopasdfghjklzxcvbnm";typedef struct {    /*apr_sdbm_t *sdbm;*/    const char *sdbm_file;    apr_pool_t *p;    long int expire;} session_ctx_t;void *session_ctx_create(apr_pool_t *p, const char *sdbm_file, long int expire){    session_ctx_t *ctx = apr_palloc(p, sizeof(session_ctx_t));        ctx->sdbm_file = sdbm_file;        ctx->p = p;        /*ctx->expire = DEF_SESS_EXPIRE_TIME;*/    if (expire)        ctx->expire = expire;        return (void *)ctx;}static void gen_id(char *id, int idlen){    int i = 0;    size_t clen = strlen(ctable);    size_t idx;    for (i=0; i<idlen; i++) {        idx = (clen * (rand() / (RAND_MAX + 1.0)));        *(id + i) = *(ctable + idx);    }    *(id + idlen) = '\0';}/* do we really need this function? what is the probability that this could * happen? */static int id_exist(const char *id, int idlen, session_ctx_t *ctx){    /*return (apr_hash_get(ctx->sessions, id, idlen)!=NULL);*/    return FAIL;    /*return apr_sdbm_exist(ctx->sdbm, id);*/}/*TODO:DONE session expiry should be taken from the configuration*/static int get_session_time(session_ctx_t *ctx, char *sess_id, long int *time){     char errbuf[256];    apr_status_t rv;    apr_sdbm_datum_t val;    apr_sdbm_datum_t key;    apr_sdbm_t *sdbm;    key.dptr = sess_id;    key.dsize = sizeof(sess_id);        if ((rv = CSPACE_SDBM_OPEN(&sdbm, ctx->sdbm_file, ctx->p)) != APR_SUCCESS)    {        apr_strerror(rv, errbuf, sizeof(errbuf));        printf("db creation failed! %s\n", errbuf);        /*TODO:log error*/        return FAIL;    }    if ((rv = apr_sdbm_fetch(sdbm, &val, key)) != APR_SUCCESS) {        apr_strerror(rv, errbuf, sizeof(errbuf));        printf("db store failed! %s\n", errbuf);        /*TODO:log error*/        return FAIL;    }        if (val.dptr) {        *time = *((long int *)(val.dptr));    } else {        printf("invalid session key requested");        return FAIL;    }        CSPACE_SDBM_CLOSE(sdbm);    return SUCC;}static int store_session_time(session_ctx_t *ctx, char *sess_id, time_t time){    char errbuf[256];    apr_status_t rv;    apr_sdbm_datum_t val;    apr_sdbm_datum_t key;    apr_sdbm_t *sdbm;    key.dptr = sess_id;    key.dsize = sizeof(sess_id);     val.dptr = (char *)(&time);    val.dsize = sizeof(long int);        if ((rv = CSPACE_SDBM_OPEN(&sdbm, ctx->sdbm_file, ctx->p)) != APR_SUCCESS)    {        apr_strerror(rv, errbuf, sizeof(errbuf));        printf("db creation failed! %s\n", errbuf);        /*TODO:log error*/        return FAIL;    }    if ((rv = apr_sdbm_store(sdbm, key, val, APR_SDBM_REPLACE))            != APR_SUCCESS) {        apr_strerror(rv, errbuf, sizeof(errbuf));        printf("db store failed! %s\n", errbuf);        /*TODO:log error*/        return FAIL;    }    CSPACE_SDBM_CLOSE(sdbm);    return SUCC;}/*void set_session_expire(void *session_ctx, long int expire){    ((session_ctx_t *)session_ctx)->expire = expire;}*/void set_session_expire_time(void *session_ctx, long int expire){    ((session_ctx_t *)session_ctx)->expire = expire;}int handle_session(char *id, int idlen, void *session_ctx){    /*struct timeval time;*/	time_t time_now;    long int time_cookie;    session_ctx_t *ctx = (session_ctx_t *) session_ctx;    if (time(&time_now) == (time_t)-1)        return FAIL;     /*time_now = time.tv_sec;*/    if (!get_session_time(ctx, (char *)id, &time_cookie))         return FAIL;    if (time_cookie)        printf("time cookie:%ld for sess id %s\n", time_cookie, id);    else         printf("time cookie:null for sessid %s\n", id);    printf("time now   :%ld\n", time_now);    if (time_cookie) {        if (time_now < time_cookie + ctx->expire) {            return SUCC;        }    }    return FAIL;}static int add_session(char* id, int idlen, session_ctx_t *ctx){    /*struct timeval time;*/    time_t time_now;    if (time(&time_now) == (time_t)-1)        return FAIL; /*what should we do here? return with error?*/        /*set time.tv_sec = 0?*/    printf("TIME: added time %ld for cookie %s\n", time_now, id);        if (!store_session_time(ctx, (char *)id, time_now))         return FAIL;    return SUCC;}int gen_session(int idlen, void *session_ctx, char **sess_id){    session_ctx_t *ctx = (session_ctx_t *) session_ctx;    time_t time_now;	/*struct timeval time;*/    if (time(&time_now) == (time_t)-1)        return FAIL;        srand((unsigned)time_now);        if (!((*sess_id) = (char *)(malloc(sizeof(char) * idlen + 1))))        return FAIL;    do {        gen_id(*sess_id, idlen);    } while (id_exist(*sess_id, idlen, ctx));        printf("generated session id:%s\n", *sess_id);        if (!add_session(*sess_id, idlen, ctx))        return FAIL;        return SUCC;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -