HomePort
value.c
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Aalborg University. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are
5  * permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  * conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  * of conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY Aalborg University ''AS IS'' AND ANY EXPRESS OR IMPLIED
15  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Aalborg University OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * The views and conclusions contained in the software and documentation are those of the
25  * authors and should not be interpreted as representing official policies, either expressed
26  */
27 
28 #include "value.h"
29 #include "hpd/common/hpd_common.h"
30 #include "hpd/common/hpd_map.h"
31 #include "comm.h"
32 #include "log.h"
33 
34 hpd_error_t value_alloc(hpd_value_t **value, const char *body, int len)
35 {
36  hpd_error_t rc;
37  HPD_CALLOC((*value), 1, hpd_value_t);
38  if ((rc = hpd_map_alloc(&(*value)->headers))) {
39  free(*value);
40  return rc;
41  }
42  if (body) {
43  HPD_STR_CPY((*value)->body, body);
44  if (len == HPD_NULL_TERMINATED) (*value)->len = strlen(body);
45  else (*value)->len = (size_t) len;
46  }
47  return HPD_E_SUCCESS;
48 
49  alloc_error:
50  value_free(*value);
51  (*value) = NULL;
53 }
54 
55 hpd_error_t value_vallocf(hpd_value_t **value, const char *fmt, va_list vp)
56 {
57  hpd_error_t rc;
58  HPD_CALLOC((*value), 1, hpd_value_t);
59  if ((rc = hpd_map_alloc(&(*value)->headers))) {
60  free(*value);
61  return rc;
62  }
63  if (fmt) {
64  HPD_VSPRINTF_ALLOC((*value)->body, fmt, vp);
65  (*value)->len = strlen((*value)->body);
66  }
67  return HPD_E_SUCCESS;
68 
69  alloc_error:
70  value_free(*value);
71  (*value) = NULL;
73 
74  vsnprintf_error:
75  value_free(*value);
76  free((*value)->body);
77  LOG_RETURN(HPD_E_UNKNOWN, "vsnprintf error.");
78 }
79 
81 {
82  hpd_error_t rc;
83  if ((rc = value_alloc(dst, src->body, (int) src->len))) return rc;
84  hpd_pair_t *pair;
85  hpd_map_foreach(rc, pair, src->headers) {
86  const char *k, *v;
87  if ((rc = hpd_pair_get(pair, &k, &v))) {
88  value_free(*dst);
89  return rc;
90  }
91  if ((rc = value_set_header(*dst, k, v))) {
92  value_free(*dst);
93  return rc;
94  }
95  }
96  return rc;
97 }
98 
100 {
102  if (value) {
103  rc = hpd_map_free(value->headers);
104  free(value->body);
105  }
106  free(value);
107  return rc;
108 }
109 
110 hpd_error_t value_set_header(hpd_value_t *value, const char *key, const char *val)
111 {
112  return hpd_map_set(value->headers, key, val);
113 }
114 
116 {
117  hpd_error_t rc;
118  const char *key, *val;
119 
120  while ((key = va_arg(vp, const char *))) {
121  val = va_arg(vp, const char *);
122  if (!val) LOG_RETURN_E_NULL();
123  if ((rc = value_set_header(value, key, val))) return rc;
124  }
125  return HPD_E_SUCCESS;
126 }
127 
128 hpd_error_t value_get_body(const hpd_value_t *value, const char **body, size_t *len)
129 {
130  if (body) (*body) = value->body;
131  if (len) (*len) = value->len;
132  return HPD_E_SUCCESS;
133 }
134 
135 hpd_error_t value_get_header(const hpd_value_t *value, const char *key, const char **val)
136 {
137  return hpd_map_get(value->headers, key, val);
138 }
139 
141 {
142  hpd_error_t rc;
143  const char *key, **val;
144 
145  while ((key = va_arg(vp, const char *))) {
146  val = va_arg(vp, const char **);
147  if (!val) LOG_RETURN_E_NULL();
148  if ((rc = value_get_header(value, key, val))) return rc;
149  }
150  return HPD_E_SUCCESS;
151 }
152 
154 {
155  return hpd_map_first(value->headers, pair);
156 }
157 
159 {
160  return hpd_map_next(pair);
161 }
162 
hpd_error_t value_copy(hpd_value_t **dst, const hpd_value_t *src)
Definition: value.c:80
#define LOG_RETURN_E_NULL()
Definition: log.h:50
#define HPD_STR_CPY(DST, SRC)
Definition: hpd_common.h:64
data value
Definition: map.c:34
#define LOG_RETURN_E_ALLOC()
Definition: log.h:51
hpd_error_t hpd_map_set(hpd_map_t *map, const char *k, const char *v)
Definition: map.c:175
hpd_error_t hpd_map_next(hpd_pair_t **pair)
Definition: map.c:60
free(data.url)
hpd_error_t value_set_headers_v(hpd_value_t *value, va_list vp)
Definition: value.c:115
size_t len
Definition: comm.h:73
hpd_error_t hpd_map_first(hpd_map_t *map, hpd_pair_t **pair)
Definition: map.c:52
hpd_error_t hpd_map_free(hpd_map_t *map)
Definition: map.c:79
#define HPD_CALLOC(PTR, NUM, CAST)
Allocates and zeros a structure.
Definition: hpd_common.h:44
#define HPD_NULL_TERMINATED
Value to be used in len parameters on \0 terminated strings.
Definition: hpd_types.h:62
hpd_error_t value_get_headers_v(const hpd_value_t *value, va_list vp)
Definition: value.c:140
hpd_error_t value_get_header(const hpd_value_t *value, const char *key, const char **val)
Definition: value.c:135
data key
Definition: comm.h:70
hpd_error_t value_alloc(hpd_value_t **value, const char *body, int len)
Definition: value.c:34
#define hpd_map_foreach(RC, PAIR, MAP)
Definition: hpd_map.h:55
enum hpd_error hpd_error_t
Definition: hpd_types.h:167
#define LOG_RETURN(E, FMT,...)
Definition: log.h:48
hpd_error_t hpd_map_get(hpd_map_t *map, const char *k, const char **v)
Definition: map.c:92
hpd_error_t value_vallocf(hpd_value_t **value, const char *fmt, va_list vp)
Definition: value.c:55
char * body
Definition: comm.h:72
hpd_error_t value_next_header(hpd_pair_t **pair)
Definition: value.c:158
hpd_error_t value_free(hpd_value_t *value)
Definition: value.c:99
hpd_error_t hpd_map_alloc(hpd_map_t **map)
Definition: map.c:40
hpd_error_t value_get_body(const hpd_value_t *value, const char **body, size_t *len)
Definition: value.c:128
#define HPD_VSPRINTF_ALLOC(DST, FMT, VP)
Definition: hpd_common.h:82
hpd_map_t * headers
Definition: comm.h:71
hpd_error_t value_set_header(hpd_value_t *value, const char *key, const char *val)
Definition: value.c:110
hpd_error_t hpd_pair_get(const hpd_pair_t *pair, const char **key, const char **value)
Definition: map.c:229
hpd_error_t value_first_header(const hpd_value_t *value, hpd_pair_t **pair)
Definition: value.c:153