From 649939a4490034e51ff69e02d5380f8b7664d882 Mon Sep 17 00:00:00 2001 From: Colomban Wendling Date: Sun, 17 Jan 2016 00:19:57 +0100 Subject: [PATCH] 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. --- geniuspaste/README | 14 ++++++++++++-- geniuspaste/src/geniuspaste.c | 34 +++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/geniuspaste/README b/geniuspaste/README index ce3b593da..d7fdff5d3 100644 --- a/geniuspaste/README +++ b/geniuspaste/README @@ -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 diff --git a/geniuspaste/src/geniuspaste.c b/geniuspaste/src/geniuspaste.c index 96149e3ba..02f9f8e19 100644 --- a/geniuspaste/src/geniuspaste.c +++ b/geniuspaste/src/geniuspaste.c @@ -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) {