aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-05-02 22:24:42 +0200
committerChristian Grothoff <christian@grothoff.org>2019-05-02 22:24:42 +0200
commit1eadd51205794a18e74f135672c96613d4f8734b (patch)
tree79dae4cf5ad1547e9e48aa87428e64c864af28f2
parent5ef270d2a7fa5c32d874fc7da2f026998ff0ab8e (diff)
downloadgnunet-1eadd51205794a18e74f135672c96613d4f8734b.tar.gz
gnunet-1eadd51205794a18e74f135672c96613d4f8734b.zip
preparatory change to GNUNET_JSON_post_parser() API for future compression support
-rw-r--r--src/include/gnunet_json_lib.h64
-rw-r--r--src/json/json_mhd.c31
2 files changed, 39 insertions, 56 deletions
diff --git a/src/include/gnunet_json_lib.h b/src/include/gnunet_json_lib.h
index c8f09d4e4..32de2208a 100644
--- a/src/include/gnunet_json_lib.h
+++ b/src/include/gnunet_json_lib.h
@@ -30,7 +30,7 @@
30#include "gnunet_util_lib.h" 30#include "gnunet_util_lib.h"
31#include "gnunet_gnsrecord_lib.h" 31#include "gnunet_gnsrecord_lib.h"
32#include <jansson.h> 32#include <jansson.h>
33 33#include <microhttpd.h>
34 34
35/* ****************** Generic parser interface ******************* */ 35/* ****************** Generic parser interface ******************* */
36 36
@@ -49,10 +49,9 @@ struct GNUNET_JSON_Specification;
49 * @return #GNUNET_SYSERR on error, 49 * @return #GNUNET_SYSERR on error,
50 * #GNUNET_OK on success 50 * #GNUNET_OK on success
51 */ 51 */
52typedef int 52typedef int (*GNUNET_JSON_Parser) (void *cls,
53(*GNUNET_JSON_Parser)(void *cls, 53 json_t *root,
54 json_t *root, 54 struct GNUNET_JSON_Specification *spec);
55 struct GNUNET_JSON_Specification *spec);
56 55
57 56
58/** 57/**
@@ -61,9 +60,8 @@ typedef int
61 * @param cls closure 60 * @param cls closure
62 * @param spec our specification entry with data to clean. 61 * @param spec our specification entry with data to clean.
63 */ 62 */
64typedef void 63typedef void (*GNUNET_JSON_Cleaner) (void *cls,
65(*GNUNET_JSON_Cleaner)(void *cls, 64 struct GNUNET_JSON_Specification *spec);
66 struct GNUNET_JSON_Specification *spec);
67 65
68 66
69/** 67/**
@@ -106,7 +104,6 @@ struct GNUNET_JSON_Specification
106 * Where should we store the final size of @e ptr. 104 * Where should we store the final size of @e ptr.
107 */ 105 */
108 size_t *size_ptr; 106 size_t *size_ptr;
109
110}; 107};
111 108
112 109
@@ -140,7 +137,6 @@ void
140GNUNET_JSON_parse_free (struct GNUNET_JSON_Specification *spec); 137GNUNET_JSON_parse_free (struct GNUNET_JSON_Specification *spec);
141 138
142 139
143
144/* ****************** Canonical parser specifications ******************* */ 140/* ****************** Canonical parser specifications ******************* */
145 141
146 142
@@ -160,9 +156,7 @@ GNUNET_JSON_spec_end (void);
160 * @param size number of bytes expected in @a obj 156 * @param size number of bytes expected in @a obj
161 */ 157 */
162struct GNUNET_JSON_Specification 158struct GNUNET_JSON_Specification
163GNUNET_JSON_spec_fixed (const char *name, 159GNUNET_JSON_spec_fixed (const char *name, void *obj, size_t size);
164 void *obj,
165 size_t size);
166 160
167 161
168/** 162/**
@@ -172,7 +166,8 @@ GNUNET_JSON_spec_fixed (const char *name,
172 * @param name name of the JSON field 166 * @param name name of the JSON field
173 * @param obj pointer where to write the data (type of `*obj` will determine size) 167 * @param obj pointer where to write the data (type of `*obj` will determine size)
174 */ 168 */
175#define GNUNET_JSON_spec_fixed_auto(name,obj) GNUNET_JSON_spec_fixed (name, obj, sizeof (*obj)) 169#define GNUNET_JSON_spec_fixed_auto(name, obj) \
170 GNUNET_JSON_spec_fixed (name, obj, sizeof (*obj))
176 171
177 172
178/** 173/**
@@ -184,9 +179,7 @@ GNUNET_JSON_spec_fixed (const char *name,
184 * @param[out] size where to store the number of bytes allocated for @a obj 179 * @param[out] size where to store the number of bytes allocated for @a obj
185 */ 180 */
186struct GNUNET_JSON_Specification 181struct GNUNET_JSON_Specification
187GNUNET_JSON_spec_varsize (const char *name, 182GNUNET_JSON_spec_varsize (const char *name, void **obj, size_t *size);
188 void **obj,
189 size_t *size);
190 183
191 184
192/** 185/**
@@ -196,8 +189,7 @@ GNUNET_JSON_spec_varsize (const char *name,
196 * @param strptr where to store a pointer to the field 189 * @param strptr where to store a pointer to the field
197 */ 190 */
198struct GNUNET_JSON_Specification 191struct GNUNET_JSON_Specification
199GNUNET_JSON_spec_string (const char *name, 192GNUNET_JSON_spec_string (const char *name, const char **strptr);
200 const char **strptr);
201 193
202/** 194/**
203 * JSON object. 195 * JSON object.
@@ -206,8 +198,7 @@ GNUNET_JSON_spec_string (const char *name,
206 * @param[out] jsonp where to store the JSON found under @a name 198 * @param[out] jsonp where to store the JSON found under @a name
207 */ 199 */
208struct GNUNET_JSON_Specification 200struct GNUNET_JSON_Specification
209GNUNET_JSON_spec_json (const char *name, 201GNUNET_JSON_spec_json (const char *name, json_t **jsonp);
210 json_t **jsonp);
211 202
212 203
213/** 204/**
@@ -217,8 +208,7 @@ GNUNET_JSON_spec_json (const char *name,
217 * @param[out] u8 where to store the integer found under @a name 208 * @param[out] u8 where to store the integer found under @a name
218 */ 209 */
219struct GNUNET_JSON_Specification 210struct GNUNET_JSON_Specification
220GNUNET_JSON_spec_uint8 (const char *name, 211GNUNET_JSON_spec_uint8 (const char *name, uint8_t *u8);
221 uint8_t *u8);
222 212
223 213
224/** 214/**
@@ -228,8 +218,7 @@ GNUNET_JSON_spec_uint8 (const char *name,
228 * @param[out] u16 where to store the integer found under @a name 218 * @param[out] u16 where to store the integer found under @a name
229 */ 219 */
230struct GNUNET_JSON_Specification 220struct GNUNET_JSON_Specification
231GNUNET_JSON_spec_uint16 (const char *name, 221GNUNET_JSON_spec_uint16 (const char *name, uint16_t *u16);
232 uint16_t *u16);
233 222
234 223
235/** 224/**
@@ -239,8 +228,7 @@ GNUNET_JSON_spec_uint16 (const char *name,
239 * @param[out] u32 where to store the integer found under @a name 228 * @param[out] u32 where to store the integer found under @a name
240 */ 229 */
241struct GNUNET_JSON_Specification 230struct GNUNET_JSON_Specification
242GNUNET_JSON_spec_uint32 (const char *name, 231GNUNET_JSON_spec_uint32 (const char *name, uint32_t *u32);
243 uint32_t *u32);
244 232
245 233
246/** 234/**
@@ -250,8 +238,7 @@ GNUNET_JSON_spec_uint32 (const char *name,
250 * @param[out] u64 where to store the integer found under @a name 238 * @param[out] u64 where to store the integer found under @a name
251 */ 239 */
252struct GNUNET_JSON_Specification 240struct GNUNET_JSON_Specification
253GNUNET_JSON_spec_uint64 (const char *name, 241GNUNET_JSON_spec_uint64 (const char *name, uint64_t *u64);
254 uint64_t *u64);
255 242
256/** 243/**
257 * Boolean (true mapped to GNUNET_YES, false mapped to GNUNET_NO). 244 * Boolean (true mapped to GNUNET_YES, false mapped to GNUNET_NO).
@@ -260,8 +247,7 @@ GNUNET_JSON_spec_uint64 (const char *name,
260 * @param[out] boolean where to store the boolean found under @a name 247 * @param[out] boolean where to store the boolean found under @a name
261 */ 248 */
262struct GNUNET_JSON_Specification 249struct GNUNET_JSON_Specification
263GNUNET_JSON_spec_boolean (const char *name, 250GNUNET_JSON_spec_boolean (const char *name, int *boolean);
264 int *boolean);
265 251
266 252
267/* ************ GNUnet-specific parser specifications ******************* */ 253/* ************ GNUnet-specific parser specifications ******************* */
@@ -285,7 +271,7 @@ GNUNET_JSON_spec_absolute_time (const char *name,
285 */ 271 */
286struct GNUNET_JSON_Specification 272struct GNUNET_JSON_Specification
287GNUNET_JSON_spec_absolute_time_nbo (const char *name, 273GNUNET_JSON_spec_absolute_time_nbo (const char *name,
288 struct GNUNET_TIME_AbsoluteNBO *at); 274 struct GNUNET_TIME_AbsoluteNBO *at);
289 275
290 276
291/** 277/**
@@ -321,7 +307,6 @@ GNUNET_JSON_spec_rsa_signature (const char *name,
321 struct GNUNET_CRYPTO_RsaSignature **sig); 307 struct GNUNET_CRYPTO_RsaSignature **sig);
322 308
323 309
324
325/** 310/**
326 * JSON Specification for GNS Records. 311 * JSON Specification for GNS Records.
327 * 312 *
@@ -346,8 +331,7 @@ GNUNET_JSON_spec_gnsrecord (struct GNUNET_GNSRECORD_Data **rd,
346 * @return json string that encodes @a data 331 * @return json string that encodes @a data
347 */ 332 */
348json_t * 333json_t *
349GNUNET_JSON_from_data (const void *data, 334GNUNET_JSON_from_data (const void *data, size_t size);
350 size_t size);
351 335
352 336
353/** 337/**
@@ -357,7 +341,8 @@ GNUNET_JSON_from_data (const void *data,
357 * @param ptr binary data, sizeof (*ptr) must yield correct size 341 * @param ptr binary data, sizeof (*ptr) must yield correct size
358 * @return json string that encodes @a data 342 * @return json string that encodes @a data
359 */ 343 */
360#define GNUNET_JSON_from_data_auto(ptr) GNUNET_JSON_from_data(ptr, sizeof (*ptr)) 344#define GNUNET_JSON_from_data_auto(ptr) \
345 GNUNET_JSON_from_data (ptr, sizeof (*ptr))
361 346
362 347
363/** 348/**
@@ -417,7 +402,7 @@ GNUNET_JSON_from_rsa_signature (const struct GNUNET_CRYPTO_RsaSignature *sig);
417 * @return corresponding JSON encoding 402 * @return corresponding JSON encoding
418 */ 403 */
419json_t * 404json_t *
420GNUNET_JSON_from_gnsrecord (const char* rname, 405GNUNET_JSON_from_gnsrecord (const char *rname,
421 const struct GNUNET_GNSRECORD_Data *rd, 406 const struct GNUNET_GNSRECORD_Data *rd,
422 unsigned int rd_count); 407 unsigned int rd_count);
423 408
@@ -426,7 +411,8 @@ GNUNET_JSON_from_gnsrecord (const char* rname,
426/** 411/**
427 * Return codes from #GNUNET_JSON_post_parser(). 412 * Return codes from #GNUNET_JSON_post_parser().
428 */ 413 */
429enum GNUNET_JSON_PostResult { 414enum GNUNET_JSON_PostResult
415{
430 /** 416 /**
431 * Parsing successful, JSON result is in `*json`. 417 * Parsing successful, JSON result is in `*json`.
432 */ 418 */
@@ -462,6 +448,7 @@ enum GNUNET_JSON_PostResult {
462 * #GNUNET_JSON_post_parser_callback(). 448 * #GNUNET_JSON_post_parser_callback().
463 * 449 *
464 * @param buffer_max maximum allowed size for the buffer 450 * @param buffer_max maximum allowed size for the buffer
451 * @param connection MHD connection handle (for meta data about the upload)
465 * @param con_cls the closure (will point to a `struct Buffer *`) 452 * @param con_cls the closure (will point to a `struct Buffer *`)
466 * @param upload_data the POST data 453 * @param upload_data the POST data
467 * @param upload_data_size number of bytes in @a upload_data 454 * @param upload_data_size number of bytes in @a upload_data
@@ -470,6 +457,7 @@ enum GNUNET_JSON_PostResult {
470 */ 457 */
471enum GNUNET_JSON_PostResult 458enum GNUNET_JSON_PostResult
472GNUNET_JSON_post_parser (size_t buffer_max, 459GNUNET_JSON_post_parser (size_t buffer_max,
460 struct MHD_Connection *connection,
473 void **con_cls, 461 void **con_cls,
474 const char *upload_data, 462 const char *upload_data,
475 size_t *upload_data_size, 463 size_t *upload_data_size,
diff --git a/src/json/json_mhd.c b/src/json/json_mhd.c
index f095e1011..30b29b88e 100644
--- a/src/json/json_mhd.c
+++ b/src/json/json_mhd.c
@@ -11,7 +11,7 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
@@ -27,12 +27,13 @@
27#include "platform.h" 27#include "platform.h"
28#include "gnunet_json_lib.h" 28#include "gnunet_json_lib.h"
29 29
30
30/** 31/**
31 * Initial size for POST request buffers. Should be big enough to 32 * Initial size for POST request buffers. Should be big enough to
32 * usually not require a reallocation, but not so big that it hurts in 33 * usually not require a reallocation, but not so big that it hurts in
33 * terms of memory use. 34 * terms of memory use.
34 */ 35 */
35#define REQUEST_BUFFER_INITIAL (2*1024) 36#define REQUEST_BUFFER_INITIAL (2 * 1024)
36 37
37 38
38/** 39/**
@@ -74,8 +75,7 @@ buffer_init (struct Buffer *buf,
74 size_t alloc_size, 75 size_t alloc_size,
75 size_t max_size) 76 size_t max_size)
76{ 77{
77 if ( (data_size > max_size) || 78 if ((data_size > max_size) || (alloc_size > max_size))
78 (alloc_size > max_size) )
79 return GNUNET_SYSERR; 79 return GNUNET_SYSERR;
80 if (data_size > alloc_size) 80 if (data_size > alloc_size)
81 alloc_size = data_size; 81 alloc_size = data_size;
@@ -145,6 +145,7 @@ buffer_append (struct Buffer *buf,
145 * #GNUNET_JSON_post_parser_callback(). 145 * #GNUNET_JSON_post_parser_callback().
146 * 146 *
147 * @param buffer_max maximum allowed size for the buffer 147 * @param buffer_max maximum allowed size for the buffer
148 * @param connection MHD connection handle (for meta data about the upload)
148 * @param con_cls the closure (will point to a `struct Buffer *`) 149 * @param con_cls the closure (will point to a `struct Buffer *`)
149 * @param upload_data the POST data 150 * @param upload_data the POST data
150 * @param upload_data_size number of bytes in @a upload_data 151 * @param upload_data_size number of bytes in @a upload_data
@@ -153,6 +154,7 @@ buffer_append (struct Buffer *buf,
153 */ 154 */
154enum GNUNET_JSON_PostResult 155enum GNUNET_JSON_PostResult
155GNUNET_JSON_post_parser (size_t buffer_max, 156GNUNET_JSON_post_parser (size_t buffer_max,
157 struct MHD_Connection *connection,
156 void **con_cls, 158 void **con_cls,
157 const char *upload_data, 159 const char *upload_data,
158 size_t *upload_data_size, 160 size_t *upload_data_size,
@@ -165,12 +167,11 @@ GNUNET_JSON_post_parser (size_t buffer_max,
165 { 167 {
166 /* We are seeing a fresh POST request. */ 168 /* We are seeing a fresh POST request. */
167 r = GNUNET_new (struct Buffer); 169 r = GNUNET_new (struct Buffer);
168 if (GNUNET_OK != 170 if (GNUNET_OK != buffer_init (r,
169 buffer_init (r, 171 upload_data,
170 upload_data, 172 *upload_data_size,
171 *upload_data_size, 173 REQUEST_BUFFER_INITIAL,
172 REQUEST_BUFFER_INITIAL, 174 buffer_max))
173 buffer_max))
174 { 175 {
175 *con_cls = NULL; 176 *con_cls = NULL;
176 buffer_deinit (r); 177 buffer_deinit (r);
@@ -187,10 +188,7 @@ GNUNET_JSON_post_parser (size_t buffer_max,
187 /* We are seeing an old request with more data available. */ 188 /* We are seeing an old request with more data available. */
188 189
189 if (GNUNET_OK != 190 if (GNUNET_OK !=
190 buffer_append (r, 191 buffer_append (r, upload_data, *upload_data_size, buffer_max))
191 upload_data,
192 *upload_data_size,
193 buffer_max))
194 { 192 {
195 /* Request too long */ 193 /* Request too long */
196 *con_cls = NULL; 194 *con_cls = NULL;
@@ -205,10 +203,7 @@ GNUNET_JSON_post_parser (size_t buffer_max,
205 203
206 /* We have seen the whole request. */ 204 /* We have seen the whole request. */
207 205
208 *json = json_loadb (r->data, 206 *json = json_loadb (r->data, r->fill, 0, NULL);
209 r->fill,
210 0,
211 NULL);
212 if (NULL == *json) 207 if (NULL == *json)
213 { 208 {
214 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 209 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,