taskrambler  0.1.8
Web server and task management solution.
http/response/asset.c
Go to the documentation of this file.
1 /**
2  * \file
3  * A response class that delivers an asset (file on disk).
4  *
5  * In future this will use a asset class, get from an asset class
6  * hash. The asset hash will be a shared resource between all
7  * workers.
8  *
9  * The asset class holds an open file descriptor wich is memory
10  * mapped and is able to give the correct pointer to neede data.
11  *
12  * This change will envolve changes in other parts of the response
13  * write system, as we no longer need to destinguish between piped
14  * and bufferd resources...we will allways work with a memory address
15  * only one time its allocated and one time a memory mapped file.
16  *
17  * \author Georg Hopp
18  *
19  * \copyright
20  * Copyright © 2012 Georg Hopp
21  *
22  * This program is free software: you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation, either version 3 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License
33  * along with this program. If not, see <http://www.gnu.org/licenses/>.
34  */
35 
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <time.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include <string.h>
42 #include <sys/types.h>
43 
44 #include "class.h"
45 #include "stream.h"
46 
47 #include "http/response.h"
48 #include "http/message.h"
49 #include "http/header.h"
50 
51 #include "utils/memory.h"
52 #include "utils/http.h"
53 #include "hash.h"
54 
55 #include "asset.h"
56 
57 
58 HttpResponse
59 httpResponseAsset(const char * fname, size_t nfname, time_t exptime)
60 {
61  HttpResponse response;
62  HttpMessage message;
63  Asset asset = assetPoolGet(fname, nfname);
64  char expires[200];
65  size_t nexpires;
66 
67  if (NULL == asset) {
68  return NULL;
69  }
70 
71  response = new(HttpResponse, "HTTP/1.1", 200, "OK");
72  message = (HttpMessage)response;
73 
74  message->asset = asset;
75  message->body = asset->data;
76  message->nbody = asset->size;
77 
78  nexpires = rfc1123Gmt(expires, sizeof(expires), &exptime);
79 
80  hashAdd(message->header,
81  new(HttpHeader, CSTRA("Content-Type"),
82  asset->mime_type, asset->nmime_type));
83  hashAdd(message->header,
84  new(HttpHeader, CSTRA("ETag"), asset->etag, asset->netag));
85  hashAdd(message->header,
86  new(HttpHeader, CSTRA("Expires"), expires, nexpires));
87  hashAdd(message->header,
88  new(HttpHeader, CSTRA("Last-Modified"),
89  asset->mtime, asset->nmtime));
90 
91  return response;
92 }
93 
94 // vim: set ts=4 sw=4:
Asset assetPoolGet(const char *, size_t)
Definition: pool.c:43
HttpResponse httpResponseAsset(const char *fname, size_t nfname, time_t exptime)
#define CSTRA(val)
Const STRing Argument.
Definition: memory.h:26
size_t rfc1123Gmt(char *, size_t, const time_t *)
Definition: utils/http.c:53
void * hashAdd(Hash, void *)
Definition: add.c:48