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