imv-swima: Created SWIMA IMV plugin
[strongswan.git] / src / libimcv / plugins / imv_swima / imv_swima_rest.c
1 /*
2 * Copyright (C) 2017 Andreas Steffen
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #define _GNU_SOURCE
17 #include <stdio.h>
18
19 #include "imv_swima_rest.h"
20
21 typedef struct private_imv_swima_rest_t private_imv_swima_rest_t;
22
23 /**
24 * Private data of an imv_swima_rest_t object.
25 */
26 struct private_imv_swima_rest_t {
27
28 /**
29 * Public members of imv_swima_rest_t
30 */
31 imv_swima_rest_t public;
32
33 /**
34 * URI of REST API
35 */
36 char *uri;
37
38 /**
39 * Timeout of REST API connection
40 */
41 u_int timeout;
42
43 };
44
45 #define HTTP_STATUS_CODE_PRECONDITION_FAILED 412
46
47 METHOD(imv_swima_rest_t, post, status_t,
48 private_imv_swima_rest_t *this, char *command, json_object *jrequest,
49 json_object **jresponse)
50 {
51 struct json_tokener *tokener;
52 chunk_t data, response = chunk_empty;
53 status_t status;
54 char *uri;
55 int code;
56
57 if (asprintf(&uri, "%s%s",this->uri, command) < 0)
58 {
59 return FAILED;
60 }
61 data = chunk_from_str((char*)json_object_to_json_string(jrequest));
62
63 status = lib->fetcher->fetch(lib->fetcher, uri, &response,
64 FETCH_TIMEOUT, this->timeout,
65 FETCH_REQUEST_DATA, data,
66 FETCH_REQUEST_TYPE, "application/json; charset=utf-8",
67 FETCH_REQUEST_HEADER, "Accept: application/json",
68 FETCH_REQUEST_HEADER, "Expect:",
69 FETCH_RESPONSE_CODE, &code,
70 FETCH_END);
71 free(uri);
72
73 if (status != SUCCESS)
74 {
75 if (code != HTTP_STATUS_CODE_PRECONDITION_FAILED || !response.ptr)
76 {
77 DBG2(DBG_IMV, "REST http request failed with status code: %d", code);
78 status = FAILED;
79 }
80 else
81 {
82 if (jresponse)
83 {
84 /* Parse HTTP response into a JSON object */
85 tokener = json_tokener_new();
86 *jresponse = json_tokener_parse_ex(tokener, response.ptr,
87 response.len);
88 json_tokener_free(tokener);
89 }
90 status = NEED_MORE;
91 }
92 }
93 free(response.ptr);
94
95 return status;
96 }
97
98 METHOD(imv_swima_rest_t, destroy, void,
99 private_imv_swima_rest_t *this)
100 {
101 free(this->uri);
102 free(this);
103 }
104
105 /**
106 * Described in header.
107 */
108 imv_swima_rest_t *imv_swima_rest_create(char *uri, u_int timeout)
109 {
110 private_imv_swima_rest_t *this;
111
112 INIT(this,
113 .public = {
114 .post = _post,
115 .destroy = _destroy,
116 },
117 .uri = strdup(uri),
118 .timeout = timeout,
119 );
120
121 return &this->public;
122 }
123
124