kore

An easy to use, scalable and secure web application framework for writing web APIs in C.
Commits | Files | Refs | README | LICENSE | git clone https://git.kore.io/kore.git

jsonrpc.h (2729B)



      1 /*
      2  * Copyright (c) 2016 Raphaƫl Monrouzeau <raphael.monrouzeau@gmail.com>
      3  *
      4  * Permission to use, copy, modify, and distribute this software for any
      5  * purpose with or without fee is hereby granted, provided that the above
      6  * copyright notice and this permission notice appear in all copies.
      7  *
      8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
      9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     15  */
     16 
     17 #if !defined(KORE_NO_HTTP)
     18 
     19 #ifndef __H_JSONRPC_H
     20 #define __H_JSONRPC_H
     21 
     22 #if defined(__cplusplus)
     23 extern "C" {
     24 #endif
     25 
     26 /* JSON RPC request handling log entry. */
     27 struct jsonrpc_log
     28 {
     29 	char			*msg;
     30 	struct jsonrpc_log	*next, *prev;
     31 	int			lvl;
     32 };
     33 
     34 /* JSON RPC request. */
     35 struct jsonrpc_request
     36 {
     37 	struct jsonrpc_log	log;
     38 	struct kore_buf		buf;
     39 	struct http_request	*http;
     40 	yajl_gen		gen;
     41 	yajl_val		json;
     42 	yajl_val		id;
     43 	char			*method;
     44 	yajl_val		params;
     45 	unsigned int		flags;
     46 	int			log_levels;
     47 };
     48 
     49 #define YAJL_GEN_CONST_STRING(CTX, STR)	\
     50 	yajl_gen_string((CTX), (unsigned char *)(STR), sizeof (STR) - 1)
     51 
     52 #define YAJL_GEN_CONST_NUMBER(CTX, STR)	\
     53 	yajl_gen_number((CTX), (unsigned char *)(STR), sizeof (STR) - 1)
     54 
     55 #define YAJL_GEN_KO(OPERATION)	\
     56 	((OPERATION) != yajl_gen_status_ok)
     57 
     58 enum jsonrpc_error_code
     59 {
     60 #define JSONRPC_PARSE_ERROR_MSG		"Parse error"
     61 	JSONRPC_PARSE_ERROR		= -32700,
     62 #define JSONRPC_INVALID_REQUEST_MSG	"Invalid Request"
     63 	JSONRPC_INVALID_REQUEST		= -32600,
     64 #define JSONRPC_METHOD_NOT_FOUND_MSG	"Method not found"
     65 	JSONRPC_METHOD_NOT_FOUND	= -32601,
     66 #define JSONRPC_INVALID_PARAMS_MSG	"Invalid params"
     67 	JSONRPC_INVALID_PARAMS		= -32602,
     68 #define JSONRPC_INTERNAL_ERROR_MSG	"Internal error"
     69 	JSONRPC_INTERNAL_ERROR		= -32603,
     70 #define JSONRPC_SERVER_ERROR_MSG	"Server error"
     71 	JSONRPC_SERVER_ERROR		= -32000,
     72 #define JSONRPC_LIMIT_REACHED_MSG	"Limit reached"
     73 	JSONRPC_LIMIT_REACHED		= -31997
     74 };
     75 
     76 void	jsonrpc_log(struct jsonrpc_request *, int, const char *, ...)
     77 	    __attribute__((format (printf, 3, 4)));
     78 int	jsonrpc_read_request(struct http_request *, struct jsonrpc_request *);
     79 void	jsonrpc_destroy_request(struct jsonrpc_request *);
     80 int	jsonrpc_error(struct jsonrpc_request *, int, const char *);
     81 int	jsonrpc_result(struct jsonrpc_request *,
     82 	    int (*)(struct jsonrpc_request *, void *), void *);
     83 #if defined(__cplusplus)
     84 }
     85 #endif
     86 #endif /* !__H_JSONRPC_H */
     87 
     88 #endif /* ! KORE_NO_HTTP */