Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / include / gnunet_rest_lib.h
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2010-2015 GNUnet e.V.
4
5       GNUnet is free software: you can redistribute it and/or modify it
6       under the terms of the GNU Affero General Public License as published
7       by the Free Software Foundation, either version 3 of the License,
8       or (at your option) any later version.
9
10       GNUnet is distributed in the hope that it will be useful, but
11       WITHOUT ANY WARRANTY; without even the implied warranty of
12       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13       Affero General Public License for more details.
14      
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/>.
17  */
18
19 /**
20  * @author Martin Schanzenbach
21  *
22  * @file
23  * API for helper library to parse/create REST
24  *
25  * @defgroup rest  REST library
26  * Helper library to parse/create REST
27  * @{
28  */
29 #ifndef GNUNET_REST_LIB_H
30 #define GNUNET_REST_LIB_H
31
32 #include "gnunet_util_lib.h"
33 #include <microhttpd.h>
34
35 #define GNUNET_REST_HANDLER_END {NULL, NULL, NULL}
36
37 struct GNUNET_REST_RequestHandle
38 {
39   /**
40    * Map of url parameters
41    */
42   struct GNUNET_CONTAINER_MultiHashMap *url_param_map;
43
44   /**
45    * Map of headers
46    */
47   struct GNUNET_CONTAINER_MultiHashMap *header_param_map;
48
49   /**
50    * The HTTP method as MHD value (see microhttpd.h)
51    */
52   const char *method;
53
54   /**
55    * The url as string
56    */
57   const char *url;
58
59   /**
60    * The POST data
61    */
62   const char *data;
63
64   /**
65    * The POST data size
66    */
67   size_t data_size;
68 };
69
70 struct GNUNET_REST_RequestHandlerError
71 {
72   int error_code;
73   char* error_text;
74 };
75
76 struct GNUNET_REST_RequestHandler
77 {
78   /**
79    * Http method to handle
80    */
81   const char *method;
82
83   /**
84    * Namespace to handle
85    */
86   const char *namespace;
87
88   /**
89    * callback handler
90    */
91   void (*proc) (struct GNUNET_REST_RequestHandle *handle,
92                 const char *url,
93                 void *cls);
94
95 };
96
97
98 /**
99  * Iterator called on obtained result for a REST result.
100  *
101  * @param cls closure
102  * @param resp the response
103  * @param status status code (HTTP)
104  */
105 typedef void (*GNUNET_REST_ResultProcessor) (void *cls,
106                                              struct MHD_Response *resp,
107                                              int status);
108
109 /**
110  * Check if namespace is in URL.
111  *
112  * @param url URL to check
113  * @param namespace namespace to check against
114  * @return GNUNET_YES if namespace matches
115  */
116 int
117 GNUNET_REST_namespace_match (const char *url, const char *namespace);
118
119 /**
120  * Create REST MHD response
121  *
122  * @param data result
123  * @return MHD response
124  */
125  struct MHD_Response*
126 GNUNET_REST_create_response (const char *data);
127
128
129 int
130 GNUNET_REST_handle_request (struct GNUNET_REST_RequestHandle *conn,
131                             const struct GNUNET_REST_RequestHandler *handlers,
132                             struct GNUNET_REST_RequestHandlerError *err,
133                             void *cls);
134
135
136 #endif
137
138 /** @} */  /* end of group */