0002-parsers-Generalise-input-to-array-of-gchar.patch 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. From 3c095487e3a6c14f2900762c18c6e2170d22a283 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?=C4=90o=C3=A0n=20Tr=E1=BA=A7n=20C=C3=B4ng=20Danh?=
  3. <congdanhqx@gmail.com>
  4. Date: Fri, 1 Mar 2024 21:56:34 +0700
  5. Subject: [PATCH 2/5] parsers: Generalise input to array of gchar
  6. In a later change, we will move to libsoup-3.0, which doesn't expose
  7. `response_body' in SoupMessage.
  8. Prepare for that move.
  9. Upstream-Status: Backport [https://github.com/xfce-mirror/xfce4-weather-plugin/commit/3c095487e3a6c14f2900762c18c6e2170d22a283]
  10. Signed-off-by: Khem Raj <raj.khem@gmail.com>
  11. ---
  12. panel-plugin/weather-config.c | 18 +++++++++++++++--
  13. panel-plugin/weather-parsers.c | 36 ++++++++++++++++++----------------
  14. panel-plugin/weather-parsers.h | 7 +++----
  15. panel-plugin/weather-search.c | 18 +++++++++++++++--
  16. panel-plugin/weather.c | 26 ++++++++++++++++++++----
  17. 5 files changed, 76 insertions(+), 29 deletions(-)
  18. diff --git a/panel-plugin/weather-config.c b/panel-plugin/weather-config.c
  19. index 2645408..19fa1d8 100644
  20. --- a/panel-plugin/weather-config.c
  21. +++ b/panel-plugin/weather-config.c
  22. @@ -245,6 +245,13 @@ cb_lookup_altitude(SoupSession *session,
  23. xfceweather_dialog *dialog = (xfceweather_dialog *) user_data;
  24. xml_altitude *altitude;
  25. gdouble alt = 0;
  26. + const gchar *body = NULL;
  27. + gsize len = 0;
  28. +
  29. + if (G_LIKELY(msg->response_body && msg->response_body->data)) {
  30. + body = msg->response_body->data;
  31. + len = msg->response_body->length;
  32. + }
  33. if (global_dialog == NULL) {
  34. weather_debug("%s called after dialog was destroyed", G_STRFUNC);
  35. @@ -252,7 +259,7 @@ cb_lookup_altitude(SoupSession *session,
  36. }
  37. altitude = (xml_altitude *)
  38. - parse_xml_document(msg, (XmlParseFunc) parse_altitude);
  39. + parse_xml_document(body, len, (XmlParseFunc) parse_altitude);
  40. if (altitude) {
  41. alt = string_to_double(altitude->altitude, -9999);
  42. @@ -274,6 +281,13 @@ cb_lookup_timezone(SoupSession *session,
  43. {
  44. xfceweather_dialog *dialog = (xfceweather_dialog *) user_data;
  45. xml_timezone *xml_tz;
  46. + const gchar *body = NULL;
  47. + gsize len = 0;
  48. +
  49. + if (G_LIKELY(msg->response_body && msg->response_body->data)) {
  50. + body = msg->response_body->data;
  51. + len = msg->response_body->length;
  52. + }
  53. if (global_dialog == NULL) {
  54. weather_debug("%s called after dialog was destroyed", G_STRFUNC);
  55. @@ -281,7 +295,7 @@ cb_lookup_timezone(SoupSession *session,
  56. }
  57. xml_tz = (xml_timezone *)
  58. - parse_xml_document(msg, (XmlParseFunc) parse_timezone);
  59. + parse_xml_document(body, len, (XmlParseFunc) parse_timezone);
  60. weather_dump(weather_dump_timezone, xml_tz);
  61. if (xml_tz) {
  62. diff --git a/panel-plugin/weather-parsers.c b/panel-plugin/weather-parsers.c
  63. index d53a2bc..28934c4 100644
  64. --- a/panel-plugin/weather-parsers.c
  65. +++ b/panel-plugin/weather-parsers.c
  66. @@ -791,49 +791,51 @@ parse_timezone(xmlNode *cur_node)
  67. xmlDoc *
  68. -get_xml_document(SoupMessage *msg)
  69. +get_xml_document(const gchar *data, gsize len)
  70. {
  71. - if (G_LIKELY(msg && msg->response_body && msg->response_body->data)) {
  72. - if (g_utf8_validate(msg->response_body->data, -1, NULL)) {
  73. + if (G_LIKELY(data && len)) {
  74. + if (g_utf8_validate(data, len, NULL)) {
  75. /* force parsing as UTF-8, the XML encoding header may lie */
  76. - return xmlReadMemory(msg->response_body->data,
  77. - strlen(msg->response_body->data),
  78. + return xmlReadMemory(data, len,
  79. NULL, "UTF-8", 0);
  80. } else {
  81. - return xmlParseMemory(msg->response_body->data,
  82. - strlen(msg->response_body->data));
  83. + return xmlParseMemory(data, len);
  84. }
  85. }
  86. return NULL;
  87. }
  88. json_object *
  89. -get_json_tree(SoupMessage *msg)
  90. +get_json_tree(const gchar *data, gsize len)
  91. {
  92. json_object *res=NULL;
  93. - enum json_tokener_error err;
  94. + struct json_tokener *tok = json_tokener_new();
  95. - if (G_LIKELY(msg && msg->response_body && msg->response_body->data)) {
  96. - res = json_tokener_parse_verbose(msg->response_body->data, &err);
  97. - if (err != json_tokener_success)
  98. - g_warning("get_json_tree: error =%d",err);
  99. + if (G_UNLIKELY(tok == NULL)) {
  100. + return NULL;
  101. + } else if (G_LIKELY(data && len)) {
  102. + res = json_tokener_parse_ex(tok, data, len);
  103. + if (res == NULL)
  104. + g_warning("get_json_tree: error =%d",
  105. + json_tokener_get_error(tok));
  106. }
  107. + json_tokener_free(tok);
  108. return res;
  109. }
  110. gpointer
  111. -parse_xml_document(SoupMessage *msg,
  112. +parse_xml_document(const gchar *data, gsize len,
  113. XmlParseFunc parse_func)
  114. {
  115. xmlDoc *doc;
  116. xmlNode *root_node;
  117. gpointer user_data = NULL;
  118. - g_assert(msg != NULL);
  119. - if (G_UNLIKELY(msg == NULL))
  120. + g_assert(data != NULL);
  121. + if (G_UNLIKELY(data == NULL || len == 0))
  122. return NULL;
  123. - doc = get_xml_document(msg);
  124. + doc = get_xml_document(data, len);
  125. if (G_LIKELY(doc)) {
  126. root_node = xmlDocGetRootElement(doc);
  127. if (G_LIKELY(root_node))
  128. diff --git a/panel-plugin/weather-parsers.h b/panel-plugin/weather-parsers.h
  129. index a9d019d..09b9c02 100644
  130. --- a/panel-plugin/weather-parsers.h
  131. +++ b/panel-plugin/weather-parsers.h
  132. @@ -22,7 +22,6 @@
  133. #include <glib.h>
  134. #include <gtk/gtk.h>
  135. #include <libxml/parser.h>
  136. -#include <libsoup/soup.h>
  137. #include <json-c/json_tokener.h>
  138. #define DATA_EXPIRY_TIME (24 * 3600)
  139. @@ -157,11 +156,11 @@ xml_astro *get_astro(const GArray *astrodata,
  140. const time_t day_t,
  141. guint *index);
  142. -xmlDoc *get_xml_document(SoupMessage *msg);
  143. +xmlDoc *get_xml_document(const gchar *data, gsize len);
  144. -json_object *get_json_tree(SoupMessage *msg);
  145. +json_object *get_json_tree(const gchar *data, gsize len);
  146. -gpointer parse_xml_document(SoupMessage *msg,
  147. +gpointer parse_xml_document(const gchar *data, gsize len,
  148. XmlParseFunc parse_func);
  149. xml_astro *xml_astro_copy(const xml_astro *src);
  150. diff --git a/panel-plugin/weather-search.c b/panel-plugin/weather-search.c
  151. index 7e87ae8..6a2ba1f 100644
  152. --- a/panel-plugin/weather-search.c
  153. +++ b/panel-plugin/weather-search.c
  154. @@ -89,6 +89,13 @@ cb_searchdone(SoupSession *session,
  155. gint found = 0;
  156. GtkTreeIter iter;
  157. GtkTreeSelection *selection;
  158. + const gchar *body = NULL;
  159. + gsize len = 0;
  160. +
  161. + if (G_LIKELY(msg->response_body && msg->response_body->data)) {
  162. + body = msg->response_body->data;
  163. + len = msg->response_body->length;
  164. + }
  165. if (global_dialog == NULL) {
  166. weather_debug("%s called after dialog was destroyed", G_STRFUNC);
  167. @@ -97,7 +104,7 @@ cb_searchdone(SoupSession *session,
  168. gtk_widget_set_sensitive(dialog->find_button, TRUE);
  169. - doc = get_xml_document(msg);
  170. + doc = get_xml_document(body, len);
  171. if (!doc)
  172. return;
  173. @@ -385,6 +392,13 @@ cb_geolocation(SoupSession *session,
  174. xml_geolocation *geo;
  175. gchar *full_loc;
  176. units_config *units;
  177. + const gchar *body = NULL;
  178. + gsize len = 0;
  179. +
  180. + if (G_LIKELY(msg->response_body && msg->response_body->data)) {
  181. + body = msg->response_body->data;
  182. + len = msg->response_body->length;
  183. + }
  184. if (global_dialog == NULL) {
  185. weather_debug("%s called after dialog was destroyed", G_STRFUNC);
  186. @@ -392,7 +406,7 @@ cb_geolocation(SoupSession *session,
  187. }
  188. geo = (xml_geolocation *)
  189. - parse_xml_document(msg, (XmlParseFunc) parse_geolocation);
  190. + parse_xml_document(body, len, (XmlParseFunc) parse_geolocation);
  191. weather_dump(weather_dump_geolocation, geo);
  192. if (!geo) {
  193. diff --git a/panel-plugin/weather.c b/panel-plugin/weather.c
  194. index 0a92b4e..b75c633 100644
  195. --- a/panel-plugin/weather.c
  196. +++ b/panel-plugin/weather.c
  197. @@ -489,11 +489,17 @@ cb_astro_update_sun(SoupSession *session,
  198. json_object *json_tree;
  199. time_t now_t;
  200. guint astro_forecast_days;
  201. + const gchar *body = NULL;
  202. + gsize len = 0;
  203. data->msg_parse->sun_msg_processed++;
  204. data->astro_update->http_status_code = msg->status_code;
  205. if ((msg->status_code == 200 || msg->status_code == 203)) {
  206. - json_tree = get_json_tree(msg);
  207. + if (G_LIKELY(msg->response_body && msg->response_body->data)) {
  208. + body = msg->response_body->data;
  209. + len = msg->response_body->length;
  210. + }
  211. + json_tree = get_json_tree(body, len);
  212. if (G_LIKELY(json_tree)) {
  213. if (!parse_astrodata_sun(json_tree, data->astrodata)) {
  214. data->msg_parse->sun_msg_parse_error++;
  215. @@ -545,11 +551,17 @@ cb_astro_update_moon(SoupSession *session,
  216. json_object *json_tree;
  217. time_t now_t;
  218. guint astro_forecast_days;
  219. + const gchar *body = NULL;
  220. + gsize len = 0;
  221. data->msg_parse->moon_msg_processed++;
  222. data->astro_update->http_status_code = msg->status_code;
  223. if ((msg->status_code == 200 || msg->status_code == 203)) {
  224. - json_tree = get_json_tree(msg);
  225. + if (G_LIKELY(msg->response_body && msg->response_body->data)) {
  226. + body = msg->response_body->data;
  227. + len = msg->response_body->length;
  228. + }
  229. + json_tree = get_json_tree(body, len);
  230. if (G_LIKELY(json_tree)) {
  231. if (!parse_astrodata_moon(json_tree, data->astrodata)) {
  232. data->msg_parse->moon_msg_parse_error++;
  233. @@ -606,17 +618,23 @@ cb_weather_update(SoupSession *session,
  234. gpointer user_data)
  235. {
  236. plugin_data *data = user_data;
  237. - xmlDoc *doc;
  238. + xmlDoc *doc = NULL;
  239. xmlNode *root_node;
  240. time_t now_t;
  241. gboolean parsing_error = TRUE;
  242. + const gchar *body = NULL;
  243. + gsize len = 0;
  244. weather_debug("Processing downloaded weather data.");
  245. time(&now_t);
  246. data->weather_update->attempt++;
  247. data->weather_update->http_status_code = msg->status_code;
  248. if (msg->status_code == 200 || msg->status_code == 203) {
  249. - doc = get_xml_document(msg);
  250. + if (G_LIKELY(msg->response_body && msg->response_body->data)) {
  251. + body = msg->response_body->data;
  252. + len = msg->response_body->length;
  253. + }
  254. + doc = get_xml_document(body, len);
  255. if (G_LIKELY(doc)) {
  256. root_node = xmlDocGetRootElement(doc);
  257. if (G_LIKELY(root_node))