Add HTTP protocol using curl v4
PATCH 1/1 Add HTTP protocol using curl v4
From: Alexander Graf <[email protected]>
Currently Qemu can read from posix I/O and NBD. This patch adds a third protocol to the game: HTTP.
In certain situations it can be useful to access HTTP data directly, for example if you want to try out an http provided OS image, but don't know if you want to download it yet.
Using this patch you can now try it on on the fly. Just use it like:
qemu -cdrom http://host/path/my.iso
In order to not reinvent the wheel, this patch uses libcurl.
v2 changes:
- fix the segfault (yay!)
- implement AIO
v3 changes:
- remove synchronous API
- implement caching
v4 changes:
- enable other protocols (HTTPS, FTP, FTPS, TFTP, SFTP, SCP) (I only tested FTP so far, but they _should_ work)
--- Makefile | 6 + Makefile.target | 2 +- block-http.c | 534 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ block.c | 9 + block.h | 7 + configure | 25 +++ 6 files changed, 582 insertions(+), 1 deletions(-) create mode 100644 block-http.c
Makefile +6 -0
@@ -77,6 +77,10 @@ endif BLOCK_OBJS += block-raw-posix.o endif +ifdef CONFIG_CURL +BLOCK_OBJS += block-http.o +endif + ###################################################################### # libqemu_common.a: Target independent part of system emulation. The # long term path is to suppress *all* target specific code in case of
@@ -185,6 +189,8 @@ endif LIBS+=$(VDE_LIBS) +LIBS+=$(CURL_LIBS) + cocoa.o: cocoa.m keymaps.o: keymaps.c keymaps.h
Makefile.target +1 -1
@@ -735,7 +735,7 @@ endif vl.o: qemu-options.h -$(QEMU_PROG): LIBS += $(SDL_LIBS) $(COCOA_LIBS) $(CURSES_LIBS) $(BRLAPI_LIBS) $(VDE_LIBS) +$(QEMU_PROG): LIBS += $(SDL_LIBS) $(COCOA_LIBS) $(CURSES_LIBS) $(BRLAPI_LIBS) $(VDE_LIBS) $(CURL_LIBS) $(QEMU_PROG): $(OBJS) ../libqemu_common.a libqemu.a $(LINK)
block-http.c new file +534 -0
534 lines not on this page · raw message
block.c +9 -0
@@ -1504,6 +1504,15 @@ void bdrv_init(void)bdrv_register (&bdrv_qcow2);bdrv_register (&bdrv_parallels);bdrv_register (&bdrv_nbd); +#ifdef CONFIG_CURL +bdrv_register (&bdrv_http); +bdrv_register (&bdrv_https); +bdrv_register (&bdrv_ftp); +bdrv_register (&bdrv_ftps); +bdrv_register (&bdrv_sftp); +bdrv_register (&bdrv_scp); +bdrv_register (&bdrv_tftp); +#endif } void aio_pool_init(AIOPool *pool, int aiocb_size,
block.h +7 -0
@@ -20,6 +20,13 @@ extern BlockDriver bdrv_vvfat;extern BlockDriver bdrv_qcow2;extern BlockDriver bdrv_parallels;extern BlockDriver bdrv_nbd; +extern BlockDriver bdrv_http; +extern BlockDriver bdrv_https; +extern BlockDriver bdrv_ftp; +extern BlockDriver bdrv_ftps; +extern BlockDriver bdrv_sftp; +extern BlockDriver bdrv_scp; +extern BlockDriver bdrv_tftp; typedef struct BlockDriverInfo { /* in bytes, 0 if irrelevant */
configure +25 -0
@@ -181,6 +181,7 @@ bsd_user="no" build_docs="no" uname_release="" curses="yes" +curl="yes" pthread="yes" aio="yes" io_thread="no"
@@ -477,6 +478,8 @@ for opt do ;; --disable-curses) curses="no" ;; + --disable-curl) curl="no" + ;; --disable-nptl) nptl="no" ;; --enable-mixemu) mixemu="yes"
@@ -600,6 +603,7 @@ echo " --disable-brlapi disable BrlAPI" echo " --disable-vnc-tls disable TLS encryption for VNC server" echo " --disable-vnc-sasl disable SASL encryption for VNC server" echo " --disable-curses disable curses output" +echo " --disable-curl disable curl connectivity" echo " --disable-bluez disable bluez stack connectivity" echo " --disable-kvm disable KVM acceleration support" echo " --disable-nptl disable usermode NPTL support"
@@ -1062,6 +1066,21 @@ EOF fi # test "$curses" ########################################## +# curl probe + +if test "$curl" = "yes" ; then + curl=no + cat > $TMPC << EOF +#include <curl/curl.h> +int main(void) { return curl_easy_init(); } +EOF + curl_libs=`curl-config --libs` + if $cc $ARCH_CFLAGS $curl_libs -o $TMPE $TMPC > /dev/null 2> /dev/null ; then + curl=yes + fi +fi # test "$curl" + +########################################## # bluez support probe if test "$bluez" = "yes" ; then `pkg-config bluez 2> /dev/null` || bluez="no"
@@ -1311,6 +1330,7 @@ if test "$sdl" != "no" ; then echo "SDL static link $sdl_static" fi echo "curses support $curses" +echo "curl support $curl" echo "mingw32 support $mingw32" echo "Audio drivers $audio_drv_list" echo "Extra audio cards $audio_card_list"
@@ -1637,6 +1657,11 @@ fi if test "$inotify" = "yes" ; then echo "#define CONFIG_INOTIFY 1" >> $config_h fi +if test "$curl" = "yes" ; then + echo "CONFIG_CURL=yes" >> $config_mak + echo "CURL_LIBS=$curl_libs" >> $config_mak + echo "#define CONFIG_CURL 1" >> $config_h +fi if test "$brlapi" = "yes" ; then echo "CONFIG_BRLAPI=yes" >> $config_mak echo "#define CONFIG_BRLAPI 1" >> $config_h
Signature · 1 line
1.6.0.2
Quoted 22 lines · [email protected] wrote:
From: Alexander Graf <[email protected]>
Currently Qemu can read from posix I/O and NBD. This patch adds a third protocol to the game: HTTP.
In certain situations it can be useful to access HTTP data directly, for example if you want to try out an http provided OS image, but don't know if you want to download it yet.
Using this patch you can now try it on on the fly. Just use it like:
qemu -cdrom http://host/path/my.iso
In order to not reinvent the wheel, this patch uses libcurl.
v2 changes:
- fix the segfault (yay!)
- implement AIOv3 changes:
Quoted 8 lines
- remove synchronous API
- implement cachingv4 changes:
- enable other protocols (HTTPS, FTP, FTPS, TFTP, SFTP, SCP) (I only tested FTP so far, but they _should_ work)
So now all of the references to http are a big fat lie? :-)
Perhaps we should s/http/curl/g.
Quoted 32 lines
block-http.c new file +25
-0@@ -0,0 +1,534 @@ +/* +* QEMU Block driver for HTTP images +* +* Copyright (c) 2009 Alexander Graf <[email protected]> +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ +#include "qemu-common.h" +#include "block_int.h"
block-http.c line 35
Quoted 10 lines
block-http.c new file +10
-0+#include <curl/curl.h> + +#define DEBUG +#define DEBUG_VERBOSE + +#ifdef DEBUG +#define printd printf +#else +#define printd (x, ...) +#endif
I'd prefer dprintf(). Also, the standard way to do this is:
//#define DEBUG_CURL
#ifdef DEBUG_CURL
#define dprintf(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0) #else
#define dprintf(fmt, ...) do { } while (0)
#endif
Also, make sure DEBUG_CURL is off by default (you have debug enabled in this patch).
block-http.c line 109
Quoted 11 lines
block-http.c new file +10
-0+#define HTTP_NUM_STATES 8 + +static size_t http_size_cb (void *ptr,size_t size,size_t nmemb,void *opaque) +{ +HTTPState *s = ((HTTPState *)opaque); +size_t realsize = size * nmemb; +uint64_t fsize; + +if (sscanf (ptr,"Content-Length: %lld" , &fsize) ==1 ) + s->s->len = fsize;
How does this work with other protocols? Certainly, there isn't a Content-Length header with scp?
block-http.c line 151
Quoted 6 lines
block-http.c new file +5
-0+static int http_find_buf (BDRVHTTPState *s,long long start,long long len, +HTTPAIOCB *acb) +{ +int i; +long long end = start + len;
size_t is the right type to represent lengths/offsets in a memory buffer.
block-http.c line 417
Quoted 5 lines
block-http.c new file +4
-0+static void http_close (BlockDriverState *bs) +{ +BDRVHTTPState *s = bs->opaque; +int i;
You strdup() url but don't free it. I'd suggest building qemu-io and running it under valgrind to verify that you aren't leaking other places too.
Quoted 88 lines
block-http.c new file +87
-0+BlockDriver bdrv_http = { + .format_name ="http" , + .protocol_name ="http" , + + .instance_size =sizeof (BDRVHTTPState), + .bdrv_open = http_open, + .bdrv_close = http_close, + .bdrv_getlength = http_getlength, + + .aiocb_size =sizeof (HTTPAIOCB ), + .bdrv_aio_readv = http_aio_readv, + .bdrv_aio_cancel = http_aio_cancel, +}; + +BlockDriver bdrv_https = { + .format_name ="https" , + .protocol_name ="https" , + + .instance_size =sizeof (BDRVHTTPState), + .bdrv_open = http_open, + .bdrv_close = http_close, + .bdrv_getlength = http_getlength, + + .aiocb_size =sizeof (HTTPAIOCB ), + .bdrv_aio_readv = http_aio_readv, + .bdrv_aio_cancel = http_aio_cancel, +}; + +BlockDriver bdrv_ftp = { + .format_name ="ftp" , + .protocol_name ="ftp" , + + .instance_size =sizeof (BDRVHTTPState), + .bdrv_open = http_open, + .bdrv_close = http_close, + .bdrv_getlength = http_getlength, + + .aiocb_size =sizeof (HTTPAIOCB ), + .bdrv_aio_readv = http_aio_readv, + .bdrv_aio_cancel = http_aio_cancel, +}; + +BlockDriver bdrv_ftps = { + .format_name ="ftps" , + .protocol_name ="ftps" , + + .instance_size =sizeof (BDRVHTTPState), + .bdrv_open = http_open, + .bdrv_close = http_close, + .bdrv_getlength = http_getlength, + + .aiocb_size =sizeof (HTTPAIOCB ), + .bdrv_aio_readv = http_aio_readv, + .bdrv_aio_cancel = http_aio_cancel, +}; + +BlockDriver bdrv_sftp = { + .format_name ="sftp" , + .protocol_name ="sftp" , + + .instance_size =sizeof (BDRVHTTPState), + .bdrv_open = http_open, + .bdrv_close = http_close, + .bdrv_getlength = http_getlength, + + .aiocb_size =sizeof (HTTPAIOCB ), + .bdrv_aio_readv = http_aio_readv, + .bdrv_aio_cancel = http_aio_cancel, +}; + +BlockDriver bdrv_scp = { + .format_name ="scp" , + .protocol_name ="scp" , + + .instance_size =sizeof (BDRVHTTPState), + .bdrv_open = http_open, + .bdrv_close = http_close, + .bdrv_getlength = http_getlength, + + .aiocb_size =sizeof (HTTPAIOCB ), + .bdrv_aio_readv = http_aio_readv, + .bdrv_aio_cancel = http_aio_cancel, +}; + +BlockDriver bdrv_tftp = { + .format_name ="tftp" , + .protocol_name ="tftp" ,
block-http.c line 534
Quoted 10 lines
block-http.c new file +10
-0+ + .instance_size = sizeof(BDRVHTTPState), + .bdrv_open = http_open, + .bdrv_close = http_close, + .bdrv_getlength = http_getlength, + + .aiocb_size = sizeof(HTTPAIOCB), + .bdrv_aio_readv = http_aio_readv, + .bdrv_aio_cancel = http_aio_cancel, +};
It's rather slick to get all of these for free.
Regards,
Anthony Liguori
Quoted 28 lines · Anthony Liguori
Quoted 27 lines · [email protected] wrote:
From: Alexander Graf <[email protected]>
Currently Qemu can read from posix I/O and NBD. This patch adds a third protocol to the game: HTTP.
In certain situations it can be useful to access HTTP data directly, for example if you want to try out an http provided OS image, but don't know if you want to download it yet.
Using this patch you can now try it on on the fly. Just use it like:
qemu -cdrom http://host/path/my.iso
In order to not reinvent the wheel, this patch uses libcurl.
v2 changes:
- fix the segfault (yay!)
- implement AIOv3 changes:
- remove synchronous API
- implement cachingv4 changes:
Quoted 7 lines
Quoted 3 lines
- enable other protocols (HTTPS, FTP, FTPS, TFTP, SFTP, SCP) (I only tested FTP so far, but they _should_ work)
So now all of the references to http are a big fat lie? :-)
Perhaps we should s/http/curl/g.
Sounds like a good idea, yeah.
Quoted 57 lines
Quoted 44 lines
block-http.c new file +6
-0@@ -0,0 +1,534 @@ +/* +* QEMU Block driver for HTTP images +* +* Copyright (c) 2009 Alexander Graf <[email protected]> +* +* Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */block-http.c new file +2
-0+#include "qemu-common.h" +#include "block_int.h" block-http.c line 35
Quoted 10 lines
block-http.c new file +10
-0+#include <curl/curl.h> + +#define DEBUG +#define DEBUG_VERBOSE + +#ifdef DEBUG +#define printd printf +#else +#define printd (x, ...) +#endif I'd prefer
dprintf(). Also, the standard way to do this is:
Quoted 7 lines
//#define
DEBUG_CURL#ifdef
DEBUG_CURL
#definedprintf(fmt, ...)do {printf(fmt, ## __VA_ARGS__); } while (0) #else
#definedprintf(fmt, ...)do { } while (0)
#endif
Ok.
Quoted 1 line
Also, make sure
DEBUG_CURLis off by default (you have debug enabled in this patch).
Ugh. Right - I checked on every former version but apparently forgot on this one.
Quoted 15 lines
block-http.c line 109
Quoted 13 lines
block-http.c new file +3
-0+#define HTTP_NUM_STATES 8 + +static size_t http_size_cb(void *ptr, size_t size, size_t nmemb,void *opaque)
block-http.c new file +7
-0+{ +HTTPState *s = ((HTTPState *)opaque); +size_t realsize = size * nmemb; +uint64_t fsize; + +if (sscanf (ptr,"Content-Length: %lld" , &fsize) ==1 ) + s->s->len = fsize;How does this work with other protocols? Certainly, there isn't a Content-Length header with scp?
Curl emulates the Content-Length write - at least it does so with FTP. As all curl versions I have lying around don't have USE_LIBSSH2 enabled, I couldn't verify that the scp backend does the same, but I suppose it does.
Quoted 9 lines
block-http.c line 151
Quoted 7 lines
+static int http_find_buf(BDRVHTTPState *s, long long start, long long len,
block-http.c new file +4
-0+ HTTPAIOCB *acb) +{ +int i; +long long end = start + len;
size_tis the right type to represent lengths/offsets in a memory buffer.
Ok.
Quoted 9 lines
block-http.c line 417
Quoted 7 lines
block-http.c new file +4
-0+static void http_close (BlockDriverState *bs) +{ +BDRVHTTPState *s = bs->opaque; +int i;You
strdup()url but don't free it. I'd suggest building qemu-io and running it under valgrind to verify that you aren't leaking other places too.
The strdup() should be the only one, but I can verify.
Quoted 92 lines
Quoted 92 lines
block-http.c new file +92
-0+BlockDriver bdrv_http = { + .format_name ="http" , + .protocol_name ="http" , + + .instance_size =sizeof (BDRVHTTPState), + .bdrv_open = http_open, + .bdrv_close = http_close, + .bdrv_getlength = http_getlength, + + .aiocb_size =sizeof (HTTPAIOCB ), + .bdrv_aio_readv = http_aio_readv, + .bdrv_aio_cancel = http_aio_cancel, +}; + +BlockDriver bdrv_https = { + .format_name ="https" , + .protocol_name ="https" , + + .instance_size =sizeof (BDRVHTTPState), + .bdrv_open = http_open, + .bdrv_close = http_close, + .bdrv_getlength = http_getlength, + + .aiocb_size =sizeof (HTTPAIOCB ), + .bdrv_aio_readv = http_aio_readv, + .bdrv_aio_cancel = http_aio_cancel, +}; + +BlockDriver bdrv_ftp = { + .format_name ="ftp" , + .protocol_name ="ftp" , + + .instance_size =sizeof (BDRVHTTPState), + .bdrv_open = http_open, + .bdrv_close = http_close, + .bdrv_getlength = http_getlength, + + .aiocb_size =sizeof (HTTPAIOCB ), + .bdrv_aio_readv = http_aio_readv, + .bdrv_aio_cancel = http_aio_cancel, +}; + +BlockDriver bdrv_ftps = { + .format_name ="ftps" , + .protocol_name ="ftps" , + + .instance_size =sizeof (BDRVHTTPState), + .bdrv_open = http_open, + .bdrv_close = http_close, + .bdrv_getlength = http_getlength, + + .aiocb_size =sizeof (HTTPAIOCB ), + .bdrv_aio_readv = http_aio_readv, + .bdrv_aio_cancel = http_aio_cancel, +}; + +BlockDriver bdrv_sftp = { + .format_name ="sftp" , + .protocol_name ="sftp" , + + .instance_size =sizeof (BDRVHTTPState), + .bdrv_open = http_open, + .bdrv_close = http_close, + .bdrv_getlength = http_getlength, + + .aiocb_size =sizeof (HTTPAIOCB ), + .bdrv_aio_readv = http_aio_readv, + .bdrv_aio_cancel = http_aio_cancel, +}; + +BlockDriver bdrv_scp = { + .format_name ="scp" , + .protocol_name ="scp" , + + .instance_size =sizeof (BDRVHTTPState), + .bdrv_open = http_open, + .bdrv_close = http_close, + .bdrv_getlength = http_getlength, + + .aiocb_size =sizeof (HTTPAIOCB ), + .bdrv_aio_readv = http_aio_readv, + .bdrv_aio_cancel = http_aio_cancel, +}; + +BlockDriver bdrv_tftp = { + .format_name ="tftp" , + .protocol_name ="tftp" , + + .instance_size =sizeof (BDRVHTTPState), + .bdrv_open = http_open, + .bdrv_close = http_close, + .bdrv_getlength = http_getlength,
Quoted 8 lines
block-http.c line 450
Quoted 6 lines
block-http.c new file +5
-0+ + .aiocb_size = sizeof(HTTPAIOCB), + .bdrv_aio_readv = http_aio_readv, + .bdrv_aio_cancel = http_aio_cancel, +};It's rather slick to get all of these for free.
Want me to enable ldap too? :-)
Alex