Make REST POST request timeout configurable
authorAndreas Steffen <andreas.steffen@strongswan.org>
Sat, 31 May 2014 18:29:21 +0000 (20:29 +0200)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Sat, 31 May 2014 19:25:47 +0000 (21:25 +0200)
src/libpts/plugins/imv_swid/imv_swid_agent.c
src/libpts/plugins/imv_swid/imv_swid_rest.c
src/libpts/plugins/imv_swid/imv_swid_rest.h

index 450bf9e..ffff6c6 100644 (file)
@@ -213,9 +213,10 @@ static TNC_Result receive_msg(private_imv_swid_agent_t *this,
                                inventory = attr_cast->get_inventory(attr_cast);
                                tag_id_count = inventory->get_count(inventory);
 
-                               DBG2(DBG_IMV, "received SWID tag ID inventory with %d items "
+                               DBG2(DBG_IMV, "received SWID tag ID inventory with %d item%s "
                                                          "for request %d at eid %d of epoch 0x%08x",
-                                                          tag_id_count, request_id, last_eid, eid_epoch);
+                                                          tag_id_count, (tag_id_count == 1) ? "" : "s",
+                                                          request_id, last_eid, eid_epoch);
 
                                if (request_id == swid_state->get_request_id(swid_state))
                                {
@@ -247,9 +248,10 @@ static TNC_Result receive_msg(private_imv_swid_agent_t *this,
                                inventory = attr_cast->get_inventory(attr_cast);
                                tag_count = inventory->get_count(inventory);
 
-                               DBG2(DBG_IMV, "received SWID tag inventory with %d items for "
+                               DBG2(DBG_IMV, "received SWID tag inventory with %d item%s for "
                                                          "request %d at eid %d of epoch 0x%08x",
-                                                          tag_count, request_id, last_eid, eid_epoch);
+                                                          tag_count, (tag_count == 1) ? "" : "s",
+                                                          request_id, last_eid, eid_epoch);
 
 
                                if (request_id == swid_state->get_request_id(swid_state))
@@ -474,7 +476,7 @@ METHOD(imv_agent_if_t, batch_ending, TNC_Result,
                swid_tag_id_t *tag_id;
                status_t status = SUCCESS;
 
-               if (this->rest_api)
+               if (this->rest_api && (received & IMV_SWID_ATTR_TAG_ID_INV))
                {
                        if (asprintf(&command, "sessions/%d/swid_measurement/",
                                                 session->get_session_id(session, NULL, NULL)) < 0)
@@ -547,7 +549,8 @@ METHOD(imv_agent_if_t, batch_ending, TNC_Result,
                                                                swid_state->get_request_id(swid_state), 0);
 
                                tag_id_count = json_object_array_length(jresponse);
-                               DBG1(DBG_IMV, "%d SWID tag targets", tag_id_count);
+                               DBG1(DBG_IMV, "%d SWID tag target%s", tag_id_count,
+                                                         (tag_id_count == 1) ? "" : "s");
 
                                for (i = 0; i < tag_id_count; i++)
                                {
@@ -675,6 +678,7 @@ imv_agent_if_t *imv_swid_agent_create(const char *name, TNC_IMVID id,
        private_imv_swid_agent_t *this;
        imv_agent_t *agent;
        char *rest_api_uri;
+       u_int rest_api_timeout;
 
        agent = imv_agent_create(name, msg_types, countof(msg_types), id,
                                                         actual_version);
@@ -698,9 +702,11 @@ imv_agent_if_t *imv_swid_agent_create(const char *name, TNC_IMVID id,
 
        rest_api_uri = lib->settings->get_str(lib->settings,
                                                "%s.plugins.imv-swid.rest_api_uri", NULL, lib->ns);
+       rest_api_timeout = lib->settings->get_int(lib->settings,
+                                               "%s.plugins.imv-swid.rest_api_timeout", 120, lib->ns);
        if (rest_api_uri)
        {
-               this->rest_api = imv_swid_rest_create(rest_api_uri);
+               this->rest_api = imv_swid_rest_create(rest_api_uri, rest_api_timeout);
        }
        libpts_init();
 
index 3ad93a3..f40cc49 100644 (file)
@@ -35,6 +35,11 @@ struct private_imv_swid_rest_t {
         */
        char *uri;
 
+       /**
+        * Timeout of REST API connection
+        */
+       u_int timeout;
+
 };
 
 #define HTTP_STATUS_CODE_PRECONDITION_FAILED   412
@@ -45,7 +50,6 @@ METHOD(imv_swid_rest_t, post, status_t,
 {
        struct json_tokener *tokener;
        chunk_t data, response = chunk_empty;
-       u_int timeout = 30;
        status_t status;
        char *uri;
        int code;
@@ -57,7 +61,7 @@ METHOD(imv_swid_rest_t, post, status_t,
        data = chunk_from_str(json_object_to_json_string(jrequest));
 
        status = lib->fetcher->fetch(lib->fetcher, uri, &response,
-                               FETCH_TIMEOUT, timeout,
+                               FETCH_TIMEOUT, this->timeout,
                                FETCH_REQUEST_DATA, data,
                                FETCH_REQUEST_TYPE, "application/json; charset=utf-8",
                                FETCH_REQUEST_HEADER, "Accept: application/json",
@@ -70,8 +74,10 @@ METHOD(imv_swid_rest_t, post, status_t,
        {
                return  SUCCESS;
        }
+
        if (code != HTTP_STATUS_CODE_PRECONDITION_FAILED || !response.ptr)
        {
+               DBG2(DBG_IMV, "REST http request failed with status code: %d", code);
                return FAILED;
        }
 
@@ -97,7 +103,7 @@ METHOD(imv_swid_rest_t, destroy, void,
 /**
  * Described in header.
  */
-imv_swid_rest_t *imv_swid_rest_create(char *uri)
+imv_swid_rest_t *imv_swid_rest_create(char *uri, u_int timeout)
 {
        private_imv_swid_rest_t *this;
 
@@ -107,6 +113,7 @@ imv_swid_rest_t *imv_swid_rest_create(char *uri)
                        .destroy = _destroy,
                },
                .uri = strdup(uri),
+               .timeout = timeout,
        );
 
        return &this->public;
index b68f72f..93e3d6a 100644 (file)
@@ -56,7 +56,8 @@ struct imv_swid_rest_t {
  * Create an imv_swid_rest_t instance
  *
  * @param uri                  REST URI (http://username:password@hostname[:port]/api/)
+ * @param timeout              Timeout of the REST connection
  */
-imv_swid_rest_t* imv_swid_rest_create(char *uri);
+imv_swid_rest_t* imv_swid_rest_create(char *uri, u_int timeout);
 
 #endif /** IMV_SWID_REST_H_ @}*/