libwebsockets
Lightweight C library for HTML5 websockets
Toggle main menu visibility
Loading...
Searching...
No Matches
lws-jrpc.h
Go to the documentation of this file.
1
/*
2
* libwebsockets - small server side websockets and web server implementation
3
*
4
* Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
5
*
6
* Permission is hereby granted, free of charge, to any person obtaining a copy
7
* of this software and associated documentation files (the "Software"), to
8
* deal in the Software without restriction, including without limitation the
9
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10
* sell copies of the Software, and to permit persons to whom the Software is
11
* furnished to do so, subject to the following conditions:
12
*
13
* The above copyright notice and this permission notice shall be included in
14
* all copies or substantial portions of the Software.
15
*
16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22
* IN THE SOFTWARE.
23
*
24
*
25
* This is a JSON-RPC parser and state management implementation that's:
26
*
27
* - Lightweight, it uses lws LEJP JSON stream parser for requests, responses,
28
* and user-defined parameter objects
29
*
30
* - Stateful... you can give it sequential input buffers randomly fragmented
31
* and it will complete when it has enough
32
*
33
* - Asynchronous... response processing can return to the event loop both
34
* while the RX is still coming and after it's all received before forming
35
* the response, eg, because it's querying on a remote connection to get the
36
* response data. Any number of RPCs can be either in flight or waiting for
37
* response processing to complete before responding.
38
*
39
* - Supports "version" extension
40
*
41
* - allows binding different method names to different callbacks
42
*
43
* - Supports both client and server roles, eg, can parse both requests and
44
* responses
45
*
46
* - No support for batch. Batching is not widely used because it doesn't
47
* add anything for the vast bulk of cases compared to sending n requests.
48
*
49
* This handles client and server RX and transaction state, creating a callback
50
* when parameters can be parsed and all of the request or notification is
51
* done.
52
*
53
* Producing JSON is usually simpler and more compact than expressing it as an
54
* object model, ie often a response can be completely formed in a single
55
* lws_snprintf(). Response JSON must be buffered on heap until the method
56
* callback is called with NULL / 0 buf len indicating that the incoming request
57
* has completed parsing.
58
*
59
*/
60
61
/* these are opaque */
62
63
struct
lws_jrpc_obj;
64
struct
lws_jrpc;
65
66
typedef
enum
{
67
LJRPC_CBRET_CONTINUE
,
68
LJRPC_CBRET_WANT_TO_EMIT
,
69
LJRPC_CBRET_FINISHED
,
70
LJRPC_CBRET_FAILED
71
}
lws_jrpc_cb_return_t
;
72
73
/*
74
* method name to lejp parsing handler map
75
*
76
* The handler is a lejp callback over the request's params; it is given
77
* LEJPCB_USER_START once the whole request is parsed and the id known. A
78
* handler that completes asynchronously, keeping a pointer to the request
79
* object, is given LEJPCB_USER_DESTROYING (with the same lejp ctx) when the
80
* object is destroyed under it, by lws_jrpc_obj_destroy() or the jrpc going
81
* away: it must drop its pointer then, nothing of the object survives the
82
* callback.
83
*/
84
85
typedef
struct
lws_jrpc_method
{
86
const
char
*
method_name
;
87
const
char
*
const
*
paths
;
88
lejp_callback
cb
;
89
int
count_paths
;
90
}
lws_jrpc_method_t
;
91
92
/*
93
* Boilerplate for forming correct requests
94
*/
95
96
/* Boilerplate to start a request */
97
#define LWSJRPCBP_REQ_START_S "{\"jsonrpc\":\"2.0\",\"method\":\"%s\""
98
/* Boilerplate to start parameters (params are left freeform for user) */
99
#define LWSJRPCBP_REQ_VERSION_S ",\"version\":\"%s\""
100
/* Boilerplate to start parameters (params are left freeform for user) */
101
#define LWSJRPCBP_REQ_PARAMS ",\"params\":"
102
/* Boilerplate to complete the result object */
103
#define LWSJRPCBP_REQ_NOTIF_END "}"
104
/* Boilerplate to complete the result object */
105
#define LWSJRPCBP_REQ_ID_END_S ",\"id\":%s}"
106
107
/*
108
* Boilerplate for forming correct responses
109
*/
110
111
/* Boilerplate to start a result */
112
#define LWSJRPCBP_RESP_RESULT "{\"jsonrpc\":\"2.0\",\"result\":"
113
/* Boilerplate to complete the result object */
114
#define LWSJRPCBP_RESP_ID_END_S ",\"id\":%s}"
115
116
/* Boilerplate to form an error */
117
#define LWSJRPCBP_RESP_ERROR_D "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":%d"
118
/* optional */
119
#define LWSJRPCBP_RESP_ERROR_MSG_S ",\"message\":\"%s\""
120
/* optional */
121
#define LWSJRPCBP_RESP_ERROR_DATA ",\"data\":"
122
/* required */
123
#define LWSJRPCBP_RESP_ERROR_END "}"
124
125
/*
126
* JSONRPC Well-known Errors
127
*/
128
129
enum
{
130
LWSJRPCE__NO_ERROR
= 0,
131
132
LWSJRPCWKE__PARSE_ERROR
= -32700,
/* invalid JSON */
133
LWSJRPCWKE__INVALID_REQUEST
= -32600,
/* not valid JSONRPC object */
134
LWSJRPCWKE__METHOD_NOT_FOUND
= -32601,
/* method not supported */
135
LWSJRPCWKE__INVALID_PARAMS
= -32602,
/* parameters are invalid */
136
LWSJRPCWKE__INTERNAL_ERROR
= -32603,
/* internal JSONRPC error */
137
LWSJRPCWKE__SERVER_ERROR_FIRST
= -32000,
/* implementation-defined...*/
138
LWSJRPCWKE__SERVER_ERROR_LAST
= -32099,
/* ... server errors range */
139
140
LWSJRPCE__INVALID_MEMBERS
= -31000,
/* reponse membs in req, vv */
141
};
142
143
enum
{
144
LWSJRPC_PARSE_REQUEST
,
145
LWSJRPC_PARSE_RESPONSE
146
};
147
148
/*
149
* APIs for the opaque JRPC request object
150
*/
151
173
174
LWS_VISIBLE
LWS_EXTERN
int
175
lws_jrpc_obj_parse
(
struct
lws_jrpc *jrpc,
int
type,
void
*opaque,
176
const
char
*buf,
size_t
l,
struct
lws_jrpc_obj **r);
177
178
/*
179
* lws_jrpc_obj_destroy() - detach and destroy a JRPC request or response
180
*
181
* \param _r: pointer to pointer to JRPC request to detach and free
182
*
183
* Detaches the req from its JRPC context and frees it and any internal
184
* allocations.
185
*/
186
LWS_VISIBLE
LWS_EXTERN
void
187
lws_jrpc_obj_destroy
(
struct
lws_jrpc_obj **_r);
188
189
/*
190
* lws_jrpc_obj_get_opaque() - retreive the opaque pointer bound to the req
191
*
192
* \param r: pointer to pointer to JRPC request
193
*
194
* Returns the opaque pointer for a req given when it was parsed / created.
195
*/
196
LWS_VISIBLE
LWS_EXTERN
void
*
197
lws_jrpc_obj_get_opaque
(
const
struct
lws_jrpc_obj *r);
198
199
/*
200
* lws_jrpc_obj_id() - retreive the object's id string
201
*
202
* \param r: pointer to pointer to JRPC object
203
*
204
* Returns a pointer to a correctly-typed id for use in a response; if a string,
205
* then it is already quoted, if an int or null then it's provided without
206
* quotes.
207
*/
208
LWS_VISIBLE
LWS_EXTERN
const
char
*
209
lws_jrpc_obj_id
(
const
struct
lws_jrpc_obj *r);
210
211
212
/*
213
* APIs for the opaque JRPC context
214
*/
215
225
LWS_VISIBLE
LWS_EXTERN
struct
lws_jrpc *
226
lws_jrpc_create
(
const
lws_jrpc_method_t
*methods,
void
*opaque);
227
228
/*
229
* lws_jrpc_destroy() - destroy an allocated JRPC context
230
*
231
* \param jrpc: pointer to pointer to jrpc to destroy
232
*
233
* Destroys any ongoing reqs in the JRPC and then destroys the JRPC and sets the
234
* given pointer to NULL.
235
*/
236
LWS_VISIBLE
LWS_EXTERN
void
237
lws_jrpc_destroy
(
struct
lws_jrpc **jrpc);
LWS_EXTERN
#define LWS_EXTERN
Definition
libwebsockets.h:327
LWS_VISIBLE
#define LWS_VISIBLE
Definition
libwebsockets.h:322
lws_jrpc_obj_id
LWS_VISIBLE LWS_EXTERN const char * lws_jrpc_obj_id(const struct lws_jrpc_obj *r)
lws_jrpc_cb_return_t
lws_jrpc_cb_return_t
Definition
lws-jrpc.h:66
LJRPC_CBRET_CONTINUE
@ LJRPC_CBRET_CONTINUE
Definition
lws-jrpc.h:67
LJRPC_CBRET_FAILED
@ LJRPC_CBRET_FAILED
Definition
lws-jrpc.h:70
LJRPC_CBRET_WANT_TO_EMIT
@ LJRPC_CBRET_WANT_TO_EMIT
Definition
lws-jrpc.h:68
LJRPC_CBRET_FINISHED
@ LJRPC_CBRET_FINISHED
Definition
lws-jrpc.h:69
lws_jrpc_create
LWS_VISIBLE LWS_EXTERN struct lws_jrpc * lws_jrpc_create(const lws_jrpc_method_t *methods, void *opaque)
lws_jrpc_method::count_paths
int count_paths
Definition
lws-jrpc.h:89
lws_jrpc_obj_get_opaque
LWS_VISIBLE LWS_EXTERN void * lws_jrpc_obj_get_opaque(const struct lws_jrpc_obj *r)
lws_jrpc_method_t
struct lws_jrpc_method lws_jrpc_method_t
lws_jrpc_method::method_name
const char * method_name
Definition
lws-jrpc.h:86
lws_jrpc_method::cb
lejp_callback cb
Definition
lws-jrpc.h:88
lws_jrpc_obj_destroy
LWS_VISIBLE LWS_EXTERN void lws_jrpc_obj_destroy(struct lws_jrpc_obj **_r)
lws_jrpc_destroy
LWS_VISIBLE LWS_EXTERN void lws_jrpc_destroy(struct lws_jrpc **jrpc)
LWSJRPC_PARSE_RESPONSE
@ LWSJRPC_PARSE_RESPONSE
Definition
lws-jrpc.h:145
LWSJRPC_PARSE_REQUEST
@ LWSJRPC_PARSE_REQUEST
Definition
lws-jrpc.h:144
lws_jrpc_obj_parse
LWS_VISIBLE LWS_EXTERN int lws_jrpc_obj_parse(struct lws_jrpc *jrpc, int type, void *opaque, const char *buf, size_t l, struct lws_jrpc_obj **r)
LWSJRPCE__NO_ERROR
@ LWSJRPCE__NO_ERROR
Definition
lws-jrpc.h:130
LWSJRPCWKE__SERVER_ERROR_LAST
@ LWSJRPCWKE__SERVER_ERROR_LAST
Definition
lws-jrpc.h:138
LWSJRPCE__INVALID_MEMBERS
@ LWSJRPCE__INVALID_MEMBERS
Definition
lws-jrpc.h:140
LWSJRPCWKE__INVALID_REQUEST
@ LWSJRPCWKE__INVALID_REQUEST
Definition
lws-jrpc.h:133
LWSJRPCWKE__INVALID_PARAMS
@ LWSJRPCWKE__INVALID_PARAMS
Definition
lws-jrpc.h:135
LWSJRPCWKE__SERVER_ERROR_FIRST
@ LWSJRPCWKE__SERVER_ERROR_FIRST
Definition
lws-jrpc.h:137
LWSJRPCWKE__INTERNAL_ERROR
@ LWSJRPCWKE__INTERNAL_ERROR
Definition
lws-jrpc.h:136
LWSJRPCWKE__METHOD_NOT_FOUND
@ LWSJRPCWKE__METHOD_NOT_FOUND
Definition
lws-jrpc.h:134
LWSJRPCWKE__PARSE_ERROR
@ LWSJRPCWKE__PARSE_ERROR
Definition
lws-jrpc.h:132
lws_jrpc_method::paths
const char *const * paths
Definition
lws-jrpc.h:87
lws_jrpc_method
Definition
lws-jrpc.h:85
lejp_callback
signed char(* lejp_callback)(struct lejp_ctx *ctx, char reason)
Definition
lws-lejp.h:191
include
libwebsockets
lws-jrpc.h
Generated on
for libwebsockets by
1.19.0