pkgsrc-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[pkgsrc/trunk]: pkgsrc/net/libfetch libfetch-2.11:



details:   https://anonhg.NetBSD.org/pkgsrc/rev/005f05486909
branches:  trunk
changeset: 541672:005f05486909
user:      joerg <joerg%pkgsrc.org@localhost>
date:      Thu Apr 24 10:21:33 2008 +0000

description:
libfetch-2.11:
Implement full quoting support in FILE and FTP protocols.

diffstat:

 net/libfetch/Makefile       |   4 +-
 net/libfetch/files/common.c |   5 +-
 net/libfetch/files/fetch.c  |  94 ++++++++++++++++++++++++++++----------------
 net/libfetch/files/fetch.h  |   3 +-
 net/libfetch/files/file.c   |  56 ++++++++++++++++++++------
 net/libfetch/files/ftp.c    |  23 ++++++++--
 6 files changed, 126 insertions(+), 59 deletions(-)

diffs (truncated from 380 to 300 lines):

diff -r 178a9da8336e -r 005f05486909 net/libfetch/Makefile
--- a/net/libfetch/Makefile     Thu Apr 24 09:51:11 2008 +0000
+++ b/net/libfetch/Makefile     Thu Apr 24 10:21:33 2008 +0000
@@ -1,7 +1,7 @@
-# $NetBSD: Makefile,v 1.13 2008/04/24 07:55:00 joerg Exp $
+# $NetBSD: Makefile,v 1.14 2008/04/24 10:21:33 joerg Exp $
 #
 
-DISTNAME=      libfetch-2.10
+DISTNAME=      libfetch-2.11
 CATEGORIES=    net
 MASTER_SITES=  # empty
 DISTFILES=     # empty
