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
    pContext->session->expire = 0;
    pContext->session->access--;
  }

  HttpSession session;
H
Hui Li 已提交
49
  memset(&session, 0, sizeof(HttpSession));
H
hzcheng 已提交
50 51 52
  session.taos = taos;
  session.expire = (int)taosGetTimestampSec() + server->sessionExpire;
  session.access = 1;
S
Shengliang Guan 已提交
53 54 55 56 57
  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 已提交
58 59 60 61
  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 已提交
62 63
    pthread_mutex_unlock(&server->serverMutex);
    return;
H
hzcheng 已提交
64 65 66
  }

  pContext->session->signature = pContext->session;
S
slguan 已提交
67 68
  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 已提交
69 70 71
  pthread_mutex_unlock(&server->serverMutex);
}

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

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

S
Shengliang Guan 已提交
79
  pContext->session = taosHashGet(server->pSessionHash, sessionId, sessonIdLen);
H
hzcheng 已提交
80 81
  if (pContext->session != NULL && pContext->session == pContext->session->signature) {
    pContext->session->access++;
S
slguan 已提交
82 83
    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 已提交
84 85 86 87 88 89 90 91 92 93
              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 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107
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 已提交
108 109 110
void httpRestoreSession(HttpContext *pContext) {
  HttpServer * server = pContext->pThread->pServer;

S
slguan 已提交
111
  // all access to the session is via serverMutex
H
hzcheng 已提交
112
  pthread_mutex_lock(&server->serverMutex);
S
slguan 已提交
113 114 115 116 117
  HttpSession *session = pContext->session;
  if (session == NULL || session != session->signature) {
    pthread_mutex_unlock(&server->serverMutex);
    return;
  }
H
hzcheng 已提交
118
  session->access--;
S
slguan 已提交
119 120
  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 已提交
121
            session->access, pContext->session->expire);
S
slguan 已提交
122
  pContext->session = NULL;
H
hzcheng 已提交
123 124 125
  pthread_mutex_unlock(&server->serverMutex);
}

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

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

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

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

bool httpInitAllSessions(HttpServer *pServer) {
  if (pServer->pSessionHash == NULL) {
S
Shengliang Guan 已提交
149
    pServer->pSessionHash = taosHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true);
H
hzcheng 已提交
150 151 152 153 154 155 156 157 158 159 160 161
  }
  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 已提交
162 163
bool httpSessionExpired(HttpSession *pSession) {
  time_t cur = taosGetTimestampSec();
H
hzcheng 已提交
164 165 166

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

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

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

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

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

S
Shengliang Guan 已提交
196
  taosHashDestroyIter(pIter);
H
hzcheng 已提交
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);
H
Hui Li 已提交
203
}