taskrambler  0.1.9
Web server and task management solution.
process.c
Go to the documentation of this file.
1 /**
2  * \file
3  *
4  * \author Georg Hopp
5  *
6  * \copyright
7  * Copyright © 2012 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 #include <sys/types.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #include <string.h>
28 #include <sys/time.h>
29 
30 #include "class.h"
31 #include "auth.h"
32 #include "queue.h"
33 #include "session.h"
34 #include "stream.h"
35 #include "hash.h"
36 
37 #include "http/worker.h"
38 #include "http/header.h"
39 #include "http/message.h"
40 #include "http/request.h"
41 #include "http/response.h"
42 #include "http/parser.h"
43 #include "config/config.h"
44 #include "config/value.h"
45 
46 #include "interface/subject.h"
47 
48 #include "utils/memory.h"
49 #include "utils/mime_type.h"
50 #include "commons.h"
51 
52 
53 HttpMessage httpWorkerGetAsset(HttpWorker, const char *);
54 void httpWorkerAddCommonHeader(HttpWorker);
55 void httpWorkerAddComputedHeader(HttpWorker);
56 
57 extern Config config;
58 
59 ssize_t
60 httpWorkerProcess(HttpWorker this, Stream st)
61 {
62  ssize_t requests = httpParserParse(this->parser, st);
63 
64  if (0 > requests) {
65  return requests;
66  }
67 
68  if (0 < requests) {
69  while (! queueEmpty(this->parser->queue)) {
70  this->current_request = queueGet(this->parser->queue);
71  this->current_response = NULL;
72 
73  /*
74  * let our observers...application (or better their
75  * http adapter) try to create an answer.
76  */
77  subjectNotify(this);
78 
79  if (NULL == this->current_response) {
80  if (0 == strcmp("POST", this->current_request->method) ||
81  0 == strcmp("PUT", this->current_request->method))
82  {
83  /*
84  * we can't do post requests on our own...
85  */
86  this->current_response = (HttpMessage)httpResponse500();
87  }
88 
89  if (0 == strcmp("GET", this->current_request->method)) {
90  ConfigValue assets_dir =
91  configGet(config, CSTRA("assets_dir"));
92 
93  char asset_path[2048];
94 
95  char html_asset[] = "/assets/html";
96  char base_asset[] = "/assets";
97  char main_asset[] = "/main.html";
98 
99  char * asset;
100  char * mime_type;
101 
102  strcpy(asset_path, (assets_dir->value).string);
103 
104  if (0 == strcmp("/", this->current_request->path)) {
105  asset = main_asset;
106  } else {
107  asset = this->current_request->path;
108  }
109 
110  mime_type = strrchr(asset, '.');
111  if (NULL != mime_type) {
112  mime_type++;
113  mime_type = getMimeType(mime_type, strlen(mime_type));
114  }
115 
116  if (NULL != mime_type &&
117  0 == memcmp(mime_type, CSTRA("text/html"))) {
118  strcat(asset_path, html_asset);
119  } else {
120  strcat(asset_path, base_asset);
121  }
122 
123  strcat(asset_path, asset);
124  this->current_response =
125  httpWorkerGetAsset(this, asset_path);
126  }
127  }
128 
129  if (NULL == this->current_response) {
130  this->current_response = (HttpMessage)httpResponse404();
131  }
132 
135  delete(this->current_request);
136  queuePut(this->writer->queue, this->current_response);
137  this->current_response = NULL;
138  }
139  }
140 
141  return this->writer->queue->nmsg;
142 }
143 
144 // vim: set ts=4 sw=4:
ConfigValue configGet(Config, const char *, size_t)
Definition: config/get.c:31
void subjectNotify(void *)
Definition: subject.c:44
ssize_t httpWorkerProcess(HttpWorker this, Stream st)
Definition: process.c:60
void * queueGet(Queue)
Definition: queue/get.c:27
HttpResponse httpResponse500()
Definition: 500.c:47
#define CSTRA(val)
Const STRing Argument.
Definition: memory.h:26
char * getMimeType(const char *, size_t)
Definition: mime_type.c:84
HttpMessage httpWorkerGetAsset(HttpWorker, const char *)
Definition: get_asset.c:36
ssize_t httpParserParse(void *, Stream)
void queuePut(Queue, void *)
Definition: queue/put.c:27
void httpWorkerAddComputedHeader(HttpWorker)
Config config
Definition: taskrambler.c:67
#define queueEmpty(this)
Definition: queue.h:55
HttpResponse httpResponse404()
Definition: 404.c:47
void httpWorkerAddCommonHeader(HttpWorker)