Server 0.0.1
HTTP/REST server implementation

src/http/worker/process.c

Go to the documentation of this file.
00001 
00023 #include <sys/types.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <time.h>
00027 #include <string.h>
00028 #include <sys/time.h>
00029 
00030 #include "class.h"
00031 #include "auth.h"
00032 
00033 #include "http/worker.h"
00034 #include "http/header.h"
00035 #include "http/message.h"
00036 #include "http/request.h"
00037 #include "http/response.h"
00038 #include "http/message/queue.h"
00039 #include "http/parser.h"
00040 #include "session.h"
00041 #include "stream.h"
00042 #include "hash.h"
00043 
00044 #include "utils/memory.h"
00045 #include "hash.h"
00046 #include "commons.h"
00047 
00048 
00049 HttpMessage httpWorkerGetAsset(HttpRequest, const char *, const char *, size_t);
00050 void        httpWorkerAddCommonHeader(HttpMessage, HttpMessage);
00051 
00052 
00053 ssize_t
00054 httpWorkerProcess(HttpWorker this, Stream st)
00055 {
00056         ssize_t size;
00057 
00058         if (0 < (size = httpParserParse(this->parser, st))) {
00059                 int              i;
00060                 HttpMessageQueue reqq  = this->parser->queue;
00061                 HttpMessageQueue respq = this->writer->queue;
00062 
00063                 for (i=0; i<reqq->nmsgs; i++) {
00064                         HttpMessage  rmessage = reqq->msgs[i];
00065                         HttpRequest  request  = (HttpRequest)(reqq->msgs[i]);
00066                         HttpMessage  response = NULL;
00067 
00073                         if (NULL == this->session) {
00074                                 HashValue sidstr = hashGet(request->cookies, CSTRA("sid"));
00075 
00076                                         if (NULL != sidstr) {
00077                                                 unsigned long sid;
00078 
00079                                         sid = strtoul((char*)(sidstr->value), NULL, 10);
00080                                                 this->session = sessionGet(this->sroot, sid);
00081                                 }
00082                         }
00083 
00084                         if (NULL != this->session) {
00085                                 if (time(NULL) < this->session->livetime) {
00086                                         this->session->livetime = time(NULL) + SESSION_LIVETIME;
00087                                 } else {
00088                                         sessionDelete(this->sroot, this->session->id);
00089                                         delete(this->session);
00090                                 }
00091                         }
00092 
00093                         if (0 == strcmp("POST", request->method)) {
00094                                 if (0 == strcmp("/login/", request->path)) {
00095                                         char   buffer[200];
00096                                         size_t nbuf;
00097 
00098                                         HashValue username = hashGet(request->post, CSTRA("username"));
00099                                         HashValue password = hashGet(request->post, CSTRA("password"));
00100 
00113                                         if (NULL == password || NULL == username) {
00114                                                 response = new(HttpResponse, "HTTP/1.1", 403, "Forbidden");
00115                                         }
00116 
00117                                         if (NULL == response) {
00118                                                 Credential cred = new(Credential,
00119                                                                 CRED_PASSWORD,
00120                                                                 (char*)(username->value), username->nvalue,
00121                                                                 (char*)(password->value), password->nvalue);
00122 
00123                                                 if (!authenticate(this->auth, cred)) {
00124                                                         response = new(HttpResponse, "HTTP/1.1", 403, "Forbidden");
00125                                                 } else {
00126                                                         if (NULL == this->session) {
00127                                         this->session = sessionAdd(
00128                                                         this->sroot,
00129                                                                                 new(Session,
00130                                                                                         username->value,
00131                                                                                         username->nvalue));
00132                                                         } else {
00133                                                                 this->session->username = malloc(username->nvalue + 1);
00134                                                                 this->session->username[username->nvalue] = 0;
00135                                                                 memcpy(this->session->username,
00136                                                                                 username->value,
00137                                                                                 username->nvalue);
00138                                                         }
00139 
00140                                                         nbuf = sprintf(buffer,
00141                                                                         "sid=%lu;Path=/",
00142                                                                         this->session->id);
00143 
00144                                                         response = (HttpMessage)httpResponseSession(
00145                                                                         this->session);
00146 
00147                                                         hashAdd(response->header,
00148                                                                         new(HttpHeader,
00149                                                                                 CSTRA("Set-Cookie"),
00150                                                                                 buffer, nbuf));
00151                                                 }
00152                                                 delete(cred);
00153                                         }
00154                                 }
00155                         }
00156 
00157                         if (0 == strcmp("GET", request->method)) {
00158 
00159                                 if (0 == strcmp("/", request->path)) {
00160                                         response = httpWorkerGetAsset(
00161                                                         request,
00162                                                         "./assets/html/main.html",
00163                                                         CSTRA("text/html"));
00164                                 }
00165 
00166                                 if (0 == strcmp("/sessinfo/", request->path)) {
00167                                         response = (HttpMessage)httpResponseSession(this->session);
00168                                 }
00169 
00170                                 if (0 == strcmp("/sess/", request->path)) {
00171                                         if (NULL == this->session) {
00172                                                 this->session = sessionAdd(
00173                                                                 this->sroot,
00174                                                                 new(Session, NULL, 0));
00175                                         }
00176                                         response = (HttpMessage)httpResponseSession(this->session);
00177                                 }
00178 
00179                                 if (0 == strcmp("/randval/", request->path)) {
00180                                         if (NULL != this->session) {
00181                                                 response = (HttpMessage)httpResponseRandval(
00182                                                                 this->val->timestamp,
00183                                                                 this->val->value);
00184                                         } else {
00185                                                 response = (HttpMessage)httpResponse403();
00186                                         }
00187                                 }
00188 
00189                                 if (0 == strcmp("/image/me", request->path)) {
00190                                         response = httpWorkerGetAsset(
00191                                                         request,
00192                                                         "./assets/image/waldschrat.jpg",
00193                                                         CSTRA("image/jpeg"));
00194                                 }
00195 
00196                                 if (0 == strcmp("/assets/js/jquery", request->path)) {
00197                                         response = httpWorkerGetAsset(
00198                                                         request,
00199                                                         "./assets/js/jquery-1.7.1.min.js",
00200                                                         CSTRA("text/javascript"));
00201                                 }
00202 
00203                                 if (0 == strcmp("/assets/js/serverval", request->path)) {
00204                                         response = httpWorkerGetAsset(
00205                                                         request,
00206                                                         "./assets/js/serverval.js",
00207                                                         CSTRA("text/javascript"));
00208                                 }
00209 
00210                                 if (0 == strcmp("/assets/js/session", request->path)) {
00211                                         response = httpWorkerGetAsset(
00212                                                         request,
00213                                                         "./assets/js/session.js",
00214                                                         CSTRA("text/javascript"));
00215                                 }
00216 
00217                                 if (0 == strcmp("/assets/js/init", request->path)) {
00218                                         response = httpWorkerGetAsset(
00219                                                         request,
00220                                                         "./assets/js/init.js",
00221                                                         CSTRA("text/javascript"));
00222                                 }
00223 
00224                                 if (0 == strcmp("/assets/style/common", request->path)) {
00225                                         response = httpWorkerGetAsset(
00226                                                         request,
00227                                                         "./assets/style/common.css",
00228                                                         CSTRA("text/css"));
00229                                 }
00230                         }
00231 
00232                         if (NULL == response) {
00233                                 response = (HttpMessage)httpResponse404();
00234                         }
00235 
00236                         httpWorkerAddCommonHeader((HttpMessage)request, response);
00237 
00238                         respq->msgs[(respq->nmsgs)++] = response;
00239                         response = NULL;
00240                         delete((reqq->msgs)[i]);
00241                 }
00242 
00243                 reqq->nmsgs = 0;
00244         }
00245 
00246         return size;
00247 }
00248 
00249 // vim: set ts=4 sw=4:
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines