Debian Codegraph @

libs/kns/url-fetcher-curl.c

raw symbols SCIP
1 /*===========================================================================
2 *
3 * PUBLIC DOMAIN NOTICE
4 * National Center for Biotechnology Information
5 *
6 * This software/database is a "United States Government Work" under the
7 * terms of the United States Copyright Act. It was written as part of
8 * the author's official duties as a United States Government employee and
9 * thus cannot be copyrighted. This software/database is freely available
10 * to the public for use. The National Library of Medicine and the U.S.
11 * Government have not placed any restriction on its use or reproduction.
12 *
13 * Although all reasonable efforts have been taken to ensure the accuracy
14 * and reliability of the software and data, the NLM and the U.S.
15 * Government do not and cannot warrant the performance or results that
16 * may be obtained by using this software or data. The NLM and the U.S.
17 * Government disclaim all warranties, express or implied, including
18 * warranties of performance, merchantability or fitness for any particular
19 * purpose.
20 *
21 * Please cite the author in any work or product based on this material.
22 *
23 * ===========================================================================
24 *
25 */
26
27 #include <kns/extern.h>
28 #include <klib/rc.h>
29 #include <kns/manager.h>
30 #include <kns/url-fetcher.h>
31
32 /* IMPORTANT define this before including the *-impl.h !!!! */
33 #define KURL_FETCHER_IMPL KUrlFetcherCurl
34 #include "url-fetcher-impl.h"
35
36 #include "kns_mgr_priv.h"
37
38 #include <sysalloc.h>
39
40 #include <stdlib.h>
41 #include <string.h>
42
43 /*
44 #include <curl/curl.h>
45 #include <curl/types.h>
46 #include <curl/easy.h>
47 */
48
49 /*--------------------------------------------------------------------------
50 * KVectorNamelist ... a generic Namelist based on a Vector
51 */
52 struct KUrlFetcherCurl
53 {
54 KUrlFetcher dad;
55
56 struct KNSManager *kns_mgr;
57 CURL *curl_handle;
58
59 /* this buffer is passed in from read() to the callback
60 it is owned by the caller not this class */
61 char *buff;
62 size_t buffsize;
63 size_t in_buff;
64
65 /* this is the spill-over buffer, used by the callback
66 if the caller-provided buffer is too small */
67 char *spill_over_buff;
68 size_t spill_over_buffsize;
69 };
70
71 /* implementation of the the virtual KUrlFetcher-Interface for curl */
72
73 static
74 rc_t CC KUrlFetcherCurlDestroy ( KUrlFetcherCurl *self )
75 {
76 self->kns_mgr->curl_easy_cleanup_fkt( self -> curl_handle );
77 KNSManagerRelease( self->kns_mgr );
78
79 if ( self -> spill_over_buff != NULL )
80 free( self -> spill_over_buff );
81 free ( self );
82 return 0;
83 }
84
85 static
86 size_t CC KUrlFetcherCurlCallback( void *ptr, size_t size, size_t nmemb, void *data )
87 {
88 size_t given_bytes = size * nmemb; /* calculate the size given in ptr */
89 KUrlFetcherCurl *self = ( KUrlFetcherCurl * )data;
90 if ( self != NULL )
91 {
92 if ( ( self -> in_buff + given_bytes ) > self -> buffsize )
93 {
94 /* the caller-provided buffer IS NOT enough... */
95 size_t to_copy = ( self -> buffsize - self -> in_buff );
96 if ( to_copy > 0 )
97 {
98 /* the caller-provided buffer can hold a part of it... */
99 if ( self -> buff )
100 {
101 memmove( &( self -> buff [ self -> in_buff ] ), ptr, given_bytes );
102 self -> in_buff += given_bytes;
103 }
104 }
105 else
106 {
107 /* the spill-over-buffer has to take care of the rest... */
108 size_t remaining = ( given_bytes - to_copy );
109 if ( self -> spill_over_buff == NULL )
110 self -> spill_over_buff = malloc( remaining );
111 else
112 self -> spill_over_buff = realloc( self -> spill_over_buff,
113 self -> spill_over_buffsize + remaining );
114 if ( self -> spill_over_buff != NULL )
115 {
116 memmove( &( self -> spill_over_buff [ self -> spill_over_buffsize ] ), ptr, remaining );
117 self -> spill_over_buffsize += remaining;
118 }
119 }
120 }
121 else
122 {
123 /* the caller-provided buffer IS enough... */
124 if ( self -> buff )
125 {
126 memmove( &( self -> buff [ self -> in_buff ] ), ptr, given_bytes );
127 self -> in_buff += given_bytes;
128 }
129 }
130 }
131 return given_bytes;
132 }
133
134
135 static
136 rc_t CC KUrlFetcherCurlRead( KUrlFetcherCurl *self, const char * uri,
137 void *dst, size_t to_read, size_t *num_read )
138 {
139 CURLcode rcc;
140
141 /* the virtual interface has already checked that self is not NULL */
142 if ( uri == NULL || dst == NULL || to_read == 0 )
143 return RC ( rcApp, rcFunction, rcConstructing, rcParam, rcNull );
144
145 self -> buff = dst;
146 self -> buffsize = to_read;
147 self -> in_buff = 0;
148
149 if ( self -> spill_over_buff != NULL )
150 free( self -> spill_over_buff );
151 self -> spill_over_buff = NULL;
152 self -> spill_over_buffsize = 0;
153
154 if ( self -> curl_handle == NULL )
155 return RC ( rcApp, rcFunction, rcConstructing, rcParam, rcNull );
156
157 rcc = self->kns_mgr->curl_easy_setopt_fkt( self -> curl_handle, CURLOPT_URL, uri );
158 if ( rcc != CURLE_OK )
159 {
160 if ( num_read ) *num_read = 0;
161 return RC ( rcApp, rcFunction, rcConstructing, rcParam, rcNull );
162 }
163 else
164 {
165 rcc = self->kns_mgr->curl_easy_perform_fkt( self-> curl_handle );
166 if ( rcc != CURLE_OK )
167 {
168 if ( num_read ) *num_read = 0;
169 return RC ( rcApp, rcFunction, rcConstructing, rcParam, rcNull );
170 }
171 }
172
173 if ( num_read )
174 *num_read = self -> in_buff;
175
176 return 0;
177 }
178
179 static KUrlFetcher_vt_v1 sKUrlFetcherCurl_vt =
180 {
181 1, 0,
182 KUrlFetcherCurlDestroy,
183 KUrlFetcherCurlRead,
184 };
185
186 LIB_EXPORT rc_t CC KUrlFetcherCurlMake( KUrlFetcher **fetcher, const bool verbose )
187 {
188 rc_t rc;
189
190 if ( fetcher == NULL )
191 rc = RC ( rcApp, rcFunction, rcConstructing, rcParam, rcNull );
192 else
193 {
194 KUrlFetcherCurl * tmp;
195 *fetcher = NULL;
196 tmp = malloc( sizeof( * tmp ) );
197 if ( tmp != NULL )
198 {
199 rc = KUrlFetcherInit ( &(tmp)->dad, (const KUrlFetcher_vt*)&sKUrlFetcherCurl_vt );
200 if ( rc != 0 )
201 free( tmp );
202 else
203 {
204 tmp -> buff = NULL;
205 tmp -> buffsize = 0;
206 tmp -> in_buff = 0;
207
208 tmp -> spill_over_buff = NULL;
209 tmp -> spill_over_buffsize = 0;
210
211 rc = KNSManagerMake( &tmp->kns_mgr );
212 if ( rc == 0 )
213 {
214 tmp -> curl_handle = tmp -> kns_mgr -> curl_easy_init_fkt();
215 if ( tmp -> curl_handle == NULL )
216 {
217 KNSManagerRelease( tmp->kns_mgr );
218 free( tmp );
219 rc = RC ( rcApp, rcFunction, rcConstructing, rcParam, rcNull );
220 }
221 else
222 {
223 CURLcode rcc;
224
225 if ( verbose )
226 tmp -> kns_mgr -> curl_easy_setopt_fkt( tmp -> curl_handle, CURLOPT_VERBOSE , 1 );
227
228 rcc = tmp -> kns_mgr -> curl_easy_setopt_fkt( tmp -> curl_handle, CURLOPT_WRITEFUNCTION, KUrlFetcherCurlCallback );
229 if ( rcc != CURLE_OK )
230 {
231 KNSManagerRelease( tmp -> kns_mgr );
232 free( tmp );
233 rc = RC ( rcApp, rcFunction, rcConstructing, rcParam, rcNull );
234 }
235 else
236 {
237 rcc = tmp -> kns_mgr -> curl_easy_setopt_fkt( tmp -> curl_handle, CURLOPT_WRITEDATA, (void *)tmp );
238 if ( rcc != CURLE_OK )
239 {
240 KNSManagerRelease( tmp -> kns_mgr );
241 free( tmp );
242 rc = RC ( rcApp, rcFunction, rcConstructing, rcParam, rcNull );
243 }
244 else
245 *fetcher = & tmp -> dad;
246 }
247 }
248 }
249 }
250 }
251 else
252 rc = RC( rcApp, rcFunction, rcListing, rcParam, rcNull );
253 }
254 return rc;
255 }