Server 0.0.1
HTTP/REST server implementation

src/http/response/session.c

Go to the documentation of this file.
00001 
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <stdio.h>
00026 #include <time.h>
00027 #include <sys/types.h>
00028 
00029 #include "class.h"
00030 
00031 #include "http/response.h"
00032 #include "http/message.h"
00033 #include "http/header.h"
00034 #include "session.h"
00035 
00036 #include "utils/memory.h"
00037 #include "hash.h"
00038 
00039 #define RESP_DATA "{\"id\":\"%lu\",\"timeout\":%d,\"timeleft\":%ld,\"username\":\"%s\"}"
00040 
00041 HttpResponse
00042 httpResponseSession(Session session)
00043 {
00044         char         buffer[200];
00045         HttpResponse response;
00046         HttpMessage  message;
00047         size_t       nbuf;
00048 
00049         response = new(HttpResponse, "HTTP/1.1", 200, "OK");
00050         message  = (HttpMessage)response;
00051 
00052         hashAdd(message->header,
00053                         new(HttpHeader, CSTRA("Content-Type"), CSTRA("application/json")));
00054 
00055         message->type  = HTTP_MESSAGE_BUFFERED;
00056 
00057         nbuf = sprintf(buffer, RESP_DATA,
00058                         (NULL != session)? session->id : 0,
00059                         (NULL != session)? SESSION_LIVETIME : 0,
00060                         (NULL != session)? session->livetime - time(NULL) : 0,
00061                         (NULL != session)? session->username : "");
00062 
00063         message->nbody = nbuf;
00064         message->body  = malloc(nbuf);
00065         memcpy(message->body, buffer, nbuf);
00066 
00067         return response;
00068 }
00069 
00070 // vim: set ts=4 sw=4:
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines