[geany/geany-plugins] 649939: geniuspaste: Add support for using the redirected URI as paste URI

Colomban Wendling git-noreply at xxxxx
Sun Feb 21 08:49:45 UTC 2016


Branch:      refs/heads/master
Author:      Colomban Wendling <ban at herbesfolles.org>
Committer:   Colomban Wendling <ban at herbesfolles.org>
Date:        Sat, 16 Jan 2016 23:19:57 UTC
Commit:      649939a4490034e51ff69e02d5380f8b7664d882
             https://github.com/geany/geany-plugins/commit/649939a4490034e51ff69e02d5380f8b7664d882

Log Message:
-----------
geniuspaste: Add support for using the redirected URI as paste URI

Most non-API-based pastebin services will redirect the client to the
newly created paste page.  It is then safer to use the redirect URI
than trying to parse some possibly complex HTML to extract the URI.


Modified Paths:
--------------
    geniuspaste/README
    geniuspaste/src/geniuspaste.c

Modified: geniuspaste/README
14 lines changed, 12 insertions(+), 2 deletions(-)
===================================================================
@@ -103,8 +103,18 @@ Each key in this section is a field, and each value that field's value.
 *[parse]* section
 +++++++++++++++++
 
-The *parse* section defines the regular expression used to parse the raw
-response from the pastebin service and build the final paste URL.
+If the *parse* section is declared, it defines the regular expression used to
+parse the raw response body from the pastebin service and build the final
+paste URL.
+
+If this section doesn't exist, the URI of the response itself is used, after
+any possible redirection.  This is usually good for non-API pastebin services,
+where the server redirects the user to the paste page.
+
+**Warning:**  If the *parse* section exists, it will be used, no matter whether
+any key is actually defined.  This means that a ``[parse]`` line is enough to
+enable response body parsing, and it will use the default *search* and
+*replace* settings.
 
 *search*
 	A regular expression (PCRE) pattern to match against the pastebin


Modified: geniuspaste/src/geniuspaste.c
34 lines changed, 21 insertions(+), 13 deletions(-)
===================================================================
@@ -544,7 +544,7 @@ static SoupMessage *pastebin_soup_message_new(const Pastebin  *pastebin,
  * or if the URL couldn't be found.
  * @warning: it may return NULL even if @error is not set */
 static gchar *pastebin_parse_response(const Pastebin  *pastebin,
-                                      const gchar     *response,
+                                      SoupMessage     *msg,
                                       GeanyDocument   *doc,
                                       const gchar     *contents,
                                       GError         **error)
@@ -554,19 +554,27 @@ static gchar *pastebin_parse_response(const Pastebin  *pastebin,
     gchar *replace;
 
     g_return_val_if_fail(pastebin != NULL, NULL);
-    g_return_val_if_fail(response != NULL, NULL);
+    g_return_val_if_fail(msg != NULL, NULL);
 
-    search = utils_get_setting_string(pastebin->config, PASTEBIN_GROUP_PARSE,
-                                      PASTEBIN_GROUP_PARSE_KEY_SEARCH,
-                                      "^[[:space:]]*(.+?)[[:space:]]*$");
-    replace = utils_get_setting_string(pastebin->config, PASTEBIN_GROUP_PARSE,
-                                       PASTEBIN_GROUP_PARSE_KEY_REPLACE, "\\1");
-    SETPTR(replace, expand_placeholders(replace, pastebin, doc, contents));
+    if (! g_key_file_has_group(pastebin->config, PASTEBIN_GROUP_PARSE))
+    {
+        /* by default, use the response URI (redirect) */
+        url = soup_uri_to_string(soup_message_get_uri(msg), FALSE);
+    }
+    else
+    {
+        search = utils_get_setting_string(pastebin->config, PASTEBIN_GROUP_PARSE,
+                                          PASTEBIN_GROUP_PARSE_KEY_SEARCH,
+                                          "^[[:space:]]*(.+?)[[:space:]]*$");
+        replace = utils_get_setting_string(pastebin->config, PASTEBIN_GROUP_PARSE,
+                                           PASTEBIN_GROUP_PARSE_KEY_REPLACE, "\\1");
+        SETPTR(replace, expand_placeholders(replace, pastebin, doc, contents));
 
-    url = regex_replace(search, response, replace, error);
+        url = regex_replace(search, msg->response_body->data, replace, error);
 
-    g_free(search);
-    g_free(replace);
+        g_free(search);
+        g_free(replace);
+    }
 
     return url;
 }
@@ -675,8 +683,8 @@ static void paste(GeanyDocument * doc, const gchar * website)
     else
     {
         GError *err = NULL;
-        gchar *p_url = pastebin_parse_response(pastebin, msg->response_body->data,
-                                               doc, f_content, &err);
+        gchar *p_url = pastebin_parse_response(pastebin, msg, doc, f_content,
+                                               &err);
 
         if (err || ! p_url)
         {



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Plugins-Commits mailing list