2012-06-03 17:01:08 +00:00
|
|
|
#ifndef _NET_HTTP_HTTP_CLIENT
|
|
|
|
#define _NET_HTTP_HTTP_CLIENT
|
|
|
|
|
|
|
|
#include "base/basictypes.h"
|
|
|
|
#include "base/buffer.h"
|
2013-05-31 21:04:42 +00:00
|
|
|
#include "thread/thread.h"
|
2012-06-03 17:01:08 +00:00
|
|
|
|
2012-11-19 00:36:07 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <winsock2.h>
|
|
|
|
#else
|
2012-12-13 04:39:44 +00:00
|
|
|
#if defined(__FreeBSD__) || defined(__SYMBIAN32__)
|
2012-11-19 00:36:07 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#else
|
2012-06-03 17:01:08 +00:00
|
|
|
#include <arpa/inet.h>
|
2012-11-19 00:36:07 +00:00
|
|
|
#endif
|
2012-06-03 17:01:08 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
class Connection {
|
2012-10-30 12:20:55 +00:00
|
|
|
public:
|
|
|
|
Connection();
|
|
|
|
virtual ~Connection();
|
2012-06-03 17:01:08 +00:00
|
|
|
|
2012-10-30 12:20:55 +00:00
|
|
|
// Inits the sockaddr_in.
|
|
|
|
bool Resolve(const char *host, int port);
|
2012-06-03 17:01:08 +00:00
|
|
|
|
2012-10-30 12:20:55 +00:00
|
|
|
void Connect();
|
|
|
|
void Disconnect();
|
2012-06-03 17:01:08 +00:00
|
|
|
|
2012-10-30 12:20:55 +00:00
|
|
|
// Disconnects, and connects. Doesn't re-resolve.
|
|
|
|
void Reconnect();
|
2012-06-03 17:01:08 +00:00
|
|
|
|
2012-10-30 12:20:55 +00:00
|
|
|
// Only to be used for bring-up and debugging.
|
|
|
|
uintptr_t sock() const { return sock_; }
|
2012-06-03 17:01:08 +00:00
|
|
|
|
2012-10-30 12:20:55 +00:00
|
|
|
protected:
|
|
|
|
// Store the remote host here, so we can send it along through HTTP/1.1 requests.
|
|
|
|
// TODO: Move to http::client?
|
|
|
|
std::string host_;
|
|
|
|
int port_;
|
2012-06-03 17:01:08 +00:00
|
|
|
|
2012-10-30 12:20:55 +00:00
|
|
|
sockaddr_in remote_;
|
|
|
|
|
|
|
|
private:
|
|
|
|
uintptr_t sock_;
|
2012-06-03 17:01:08 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2012-10-30 12:20:55 +00:00
|
|
|
} // namespace net
|
2012-06-03 17:01:08 +00:00
|
|
|
|
|
|
|
namespace http {
|
|
|
|
|
|
|
|
class Client : public net::Connection {
|
2012-10-30 12:20:55 +00:00
|
|
|
public:
|
|
|
|
Client();
|
|
|
|
~Client();
|
2012-06-03 17:01:08 +00:00
|
|
|
|
2013-05-31 21:04:42 +00:00
|
|
|
// Return value is the HTTP return code. 200 means OK. < 0 means some local error.
|
|
|
|
int GET(const char *resource, Buffer *output);
|
2012-06-03 17:01:08 +00:00
|
|
|
|
2012-10-30 12:20:55 +00:00
|
|
|
// Return value is the HTTP return code.
|
2013-03-01 08:04:42 +00:00
|
|
|
int POST(const char *resource, const std::string &data, const std::string &mime, Buffer *output);
|
2012-10-30 12:20:55 +00:00
|
|
|
int POST(const char *resource, const std::string &data, Buffer *output);
|
2012-06-03 17:01:08 +00:00
|
|
|
|
2012-10-30 12:20:55 +00:00
|
|
|
// HEAD, PUT, DELETE aren't implemented yet.
|
2012-06-03 17:01:08 +00:00
|
|
|
};
|
|
|
|
|
2013-05-31 21:04:42 +00:00
|
|
|
// Not particularly efficient, but hey - it's a background download, that's pretty cool :P
|
|
|
|
class Download {
|
|
|
|
public:
|
|
|
|
Download(const std::string &url, const std::string &outfile);
|
|
|
|
~Download();
|
|
|
|
|
|
|
|
// Returns 1.0 when done. That one value can be compared exactly.
|
|
|
|
float Progress() const { return progress_; }
|
|
|
|
bool Failed() const { return failed_; }
|
|
|
|
|
|
|
|
std::string url() const { return url_; }
|
|
|
|
std::string outfile() const { return outfile_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void Do(); // Actually does the download. Runs on thread.
|
|
|
|
|
|
|
|
float progress_;
|
|
|
|
Buffer buffer_;
|
|
|
|
std::string url_;
|
|
|
|
std::string outfile_;
|
|
|
|
bool failed_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Downloader {
|
|
|
|
public:
|
|
|
|
std::shared_ptr<Download> StartDownload(const std::string &url, const std::string &outfile);
|
|
|
|
|
|
|
|
// Drops finished downloads from the list.
|
|
|
|
void Update();
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<std::shared_ptr<Download>> downloads_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-10-30 12:20:55 +00:00
|
|
|
} // http
|
2012-06-03 17:01:08 +00:00
|
|
|
|
2012-10-30 12:20:55 +00:00
|
|
|
#endif // _NET_HTTP_HTTP_CLIENT
|
2012-06-03 17:01:08 +00:00
|
|
|
|