Skip to content

Commit 6c99cdd

Browse files
committed
feat: support ssl cert
1 parent dbea47c commit 6c99cdd

File tree

7 files changed

+44
-23
lines changed

7 files changed

+44
-23
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Build directories of CLion.
2+
cmake-build-*
3+
4+
.idea
5+
16
# Prerequisites
27
*.d
38

include/webhdfspp/webhdfspp.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "status.h"
2222

2323
#include <functional>
24+
#include <memory>
2425
#include <string>
2526
#include <utility>
2627
#include <vector>
@@ -34,9 +35,16 @@ class non_copyable {
3435
void operator=(non_copyable const &) = delete;
3536
};
3637

38+
struct Options {
39+
std::vector<std::pair<std::string, short>> namenodes;
40+
char* ssl_cert;
41+
char* ssl_key;
42+
std::string scheme;
43+
};
44+
3745
class IoService : non_copyable {
3846
public:
39-
static IoService *New();
47+
static IoService *New(const Options &options);
4048
virtual Status Run() = 0;
4149
virtual void Stop() = 0;
4250
virtual ~IoService();
@@ -45,10 +53,6 @@ class IoService : non_copyable {
4553
class InputStream;
4654
class OutputStream;
4755

48-
struct Options {
49-
std::vector<std::pair<std::string, short>> namenodes;
50-
};
51-
5256
struct FileStatus {
5357
enum FileType {
5458
FILE,

lib/webhdfspp/filesystem.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ WebHdfsFileSystem::WebHdfsFileSystem(const Options &options,
6666

6767
Status WebHdfsFileSystem::Delete(const std::string &path, bool recursive) {
6868
const auto &nn = options_.namenodes[active_endpoint_];
69-
URIBuilder builder;
70-
auto uri = builder.Scheme("http")
69+
const auto &scheme = options_.scheme;
70+
71+
URIBuilder builder;
72+
auto uri = builder.Scheme(scheme)
7173
.Host(nn.first)
7274
.Port(nn.second)
7375
.Path("/webhdfs/v1" + path)
@@ -88,8 +90,10 @@ Status WebHdfsFileSystem::Delete(const std::string &path, bool recursive) {
8890
Status WebHdfsFileSystem::GetFileStatus(const std::string &path,
8991
FileStatus *stat) {
9092
const auto &nn = options_.namenodes[active_endpoint_];
91-
URIBuilder builder;
92-
auto uri = builder.Scheme("http")
93+
const auto &scheme = options_.scheme;
94+
95+
URIBuilder builder;
96+
auto uri = builder.Scheme(scheme)
9397
.Host(nn.first)
9498
.Port(nn.second)
9599
.Path("/webhdfs/v1" + path)
@@ -142,8 +146,7 @@ Status WebHdfsFileSystem::Exists(const std::string &path, bool *result) {
142146

143147
Status WebHdfsFileSystem::Open(const std::string &path,
144148
std::unique_ptr<InputStream> *is) {
145-
const auto &nn = options_.namenodes[active_endpoint_];
146-
auto is_ptr = new InputStreamImpl(nn, path, io_service_);
149+
auto is_ptr = new InputStreamImpl(options_, path, io_service_, active_endpoint_);
147150
is->reset(is_ptr);
148151
return Status::OK();
149152
}

lib/webhdfspp/inputstream.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,19 @@ namespace webhdfspp {
2222

2323
InputStream::~InputStream() {}
2424

25-
InputStreamImpl::InputStreamImpl(const std::pair<std::string, short> &nn,
25+
InputStreamImpl::InputStreamImpl(const Options &options,
2626
const std::string &path,
27-
std::shared_ptr<IoServiceImpl> io_service)
28-
: nn_(nn), path_(path), io_service_(io_service) {}
27+
std::shared_ptr<IoServiceImpl> io_service, int active_endpoint)
28+
: options_(options), path_(path), io_service_(io_service), active_endpoint_(active_endpoint) {}
2929

3030
Status InputStreamImpl::PositionRead(
3131
size_t max_read_bytes, size_t offset,
3232
const std::function<size_t(const char *, size_t)> &on_data_arrived) {
33+
const auto nn = options_.namenodes[active_endpoint_];
3334
URIBuilder builder;
34-
auto uri = builder.Scheme("http")
35-
.Host(nn_.first)
36-
.Port(nn_.second)
35+
auto uri = builder.Scheme(options_.scheme)
36+
.Host(nn.first)
37+
.Port(nn.second)
3738
.Path("/webhdfs/v1" + path_)
3839
.Param("op", "OPEN");
3940

lib/webhdfspp/inputstream_impl.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@ namespace webhdfspp {
2525

2626
class InputStreamImpl : public InputStream {
2727
public:
28-
InputStreamImpl(const std::pair<std::string, short> &nn,
28+
InputStreamImpl(const Options &options,
2929
const std::string &path,
30-
std::shared_ptr<IoServiceImpl> io_service);
30+
std::shared_ptr<IoServiceImpl> io_service, int active_endpoint);
3131
virtual Status PositionRead(size_t max_read_bytes, size_t offset,
3232
const std::function<size_t(const char *, size_t)>
3333
&on_data_arrived) override;
3434

3535
private:
36-
std::pair<std::string, short> nn_;
36+
const Options options_;
3737
std::string path_;
3838
std::shared_ptr<IoServiceImpl> io_service_;
39+
int active_endpoint_;
3940
};
4041

4142
} // namespace webhdfspp

lib/webhdfspp/io_service.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323

2424
namespace webhdfspp {
2525

26-
IoServiceImpl::IoServiceImpl() {}
26+
IoServiceImpl::IoServiceImpl(const Options &options) : options_(options) {}
2727

2828
IoServiceImpl::~IoServiceImpl() {}
2929

30-
IoService *IoService::New() { return new IoServiceImpl(); }
30+
IoService *IoService::New(const Options &options) { return new IoServiceImpl(options); }
3131

3232
IoService::~IoService() {}
3333

@@ -44,6 +44,11 @@ Status IoServiceImpl::DoNNRequest(const URIBuilder &uri,
4444
char error_buffer[CURL_ERROR_SIZE];
4545
auto uri_str = uri.Build();
4646

47+
if (options_.ssl_cert && options_.ssl_key) {
48+
curl_easy_setopt(handle, CURLOPT_SSLCERT, options_.ssl_cert);
49+
curl_easy_setopt(handle, CURLOPT_SSLKEY, options_.ssl_key);
50+
}
51+
4752
curl_easy_setopt(handle, CURLOPT_URL, uri_str.c_str());
4853
curl_easy_setopt(handle, CURLOPT_ERRORBUFFER, error_buffer);
4954
curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, method.c_str());

lib/webhdfspp/io_service_impl.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace webhdfspp {
2929
class IoServiceImpl : public IoService,
3030
public std::enable_shared_from_this<IoServiceImpl> {
3131
public:
32-
IoServiceImpl();
32+
IoServiceImpl(const Options &options);
3333
~IoServiceImpl();
3434
virtual Status Run() override;
3535
virtual void Stop() override;
@@ -46,6 +46,8 @@ class IoServiceImpl : public IoService,
4646
static CURLcode
4747
DNGetCallback(char *in, size_t size, size_t nmemb,
4848
std::function<size_t(const char *, size_t)> *on_data_arrived);
49+
50+
const Options options_;
4951
};
5052
} // namespace webhdfspp
5153

0 commit comments

Comments
 (0)