Several years ago I implemented knetfile for accessing remote files on ftp and http as if they are local (see also this blog post). I have been using the implementation for a while and the end users like the feature. However, with the increasing use of https among file sharing and cloud computing providers, supporting secured connection becomes more important. Several users have requested this feature. As a response, I implemented a new library kurl on top of libcurl.
Kurl is inspired by and learns from fopen.c, an example from the curl source code package. It supports random access and uses fixed-length buffer. It also fixes an issue where we may be waiting too long for select(). The APIs largely resemble knetfile, zlib and stdio. The following is a small example:
#include <stdio.h>
#include "kurl.h"
int main() {
kurl_t *fp;
unsigned char buf[256];
fp = kurl_open("https://github.com", 0);
kurl_seek(fp, 100, SEEK_SET);
kurl_read(fp, buf, 256);
kurl_close(fp);
return 0;
}
In addition, kurl.c also comes with a simple main() function to achieve the basic curl functionality, which can be compiled with:
gcc -g -Wall -O2 -lcurl -DKURL_MAIN kurl.c -o kurl
Here are a little more details about kurl:
- Two-file library. No installation.
- The only dependency is libcurl, though libcurl may further depend on other libraries: e.g. openssl for https; libssh2 for sftp.
- Directly accesses files in S3 with
kurl_open("s3://bucket/object", 0)AWS credentials are either provided to kurl_open(), or by default read from ~/.awssecret (AccessKeyId and SecretKey on two lines; see Tim Kay’s aws tool for details).
- Compilable with C++ compilers.
- Buffered reading with a fixed buffer length. No potential buffer bloat.
Leave a Reply