ppsspp/Common/Net/HTTPClient.h

141 lines
4.1 KiB
C
Raw Normal View History

#pragma once
#include <functional>
2013-06-01 00:50:08 +02:00
#include <memory>
#include <thread>
#include <cstdint>
2021-05-14 22:46:03 -07:00
#include "Common/File/Path.h"
#include "Common/Net/NetBuffer.h"
#include "Common/Net/Resolve.h"
#include "Common/Net/HTTPRequest.h"
namespace net {
class Connection {
2012-10-30 13:20:55 +01:00
public:
Connection();
virtual ~Connection();
2012-10-30 13:20:55 +01:00
// Inits the sockaddr_in.
bool Resolve(const char *host, int port, DNSType type = DNSType::ANY);
bool Connect(int maxTries = 2, double timeout = 20.0f, bool *cancelConnect = nullptr);
2012-10-30 13:20:55 +01:00
void Disconnect();
2012-10-30 13:20:55 +01:00
// Only to be used for bring-up and debugging.
uintptr_t sock() const { return sock_; }
2012-10-30 13:20:55 +01: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_ = -1;
addrinfo *resolved_ = nullptr;
2012-10-30 13:20:55 +01:00
private:
uintptr_t sock_ = -1;
};
2012-10-30 13:20:55 +01:00
} // namespace net
namespace http {
bool GetHeaderValue(const std::vector<std::string> &responseHeaders, const std::string &header, std::string *value);
class RequestParams {
public:
RequestParams() {}
explicit RequestParams(const char *r) : resource(r) {}
RequestParams(const std::string &r, const char *a) : resource(r), acceptMime(a) {}
std::string resource;
const char *acceptMime = "*/*";
};
class Client : public net::Connection {
2012-10-30 13:20:55 +01:00
public:
Client();
~Client();
// Return value is the HTTP return code. 200 means OK. < 0 means some local error.
int GET(const RequestParams &req, Buffer *output, net::RequestProgress *progress);
int GET(const RequestParams &req, Buffer *output, std::vector<std::string> &responseHeaders, net::RequestProgress *progress);
2012-10-30 13:20:55 +01:00
// Return value is the HTTP return code.
int POST(const RequestParams &req, const std::string &data, const std::string &mime, Buffer *output, net::RequestProgress *progress);
int POST(const RequestParams &req, const std::string &data, Buffer *output, net::RequestProgress *progress);
2014-11-25 08:49:05 -08:00
// HEAD, PUT, DELETE aren't implemented yet, but can be done with SendRequest.
int SendRequest(const char *method, const RequestParams &req, const char *otherHeaders, net::RequestProgress *progress);
int SendRequestWithData(const char *method, const RequestParams &req, const std::string &data, const char *otherHeaders, net::RequestProgress *progress);
int ReadResponseHeaders(net::Buffer *readbuf, std::vector<std::string> &responseHeaders, net::RequestProgress *progress);
// If your response contains a response, you must read it.
int ReadResponseEntity(net::Buffer *readbuf, const std::vector<std::string> &responseHeaders, Buffer *output, net::RequestProgress *progress);
void SetDataTimeout(double t) {
dataTimeout_ = t;
}
void SetUserAgent(const std::string &value) {
2021-04-30 23:12:42 -07:00
userAgent_ = value;
}
protected:
2021-04-30 23:12:42 -07:00
std::string userAgent_;
double dataTimeout_ = 900.0;
};
// Really an asynchronous request.
class HTTPRequest : public Request {
public:
HTTPRequest(RequestMethod method, const std::string &url, const std::string &postData, const std::string &postMime, const Path &outfile, ProgressBarMode progressBarMode = ProgressBarMode::DELAYED, const std::string &name = "");
~HTTPRequest();
void Start() override;
void Join() override;
bool Done() override { return completed_; }
bool Failed() const override { return failed_; }
2013-11-29 17:33:56 +01:00
// NOTE! The value of ResultCode is INVALID until Done() returns true.
int ResultCode() const override { return resultCode_; }
2013-06-02 23:44:28 +02:00
const Path &outfile() const override { return outfile_; }
2013-06-01 18:59:03 +02:00
// If not downloading to a file, access this to get the result.
Buffer &buffer() override { return buffer_; }
const Buffer &buffer() const override { return buffer_; }
2013-06-01 18:59:03 +02:00
void Cancel() override {
cancelled_ = true;
}
bool IsCancelled() const override {
2015-10-06 19:07:09 +02:00
return cancelled_;
}
private:
void Do(); // Actually does the download. Runs on thread.
int Perform(const std::string &url);
2019-06-23 12:04:59 -07:00
std::string RedirectLocation(const std::string &baseUrl);
void SetFailed(int code);
std::string postData_;
Buffer buffer_;
2019-06-23 12:04:59 -07:00
std::vector<std::string> responseHeaders_;
2021-05-14 22:46:03 -07:00
Path outfile_;
2019-09-28 11:40:10 -07:00
std::thread thread_;
std::string postMime_;
2019-09-28 11:40:10 -07:00
int resultCode_ = 0;
bool completed_ = false;
bool failed_ = false;
bool cancelled_ = false;
bool joined_ = false;
};
} // http