]> git.sur5r.net Git - openldap/blob - libraries/liblutil/fetch.c
49a72c6e4ca4b0a9c3c5aff53b6098408d829cc9
[openldap] / libraries / liblutil / fetch.c
1 /* fetch.c - routines for fetching data at URLs */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1999-2005 The OpenLDAP Foundation.
6  * Portions Copyright 1999-2003 Kurt D. Zeilenga.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* This work was initially developed by Kurt D. Zeilenga for
18  * inclusion in OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24
25 #include <ac/stdlib.h>
26
27 #include <ac/string.h>
28 #include <ac/socket.h>
29 #include <ac/time.h>
30
31 #ifdef HAVE_FETCH
32 #include <fetch.h>
33 #endif
34
35 #include "ldap_log.h"
36 #include "lber_pvt.h"
37 #include "ldap_pvt.h"
38 #include "ldap_config.h"
39 #include "ldif.h"
40
41 int
42 ldif_fetch_url(
43     LDAP_CONST char     *urlstr,
44     char        **valuep,
45     ber_len_t *vlenp
46 )
47 {
48         FILE *url;
49         char buffer[1024];
50         char *p = NULL;
51         size_t total;
52         size_t bytes;
53
54         *valuep = NULL;
55         *vlenp = 0;
56
57 #ifdef HAVE_FETCH
58         url = fetchGetURL( (char*) urlstr, "" );
59
60 #else
61         if( strncasecmp( "file://", urlstr, sizeof("file://")-1 ) == 0 ) {
62                 p = strchr( &urlstr[sizeof("file://")-1], '/' );
63                 if( p == NULL ) {
64                         return -1;
65                 }
66
67                 /* we don't check for LDAP_DIRSEP since URLs should contain '/' */
68                 if( *p != '/' ) {
69                         /* skip over false root */
70                         p++;
71                 }
72
73                 p = ber_strdup( p );
74                 ldap_pvt_hex_unescape( p );
75
76                 url = fopen( p, "rb" );
77
78         } else {
79                 return -1;
80         }
81 #endif
82
83         if( url == NULL ) {
84                 return -1;
85         }
86
87         total = 0;
88
89         while( (bytes = fread( buffer, 1, sizeof(buffer), url )) != 0 ) {
90                 char *newp = ber_memrealloc( p, total + bytes + 1 );
91                 if( newp == NULL ) {
92                         ber_memfree( p );
93                         fclose( url );
94                         return -1;
95                 }
96                 p = newp;
97                 AC_MEMCPY( &p[total], buffer, bytes );
98                 total += bytes;
99         }
100
101         fclose( url );
102
103         if( total == 0 ) {
104                 char *newp = ber_memrealloc( p, 1 );
105                 if( newp == NULL ) {
106                         ber_memfree( p );
107                         return -1;
108                 }
109                 p = newp;
110         }
111
112         p[total] = '\0';
113         *valuep = p;
114         *vlenp = total;
115
116         return 0;
117 }