taskrambler  0.1.9
Web server and task management solution.
process.c File Reference
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <sys/time.h>
#include "class.h"
#include "auth.h"
#include "queue.h"
#include "session.h"
#include "stream.h"
#include "hash.h"
#include "http/worker.h"
#include "http/header.h"
#include "http/message.h"
#include "http/request.h"
#include "http/response.h"
#include "http/parser.h"
#include "config/config.h"
#include "config/value.h"
#include "interface/subject.h"
#include "utils/memory.h"
#include "utils/mime_type.h"
#include "commons.h"
+ Include dependency graph for process.c:

Go to the source code of this file.

Functions

HttpMessage httpWorkerGetAsset (HttpWorker, const char *)
 
void httpWorkerAddCommonHeader (HttpWorker)
 
void httpWorkerAddComputedHeader (HttpWorker)
 
ssize_t httpWorkerProcess (HttpWorker this, Stream st)
 

Variables

Config config
 

Detailed Description

Author
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 http://www.gnu.org/licenses/.

Definition in file process.c.

Function Documentation

void httpWorkerAddCommonHeader ( HttpWorker  )

Definition at line 40 of file add_common_header.c.

References CSTRA, hashAdd(), httpMessageHasKeepAlive(), and rfc1123GmtNow().

Referenced by httpWorkerProcess().

41 {
42  char buffer[200];
43  size_t nbuf;
44 
45  if (httpMessageHasKeepAlive((HttpMessage)this->current_request)) {
46  hashAdd(this->current_response->header,
47  new(HttpHeader, CSTRA("Connection"), CSTRA("Keep-Alive")));
48  }
49  else {
50  hashAdd(this->current_response->header,
51  new(HttpHeader, CSTRA("Connection"), CSTRA("Close")));
52  }
53 
54  hashAdd(this->current_response->header,
55  new(HttpHeader, CSTRA("Server"), CSTRA(PACKAGE_STRING)));
56 
57  switch(((HttpResponse)this->current_response)->status) {
58  case 304:
59  break;
60 
61  default:
62  nbuf = sprintf(buffer, "%d", this->current_response->nbody);
63  hashAdd(this->current_response->header,
64  new(HttpHeader, CSTRA("Content-Length"), buffer, nbuf));
65  }
66 
67  nbuf = rfc1123GmtNow(buffer, sizeof(buffer));
68  hashAdd(this->current_response->header,
69  new(HttpHeader, CSTRA("Date"), buffer, nbuf));
70 }
#define CSTRA(val)
Const STRing Argument.
Definition: memory.h:26
void * hashAdd(Hash, void *)
Definition: add.c:48
size_t rfc1123GmtNow(char *, size_t)
Definition: utils/http.c:69
char httpMessageHasKeepAlive(HttpMessage)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void httpWorkerAddComputedHeader ( HttpWorker  )

Definition at line 37 of file add_computed_header.c.

References hashAdd(), and queueGet().

Referenced by httpWorkerProcess().

38 {
39  HttpHeader header = (HttpHeader)queueGet(this->additional_headers);
40 
41  while(NULL != header) {
42  hashAdd(this->current_response->header, header);
43  header = (HttpHeader)queueGet(this->additional_headers);
44  }
45 }
void * queueGet(Queue)
Definition: queue/get.c:27
void * hashAdd(Hash, void *)
Definition: add.c:48

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

HttpMessage httpWorkerGetAsset ( HttpWorker  ,
const char *   
)

Definition at line 36 of file get_asset.c.

References CSTRA, hashGet(), httpResponse304(), httpResponse404(), and httpResponseAsset().

Referenced by httpWorkerProcess().

37 {
38  char * match;
39  size_t nmatch;
40  HttpHeader header;
41  HttpMessage message;
42 
43  size_t nfname = strlen(fname);
44 
45  header = hashGet(
46  ((HttpMessage)this->current_request)->header,
47  CSTRA("If-None-Match"));
48 
49  if (NULL == header) {
50  match = "";
51  nmatch = 0;
52  }
53  else {
54  match = (header->value)[0];
55  nmatch = (header->nvalue)[0];
56  }
57 
58  /*
59  * assets expire after 12 hours...
60  */
61  message = (HttpMessage)httpResponseAsset(
62  fname,
63  nfname,
64  time(NULL) + 43200);
65 
66  if (NULL == message) {
67  return (HttpMessage)httpResponse404();
68  }
69 
70  if (message->asset->netag == nmatch
71  && 0 == memcmp(message->asset->etag, match, nmatch)) {
72  HttpMessage new_message;
73 
74  new_message = (HttpMessage)httpResponse304(
75  message->asset->mime_type, message->asset->nmime_type,
76  message->asset->etag, message->asset->netag,
77  message->asset->mtime, message->asset->nmtime);
78 
79  delete(message);
80 
81  return new_message;
82  }
83 
84  return message;
85 }
HttpResponse httpResponseAsset(const char *, size_t, time_t)
#define CSTRA(val)
Const STRing Argument.
Definition: memory.h:26
void * hashGet(Hash, const char *, size_t)
Definition: hash/get.c:51
HttpResponse httpResponse304(const char *, size_t, const char *, size_t, const char *, size_t)
Definition: 304.c:35
HttpResponse httpResponse404()
Definition: 404.c:47

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ssize_t httpWorkerProcess ( HttpWorker  this,
Stream  st 
)

Definition at line 60 of file process.c.

References config, configGet(), CSTRA, getMimeType(), httpParserParse(), httpResponse404(), httpResponse500(), httpWorkerAddCommonHeader(), httpWorkerAddComputedHeader(), httpWorkerGetAsset(), queueEmpty, queueGet(), queuePut(), and subjectNotify().

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 }
ConfigValue configGet(Config, const char *, size_t)
Definition: config/get.c:31
void subjectNotify(void *)
Definition: subject.c:44
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)

+ Here is the call graph for this function:

Variable Documentation

Config config

Definition at line 67 of file taskrambler.c.

Referenced by CLASS(), configCtor(), configDtor(), configGet(), httpWorkerProcess(), and main().