[PATCH] connectivity: allow 204 (no content) as connectivity test



If the user is requesting an empty response ("") as expected string,
let the connectivity check succeed if we actually get a 204 HTTP
response code (reporting a successful request but without content).

This allows using e.g. Android's default URLs for the connectivity
check purpose:

    [connectivity]
    uri=http://google.com/generate_204
    interval=60
    response=
---

Hey hey,

What do you think of this patch? This should work for all cases, including when trying to detect a captive 
portal (as the portal shouldn't return a 204 in any case).

Cheers!

---
 src/nm-connectivity.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/src/nm-connectivity.c b/src/nm-connectivity.c
index 4ccc5719e..d29de2d86 100644
--- a/src/nm-connectivity.c
+++ b/src/nm-connectivity.c
@@ -138,6 +138,7 @@ curl_check_connectivity (CURLM *mhandle, CURLMcode ret)
        CURLMsg *msg;
        CURLcode eret;
        gint m_left;
+       long response_code;

        if (ret != CURLM_OK)
                _LOGW ("connectivity check failed");
@@ -157,11 +158,21 @@ curl_check_connectivity (CURLM *mhandle, CURLMcode ret)
                        /* If cb_data is still there this message hasn't been
                         * taken care of. Do so now. */
                        if (msg->data.result == CURLE_OK) {
+                               /* If we got a 204 error (No content) and we actually requested no content,
+                                * report full connectivity. */
+                               if (!cb_data->response[0] &&
+                                   (curl_easy_getinfo (msg->easy_handle, CURLINFO_RESPONSE_CODE, 
&response_code) == CURLE_OK) &&
+                                   response_code == 204) {
+                                       _LOG2D ("response with no content received, check successful");
+                                       finish_cb_data (cb_data, NM_CONNECTIVITY_FULL);
+                               }
                                /* If we get here, it means that easy_write_cb() didn't read enough
                                 * bytes to be able to do a match. */
-                               _LOG2I ("response shorter than expected '%s'; assuming captive portal.",
-                                       cb_data->response);
-                               finish_cb_data (cb_data, NM_CONNECTIVITY_PORTAL);
+                               else {
+                                       _LOG2I ("response shorter than expected '%s'; assuming captive 
portal.",
+                                               cb_data->response);
+                                       finish_cb_data (cb_data, NM_CONNECTIVITY_PORTAL);
+                               }
                        } else {
                                _LOG2D ("check failed (%d)", msg->data.result);
                                finish_cb_data (cb_data, NM_CONNECTIVITY_LIMITED);
--
2.15.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]