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