taskrambler  0.1.9
Web server and task management solution.
header.c
Go to the documentation of this file.
1 /**
2  * \file
3  * Interface implementation for HTTP header class.
4  *
5  * \author Georg Hopp
6  *
7  * \copyright
8  * Copyright © 2012 Georg Hopp
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "class.h"
28 #include "hash.h"
29 #include "http/header.h"
30 
31 #include "utils/hash.h"
32 #include "utils/memory.h"
33 
34 static
35 int
36 httpHeaderCtor(void * _this, va_list * params) {
37  HttpHeader this = _this;
38  char * name;
39  char * value;
40 
41  name = va_arg(* params, char *);
42  this->nname = va_arg(* params, size_t);
43  value = va_arg(* params, char *);
44  this->nvalue[0] = va_arg(* params, size_t);
45 
46  this->name = memMalloc(this->nname + 1);
47  this->name[this->nname] = 0;
48  memcpy(this->name, name, this->nname);
49 
50  this->hash = sdbm((unsigned char *)name, this->nname);
51 
52  (this->value)[0] = memMalloc((this->nvalue)[0] + 1);
53  (this->value)[0][(this->nvalue)[0]] = 0;
54  memcpy((this->value)[0], value, (this->nvalue)[0]);
55  this->cvalue = 1;
56  this->size = this->nname + 2 + (this->nvalue)[0] + 2;
57 
58  return 0;
59 }
60 
61 static
62 void
63 httpHeaderDtor(void * _this)
64 {
65  HttpHeader this = _this;
66  size_t i;
67 
68  MEM_FREE(this->name);
69 
70  for (i=0; i<this->cvalue; i++) {
71  MEM_FREE(this->value[i]);
72  }
73 }
74 
75 static
76 unsigned long
77 httpHeaderGetHash(void * _this)
78 {
79  HttpHeader this = _this;
80 
81  return this->hash;
82 }
83 
84 static
85 void
86 httpHeaderHandleDouble(void * _this, void * _double)
87 {
88  HttpHeader this = _this;
89  HttpHeader doub = _double;
90 
91  if (this->cvalue >= N_VALUES) {
92  //! \todo do dome error logging...or change to HEAP
93  return;
94  }
95 
96  (this->nvalue)[this->cvalue] = (doub->nvalue)[0];
97  (this->value)[(this->cvalue)++] = (doub->value)[0];
98  this->size += doub->size;
99  (doub->value)[0] = NULL;
100 }
101 
104 CREATE_CLASS(HttpHeader, NULL, IFACE(Class), IFACE(Hashable));
105 
106 // vim: set ts=4 sw=4:
#define MEM_FREE(seg)
Definition: memory.h:28
static unsigned long httpHeaderGetHash(void *_this)
Definition: header.c:77
INIT_IFACE(Class, httpHeaderCtor, httpHeaderDtor, NULL)
unsigned long sdbm(const unsigned char *, size_t)
Definition: utils/hash.c:45
void * memMalloc(size_t)
Definition: memory.c:783
static void httpHeaderHandleDouble(void *_this, void *_double)
Definition: header.c:86
const char * name
Definition: interface.h:43
#define IFACE(name)
Definition: interface.h:34
static size_t size
static int httpHeaderCtor(void *_this, va_list *params)
Definition: header.c:36
CREATE_CLASS(HttpHeader, NULL, IFACE(Class), IFACE(Hashable))
static void httpHeaderDtor(void *_this)
Definition: header.c:63
#define N_VALUES
Definition: header.h:32