taskrambler  v0.1.8
Web server and task management solution.
get_asset.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 <time.h>
25 
26 #include "http/header.h"
27 #include "http/message.h"
28 #include "http/request.h"
29 #include "http/response.h"
30 #include "http/worker.h"
31 
32 #include "utils/memory.h"
33 #include "hash.h"
34 
35 HttpMessage
36 httpWorkerGetAsset(HttpWorker this, const char * fname)
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 }
86 
87 // vim: set ts=4 sw=4:
HttpResponse httpResponseAsset(const char *, size_t, time_t)
HttpMessage httpWorkerGetAsset(HttpWorker this, const char *fname)
Definition: get_asset.c:36
#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