Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get basic Naett requests to work (the store works in https mode)
- Loading branch information
Showing
12 changed files
with
202 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,92 @@ | ||
#include <cstring> | ||
|
||
#include "Common/Net/HTTPRequest.h" | ||
#include "Common/Net/HTTPNaettRequest.h" | ||
#include "Common/Thread/ThreadUtil.h" | ||
#include "Common/Log.h" | ||
|
||
#include "ext/naett/naett.h" | ||
|
||
namespace http { | ||
|
||
HTTPSDownload::HTTPSDownload(RequestMethod method, const std::string &url, const std::string &postData, const std::string &postMime, const Path &outfile, ProgressBarMode progressBarMode, const std::string &name) | ||
: Download(url, name, progress_.cancelled), method_(method), postData_(postData), postMime_(postMime), outfile_(outfile), progressBarMode_(progressBarMode) { | ||
} | ||
|
||
HTTPSDownload::~HTTPSDownload() { | ||
Join(); | ||
} | ||
|
||
void HTTPSDownload::Start() { | ||
_dbg_assert_(!req_); | ||
|
||
const char *methodStr = method_ == RequestMethod::GET ? "GET" : "POST"; | ||
req_ = naettRequest_va(url_.c_str(), !postMime_.empty() ? 3 : 2, naettMethod(methodStr), naettHeader("accept", "application/json, text/*; q=0.9, */*; q=0.8"), naettHeader("Content-Type", postMime_.c_str())); | ||
res_ = naettMake(req_); | ||
} | ||
|
||
void HTTPSDownload::Join() { | ||
if (!res_ || !req_) | ||
return; // No pending operation. | ||
// Tear down. | ||
if (completed_ && res_) { | ||
_dbg_assert_(req_); | ||
naettClose(res_); | ||
naettFree(req_); | ||
res_ = nullptr; | ||
req_ = nullptr; | ||
} else { | ||
ERROR_LOG(IO, "HTTPSDownload::Join not implemented"); | ||
} | ||
} | ||
|
||
bool HTTPSDownload::Done() { | ||
if (completed_) | ||
return true; | ||
|
||
if (naettComplete(res_)) { | ||
resultCode_ = naettGetStatus(res_); | ||
if (resultCode_ < 0) { | ||
// It's a naett error. Translate and handle. | ||
switch (resultCode_) { | ||
case naettConnectionError: // -1 | ||
ERROR_LOG(IO, "Connection error"); | ||
break; | ||
case naettProtocolError: // -2 | ||
ERROR_LOG(IO, "Protocol error"); | ||
break; | ||
case naettReadError: // -3 | ||
ERROR_LOG(IO, "Read error"); | ||
break; | ||
case naettWriteError: // -4 | ||
ERROR_LOG(IO, "Write error"); | ||
break; | ||
case naettGenericError: // -4 | ||
ERROR_LOG(IO, "Generic error"); | ||
break; | ||
} | ||
} else if (resultCode_ == 200) { | ||
int bodyLength; | ||
const void *body = naettGetBody(res_, &bodyLength); | ||
char *dest = buffer_.Append(bodyLength); | ||
memcpy(dest, body, bodyLength); | ||
|
||
if (!outfile_.empty() && !buffer_.FlushToFile(outfile_)) { | ||
ERROR_LOG(IO, "Failed writing download to '%s'", outfile_.c_str()); | ||
} | ||
} else { | ||
WARN_LOG(IO, "Naett request failed: %d", resultCode_); | ||
failed_ = true; | ||
} | ||
|
||
completed_ = true; | ||
|
||
if (callback_) | ||
callback_(*this); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
} // namespace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,68 @@ | ||
#pragma once | ||
|
||
#include "Common/Net/HTTPRequest.h" | ||
#include "ext/naett/naett.h" | ||
#include <thread> | ||
|
||
namespace http { | ||
|
||
// Really an asynchronous request. | ||
class HTTPSDownload : public Download { | ||
public: | ||
HTTPSDownload(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 = ""); | ||
~HTTPSDownload(); | ||
|
||
void SetAccept(const char *mime) override { | ||
acceptMime_ = mime; | ||
} | ||
|
||
void SetUserAgent(const std::string &userAgent) override { | ||
userAgent_ = userAgent; | ||
} | ||
|
||
void Start() override; | ||
void Join() override; | ||
|
||
// Also acts as a Poll. | ||
bool Done() override; | ||
bool Failed() const override { return failed_; } | ||
|
||
// NOTE! The value of ResultCode is INVALID until Done() returns true. | ||
int ResultCode() const override { return resultCode_; } | ||
|
||
const Path &outfile() const override { return outfile_; } | ||
|
||
// If not downloading to a file, access this to get the result. | ||
Buffer &buffer() override { return buffer_; } | ||
const Buffer &buffer() const override { return buffer_; } | ||
|
||
void Cancel() override { | ||
cancelled_ = true; | ||
} | ||
|
||
bool IsCancelled() const override { | ||
return cancelled_; | ||
} | ||
|
||
private: | ||
RequestMethod method_; | ||
std::string postData_; | ||
std::string userAgent_; | ||
Buffer buffer_; | ||
std::vector<std::string> responseHeaders_; | ||
Path outfile_; | ||
const char *acceptMime_ = "*/*"; | ||
std::string postMime_; | ||
int resultCode_ = 0; | ||
bool completed_ = false; | ||
bool failed_ = false; | ||
bool cancelled_ = false; | ||
ProgressBarMode progressBarMode_; | ||
bool joined_ = false; | ||
|
||
// Naett state | ||
naettReq *req_ = nullptr; | ||
naettRes *res_ = nullptr; | ||
}; | ||
|
||
} // namespace http |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters