rewriting http.c and http.h, because the current version is insane
This commit is contained in:
parent
7a45e8b804
commit
121da584c0
|
@ -2,3 +2,4 @@ tmp
|
|||
*.o
|
||||
mount-http-dir
|
||||
*_test.txt
|
||||
*.kate-swp
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
- implement fseek
|
||||
- work on linklist again - append it so it includes curl data structure
|
||||
- implement function which generates the initial file list.
|
66
http.c
66
http.c
|
@ -1,49 +1,7 @@
|
|||
/*****************************************************************************
|
||||
*
|
||||
* 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_ .
|
||||
*
|
||||
* 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
|
||||
*
|
||||
* Copyright (c) 2003, 2017 Simtec Electronics
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* This example requires libcurl 7.9.7 or later.
|
||||
/**
|
||||
* \file http.c
|
||||
* \todo WARNING please fix url_feof
|
||||
*/
|
||||
/* <DESC>
|
||||
* implements an fopen() abstraction allowing reading from URLs
|
||||
* </DESC>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -54,6 +12,8 @@
|
|||
/* we use a global one for convenience */
|
||||
static CURLM *multi_handle;
|
||||
|
||||
static int num_transfers = 0;
|
||||
|
||||
/* curl calls this routine to get more data */
|
||||
static size_t write_callback(char *buffer, size_t size,
|
||||
size_t nitems, void *userp)
|
||||
|
@ -106,6 +66,7 @@ static size_t header_callback(char *buffer, size_t size,
|
|||
/* realloc succeeded increase buffer size*/
|
||||
url->header_len += size - rembuff;
|
||||
url->header = newbuff;
|
||||
url->header[url->header_len] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -248,6 +209,7 @@ URL_FILE *url_fopen(const char *url, const char *operation)
|
|||
|
||||
file = calloc(1, sizeof(URL_FILE));
|
||||
if (!file) {
|
||||
fprintf(stderr, "url_fopen: URL_FILE memory allocation failure.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -255,6 +217,9 @@ URL_FILE *url_fopen(const char *url, const char *operation)
|
|||
|
||||
curl_easy_setopt(file->handle, CURLOPT_URL, url);
|
||||
curl_easy_setopt(file->handle, CURLOPT_VERBOSE, 0L);
|
||||
curl_easy_setopt(file->handle, CURLOPT_TCP_KEEPALIVE, 1L);
|
||||
/* By default we don't want to download anything */
|
||||
curl_easy_setopt(file->handle, CURLOPT_NOBODY, 1L);
|
||||
|
||||
for (const char *c = operation; *c; c++) {
|
||||
switch (*c) {
|
||||
|
@ -263,14 +228,14 @@ URL_FILE *url_fopen(const char *url, const char *operation)
|
|||
CURLOPT_WRITEDATA, file);
|
||||
curl_easy_setopt(file->handle,
|
||||
CURLOPT_WRITEFUNCTION, write_callback);
|
||||
curl_easy_setopt(file->handle,
|
||||
CURLOPT_NOBODY, 0L);
|
||||
break;
|
||||
case 'h':
|
||||
curl_easy_setopt(file->handle,
|
||||
CURLOPT_HEADERDATA, file);
|
||||
curl_easy_setopt(file->handle,
|
||||
CURLOPT_HEADERFUNCTION, header_callback);
|
||||
curl_easy_setopt(file->handle,
|
||||
CURLOPT_NOBODY, 1L);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "url_fopen: invalid operation %c", *c);
|
||||
|
@ -284,8 +249,6 @@ URL_FILE *url_fopen(const char *url, const char *operation)
|
|||
|
||||
curl_multi_add_handle(multi_handle, file->handle);
|
||||
|
||||
start_fetching(file);
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
|
@ -383,6 +346,9 @@ void url_rewind(URL_FILE *file)
|
|||
file->buffer_pos = 0;
|
||||
file->buffer_len = 0;
|
||||
|
||||
start_fetching(file);
|
||||
free(file->header);
|
||||
file->header = NULL;
|
||||
file->header_len = 0;
|
||||
file->header_pos = 0;
|
||||
}
|
||||
|
||||
|
|
2
http.h
2
http.h
|
@ -6,8 +6,6 @@
|
|||
typedef struct {
|
||||
CURL *handle; /* handle */
|
||||
|
||||
int still_running; /* Is background url fetch still in progress */
|
||||
|
||||
char *buffer; /* buffer to store cached data*/
|
||||
size_t buffer_len; /* currently allocated buffers length */
|
||||
size_t buffer_pos; /* end of data in buffer*/
|
||||
|
|
29
test.c
29
test.c
|
@ -2,6 +2,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "link.h"
|
||||
#include "test.h"
|
||||
|
@ -17,31 +18,37 @@ int http_test()
|
|||
char buffer[256];
|
||||
const char *url;
|
||||
|
||||
url = "http://127.0.0.1/~fangfufu/test.txt";
|
||||
// url = "https://www.fangfufu.co.uk/~fangfufu/Unison-Windows-2.48.4.zip";
|
||||
/* ------------------------Test header-only--------------------------*/
|
||||
/* open the input file */
|
||||
handle = url_fopen(url, "h");
|
||||
if(!handle) {
|
||||
printf("couldn't url_fopen() %s\n", url);
|
||||
URL_FILE *header_handle = url_fopen(
|
||||
"http://ipv4.download.thinkbroadband.com/1GB.zip",
|
||||
"rh");
|
||||
if(!header_handle) {
|
||||
printf("couldn't url_fopen() \n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
// printf("start fgets\n");
|
||||
/* Read 2 character seem to be enough to get the header*/
|
||||
url_fgets(buffer, 2, handle);
|
||||
url_fgets(buffer, 256, header_handle);
|
||||
// printf("end fgets\n");
|
||||
|
||||
|
||||
/* Print the header */
|
||||
printf(handle->header);
|
||||
printf("\n");
|
||||
printf("accept-range: %d\n", handle->accept_range);
|
||||
printf("filesize: %d\n", handle->content_length);
|
||||
// printf(header_handle->header);
|
||||
|
||||
// printf("accept-range: %d\n", header_handle->accept_range);
|
||||
// printf("filesize: %d\n", header_handle->content_length);
|
||||
printf("test fgets");
|
||||
|
||||
|
||||
/* close the URL handle */
|
||||
url_fclose(handle);
|
||||
// url_fclose(header_handle);
|
||||
|
||||
/* ---------------------------Test fgets ----------------------------*/
|
||||
|
||||
/* open the input file */
|
||||
url = "http://127.0.0.1/~fangfufu/test.txt";
|
||||
handle = url_fopen(url, "h");
|
||||
if(!handle) {
|
||||
printf("couldn't url_fopen() %s\n", url);
|
||||
|
|
Loading…
Reference in New Issue