/** * \file * * \author Georg Hopp * * \copyright * Copyright (C) 2012 Georg Hopp * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * 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. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include #include #include "class.h" #include "interface/class.h" #include "http/worker.h" #include "http/message.h" #include "http/request.h" #include "http/response.h" #include "http/message/queue.h" #include "http/parser.h" #include "session.h" #include "stream.h" #include "utils/memory.h" HttpMessage httpWorkerGetAsset(HttpRequest, const char *, const char *, size_t); void httpWorkerAddCommonHeader(HttpMessage, HttpMessage); ssize_t httpWorkerProcess(HttpWorker this, Stream st) { time_t t; struct tm * tmp; char buffer[200]; ssize_t size; if (0 < (size = httpParserParse(this->parser, st))) { int i; HttpMessageQueue reqq = this->parser->queue; HttpMessageQueue respq = this->writer->queue; for (i=0; inmsgs; i++) { HttpMessage rmessage = reqq->msgs[i]; HttpRequest request = (HttpRequest)(reqq->msgs[i]); HttpMessage response = NULL; HttpHeader cookie = httpHeaderGet( &(rmessage->header), CSTRA("cookie")); if (NULL == this->session && NULL != cookie) { int i; for (i=0; icvalue; i++) { char * sidstr = strstr(cookie->value[i], "sid"); if (NULL != sidstr) { unsigned long sid; sidstr = strchr(sidstr, '=')+1; sid = strtoul(sidstr, NULL, 10); this->session = sessionGet(this->sroot, sid); break; } } } if (NULL != this->session) { if (time(NULL) < this->session->livetime) { this->session->livetime = time(NULL) + SESSION_LIVETIME; } else { sessionDelete(this->sroot, this->session->id); delete(this->session); } } if (0 == strcmp("POST", request->method)) { if (0 == strcmp("/login/", request->uri)) { char * delim = memchr(rmessage->body, '=', rmessage->nbody); char * val; size_t nkey, nval; char buffer[200]; size_t nbuf; nkey = delim - rmessage->body - 1; *delim = 0; val = delim + 1; nval = rmessage->nbody - (val - rmessage->body); this->session = sessionAdd( this->sroot, new(Session, val, nval)); nbuf = sprintf(buffer, "sid=%lu;Path=/", this->session->id); response = (HttpMessage)httpResponseSession(this->session); httpHeaderAdd( &(response->header), new(HttpHeader, CSTRA("Set-Cookie"), buffer, nbuf)); } } if (0 == strcmp("GET", request->method)) { if (0 == strcmp("/", request->uri)) { response = httpWorkerGetAsset( request, "./assets/html/main.html", CSTRA("text/html")); } if (0 == strcmp("/sessinfo/", request->uri)) { response = (HttpMessage)httpResponseSession(this->session); } if (0 == strcmp("/randval/", request->uri)) { if (NULL != this->session) { response = (HttpMessage)httpResponseRandval( this->val->timestamp, this->val->value); } else { response = (HttpMessage)httpResponse403(); } } if (0 == strcmp("/image/me", request->uri)) { response = httpWorkerGetAsset( request, "./assets/image/waldschrat.jpg", CSTRA("image/jpeg")); } if (0 == strcmp("/assets/js/jquery", request->uri)) { response = httpWorkerGetAsset( request, "./assets/js/jquery-1.7.1.min.js", CSTRA("text/javascript")); } if (0 == strcmp("/assets/js/serverval", request->uri)) { response = httpWorkerGetAsset( request, "./assets/js/serverval.js", CSTRA("text/javascript")); } if (0 == strcmp("/assets/js/session", request->uri)) { response = httpWorkerGetAsset( request, "./assets/js/session.js", CSTRA("text/javascript")); } if (0 == strcmp("/assets/js/init", request->uri)) { response = httpWorkerGetAsset( request, "./assets/js/init.js", CSTRA("text/javascript")); } if (0 == strcmp("/assets/style/common", request->uri)) { response = httpWorkerGetAsset( request, "./assets/style/common.css", CSTRA("text/css")); } } if (NULL == response) { response = (HttpMessage)httpResponse404(); } httpWorkerAddCommonHeader((HttpMessage)request, response); t = time(NULL); tmp = localtime(&t); strftime(buffer, sizeof(buffer), "%a, %d %b %Y %T %Z", tmp); httpHeaderAdd(&(response->header), new(HttpHeader, "Date", buffer)); respq->msgs[(respq->nmsgs)++] = response; response = NULL; delete((reqq->msgs)[i]); } reqq->nmsgs = 0; size = respq->nmsgs; } return size; } // vim: set ts=4 sw=4: