]> source.dussan.org Git - rspamd.git/commitdiff
Add utility function for testing purposes.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 2 Mar 2015 14:13:29 +0000 (14:13 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 2 Mar 2015 14:13:29 +0000 (14:13 +0000)
src/libserver/url.c
src/libserver/url.h

index c193377b066b143558abc86f6dc7a29f1198c605..b43189719a42b20a867408764b4b069db7ed6990 100644 (file)
@@ -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
  */
index f2aed42c6ab1f50375e3ad7ebfcfc9685f219700..889458c4470c70495f74db062db4138455194fd9 100644 (file)
@@ -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