diff -r 178a9da8336e -r 005f05486909 net/libfetch/files/common.c
--- a/net/libfetch/files/common.c       Thu Apr 24 09:51:11 2008 +0000
+++ b/net/libfetch/files/common.c       Thu Apr 24 10:21:33 2008 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: common.c,v 1.10 2008/04/24 07:55:00 joerg Exp $        */
+/*     $NetBSD: common.c,v 1.11 2008/04/24 10:21:33 joerg Exp $        */
 /*-
  * Copyright (c) 1998-2004 Dag-Erling Coïdan Smørgrav
  * Copyright (c) 2008 Joerg Sonnenberger <joerg%NetBSD.org@localhost>
@@ -647,8 +647,7 @@
                        ++name_len;
        }
 
-       name_len = strlen(name);
-       tmp_name = malloc( base_doc_len + name_len + 2);
+       tmp_name = malloc( base_doc_len + name_len + 1);
        if (tmp_name == NULL) {
                errno = ENOMEM;
                fetch_syserr();
diff -r 178a9da8336e -r 005f05486909 net/libfetch/files/fetch.c
--- a/net/libfetch/files/fetch.c        Thu Apr 24 09:51:11 2008 +0000
+++ b/net/libfetch/files/fetch.c        Thu Apr 24 10:21:33 2008 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fetch.c,v 1.7 2008/04/24 07:55:00 joerg Exp $  */
+/*     $NetBSD: fetch.c,v 1.8 2008/04/24 10:21:33 joerg Exp $  */
 /*-
  * Copyright (c) 1998-2004 Dag-Erling Coïdan Smørgrav
  * All rights reserved.
@@ -366,6 +366,7 @@
        const char *p, *q;
        struct url *u;
        size_t i, count;
+       int pre_quoted;
 
        /* allocate struct url */
        if ((u = calloc(1, sizeof(*u))) == NULL) {
@@ -374,11 +375,13 @@
        }
 
        if (*URL == '/') {
+               pre_quoted = 0;
                strcpy(u->scheme, SCHEME_FILE);
                p = URL;
                goto quote_doc;
        }
        if (strncmp(URL, "file:", 5) == 0) {
+               pre_quoted = 1;
                strcpy(u->scheme, SCHEME_FILE);
                URL += 5;
                if (URL[0] != '/' || URL[1] != '/' || URL[2] != '/') {
@@ -390,6 +393,7 @@
        }
        if (strncmp(URL, "http:", 5) == 0 ||
            strncmp(URL, "https:", 6) == 0) {
+               pre_quoted = 1;
                if (URL[4] == ':') {
                        strcpy(u->scheme, SCHEME_HTTP);
                        URL += 5;
@@ -407,6 +411,7 @@
                goto find_hostname;
        }
        if (strncmp(URL, "ftp:", 4) == 0) {
+               pre_quoted = 1;
                strcpy(u->scheme, SCHEME_FTP);
                URL += 4;
                if (URL[0] != '/' || URL[1] != '/') {
@@ -476,16 +481,21 @@
 
 quote_doc:
        count = 1;
-       for (i = 0; p[i] != '\0'; ++i)
-               count += fetch_urlpath_safe(p[i]) ? 1 : 3;
+       for (i = 0; p[i] != '\0'; ++i) {
+               if ((!pre_quoted && p[i] == '%') ||
+                   !fetch_urlpath_safe(p[i]))
+                       count += 3;
+               else
+                       ++count;
+       }
+
        if ((u->doc = malloc(count)) == NULL) {
                fetch_syserr();
                goto ouch;
        }
        for (i = 0; *p != '\0'; ++p) {
-               if (fetch_urlpath_safe(*p))
-                       u->doc[i++] = *p;
-               else {
+               if ((!pre_quoted && *p == '%') ||
+                   !fetch_urlpath_safe(*p)) {
                        u->doc[i++] = '%';
                        if ((unsigned char)*p < 160)
                                u->doc[i++] = '0' + ((unsigned char)*p) / 16;
@@ -495,7 +505,8 @@
                                u->doc[i++] = '0' + ((unsigned char)*p) % 16;
                        else
                                u->doc[i++] = 'a' - 10 + ((unsigned char)*p) % 16;
-               }
+               } else
+                       u->doc[i++] = *p;
        }
        u->doc[i] = '\0';
 
@@ -529,37 +540,52 @@
 }
 
 /*
+ * Unquote whole URL.
+ * Skips optional parts like query or fragment identifier.
+ */ 
+char *
+fetch_unquote_doc(struct url *url)
+{
+       char *unquoted;
+       const char *iter;
+       size_t i;
+
+       if ((unquoted = malloc(strlen(url->doc) + 1)) == NULL)
+               return NULL;
+
+       for (i = 0, iter = url->doc; *iter != '\0'; ++iter) {
+               if (*iter == '#' || *iter == '?')
+                       break;
+               if (iter[0] != '%' ||
+                   !isxdigit((unsigned char)iter[1]) ||
+                   !isxdigit((unsigned char)iter[2])) {
+                       unquoted[i++] = *iter;
+                       continue;
+               }
+               unquoted[i++] = xdigit2digit(iter[1]) * 16 +
+                   xdigit2digit(iter[2]);
+               iter += 2;
+       }
+       unquoted[i] = '\0';
+       return unquoted;
+}
+
+
+/*
  * Extract the file name component of a URL.
  */
 char *
 fetch_extract_filename(struct url *url)
 {
-       char *name, *name_iter;
-       const char *last_slash, *iter;
+       char *unquoted, *filename;
+       const char *last_slash;
+
+       if ((unquoted = fetch_unquote_doc(url)) == NULL)
+               return NULL;
 
-       last_slash = url->doc;
-       if (*last_slash == '\0')
-               return strdup("");
-       for (iter = last_slash + 1; *iter; ++iter) {
-               if (*iter == '#' || *iter == '?')
-                       break;
-               if (*iter == '/')
-                       last_slash = iter;
-       }
-       if (last_slash + 1 == iter)
-               return strdup("");
-       name_iter = name = malloc(iter - last_slash);
-       while (++last_slash < iter) {
-               if (*last_slash != '%' ||
-                   !isxdigit((unsigned char)last_slash[1]) ||
-                   !isxdigit((unsigned char)last_slash[2])) {
-                       *name_iter++ = *last_slash;
-                       continue;
-               }
-               *name_iter++ = xdigit2digit(last_slash[1]) * 16 +
-                   xdigit2digit(last_slash[2]);
-               last_slash += 2;
-       }
-       *name_iter = '\0';
-       return name;
+       if ((last_slash = strrchr(unquoted, '/')) == NULL)
+               return unquoted;
+       filename = strdup(last_slash + 1);
+       free(unquoted);
+       return filename;
 }
diff -r 178a9da8336e -r 005f05486909 net/libfetch/files/fetch.h
--- a/net/libfetch/files/fetch.h        Thu Apr 24 09:51:11 2008 +0000
+++ b/net/libfetch/files/fetch.h        Thu Apr 24 10:21:33 2008 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fetch.h,v 1.8 2008/04/21 17:15:31 joerg Exp $  */
+/*     $NetBSD: fetch.h,v 1.9 2008/04/24 10:21:33 joerg Exp $  */
 /*-
  * Copyright (c) 1998-2004 Dag-Erling Coïdan Smørgrav
  * All rights reserved.
@@ -151,6 +151,7 @@
 /* URL listening */
 void            fetch_init_url_list(struct url_list *);
 void            fetch_free_url_list(struct url_list *);
+char           *fetch_unquote_doc(struct url *);
 char           *fetch_extract_filename(struct url *);
 
 /* Authentication */
diff -r 178a9da8336e -r 005f05486909 net/libfetch/files/file.c
--- a/net/libfetch/files/file.c Thu Apr 24 09:51:11 2008 +0000
+++ b/net/libfetch/files/file.c Thu Apr 24 10:21:33 2008 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: file.c,v 1.8 2008/04/24 07:55:00 joerg Exp $   */
+/*     $NetBSD: file.c,v 1.9 2008/04/24 10:21:33 joerg Exp $   */
 /*-
  * Copyright (c) 1998-2004 Dag-Erling Coïdan Smørgrav
  * All rights reserved.
@@ -69,10 +69,17 @@
 fetchIO *
 fetchXGetFile(struct url *u, struct url_stat *us, const char *flags)
 {
+       char *path;
        fetchIO *f;
        int fd, *cookie;
 
-       fd = open(u->doc, O_RDONLY);
+       if ((path = fetch_unquote_doc(u)) == NULL) {
+               fetch_syserr();
+               return NULL;
+       }
+
+       fd = open(path, O_RDONLY);
+       free(path);
        if (fd == -1) {
                fetch_syserr();
                return NULL;
@@ -114,13 +121,21 @@
 fetchIO *
 fetchPutFile(struct url *u, const char *flags)
 {
+       char *path;
        fetchIO *f;
        int fd, *cookie;
 
+       if ((path = fetch_unquote_doc(u)) == NULL) {
+               fetch_syserr();
+               return NULL;
+       }
+
        if (CHECK_FLAG('a'))
-               fd = open(u->doc, O_WRONLY | O_APPEND);
+               fd = open(path, O_WRONLY | O_APPEND);
        else
-               fd = open(u->doc, O_WRONLY);
+               fd = open(path, O_WRONLY);
+
+       free(path);
 
        if (fd == -1) {
                fetch_syserr();
@@ -166,34 +181,47 @@
        return (0);
 }
 
-static int
-fetch_stat_file2(const char *fn, struct url_stat *us)
+int
+fetchStatFile(struct url *u, struct url_stat *us, const char *flags)
 {
+       char *path;
        int fd, rv;
 
-       fd = open(fn, O_RDONLY);
+       if ((path = fetch_unquote_doc(u)) == NULL) {
+               fetch_syserr();
+               return -1;
+       }
+
+       fd = open(path, O_RDONLY);
+       free(path);
+
        if (fd == -1) {
                fetch_syserr();
                return -1;
        }
+
        rv = fetch_stat_file(fd, us);
        close(fd);
+
        return rv;
 }
 
 int
-fetchStatFile(struct url *u, struct url_stat *us, const char *flags)
-{



Home | Main Index | Thread Index | Old Index