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