fgenesis / minihttp

A minimal 2-file (cpp+h) TCP & HTTP client implementation. Supports GET & simple POST. Optional SSL via PolarSSL/mbedTLS. Cross-platform, uses POSIX or Win32 API. C++03 with STL.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Direct way to download and buffer?

IngwiePhoenix opened this issue · comments

Hey.

I am trying to implement the download and buffer method. I.e.:

std::string mydata = ht->Download("http://google.com");

Kind regards, Ingwie

This is not easily possible, that's why it's not supported. Keep in mind that TCP networking is inherently asynchronous, so you'll either have to blocking-wait until the requested data arrive (if they do at all), or do networking stuff in background from another thread and rely on callbacks, so you can do other things in the meantime.
I wanted to keep the implementation minimalistic (ie. no threading library and other things), because most people have their own preferences for threading anyway. Plus I don't see the point of dragging in tons of external libraries and enforcing those on the user just to get a single thing done ;)

An implementation where I used the asynchonous approach can be found here:
https://github.com/fgenesis/Aquaria/blob/master/Aquaria/Network.cpp
https://github.com/fgenesis/Aquaria/blob/master/Aquaria/Network.h
Using SDL-based multithreading primitives:
https://github.com/fgenesis/Aquaria/blob/master/BBGE/MT.h
https://github.com/fgenesis/Aquaria/blob/master/BBGE/MT.cpp

The high-level code which uses the above is here:
https://github.com/fgenesis/Aquaria/blob/master/Aquaria/ModDownloader.cpp
https://github.com/fgenesis/Aquaria/blob/master/Aquaria/ModDownloader.h

Hope this helps.

(Aquaria is GPL but all of the code linked above is written by me, and i don't care, so feel free to steal!)

Ask and you shall receive: Done in d72ac19. See example1.cpp, and the related warning in minihttp.h.
Nevertheless, adding a simple and direct download function was a fairly good idea, and easy enough to do without pulling in additional dependency garbage.

EDIT: 2014? This issue was from TWO years ago? Oh well. I'm slow ._.