httpSession.c 7.3 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
slguan 已提交
16 17
#define _DEFAULT_SOURCE
#include "os.h"
S
Shengliang Guan 已提交
18
#include "hash.h"
H
hzcheng 已提交
19 20 21
#include "taos.h"
#include "ttime.h"
#include "ttimer.h"
S
slguan 已提交
22
#include "http.h"
S
slguan 已提交
23
#include "httpLog.h"
S
slguan 已提交
24 25 26 27
#include "httpCode.h"
#include "httpHandle.h"
#include "httpResp.h"

H
hzcheng 已提交
28
void httpAccessSession(HttpContext *pContext) {
S
slguan 已提交
29 30 31 32 33 34
  HttpServer *server = pContext->pThread->pServer;
  pthread_mutex_lock(&server->serverMutex);
  if (pContext->session == pContext->session->signature) {
    pContext->session->expire = (int) taosGetTimestampSec() + pContext->pThread->pServer->sessionExpire;
  }
  pthread_mutex_unlock(&server->serverMutex);
H
hzcheng 已提交
35 36 37 38 39 40 41
}

void httpCreateSession(HttpContext *pContext, void *taos) {
  HttpServer *server = pContext->pThread->pServer;
  pthread_mutex_lock(&server->serverMutex);

  if (pContext->session != NULL && pContext->session == pContext->session->signature) {
S
slguan 已提交
42 43
    httpTrace("context:%p, fd:%d, ip:%s, user:%s, set exist session:%p:%p expired", pContext, pContext->fd,
              pContext->ipstr, pContext->user, pContext->session, pContext->session->taos);
H
hzcheng 已提交
44 45 46 47 48 49 50 51
    pContext->session->expire = 0;
    pContext->session->access--;
  }

  HttpSession session;
  session.taos = taos;
  session.expire = (int)taosGetTimestampSec() + server->sessionExpire;
  session.access = 1;
S
Shengliang Guan 已提交
52 53 54 55 56
  int sessionIdLen = snprintf(session.id, HTTP_SESSION_ID_LEN, "%s.%s", pContext->user, pContext->pass);

  taosHashPut(server->pSessionHash, session.id, sessionIdLen, (char *)(&session), sizeof(HttpSession));
  pContext->session = taosHashGet(server->pSessionHash, session.id, sessionIdLen);

H
hzcheng 已提交
57 58 59 60
  if (pContext->session == NULL) {
    httpError("context:%p, fd:%d, ip:%s, user:%s, error:%s", pContext, pContext->fd, pContext->ipstr, pContext->user,
              httpMsg[HTTP_SESSION_FULL]);
    taos_close(taos);
S
slguan 已提交
61 62
    pthread_mutex_unlock(&server->serverMutex);
    return;
H
hzcheng 已提交
63 64 65
  }

  pContext->session->signature = pContext->session;
S
slguan 已提交
66 67
  httpTrace("context:%p, fd:%d, ip:%s, user:%s, create a new session:%p:%p", pContext, pContext->fd, pContext->ipstr,
            pContext->user, pContext->session, pContext->session->taos);
H
hzcheng 已提交
68 69 70
  pthread_mutex_unlock(&server->serverMutex);
}

S
slguan 已提交
71
void httpFetchSessionImp(HttpContext *pContext) {
H
hzcheng 已提交
72 73 74
  HttpServer *server = pContext->pThread->pServer;
  pthread_mutex_lock(&server->serverMutex);

S
slguan 已提交
75
  char sessionId[HTTP_SESSION_ID_LEN];
S
Shengliang Guan 已提交
76
  int sessonIdLen = snprintf(sessionId, HTTP_SESSION_ID_LEN, "%s.%s", pContext->user, pContext->pass);
S
slguan 已提交
77

S
Shengliang Guan 已提交
78
  pContext->session = taosHashGet(server->pSessionHash, sessionId, sessonIdLen);
H
hzcheng 已提交
79 80
  if (pContext->session != NULL && pContext->session == pContext->session->signature) {
    pContext->session->access++;
S
slguan 已提交
81 82
    httpTrace("context:%p, fd:%d, ip:%s, user:%s, find an exist session:%p:%p, access:%d, expire:%d",
              pContext, pContext->fd, pContext->ipstr, pContext->user, pContext->session,
H
hzcheng 已提交
83 84 85 86 87 88 89 90 91 92
              pContext->session->taos, pContext->session->access, pContext->session->expire);
    pContext->session->expire = (int)taosGetTimestampSec() + server->sessionExpire;
  } else {
    httpTrace("context:%p, fd:%d, ip:%s, user:%s, session not found", pContext, pContext->fd, pContext->ipstr,
              pContext->user);
  }

  pthread_mutex_unlock(&server->serverMutex);
}

S
slguan 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106
void httpFetchSession(HttpContext *pContext) {
  if (pContext->session == NULL) {
    httpFetchSessionImp(pContext);
  } else {
    char sessionId[HTTP_SESSION_ID_LEN];
    snprintf(sessionId, HTTP_SESSION_ID_LEN, "%s.%s", pContext->user, pContext->pass);
    if (strcmp(pContext->session->id, sessionId) != 0) {
      httpError("context:%p, fd:%d, ip:%s, user:%s, password may be changed", pContext, pContext->fd, pContext->ipstr, pContext->user);
      httpRestoreSession(pContext);
      httpFetchSessionImp(pContext);
    }
  }
}

