2002-05-13 03:28:10 -04:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
2003-08-11 17:34:52 -04:00
|
|
|
* This example source code introduces a c library buffered I/O interface to
|
|
|
|
* URL reads it supports fopen(), fread(), fgets(), feof(), fclose(),
|
|
|
|
* rewind(). Supported functions have identical prototypes to their normal c
|
|
|
|
* lib namesakes and are preceaded by url_ .
|
2002-05-13 03:28:10 -04:00
|
|
|
*
|
2003-08-11 17:34:52 -04:00
|
|
|
* Using this code you can replace your program's fopen() with url_fopen()
|
|
|
|
* and fread() with url_fread() and it become possible to read remote streams
|
|
|
|
* instead of (only) local files. Local files (ie those that can be directly
|
|
|
|
* fopened) will drop back to using the underlying clib implementations
|
2002-05-13 03:28:10 -04:00
|
|
|
*
|
2003-08-11 17:34:52 -04:00
|
|
|
* See the main() function at the bottom that shows an app that retrives from a
|
|
|
|
* specified url using fgets() and fread() and saves as two output files.
|
2002-05-13 03:28:10 -04:00
|
|
|
*
|
2010-02-26 17:58:24 -05:00
|
|
|
* Copyright (c) 2003 Simtec Electronics
|
2003-08-11 17:34:52 -04:00
|
|
|
*
|
|
|
|
* Re-implemented by Vincent Sanders <vince@kyllikki.org> with extensive
|
|
|
|
* reference to original curl example code
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2002-05-13 03:28:10 -04:00
|
|
|
*
|
|
|
|
* This example requires libcurl 7.9.7 or later.
|
|
|
|
*/
|
2015-06-18 05:38:54 -04:00
|
|
|
/* <DESC>
|
|
|
|
* implements an fopen() abstraction allowing reading from URLs
|
|
|
|
* </DESC>
|
|
|
|
*/
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2002-05-13 03:28:10 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2008-08-31 08:12:35 -04:00
|
|
|
#ifndef WIN32
|
|
|
|
# include <sys/time.h>
|
|
|
|
#endif
|
2003-01-27 05:25:20 -05:00
|
|
|
#include <stdlib.h>
|
2003-08-11 17:34:52 -04:00
|
|
|
#include <errno.h>
|
2002-05-13 03:28:10 -04:00
|
|
|
|
|
|
|
#include <curl/curl.h>
|
2003-01-27 05:25:20 -05:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
enum fcurl_type_e {
|
|
|
|
CFTYPE_NONE=0,
|
|
|
|
CFTYPE_FILE=1,
|
|
|
|
CFTYPE_CURL=2
|
|
|
|
};
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2003-08-11 17:34:52 -04:00
|
|
|
struct fcurl_data
|
|
|
|
{
|
2010-11-04 06:42:27 -04:00
|
|
|
enum fcurl_type_e type; /* type of handle */
|
|
|
|
union {
|
|
|
|
CURL *curl;
|
|
|
|
FILE *file;
|
|
|
|
} handle; /* handle */
|
|
|
|
|
|
|
|
char *buffer; /* buffer to store cached data*/
|
2010-12-16 18:07:34 -05:00
|
|
|
size_t buffer_len; /* currently allocated buffers length */
|
|
|
|
size_t buffer_pos; /* end of data in buffer*/
|
2010-11-04 06:42:27 -04:00
|
|
|
int still_running; /* Is background url fetch still in progress */
|
2002-05-13 03:28:10 -04:00
|
|
|
};
|
|
|
|
|
2003-08-11 17:34:52 -04:00
|
|
|
typedef struct fcurl_data URL_FILE;
|
|
|
|
|
|
|
|
/* exported functions */
|
2016-02-11 03:42:38 -05:00
|
|
|
URL_FILE *url_fopen(const char *url, const char *operation);
|
2003-08-11 17:34:52 -04:00
|
|
|
int url_fclose(URL_FILE *file);
|
|
|
|
int url_feof(URL_FILE *file);
|
|
|
|
size_t url_fread(void *ptr, size_t size, size_t nmemb, URL_FILE *file);
|
2010-12-16 18:07:34 -05:00
|
|
|
char * url_fgets(char *ptr, size_t size, URL_FILE *file);
|
2003-08-11 17:34:52 -04:00
|
|
|
void url_rewind(URL_FILE *file);
|
2002-05-13 03:28:10 -04:00
|
|
|
|
|
|
|
/* we use a global one for convenience */
|
|
|
|
CURLM *multi_handle;
|
|
|
|
|
2003-08-11 17:34:52 -04:00
|
|
|
/* curl calls this routine to get more data */
|
2010-11-04 06:42:27 -04:00
|
|
|
static size_t write_callback(char *buffer,
|
|
|
|
size_t size,
|
|
|
|
size_t nitems,
|
|
|
|
void *userp)
|
2002-05-13 03:28:10 -04:00
|
|
|
{
|
2010-11-04 06:42:27 -04:00
|
|
|
char *newbuff;
|
2010-12-16 18:07:34 -05:00
|
|
|
size_t rembuff;
|
2010-11-04 06:42:27 -04:00
|
|
|
|
|
|
|
URL_FILE *url = (URL_FILE *)userp;
|
|
|
|
size *= nitems;
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
rembuff=url->buffer_len - url->buffer_pos; /* remaining space in buffer */
|
|
|
|
|
|
|
|
if(size > rembuff) {
|
|
|
|
/* not enough space in buffer */
|
2016-02-11 03:42:38 -05:00
|
|
|
newbuff=realloc(url->buffer, url->buffer_len + (size - rembuff));
|
2010-11-04 06:42:27 -04:00
|
|
|
if(newbuff==NULL) {
|
2016-02-11 03:42:38 -05:00
|
|
|
fprintf(stderr, "callback buffer grow failed\n");
|
2010-11-04 06:42:27 -04:00
|
|
|
size=rembuff;
|
|
|
|
}
|
|
|
|
else {
|
2015-06-08 07:22:54 -04:00
|
|
|
/* realloc succeeded increase buffer size*/
|
2010-11-04 06:42:27 -04:00
|
|
|
url->buffer_len+=size - rembuff;
|
|
|
|
url->buffer=newbuff;
|
|
|
|
}
|
|
|
|
}
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
memcpy(&url->buffer[url->buffer_pos], buffer, size);
|
|
|
|
url->buffer_pos += size;
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
return size;
|
2003-08-11 17:34:52 -04:00
|
|
|
}
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2003-08-11 17:34:52 -04:00
|
|
|
/* use to attempt to fill the read buffer up to requested number of bytes */
|
2010-12-16 18:07:34 -05:00
|
|
|
static int fill_buffer(URL_FILE *file, size_t want)
|
2003-08-11 17:34:52 -04:00
|
|
|
{
|
2010-11-04 06:42:27 -04:00
|
|
|
fd_set fdread;
|
|
|
|
fd_set fdwrite;
|
|
|
|
fd_set fdexcep;
|
|
|
|
struct timeval timeout;
|
|
|
|
int rc;
|
2014-11-14 03:59:46 -05:00
|
|
|
CURLMcode mc; /* curl_multi_fdset() return code */
|
2010-11-04 06:42:27 -04:00
|
|
|
|
|
|
|
/* only attempt to fill buffer if transactions still running and buffer
|
2015-06-08 07:22:54 -04:00
|
|
|
* doesn't exceed required size already
|
2010-11-04 06:42:27 -04:00
|
|
|
*/
|
|
|
|
if((!file->still_running) || (file->buffer_pos > want))
|
|
|
|
return 0;
|
2003-01-27 05:25:20 -05:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
/* attempt to fill buffer */
|
|
|
|
do {
|
|
|
|
int maxfd = -1;
|
|
|
|
long curl_timeo = -1;
|
|
|
|
|
|
|
|
FD_ZERO(&fdread);
|
|
|
|
FD_ZERO(&fdwrite);
|
|
|
|
FD_ZERO(&fdexcep);
|
|
|
|
|
|
|
|
/* set a suitable timeout to fail on */
|
|
|
|
timeout.tv_sec = 60; /* 1 minute */
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
curl_multi_timeout(multi_handle, &curl_timeo);
|
|
|
|
if(curl_timeo >= 0) {
|
|
|
|
timeout.tv_sec = curl_timeo / 1000;
|
|
|
|
if(timeout.tv_sec > 1)
|
|
|
|
timeout.tv_sec = 1;
|
|
|
|
else
|
|
|
|
timeout.tv_usec = (curl_timeo % 1000) * 1000;
|
2003-08-11 17:34:52 -04:00
|
|
|
}
|
2010-11-04 06:42:27 -04:00
|
|
|
|
|
|
|
/* get file descriptors from the transfers */
|
2014-11-14 03:59:46 -05:00
|
|
|
mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
|
2010-11-04 06:42:27 -04:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
if(mc != CURLM_OK) {
|
2014-11-14 03:59:46 -05:00
|
|
|
fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
|
|
|
|
break;
|
|
|
|
}
|
2010-11-04 06:42:27 -04:00
|
|
|
|
2014-11-14 03:59:46 -05:00
|
|
|
/* On success the value of maxfd is guaranteed to be >= -1. We call
|
2014-11-17 02:26:03 -05:00
|
|
|
select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
|
|
|
|
no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
|
|
|
|
to sleep 100ms, which is the minimum suggested value in the
|
2014-11-14 03:59:46 -05:00
|
|
|
curl_multi_fdset() doc. */
|
|
|
|
|
|
|
|
if(maxfd == -1) {
|
2014-11-17 02:26:03 -05:00
|
|
|
#ifdef _WIN32
|
2014-11-14 03:59:46 -05:00
|
|
|
Sleep(100);
|
|
|
|
rc = 0;
|
2014-11-17 02:26:03 -05:00
|
|
|
#else
|
|
|
|
/* Portable sleep for platforms other than Windows. */
|
|
|
|
struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
|
|
|
|
rc = select(0, NULL, NULL, NULL, &wait);
|
2014-11-14 03:59:46 -05:00
|
|
|
#endif
|
2014-11-17 02:26:03 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Note that on some platforms 'timeout' may be modified by select().
|
|
|
|
If you need access to the original value save a copy beforehand. */
|
2014-11-14 03:59:46 -05:00
|
|
|
rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
|
|
|
|
}
|
2010-11-04 06:42:27 -04:00
|
|
|
|
|
|
|
switch(rc) {
|
|
|
|
case -1:
|
|
|
|
/* select error */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
default:
|
|
|
|
/* timeout or readable/writable sockets */
|
|
|
|
curl_multi_perform(multi_handle, &file->still_running);
|
|
|
|
break;
|
2003-08-11 17:34:52 -04:00
|
|
|
}
|
2010-11-04 06:42:27 -04:00
|
|
|
} while(file->still_running && (file->buffer_pos < want));
|
|
|
|
return 1;
|
2002-05-13 03:28:10 -04:00
|
|
|
}
|
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
/* use to remove want bytes from the front of a files buffer */
|
2015-06-08 07:47:33 -04:00
|
|
|
static int use_buffer(URL_FILE *file, size_t want)
|
2010-11-04 06:42:27 -04:00
|
|
|
{
|
|
|
|
/* sort out buffer */
|
|
|
|
if((file->buffer_pos - want) <=0) {
|
|
|
|
/* ditch buffer - write will recreate */
|
2015-03-11 12:41:01 -04:00
|
|
|
free(file->buffer);
|
2010-11-04 06:42:27 -04:00
|
|
|
file->buffer=NULL;
|
|
|
|
file->buffer_pos=0;
|
|
|
|
file->buffer_len=0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* move rest down make it available for later */
|
|
|
|
memmove(file->buffer,
|
|
|
|
&file->buffer[want],
|
|
|
|
(file->buffer_pos - want));
|
|
|
|
|
|
|
|
file->buffer_pos -= want;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
URL_FILE *url_fopen(const char *url, const char *operation)
|
2002-05-13 03:28:10 -04:00
|
|
|
{
|
2010-11-04 06:42:27 -04:00
|
|
|
/* this code could check for URLs or types in the 'url' and
|
2015-06-08 07:22:54 -04:00
|
|
|
basically use the real fopen() for standard files */
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
URL_FILE *file;
|
|
|
|
(void)operation;
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
file = malloc(sizeof(URL_FILE));
|
|
|
|
if(!file)
|
|
|
|
return NULL;
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
memset(file, 0, sizeof(URL_FILE));
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
if((file->handle.file=fopen(url, operation)))
|
2010-11-04 06:42:27 -04:00
|
|
|
file->type = CFTYPE_FILE; /* marked as URL */
|
|
|
|
|
|
|
|
else {
|
|
|
|
file->type = CFTYPE_CURL; /* marked as URL */
|
|
|
|
file->handle.curl = curl_easy_init();
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
curl_easy_setopt(file->handle.curl, CURLOPT_URL, url);
|
|
|
|
curl_easy_setopt(file->handle.curl, CURLOPT_WRITEDATA, file);
|
|
|
|
curl_easy_setopt(file->handle.curl, CURLOPT_VERBOSE, 0L);
|
|
|
|
curl_easy_setopt(file->handle.curl, CURLOPT_WRITEFUNCTION, write_callback);
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
if(!multi_handle)
|
|
|
|
multi_handle = curl_multi_init();
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
curl_multi_add_handle(multi_handle, file->handle.curl);
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
/* lets start the fetch */
|
|
|
|
curl_multi_perform(multi_handle, &file->still_running);
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
if((file->buffer_pos == 0) && (!file->still_running)) {
|
|
|
|
/* if still_running is 0 now, we should return NULL */
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
/* make sure the easy handle is not in the multi handle anymore */
|
|
|
|
curl_multi_remove_handle(multi_handle, file->handle.curl);
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
/* cleanup */
|
|
|
|
curl_easy_cleanup(file->handle.curl);
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
free(file);
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
file = NULL;
|
2003-08-11 17:34:52 -04:00
|
|
|
}
|
2010-11-04 06:42:27 -04:00
|
|
|
}
|
|
|
|
return file;
|
2002-05-13 03:28:10 -04:00
|
|
|
}
|
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
int url_fclose(URL_FILE *file)
|
2002-05-13 03:28:10 -04:00
|
|
|
{
|
2010-11-04 06:42:27 -04:00
|
|
|
int ret=0;/* default is good return */
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
switch(file->type) {
|
|
|
|
case CFTYPE_FILE:
|
|
|
|
ret=fclose(file->handle.file); /* passthrough */
|
|
|
|
break;
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
case CFTYPE_CURL:
|
|
|
|
/* make sure the easy handle is not in the multi handle anymore */
|
|
|
|
curl_multi_remove_handle(multi_handle, file->handle.curl);
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
/* cleanup */
|
|
|
|
curl_easy_cleanup(file->handle.curl);
|
|
|
|
break;
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
default: /* unknown or supported type - oh dear */
|
|
|
|
ret=EOF;
|
|
|
|
errno=EBADF;
|
|
|
|
break;
|
|
|
|
}
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2015-03-11 12:41:01 -04:00
|
|
|
free(file->buffer);/* free any allocated buffer space */
|
2010-11-04 06:42:27 -04:00
|
|
|
free(file);
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
return ret;
|
2003-08-11 17:34:52 -04:00
|
|
|
}
|
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
int url_feof(URL_FILE *file)
|
2002-05-13 03:28:10 -04:00
|
|
|
{
|
2010-11-04 06:42:27 -04:00
|
|
|
int ret=0;
|
|
|
|
|
|
|
|
switch(file->type) {
|
|
|
|
case CFTYPE_FILE:
|
|
|
|
ret=feof(file->handle.file);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CFTYPE_CURL:
|
|
|
|
if((file->buffer_pos == 0) && (!file->still_running))
|
|
|
|
ret = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: /* unknown or supported type - oh dear */
|
|
|
|
ret=-1;
|
|
|
|
errno=EBADF;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
2003-08-11 17:34:52 -04:00
|
|
|
}
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
size_t url_fread(void *ptr, size_t size, size_t nmemb, URL_FILE *file)
|
2003-08-11 17:34:52 -04:00
|
|
|
{
|
2010-11-04 06:42:27 -04:00
|
|
|
size_t want;
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
switch(file->type) {
|
|
|
|
case CFTYPE_FILE:
|
2016-02-11 03:42:38 -05:00
|
|
|
want=fread(ptr, size, nmemb, file->handle.file);
|
2010-11-04 06:42:27 -04:00
|
|
|
break;
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
case CFTYPE_CURL:
|
|
|
|
want = nmemb * size;
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
fill_buffer(file, want);
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
/* check if theres data in the buffer - if not fill_buffer()
|
|
|
|
* either errored or EOF */
|
|
|
|
if(!file->buffer_pos)
|
|
|
|
return 0;
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
/* ensure only available data is considered */
|
|
|
|
if(file->buffer_pos < want)
|
|
|
|
want = file->buffer_pos;
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
/* xfer data to caller */
|
|
|
|
memcpy(ptr, file->buffer, want);
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
use_buffer(file, want);
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
want = want / size; /* number of items */
|
|
|
|
break;
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
default: /* unknown or supported type - oh dear */
|
|
|
|
want=0;
|
|
|
|
errno=EBADF;
|
|
|
|
break;
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
}
|
|
|
|
return want;
|
2002-05-13 03:28:10 -04:00
|
|
|
}
|
|
|
|
|
2010-12-16 18:07:34 -05:00
|
|
|
char *url_fgets(char *ptr, size_t size, URL_FILE *file)
|
2003-08-11 17:34:52 -04:00
|
|
|
{
|
2010-12-16 18:07:34 -05:00
|
|
|
size_t want = size - 1;/* always need to leave room for zero termination */
|
|
|
|
size_t loop;
|
2010-11-04 06:42:27 -04:00
|
|
|
|
|
|
|
switch(file->type) {
|
|
|
|
case CFTYPE_FILE:
|
2015-06-08 07:47:33 -04:00
|
|
|
ptr = fgets(ptr, (int)size, file->handle.file);
|
2010-11-04 06:42:27 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CFTYPE_CURL:
|
2016-02-11 03:42:38 -05:00
|
|
|
fill_buffer(file, want);
|
2010-11-04 06:42:27 -04:00
|
|
|
|
|
|
|
/* check if theres data in the buffer - if not fill either errored or
|
|
|
|
* EOF */
|
|
|
|
if(!file->buffer_pos)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* ensure only available data is considered */
|
|
|
|
if(file->buffer_pos < want)
|
|
|
|
want = file->buffer_pos;
|
|
|
|
|
|
|
|
/*buffer contains data */
|
|
|
|
/* look for newline or eof */
|
|
|
|
for(loop=0;loop < want;loop++) {
|
|
|
|
if(file->buffer[loop] == '\n') {
|
|
|
|
want=loop+1;/* include newline */
|
2004-10-06 03:50:18 -04:00
|
|
|
break;
|
2010-11-04 06:42:27 -04:00
|
|
|
}
|
|
|
|
}
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
/* xfer data to caller */
|
|
|
|
memcpy(ptr, file->buffer, want);
|
|
|
|
ptr[want]=0;/* allways null terminate */
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
use_buffer(file, want);
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
break;
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
default: /* unknown or supported type - oh dear */
|
|
|
|
ptr=NULL;
|
|
|
|
errno=EBADF;
|
|
|
|
break;
|
|
|
|
}
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
return ptr;/*success */
|
2003-08-11 17:34:52 -04:00
|
|
|
}
|
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
void url_rewind(URL_FILE *file)
|
2003-08-11 17:34:52 -04:00
|
|
|
{
|
2010-11-04 06:42:27 -04:00
|
|
|
switch(file->type) {
|
|
|
|
case CFTYPE_FILE:
|
|
|
|
rewind(file->handle.file); /* passthrough */
|
|
|
|
break;
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
case CFTYPE_CURL:
|
|
|
|
/* halt transaction */
|
|
|
|
curl_multi_remove_handle(multi_handle, file->handle.curl);
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
/* restart */
|
|
|
|
curl_multi_add_handle(multi_handle, file->handle.curl);
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
/* ditch buffer - write will recreate - resets stream pos*/
|
2015-03-11 12:41:01 -04:00
|
|
|
free(file->buffer);
|
2010-11-04 06:42:27 -04:00
|
|
|
file->buffer=NULL;
|
|
|
|
file->buffer_pos=0;
|
|
|
|
file->buffer_len=0;
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
break;
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
default: /* unknown or supported type - oh dear */
|
|
|
|
break;
|
|
|
|
}
|
2003-08-11 17:34:52 -04:00
|
|
|
}
|
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
#define FGETSFILE "fgets.test"
|
|
|
|
#define FREADFILE "fread.test"
|
|
|
|
#define REWINDFILE "rewind.test"
|
|
|
|
|
2003-08-11 17:34:52 -04:00
|
|
|
/* Small main program to retrive from a url using fgets and fread saving the
|
|
|
|
* output to two test files (note the fgets method will corrupt binary files if
|
|
|
|
* they contain 0 chars */
|
2010-11-04 06:42:27 -04:00
|
|
|
int main(int argc, char *argv[])
|
2002-05-13 03:28:10 -04:00
|
|
|
{
|
2010-11-04 06:42:27 -04:00
|
|
|
URL_FILE *handle;
|
|
|
|
FILE *outf;
|
|
|
|
|
2015-06-08 07:47:33 -04:00
|
|
|
size_t nread;
|
2010-11-04 06:42:27 -04:00
|
|
|
char buffer[256];
|
|
|
|
const char *url;
|
|
|
|
|
|
|
|
if(argc < 2)
|
|
|
|
url="http://192.168.7.3/testfile";/* default to testurl */
|
|
|
|
else
|
|
|
|
url=argv[1];/* use passed url */
|
|
|
|
|
|
|
|
/* copy from url line by line with fgets */
|
2016-02-11 03:42:38 -05:00
|
|
|
outf=fopen(FGETSFILE, "wb+");
|
2010-11-04 06:42:27 -04:00
|
|
|
if(!outf) {
|
|
|
|
perror("couldn't open fgets output file\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
handle = url_fopen(url, "r");
|
|
|
|
if(!handle) {
|
|
|
|
printf("couldn't url_fopen() %s\n", url);
|
|
|
|
fclose(outf);
|
|
|
|
return 2;
|
|
|
|
}
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
while(!url_feof(handle)) {
|
2016-02-11 03:42:38 -05:00
|
|
|
url_fgets(buffer, sizeof(buffer), handle);
|
|
|
|
fwrite(buffer, 1, strlen(buffer), outf);
|
2010-11-04 06:42:27 -04:00
|
|
|
}
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
url_fclose(handle);
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
fclose(outf);
|
2003-08-11 17:34:52 -04:00
|
|
|
|
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
/* Copy from url with fread */
|
2016-02-11 03:42:38 -05:00
|
|
|
outf=fopen(FREADFILE, "wb+");
|
2010-11-04 06:42:27 -04:00
|
|
|
if(!outf) {
|
|
|
|
perror("couldn't open fread output file\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
handle = url_fopen("testfile", "r");
|
|
|
|
if(!handle) {
|
|
|
|
printf("couldn't url_fopen() testfile\n");
|
2003-08-11 17:34:52 -04:00
|
|
|
fclose(outf);
|
2010-11-04 06:42:27 -04:00
|
|
|
return 2;
|
|
|
|
}
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
do {
|
2015-06-08 07:47:33 -04:00
|
|
|
nread = url_fread(buffer, 1, sizeof(buffer), handle);
|
2016-02-11 03:42:38 -05:00
|
|
|
fwrite(buffer, 1, nread, outf);
|
2010-11-04 06:42:27 -04:00
|
|
|
} while(nread);
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
url_fclose(handle);
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
fclose(outf);
|
2003-08-11 17:34:52 -04:00
|
|
|
|
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
/* Test rewind */
|
2016-02-11 03:42:38 -05:00
|
|
|
outf=fopen(REWINDFILE, "wb+");
|
2010-11-04 06:42:27 -04:00
|
|
|
if(!outf) {
|
|
|
|
perror("couldn't open fread output file\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
handle = url_fopen("testfile", "r");
|
|
|
|
if(!handle) {
|
|
|
|
printf("couldn't url_fopen() testfile\n");
|
2003-08-11 17:34:52 -04:00
|
|
|
fclose(outf);
|
2010-11-04 06:42:27 -04:00
|
|
|
return 2;
|
|
|
|
}
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
nread = url_fread(buffer, 1, sizeof(buffer), handle);
|
|
|
|
fwrite(buffer, 1, nread, outf);
|
2010-11-04 06:42:27 -04:00
|
|
|
url_rewind(handle);
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
buffer[0]='\n';
|
2016-02-11 03:42:38 -05:00
|
|
|
fwrite(buffer, 1, 1, outf);
|
2003-08-11 17:34:52 -04:00
|
|
|
|
2016-02-11 03:42:38 -05:00
|
|
|
nread = url_fread(buffer, 1, sizeof(buffer), handle);
|
|
|
|
fwrite(buffer, 1, nread, outf);
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
url_fclose(handle);
|
2003-01-27 05:25:20 -05:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
fclose(outf);
|
2002-05-13 03:28:10 -04:00
|
|
|
|
2010-11-04 06:42:27 -04:00
|
|
|
return 0;/* all done */
|
2002-05-13 03:28:10 -04:00
|
|
|
}
|