33 #include <libcjson/cJSON.h> 34 #include <libmediaprocsutils/uri_parser.h> 35 #include <libmediaprocsutils/log.h> 36 #include <libmediaprocsutils/stat_codes.h> 37 #include <libmediaprocsutils/check_utils.h> 52 #define RESPONSE_FMT "{\"code\":%d,\"status\":%s,\"message\":%s,\"data\":%s}" 57 #define URL_HAS(NEEDLE) \ 58 (url!= NULL && strstr(url, NEEDLE)!= NULL) 63 #define URL_METHOD_IS(TAG) \ 64 (request_method!= NULL && \ 65 strncmp(request_method, TAG, strlen(TAG))== 0) 70 #define HTTP_OK "\"OK\"" 71 #define HTTP_CREATED "\"Created\"" 72 #define HTTP_NOCONTENT "\"No Content\"" 73 #define HTTP_NOTFOUND "\"Not Found\"" 74 #define HTTP_NOTMODIF "\"Not Modified\"" 75 #define HTTP_CREATED "\"Created\"" 76 #define HTTP_CONFLICT "\"Conflict\"" 83 const char *query_string,
const char *request_method,
char *content,
84 size_t content_len,
char **ref_str_response)
87 const char *http_status_str, *msg_str;
89 int proc_id, ret_code, end_code= STAT_ERROR;
90 char *proc_name_str= NULL, *response_str= NULL, *data_obj_str= NULL;
97 CHECK_DO(url!= NULL,
return STAT_ERROR);
98 CHECK_DO(request_method!= NULL,
return STAT_ERROR);
99 CHECK_DO(ref_str_response!= NULL,
return STAT_ERROR);
106 *ref_str_response= NULL;
110 end_code= STAT_ENOTFOUND;
126 if(query_string== NULL ||
127 (proc_name_str= uri_parser_query_str_get_value(
"proc_name",
128 query_string))== NULL) {
129 end_code= STAT_EINVAL;
132 end_code=
procs_opt(procs_ctx,
"PROCS_POST", proc_name_str,
133 query_string, &data_obj_str);
136 end_code=
procs_opt(procs_ctx,
"PROCS_GET", &data_obj_str, NULL);
138 end_code= STAT_ENOTFOUND;
140 }
else if(
URL_HAS(
"/procs/")) {
145 ret_code= uri_parser_get_id_from_rest_url(url,
"/procs/",
146 (
long long*)&aux_id);
147 proc_id= (int)aux_id;
148 if(proc_id< -1 || ret_code!= STAT_SUCCESS) {
149 end_code= STAT_ENOTFOUND;
154 end_code=
procs_opt(procs_ctx,
"PROCS_ID_PUT", proc_id,
157 end_code=
procs_opt(procs_ctx,
"PROCS_ID_GET", proc_id,
162 end_code= STAT_ENOTFOUND;
164 end_code= STAT_ENOTFOUND;
183 http_code= 201; http_status_str= HTTP_CREATED;
185 http_code= 200; http_status_str=
HTTP_OK;
190 http_code= 204; http_status_str= HTTP_NOCONTENT;
192 http_code= 404; http_status_str= HTTP_NOTFOUND;
195 case STAT_NOTMODIFIED:
198 http_code= 304; http_status_str= HTTP_NOTMODIF;
200 http_code= 204; http_status_str= HTTP_NOCONTENT;
202 http_code= 409; http_status_str= HTTP_CONFLICT;
204 http_code= 404; http_status_str= HTTP_NOTFOUND;
209 http_code= 404; http_status_str= HTTP_NOTFOUND;
214 msg_str= stat_codes_get_description(end_code);
216 response_size+= http_status_str!= NULL? strlen(http_status_str): 4;
217 response_size+= msg_str!= NULL? strlen(msg_str): 4;
218 response_size+= data_obj_str!= NULL? strlen(data_obj_str): 4;
219 ASSERT((response_str= (
char*)malloc(response_size))!= NULL);
220 if(response_str!= NULL) {
221 snprintf(response_str, response_size,
RESPONSE_FMT, http_code,
222 http_status_str!= NULL? http_status_str:
"null",
223 msg_str!= NULL && strlen(msg_str)> 0? msg_str:
"null",
224 data_obj_str!= NULL? data_obj_str:
"null");
225 *ref_str_response= response_str;
229 if(response_str!= NULL)
231 if(proc_name_str!= NULL)
233 if(data_obj_str!= NULL)
Generic processors (PROC) module.
int procs_opt(procs_ctx_t *procs_ctx, const char *tag,...)
#define URL_METHOD_IS(TAG)
Processors (PROCS) module HTTP API adaptation layer.
#define CHECK_DO(COND, ACTION)
int procs_api_http_req_handler(procs_ctx_t *procs_ctx, const char *url, const char *query_string, const char *request_method, char *content, size_t content_len, char **ref_str_response)