Selaa lähdekoodia

http_client Extend API with function to retrieve content-type of previous
transaction in the same process

Olle E. Johansson 9 vuotta sitten
vanhempi
commit
aa9d74c08d

+ 14 - 0
modules/http_client/README

@@ -90,6 +90,7 @@ Hugh Waite
 
               1.2. int http_connection_exists(str *connection)
               1.3. http_query(msg, url, dest, post)
+              1.4. http_get_content_type(str connection)
 
    List of Examples
 
@@ -753,12 +754,14 @@ Chapter 2. Developer Guide
 
         1.2. int http_connection_exists(str *connection)
         1.3. http_query(msg, url, dest, post)
+        1.4. http_get_content_type(str connection)
 
 1. Available Functions
 
    1.1. http_connect(msg, connection, url, result, content_type, post)
    1.2. int http_connection_exists(str *connection)
    1.3. http_query(msg, url, dest, post)
+   1.4. http_get_content_type(str connection)
 
 1.1. http_connect(msg, connection, url, result, content_type, post)
 
@@ -815,3 +818,14 @@ Chapter 2. Developer Guide
        the http_client module and must be freed by the caller.
      * const char *post
        If not null, the data will be posted to the URL.
+
+1.4. http_get_content_type(str connection)
+
+   Get the content-type of the last result for this connection. This will
+   be something like "text/html" for html or "application/json" for JSON
+   data.
+
+   Result will be a pointer to a char string (char *). This is per
+   process, so the connection will have to be done in the same process.
+   Returns a NULL pointer if the connection does not exist or there's no
+   previous connection data delivered.

+ 2 - 0
modules/http_client/curl_api.c

@@ -1,5 +1,6 @@
 /*
  * Copyright (C) 2015 Hugh Waite
+ * Copyright (C) 2016 Edvina AB, Olle E. Johansson
  *
  * This file is part of Kamailio, a free SIP server.
  *
@@ -31,6 +32,7 @@ int bind_httpc_api(httpc_api_t *api)
 	api->http_connect = curl_con_query_url;
 	api->http_query = http_query;
 	api->http_connection_exists = http_connection_exists;
+	api->http_get_content_type = http_get_content_type;
 
 	return 0;
 }

+ 2 - 0
modules/http_client/curl_api.h

@@ -35,12 +35,14 @@
 typedef int (*httpcapi_httpconnect_f)(struct sip_msg *msg, const str *connection, const str* _url, str* _result, const char *contenttype, const str* _post);
 typedef int (*httpcapi_httpquery_f)(struct sip_msg* _m, char* _url, str* _dst, char* _post);
 typedef int (*httpcapi_curlcon_exists_f)(str* _name);
+typedef char * (*httpcapi_res_content_type_f)(const str* _name);
 
 
 typedef struct httpc_api {
 	httpcapi_httpconnect_f	http_connect;
 	httpcapi_httpquery_f	http_query;
 	httpcapi_curlcon_exists_f	http_connection_exists;
+	httpcapi_res_content_type_f	http_get_content_type;
 } httpc_api_t;
 
 typedef int (*bind_httpc_api_f)(httpc_api_t *api);

+ 15 - 0
modules/http_client/doc/http_client_devel.xml

@@ -124,6 +124,21 @@
 			</listitem>
 			</itemizedlist>
 		</section>
+		<section>
+			<title>
+			<function moreinfo="none">http_get_content_type(str connection)</function>
+			</title>
+			<para>
+			Get the content-type of the last result for this connection. This will
+			be something like "text/html" for html or "application/json" for JSON data.
+			</para>
+			<para>
+			Result will be a pointer to a char string (char *). This is per process,
+			so the connection will have to be done in the same process. Returns a NULL
+			pointer if the connection does not exist or there's no previous connection
+			data delivered.
+			</para>
+		</section>
 
 
 	</section>

+ 27 - 0
modules/http_client/functions.c

@@ -593,3 +593,30 @@ int http_query(struct sip_msg* _m, char* _url, str* _dst, char* _post)
 
 	return res;
 }
+
+
+char *http_get_content_type(const str *connection)
+{
+	curl_con_t *conn = NULL;
+	curl_con_pkg_t *pconn = NULL;
+	str rval;
+
+	/* Find connection if it exists */
+	if (!connection) {
+		LM_ERR("No cURL connection specified\n");
+		return NULL;
+	}
+	LM_DBG("******** CURL Connection %.*s\n", connection->len, connection->s);
+	conn = curl_get_connection((str*)connection);
+	if (conn == NULL) {
+		LM_ERR("No cURL connection found: %.*s\n", connection->len, connection->s);
+		return NULL;
+	}
+	pconn = curl_get_pkg_connection(conn);
+	if (pconn == NULL) {
+		LM_ERR("No cURL connection data found: %.*s\n", connection->len, connection->s);
+		return NULL;
+	}
+
+	return pconn->result_content_type;
+}

+ 2 - 0
modules/http_client/functions.h

@@ -51,4 +51,6 @@ int curl_get_redirect(struct sip_msg* _m, const str *connection, str* result);
 int http_query(struct sip_msg* _m, char* _url, str* _dst, char* _post);
 
 
+char *http_get_content_type(const str *connection);
+
 #endif /* CURL_FUNCTIONS_H */