H
hzcheng 已提交
107 108 109
void httpRestoreSession(HttpContext *pContext) {
  HttpServer * server = pContext->pThread->pServer;

S
slguan 已提交
110
  // all access to the session is via serverMutex
H
hzcheng 已提交
111
  pthread_mutex_lock(&server->serverMutex);
S
slguan 已提交
112 113 114 115 116
  HttpSession *session = pContext->session;
  if (session == NULL || session != session->signature) {
    pthread_mutex_unlock(&server->serverMutex);
    return;
  }
H
hzcheng 已提交
117
  session->access--;
S
slguan 已提交
118 119
  httpTrace("context:%p, ip:%s, user:%s, restore session:%p:%p, access:%d, expire:%d",
            pContext, pContext->ipstr, pContext->user, session, session->taos,
H
hzcheng 已提交
120
            session->access, pContext->session->expire);
S
slguan 已提交
121
  pContext->session = NULL;
H
hzcheng 已提交
122 123 124
  pthread_mutex_unlock(&server->serverMutex);
}

S
Shengliang Guan 已提交
125
void httpResetSession(HttpSession *pSession) {
S
slguan 已提交
126
  httpTrace("close session:%p:%p", pSession, pSession->taos);
H
hzcheng 已提交
127 128 129 130 131 132 133 134
  if (pSession->taos != NULL) {
    taos_close(pSession->taos);
    pSession->taos = NULL;
  }
  pSession->signature = NULL;
}

void httpRemoveAllSessions(HttpServer *pServer) {
S
Shengliang Guan 已提交
135 136 137 138 139 140
  SHashMutableIterator *pIter = taosHashCreateIter(pServer->pSessionHash);

  while (taosHashIterNext(pIter)) {
    HttpSession *pSession = taosHashIterGet(pIter);
    if (pSession == NULL) continue;
    httpResetSession(pSession);
H
hzcheng 已提交
141
  }
S
Shengliang Guan 已提交
142 143

  taosHashDestroyIter(pIter);
H
hzcheng 已提交
144 145 146 147
}

bool httpInitAllSessions(HttpServer *pServer) {
  if (pServer->pSessionHash == NULL) {
S
Shengliang Guan 已提交
148
    pServer->pSessionHash = taosHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true);
H
hzcheng 已提交
149 150 151 152 153 154 155 156 157 158 159 160
  }
  if (pServer->pSessionHash == NULL) {
    httpError("http init session pool failed");
    return false;
  }
  if (pServer->expireTimer == NULL) {
    taosTmrReset(httpProcessSessionExpire, 50000, pServer, pServer->timerHandle, &pServer->expireTimer);
  }

  return true;
}

S
Shengliang Guan 已提交
161 162
bool httpSessionExpired(HttpSession *pSession) {
  time_t cur = taosGetTimestampSec();
H
hzcheng 已提交
163 164 165

  if (pSession->taos != NULL) {
    if (pSession->expire > cur) {
S
Shengliang Guan 已提交
166
      return false;  // un-expired, so return false
H
hzcheng 已提交
167 168
    }
    if (pSession->access > 0) {
S
slguan 已提交
169
      httpTrace("session:%p:%p is expired, but still access:%d", pSession, pSession->taos,
H
hzcheng 已提交
170
                pSession->access);
S
Shengliang Guan 已提交
171
      return false;  // still used, so return false
H
hzcheng 已提交
172
    }
S
slguan 已提交
173 174
    httpTrace("need close session:%p:%p for it expired, cur:%d, expire:%d, invertal:%d",
              pSession, pSession->taos, cur, pSession->expire, cur - pSession->expire);
H
hzcheng 已提交
175 176
  }

S
Shengliang Guan 已提交
177
  return true;
H
hzcheng 已提交
178 179
}

S
Shengliang Guan 已提交
180 181
void httpRemoveExpireSessions(HttpServer *pServer) {  
  SHashMutableIterator *pIter = taosHashCreateIter(pServer->pSessionHash);
H
hzcheng 已提交
182

S
Shengliang Guan 已提交
183 184 185
  while (taosHashIterNext(pIter)) {
    HttpSession *pSession = taosHashIterGet(pIter);
    if (pSession == NULL) continue;
H
hzcheng 已提交
186

S
Shengliang Guan 已提交
187 188 189 190 191
    pthread_mutex_lock(&pServer->serverMutex);
    if (httpSessionExpired(pSession)) {
      httpResetSession(pSession);
      taosHashRemove(pServer->pSessionHash, pSession->id, strlen(pSession->id));
    }
H
hzcheng 已提交
192
    pthread_mutex_unlock(&pServer->serverMutex);
S
Shengliang Guan 已提交
193
  }
H
hzcheng 已提交
194

S
Shengliang Guan 已提交
195
  taosHashDestroyIter(pIter);
H
hzcheng 已提交
196 197 198 199 200 201 202
}

void httpProcessSessionExpire(void *handle, void *tmrId) {
  HttpServer *pServer = (HttpServer *)handle;
  httpRemoveExpireSessions(pServer);
  taosTmrReset(httpProcessSessionExpire, 60000, pServer, pServer->timerHandle, &pServer->expireTimer);
}