taskrambler  v0.1.8
Web server and task management solution.
application/adapter/http/update.c
Go to the documentation of this file.
1 /**
2  * \file
3  *
4  * \author Georg Hopp
5  *
6  * \copyright
7  * Copyright © 2013 Georg Hopp
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #define _GNU_SOURCE
24 
25 #include <stdio.h>
26 #include <sys/types.h>
27 
30 #include "hash.h"
31 #include "http/worker.h"
32 #include "http/header.h"
33 #include "http/message.h"
34 #include "router.h"
35 
36 #define NO_SESSION_SID NULL
37 
38 static
39 inline
40 char *
41 getSessionId(Hash cookies)
42 {
43  HashValue sidstr = hashGet(cookies, CSTRA("sid"));
44 
45  if (NULL != sidstr) {
46  return (char*)sidstr->value;
47  }
48 
49  return NO_SESSION_SID;
50 }
51 
52 void
53 applicationAdapterHttpUpdate(void * _this, void * subject)
54 {
55  ApplicationAdapterHttp this = _this;
56  HttpWorker worker = (HttpWorker)subject;
57  Session session = NULL;
58  time_t now = time(NULL);
59 
60  char * sid;
61  char buf[1000];
62  size_t nbuf;
63 
64  applicationSessionCleanup(this->application, now);
65 
66  sid = getSessionId(worker->current_request->cookies);
67  session = applicationSessionGet(this->application, sid);
68 
69  if (NULL == session) {
70  session = applicationSessionStart(this->application);
71  }
72 
73  // send session cookie
74  nbuf = sprintf(buf, "sid=%s;Path=/", session->id);
75  queuePut(
76  worker->additional_headers,
77  new(HttpHeader, CSTRA("Set-Cookie"), buf, nbuf));
78 
79  worker->current_response = (HttpMessage)routerRoute(
80  this->router,
81  worker->current_request,
82  session);
83 }
84 
85 // vim: set ts=4 sw=4:
Session applicationSessionGet(Application, const char *)
Definition: session_get.c:36
static char * getSessionId(Hash cookies)
void applicationSessionCleanup(Application, time_t)
#define CSTRA(val)
Const STRing Argument.
Definition: memory.h:26
void applicationAdapterHttpUpdate(void *_this, void *subject)
void * hashGet(Hash, const char *, size_t)
Definition: hash/get.c:51
void queuePut(Queue, void *)
Definition: queue/put.c:27
Session applicationSessionStart(Application)
Definition: session_start.c:36
HttpResponse routerRoute(Router, HttpRequest, Session)
Definition: route.c:53
#define NO_SESSION_SID