From: Vsevolod Stakhov Date: Mon, 2 Mar 2015 14:13:29 +0000 (+0000) Subject: Add utility function for testing purposes. X-Git-Tag: 0.9.0~578 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2f3618bf805c290c36b893ae48e0a8c59dc3c3de;p=rspamd.git Add utility function for testing purposes. --- diff --git a/src/libserver/url.c b/src/libserver/url.c index c193377b0..b43189719 100644 --- a/src/libserver/url.c +++ b/src/libserver/url.c @@ -1870,6 +1870,63 @@ rspamd_url_find (rspamd_mempool_t *pool, return FALSE; } +struct rspamd_url * +rspamd_url_get_next (rspamd_mempool_t *pool, + const gchar *start, gchar const **pos) +{ + const gchar *p, *end; + gchar *url_str = NULL, *url_start, *url_end; + struct rspamd_url *new; + gint rc; + + end = start + strlen (start); + + if (pos == NULL || *pos == NULL) { + p = start; + } + else { + p = *pos; + } + + if (p < end) { + if (rspamd_url_find (pool, p, end - p, &url_start, &url_end, &url_str, + FALSE)) { + if (url_str != NULL) { + new = rspamd_mempool_alloc0 (pool, sizeof (struct rspamd_url)); + + if (new != NULL) { + g_strstrip (url_str); + rc = rspamd_url_parse (new, url_str, strlen (url_str), pool); + if (rc == URI_ERRNO_OK && + new->hostlen > 0) { + + if (new->protocol == PROTOCOL_MAILTO) { + if (new->userlen > 0) { + return new; + } + } + else { + return new; + } + } + else if (rc != URI_ERRNO_OK) { + msg_info ("extract of url '%s' failed: %s", + url_str, + rspamd_url_strerror (rc)); + } + } + } + } + p = url_end + 1; + + if (pos != NULL) { + *pos = p; + } + } + + return NULL; +} + /* * vi: ts=4 */ diff --git a/src/libserver/url.h b/src/libserver/url.h index f2aed42c6..889458c44 100644 --- a/src/libserver/url.h +++ b/src/libserver/url.h @@ -108,4 +108,15 @@ gboolean rspamd_url_find (rspamd_mempool_t *pool, */ const gchar * rspamd_url_strerror (enum uri_errno err); +/** + * Convenience routine to extract urls from an arbitrarty text + * @param pool + * @param start + * @param pos + * @return url or NULL + */ +struct rspamd_url * +rspamd_url_get_next (rspamd_mempool_t *pool, + const gchar *start, gchar const **pos); + #endif