return early on non web uri and simplify host parsing - surf-adblock - Surf adblock web extension
 (HTM) git clone git://git.codemadness.org/surf-adblock
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit c882cd7277bb06ca5e14dcd0671a610141de6122
 (DIR) parent cbebae04e483491bf237fe84393ac46c759742c8
 (HTM) Author: Quentin Rameau <quinq@fifth.space>
       Date:   Sat, 16 Jul 2016 20:42:21 +0200
       
       return early on non web uri and simplify host parsing
       
       We now only duplicate the domain part instead of the full uri string.
       
       Diffstat:
         M surf-adblock.c                      |      57 ++++++++++++++++++-------------
       
       1 file changed, 33 insertions(+), 24 deletions(-)
       ---
 (DIR) diff --git a/surf-adblock.c b/surf-adblock.c
       @@ -163,6 +163,16 @@ ecalloc(size_t nmemb, size_t size)
        }
        
        char *
       +estrndup(const char *s, size_t n)
       +{
       +        char *p;
       +
       +        if (!(p = strndup(s, n)))
       +                eprintf("strndup: %s\n", strerror(errno));
       +        return p;
       +}
       +
       +char *
        estrdup(const char *s)
        {
                char *p;
       @@ -702,17 +712,17 @@ documentloaded(WebKitWebPage *wp, Page *p)
                WebKitDOMElement *el;
                String sitecss;
                struct filterrule *r;
       -        char *uri = estrdup(webkit_web_page_get_uri(p->webpage));
       -        char *domain, *s;
       +        const char *s, *uri = webkit_web_page_get_uri(p->webpage);
       +        char *domain;
        
       -        /* TODO: improve, hacky */
       -        if ((domain = strstr(uri, "://"))) {
       -                domain += sizeof("://") - 1;
       -        } else {
       -                domain = uri;
       -        }
       -        if ((s = strchr(domain, '/')))
       -                *s = '\0';
       +        if (!uri || strncmp(uri, "http://", sizeof("http://") - 1) &&
       +            strncmp(uri, "https://", sizeof("https://") - 1))
       +                return;
       +
       +        domain = strstr(uri, "://") + sizeof("://") - 1;
       +        if (!(s = strchr(domain, '/')))
       +                for(s = domain; *s; ++s) ;
       +        domain = estrndup(domain, s - domain);
        
                printf("uri: %s\n", uri);
                printf("domain: %s\n", domain);
       @@ -745,7 +755,7 @@ documentloaded(WebKitWebPage *wp, Page *p)
                                             WEBKIT_DOM_NODE(el), NULL);
        #endif
        
       -        free(uri);
       +        free(domain);
                free(sitecss.data);
        }
        
       @@ -755,19 +765,18 @@ sendrequest(WebKitWebPage *wp, WebKitURIRequest *req,
        {
                struct filterrule *r;
        
       -        const char *requri = webkit_uri_request_get_uri(req);
       -        char *uri = estrdup(webkit_web_page_get_uri(p->webpage));
       -        char *domain, *s;
       +        const char *s, *uri = webkit_web_page_get_uri(p->webpage),
       +                   *requri = webkit_uri_request_get_uri(req);
       +        char *domain;
        
       -        /* TODO: improve, hacky */
       -        if ((domain = strstr(uri, "://"))) {
       -                domain += sizeof("://") - 1;
       -        } else {
       -                domain = uri;
       -        }
       +        if (!uri || strncmp(uri, "http://", sizeof("http://") - 1) &&
       +            strncmp(uri, "https://", sizeof("https://") - 1))
       +                return FALSE;
        
       -        if ((s = strchr(domain, '/')))
       -                *s = '\0';
       +        domain = strstr(uri, "://") + sizeof("://") - 1;
       +        if (!(s = strchr(domain, '/')))
       +                for(s = domain; *s; ++s) ;
       +        domain = estrndup(domain, s - domain);
        
                /* match rules */
                for (r = rules; r; r = r->next) {
       @@ -777,11 +786,11 @@ sendrequest(WebKitWebPage *wp, WebKitURIRequest *req,
                                printf("domain: %s\n", domain);
        
                                fprintf(stderr, "blocked: %s, %s\n", domain, requri);
       -                        free(uri);
       +                        free(domain);
                                return TRUE;
                        }
                }
       -        free(uri);
       +        free(domain);
        
                return FALSE;
        }