]> git.sur5r.net Git - openldap/blob - libraries/liblutil/fetch.c
5ed0ad9cb094b35aa7b26cece71d76b39ec20d5c
[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 FILE *
42 ldif_open_url(
43         LDAP_CONST char *urlstr
44 )
45 {
46         FILE *url;
47         char *p = NULL;
48 #ifdef HAVE_FETCH
49         url = fetchGetURL( (char*) urlstr, "" );
50
51 #else
52         if( strncasecmp( "file://", urlstr, sizeof("file://")-1 ) == 0 ) {
53                 p = strchr( &urlstr[sizeof("file://")-1], '/' );
54                 if( p == NULL ) {
55                         return NULL;
56                 }
57
58                 /* we don't check for LDAP_DIRSEP since URLs should contain '/' */
59                 if( *p != '/' ) {
60                         /* skip over false root */
61                         p++;
62                 }
63
64                 p = ber_strdup( p );
65                 ldap_pvt_hex_unescape( p );
66
67                 url = fopen( p, "rb" );
68
69                 ber_memfree( p );
70         } else {
71                 return NULL;
72         }
73 #endif
74         return url;
75 }
76
77 int
78 ldif_fetch_url(
79     LDAP_CONST char     *urlstr,
80     char        **valuep,
81     ber_len_t *vlenp
82 )
83 {
84         FILE *url;
85         char buffer[1024];
86         char *p = NULL;
87         size_t total;
88         size_t bytes;
89
90         *valuep = NULL;
91         *vlenp = 0;
92
93         url = ldif_open_url( urlstr );
94
95         if( url == NULL ) {
96                 return -1;
97         }
98
99         total = 0;
100
101         while( (bytes = fread( buffer, 1, sizeof(buffer), url )) != 0 ) {
102                 char *newp = ber_memrealloc( p, total + bytes + 1 );
103                 if( newp == NULL ) {
104                         ber_memfree( p );
105                         fclose( url );
106                         return -1;
107                 }
108                 p = newp;
109                 AC_MEMCPY( &p[total], buffer, bytes );
110                 total += bytes;
111         }
112
113         fclose( url );
114
115         if( total == 0 ) {
116                 char *newp = ber_memrealloc( p, 1 );
117                 if( newp == NULL ) {
118                         ber_memfree( p );
119                         return -1;
120                 }
121                 p = newp;
122         }
123
124         p[total] = '\0';
125         *valuep = p;
126         *vlenp = total;
127
128         return 0;
129 }