ppsspp/net/http_client.h

109 lines
2.3 KiB
C
Raw Normal View History

#ifndef _NET_HTTP_HTTP_CLIENT
#define _NET_HTTP_HTTP_CLIENT
#include "base/basictypes.h"
#include "base/buffer.h"
#include "thread/thread.h"
#ifdef _WIN32
#include <winsock2.h>
#else
#if defined(__FreeBSD__) || defined(__SYMBIAN32__)
#include <netinet/in.h>
#else
#include <arpa/inet.h>
#endif
#include <sys/socket.h>
#endif
namespace net {
class Connection {
2012-10-30 12:20:55 +00:00
public:
Connection();
virtual ~Connection();
2012-10-30 12:20:55 +00:00
// Inits the sockaddr_in.
bool Resolve(const char *host, int port);
2012-10-30 12:20:55 +00:00
void Connect();
void Disconnect();
2012-10-30 12:20:55 +00:00
// Disconnects, and connects. Doesn't re-resolve.
void Reconnect();
2012-10-30 12:20:55 +00:00
// Only to be used for bring-up and debugging.
uintptr_t sock() const { return sock_; }
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-10-30 12:20:55 +00:00
sockaddr_in remote_;
private:
uintptr_t sock_;
};
2012-10-30 12:20:55 +00:00
} // namespace net
namespace http {
class Client : public net::Connection {
2012-10-30 12:20:55 +00:00
public:
Client();
~Client();
// Return value is the HTTP return code. 200 means OK. < 0 means some local error.
int GET(const char *resource, Buffer *output);
2012-10-30 12:20:55 +00:00
// Return value is the HTTP return code.
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-10-30 12:20:55 +00:00
// HEAD, PUT, DELETE aren't implemented yet.
};
// 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-10-30 12:20:55 +00:00
#endif // _NET_HTTP_HTTP_CLIENT