diff options
author | Daniel Kolesa <dkolesa@igalia.com> | 2021-03-24 15:38:54 +0100 |
---|---|---|
committer | Emmanuele Bassi <ebassi@gnome.org> | 2021-11-10 17:02:11 +0000 |
commit | 1d77a74d2e9c87a328229e43cb0e0120489d687f (patch) | |
tree | 0d99c4d050c3447d1db1627ce7ad79ebc197f2af /libgweather | |
parent | 515226e1a7a44b44e72922151c31ac6e678232bf (diff) | |
download | libgweather-1d77a74d2e9c87a328229e43cb0e0120489d687f.tar.gz |
Port to libsoup3
Diffstat (limited to 'libgweather')
-rw-r--r-- | libgweather/gweather-private.h | 10 | ||||
-rw-r--r-- | libgweather/gweather-weather.c | 35 | ||||
-rw-r--r-- | libgweather/meson.build | 12 | ||||
-rw-r--r-- | libgweather/tests/test_libgweather.c | 23 | ||||
-rw-r--r-- | libgweather/weather-iwin.c | 49 | ||||
-rw-r--r-- | libgweather/weather-metar.c | 95 | ||||
-rw-r--r-- | libgweather/weather-metno.c | 64 | ||||
-rw-r--r-- | libgweather/weather-owm.c | 61 |
8 files changed, 310 insertions, 39 deletions
diff --git a/libgweather/gweather-private.h b/libgweather/gweather-private.h index 8682de3..68d5665 100644 --- a/libgweather/gweather-private.h +++ b/libgweather/gweather-private.h @@ -230,6 +230,16 @@ void _gweather_info_request_done (GWeatherInfo *info, SoupMessage *message); +#if SOUP_CHECK_VERSION (2, 99, 2) +void _gweather_info_queue_request (GWeatherInfo *info, + SoupMessage *message, + GAsyncReadyCallback callback); +#else +void _gweather_info_queue_request (GWeatherInfo *info, + SoupMessage *message, + SoupSessionCallback callback); +#endif + void ecl2equ (double t, double eclipLon, diff --git a/libgweather/gweather-weather.c b/libgweather/gweather-weather.c index 21dd299..01343f7 100644 --- a/libgweather/gweather-weather.c +++ b/libgweather/gweather-weather.c @@ -365,7 +365,7 @@ _gweather_info_request_done (GWeatherInfo *info, SoupMessage *message) { info->requests_pending = g_slist_remove (info->requests_pending, message); - g_object_ref (message); + g_object_unref (message); if (info->requests_pending == NULL) { fixup_current_conditions (info); @@ -376,6 +376,29 @@ _gweather_info_request_done (GWeatherInfo *info, } } +#if SOUP_CHECK_VERSION (2, 99, 2) +void +_gweather_info_queue_request (GWeatherInfo *info, + SoupMessage *msg, + GAsyncReadyCallback callback) +{ + GCancellable *cancellable = g_cancellable_new (); + g_object_set_data_full (G_OBJECT (msg), "request-cancellable", + cancellable, g_object_unref); + soup_session_send_and_read_async (info->session, msg, G_PRIORITY_DEFAULT, + cancellable, callback, info); + g_object_unref (msg); +} +#else +void +_gweather_info_queue_request (GWeatherInfo *info, + SoupMessage *msg, + SoupSessionCallback callback) +{ + soup_session_queue_message (info->session, msg, callback, info); +} +#endif + /* it's OK to pass in NULL */ void free_forecast_list (GWeatherInfo *info) @@ -598,7 +621,7 @@ ref_session (GWeatherInfo *info) LIBGWEATHER_VERSION, info->application_id, info->contact_info); - g_object_set (G_OBJECT (session), SOUP_SESSION_USER_AGENT, user_agent, NULL); + g_object_set (G_OBJECT (session), "user-agent", user_agent, NULL); cache = get_cache (); if (cache != NULL) { @@ -707,8 +730,14 @@ gweather_info_abort (GWeatherInfo *info) /* to block updated signals */ info->requests_pending = &dummy; - for (iter = list; iter; iter = iter->next) + for (iter = list; iter; iter = iter->next) { +#if SOUP_CHECK_VERSION (2, 99, 2) + g_cancellable_cancel (g_object_get_data (iter->data, "request-cancellable")); +#else soup_session_cancel_message (info->session, iter->data, SOUP_STATUS_CANCELLED); +#endif + } + g_slist_free (list); info->requests_pending = NULL; diff --git a/libgweather/meson.build b/libgweather/meson.build index da24cf9..f77ea8f 100644 --- a/libgweather/meson.build +++ b/libgweather/meson.build @@ -96,9 +96,19 @@ configure_file( install_dir: includedir / header_subdir, ) +if get_option('soup2') + libsoup_name = 'libsoup-2.4' + libsoup_req_version = '>= 2.44.0' +else + libsoup_name = 'libsoup-3.0' + libsoup_req_version = '>= 2.99.2' +endif + +libsoup_dep = dependency(libsoup_name, version: libsoup_req_version) + deps_libgweather = [ dependency('gio-2.0', version: glib_req_version), - dependency('libsoup-2.4', version: libsoup_req_version), + libsoup_dep, dependency('libxml-2.0', version: libxml_req_version), dependency('geocode-glib-1.0'), diff --git a/libgweather/tests/test_libgweather.c b/libgweather/tests/test_libgweather.c index fb6bd5c..0ab4dd5 100644 --- a/libgweather/tests/test_libgweather.c +++ b/libgweather/tests/test_libgweather.c @@ -403,24 +403,43 @@ test_metar_weather_stations (void) SoupSession *session; GHashTable *stations_ht; char *contents; +#if SOUP_CHECK_VERSION (2, 99, 2) + GBytes *body; + GError *error = NULL; + gsize bsize; +#endif world = gweather_location_get_world (); g_assert_nonnull (world); msg = soup_message_new ("GET", METAR_SOURCES); session = soup_session_new (); +#if SOUP_CHECK_VERSION (2, 99, 2) + body = soup_session_send_and_read (session, msg, NULL, &error); + if (error && error->domain == G_TLS_ERROR) { +#else soup_session_send_message (session, msg); if (msg->status_code == SOUP_STATUS_SSL_FAILED) { +#endif g_test_message ("SSL/TLS failure, please check your glib-networking installation"); g_test_failed (); return; } +#if SOUP_CHECK_VERSION (2, 99, 2) + g_assert_null (error); + g_assert_cmpint (soup_message_get_status (msg), >=, 200); + g_assert_cmpint (soup_message_get_status (msg), <, 300); + g_assert_nonnull (body); + contents = g_bytes_unref_to_data (body, &bsize); + contents = g_strndup (contents, bsize); + g_bytes_unref (body); +#else g_assert_cmpint (msg->status_code, >=, 200); g_assert_cmpint (msg->status_code, <, 300); - g_object_unref (session); g_assert_nonnull (msg->response_body); - contents = g_strndup (msg->response_body->data, msg->response_body->length); +#endif + g_object_unref (session); g_object_unref (msg); stations_ht = parse_metar_stations (contents); diff --git a/libgweather/weather-iwin.c b/libgweather/weather-iwin.c index 9b420e0..1176553 100644 --- a/libgweather/weather-iwin.c +++ b/libgweather/weather-iwin.c @@ -298,6 +298,52 @@ parseForecastXml (const char *buff, GWeatherInfo *original_info) return res; } +#if SOUP_CHECK_VERSION (2, 99, 2) +static void +iwin_finish (GObject *source, GAsyncResult *result, gpointer data) +{ + GWeatherInfo *info; + WeatherLocation *loc; + SoupSession *session = SOUP_SESSION (source); + SoupMessage *msg = soup_session_get_async_result_message (session, result); + GBytes *body; + GError *error = NULL; + const char *content; + + body = soup_session_send_and_read_finish (session, result, &error); + + if (!body) { + /* forecast data is not really interesting anyway ;) */ + if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { + g_debug ("Failed to get IWIN forecast data: %s\n", error->message); + return; + } + g_warning ("Failed to get IWIN forecast data: %s\n", error->message); + g_clear_error (&error); + _gweather_info_request_done (data, msg); + return; + } else if (!SOUP_STATUS_IS_SUCCESSFUL (soup_message_get_status (msg))) { + g_bytes_unref (body); + g_warning ("Failed to get IWIN forecast data: %d %s\n", + soup_message_get_status (msg), + soup_message_get_reason_phrase (msg)); + _gweather_info_request_done (data, msg); + return; + } + + info = data; + loc = &info->location; + content = g_bytes_get_data (body, NULL); + + g_debug ("iwin data for %s", loc->zone); + g_debug ("%s", content); + + info->forecast_list = parseForecastXml (content, info); + g_bytes_unref (body); + + _gweather_info_request_done (info, msg); +} +#else static void iwin_finish (SoupSession *session, SoupMessage *msg, gpointer data) { @@ -329,6 +375,7 @@ iwin_finish (SoupSession *session, SoupMessage *msg, gpointer data) _gweather_info_request_done (info, msg); } +#endif /* Get forecast into newly alloc'ed string */ gboolean @@ -374,7 +421,7 @@ iwin_start_open (GWeatherInfo *info) g_debug ("iwin_start_open, requesting: %s", url); msg = soup_message_new ("GET", url); _gweather_info_begin_request (info, msg); - soup_session_queue_message (info->session, msg, iwin_finish, info); + _gweather_info_queue_request (info, msg, iwin_finish); g_free (url); diff --git a/libgweather/weather-metar.c b/libgweather/weather-metar.c index 88bd86c..9460076 100644 --- a/libgweather/weather-metar.c +++ b/libgweather/weather-metar.c @@ -546,15 +546,46 @@ metar_parse (gchar *metar, GWeatherInfo *info) return TRUE; } +#if SOUP_CHECK_VERSION (2, 99, 2) +static void +metar_finish (GObject *source, GAsyncResult *result, gpointer data) +#else static void metar_finish (SoupSession *session, SoupMessage *msg, gpointer data) +#endif { +#if SOUP_CHECK_VERSION (2, 99, 2) + SoupSession *session = SOUP_SESSION (source); + SoupMessage *msg = soup_session_get_async_result_message (session, result); + GError *error = NULL; + GBytes *body; +#endif GWeatherInfo *info; WeatherLocation *loc; - const gchar *p, *eoln; + const gchar *p, *eoln, *response_body; gchar *searchkey, *metar; gboolean success = FALSE; + info = data; + +#if SOUP_CHECK_VERSION (2, 99, 2) + body = soup_session_send_and_read_finish (session, result, &error); + if (!body) { + if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { + g_debug ("Failed to get METAR data: %s.\n", error->message); + return; + } + g_warning ("Failed to get METAR data: %s.\n", error->message); + _gweather_info_request_done (info, msg); + return; + } else if (!SOUP_STATUS_IS_SUCCESSFUL (soup_message_get_status (msg))) { + g_bytes_unref (body); + info->network_error = TRUE; + _gweather_info_request_done (info, msg); + return; + } + response_body = g_bytes_get_data (body, NULL); +#else if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code)) { if (msg->status_code == SOUP_STATUS_CANCELLED) { g_debug ("Failed to get METAR data: %d %s.\n", @@ -576,15 +607,16 @@ metar_finish (SoupSession *session, SoupMessage *msg, gpointer data) _gweather_info_request_done (info, msg); return; } + response_body = msg->response_body->data; +#endif - info = data; loc = &info->location; g_debug ("METAR data for %s", loc->code); - g_debug ("%s", msg->response_body->data); + g_debug ("%s", response_body); searchkey = g_strdup_printf ("<raw_text>%s ", loc->code); - p = strstr (msg->response_body->data, searchkey); + p = strstr (response_body, searchkey); if (p) { p += strlen (searchkey); @@ -599,7 +631,7 @@ metar_finish (SoupSession *session, SoupMessage *msg, gpointer data) g_debug ("Successfully parsed METAR for %s", loc->code); else g_debug ("Failed to parse raw_text METAR for %s", loc->code); - } else if (!strstr (msg->response_body->data, "aviationweather.gov")) { + } else if (!strstr (response_body, "aviationweather.gov")) { /* The response doesn't even seem to have come from NOAA... * most likely it is a wifi hotspot login page. Call that a * network error. @@ -614,6 +646,10 @@ metar_finish (SoupSession *session, SoupMessage *msg, gpointer data) if (!info->valid) info->valid = success; + +#if SOUP_CHECK_VERSION (2, 99, 2) + g_bytes_unref (body); +#endif _gweather_info_request_done (info, msg); } @@ -623,6 +659,12 @@ metar_start_open (GWeatherInfo *info) { WeatherLocation *loc; SoupMessage *msg; +#if SOUP_CHECK_VERSION (2, 99, 2) + GUri *uri; +#else + SoupURI *uri; +#endif + char *query; g_return_if_fail (info != NULL); @@ -633,24 +675,29 @@ metar_start_open (GWeatherInfo *info) return; g_debug ("metar_start_open, requesting: https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=3&mostRecent=true&fields=raw_text&stationString=%s", loc->code); - msg = soup_form_request_new ( - "GET", - "https://www.aviationweather.gov/adds/dataserver_current/httpparam", - "dataSource", - "metars", - "requestType", - "retrieve", - "format", - "xml", - "hoursBeforeNow", - "3", - "mostRecent", - "true", - "fields", - "raw_text", - "stationString", - loc->code, - NULL); + query = soup_form_encode ( + "dataSource", "metars", + "requestType", "retrieve", + "format", "xml", + "hoursBeforeNow", "3", + "mostRecent", "true", + "fields", "raw_text", + "stationString", loc->code, + NULL + ); +#if SOUP_CHECK_VERSION (2, 99, 2) + uri = g_uri_build (SOUP_HTTP_URI_FLAGS, + "https", NULL, + "www.aviationweather.gov", -1, + "/adds/dataserver_current/httpparam", + query, + NULL); + g_free (query); +#else + uri = soup_uri_new ("https://www.aviationweather.gov/adds/dataserver_current/httpparam"); + uri->query = query; +#endif + msg = soup_message_new_from_uri ("GET", uri); _gweather_info_begin_request (info, msg); - soup_session_queue_message (info->session, msg, metar_finish, info); + _gweather_info_queue_request (info, msg, metar_finish); } diff --git a/libgweather/weather-metno.c b/libgweather/weather-metno.c index 8a04490..242a8dd 100644 --- a/libgweather/weather-metno.c +++ b/libgweather/weather-metno.c @@ -293,14 +293,15 @@ fill_info_from_node (GWeatherInfo *info, static void parse_forecast_xml_new (GWeatherInfo *original_info, - SoupMessageBody *body) + const char *data, + gsize length) { xmlDocPtr doc; xmlXPathContextPtr xpath_ctx; xmlXPathObjectPtr xpath_result; int i; - doc = xmlParseMemory (body->data, body->length); + doc = xmlParseMemory (data, length); if (!doc) return; @@ -377,6 +378,59 @@ out: xmlFreeDoc (doc); } +#if SOUP_CHECK_VERSION (2, 99, 2) +static void +metno_finish_new (GObject *source, + GAsyncResult *result, + gpointer data) +{ + GWeatherInfo *info; + WeatherLocation *loc; + SoupSession *session = SOUP_SESSION (source); + SoupMessage *msg = soup_session_get_async_result_message (session, result); + GBytes *body; + GError *error = NULL; + guint num_forecasts; + const char *content; + gsize body_size; + + body = soup_session_send_and_read_finish (session, result, &error); + + if (!body) { + /* forecast data is not really interesting anyway ;) */ + if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { + g_debug ("Failed to get met.no forecast data: %s\n", error->message); + return; + } + g_message ("Failed to get met.no forecast data: %s\n", error->message); + g_clear_error (&error); + _gweather_info_request_done (data, msg); + return; + } else if (!SOUP_STATUS_IS_SUCCESSFUL (soup_message_get_status (msg))) { + g_message ("Failed to get met.no forecast data: %d %s\n", + soup_message_get_status (msg), + soup_message_get_reason_phrase (msg)); + _gweather_info_request_done (data, msg); + return; + } + + content = g_bytes_get_data (body, &body_size); + + info = data; + loc = &info->location; + g_debug ("metno data for %lf, %lf", loc->latitude, loc->longitude); + g_debug ("%s", content); + + parse_forecast_xml_new (info, content, body_size); + num_forecasts = g_slist_length (info->forecast_list); + g_debug ("metno parsed %d forecast infos", num_forecasts); + if (!info->valid) + info->valid = (num_forecasts > 0); + + g_bytes_unref (body); + _gweather_info_request_done (info, msg); +} +#else static void metno_finish_new (SoupSession *session, SoupMessage *msg, @@ -406,7 +460,8 @@ metno_finish_new (SoupSession *session, g_debug ("metno data for %lf, %lf", loc->latitude, loc->longitude); g_debug ("%s", msg->response_body->data); - parse_forecast_xml_new (info, msg->response_body); + parse_forecast_xml_new (info, msg->response_body->data, + msg->response_body->length); num_forecasts = g_slist_length (info->forecast_list); g_debug ("metno parsed %d forecast infos", num_forecasts); if (!info->valid) @@ -414,6 +469,7 @@ metno_finish_new (SoupSession *session, _gweather_info_request_done (info, msg); } +#endif gboolean metno_start_open (GWeatherInfo *info) @@ -439,7 +495,7 @@ metno_start_open (GWeatherInfo *info) message = soup_message_new ("GET", url); _gweather_info_begin_request (info, message); - soup_session_queue_message (info->session, message, metno_finish_new, info); + _gweather_info_queue_request (info, message, metno_finish_new); g_free (url); diff --git a/libgweather/weather-owm.c b/libgweather/weather-owm.c index 8c81da4..10b06cd 100644 --- a/libgweather/weather-owm.c +++ b/libgweather/weather-owm.c @@ -347,14 +347,15 @@ make_info_from_node (GWeatherInfo *original_info, static void parse_forecast_xml (GWeatherInfo *original_info, - SoupMessageBody *body) + const char *data, + gsize length) { xmlDocPtr doc; xmlXPathContextPtr xpath_ctx; xmlXPathObjectPtr xpath_result; int i; - doc = xmlParseMemory (body->data, body->length); + doc = xmlParseMemory (data, length); if (!doc) return; @@ -389,6 +390,56 @@ out: xmlFreeDoc (doc); } +#if SOUP_CHECK_VERSION (2, 99, 2) +static void +owm_finish (GObject *source, + GAsyncResult *result, + gpointer data) +{ + GWeatherInfo *info; + WeatherLocation *loc; + SoupSession *session = SOUP_SESSION (source); + SoupMessage *msg = soup_session_get_async_result_message (session, result); + GBytes *body; + GError *error = NULL; + const char *content; + gsize length; + + body = soup_session_send_and_read_finish (session, result, &error); + + if (!body) { + /* forecast data is not really interesting anyway ;) */ + if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) { + g_debug ("Failed to get OpenWeatheRMap forecast data: %s\n", + error->message); + return; + } + g_warning ("Failed to get OpenWeatherMap forecast data: %s\n", + error->message); + g_clear_error (&error); + _gweather_info_request_done (data, msg); + return; + } else if (!SOUP_STATUS_IS_SUCCESSFUL (soup_message_get_status (msg))) { + g_bytes_unref (body); + g_warning ("Failed to get OpenWeatherMap forecast data: %d %s\n", + soup_message_get_status (msg), + soup_message_get_reason_phrase (msg)); + _gweather_info_request_done (data, msg); + return; + } + + content = g_bytes_get_data (body, &length); + + info = data; + loc = &info->location; + g_debug ("owm data for %lf, %lf", loc->latitude, loc->longitude); + g_debug ("%s", content); + + parse_forecast_xml (info, content, length); + g_bytes_unref (body); + _gweather_info_request_done (info, msg); +} +#else static void owm_finish (SoupSession *session, SoupMessage *msg, @@ -417,9 +468,11 @@ owm_finish (SoupSession *session, g_debug ("owm data for %lf, %lf", loc->latitude, loc->longitude); g_debug ("%s", msg->response_body->data); - parse_forecast_xml (info, msg->response_body); + parse_forecast_xml (info, msg->response_body->data, + msg->response_body->length); _gweather_info_request_done (info, msg); } +#endif gboolean owm_start_open (GWeatherInfo *info) @@ -454,7 +507,7 @@ owm_start_open (GWeatherInfo *info) message = soup_message_new ("GET", url); _gweather_info_begin_request (info, message); - soup_session_queue_message (info->session, message, owm_finish, info); + _gweather_info_queue_request (info, message, owm_finish); g_free (url); |