Skip to content

Commit b4f47d0

Browse files
feat: getters for fd
1 parent 5b9839f commit b4f47d0

File tree

9 files changed

+80
-19
lines changed

9 files changed

+80
-19
lines changed

src/handler/delete_handler.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
DeleteHandler::DeleteHandler(const std::string& path)
1616
: file_path_(path),
17-
headers_off_(0)
17+
headers_off_(0),
18+
read_fd_(-1),
19+
write_fd_(-1)
1820
{
1921
struct stat file_stat;
2022
HttpResponse res;
@@ -52,6 +54,14 @@ DeleteHandler::DeleteHandler(const std::string& path)
5254

5355
DeleteHandler::~DeleteHandler()
5456
{
57+
if (read_fd_ != -1) {
58+
close(read_fd_);
59+
read_fd_ = -1;
60+
}
61+
if (write_fd_ != -1) {
62+
close(write_fd_);
63+
write_fd_ = -1;
64+
}
5565
}
5666

5767
size_t DeleteHandler::read_data(char* buf, size_t n)

src/handler/delete_handler.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ class DeleteHandler : public Handler {
1212
explicit DeleteHandler(const std::string& path);
1313
virtual ~DeleteHandler();
1414

15+
virtual int get_read_fd() const { return read_fd_; };
16+
virtual int get_write_fd() const { return write_fd_; };
17+
1518
virtual size_t read_data(char* buf, size_t n);
1619
virtual size_t write_data(const char* buf, size_t n);
1720

@@ -30,6 +33,9 @@ class DeleteHandler : public Handler {
3033

3134
std::string headers_;
3235
size_t headers_off_;
36+
37+
int read_fd_;
38+
int write_fd_;
3339
};
3440

3541
#endif

src/handler/error_handler.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "util/log_message.hpp"
44

5+
#include <unistd.h>
6+
57
#include <cstring>
68
#include <iostream>
79

@@ -23,7 +25,9 @@ size_t ErrorHandler::write_data(const char* buf, size_t n)
2325
}
2426

2527
ErrorHandler::ErrorHandler(HttpResponse::Status code)
26-
: res_sent_(false)
28+
: res_sent_(false),
29+
read_fd_(-1),
30+
write_fd_(-1)
2731
{
2832
res_.code = code;
2933
res_.inline_body = "<h1> some error code to be shown here <h1>";
@@ -32,4 +36,12 @@ ErrorHandler::ErrorHandler(HttpResponse::Status code)
3236

3337
ErrorHandler::~ErrorHandler()
3438
{
39+
if (read_fd_ != -1) {
40+
close(read_fd_);
41+
read_fd_ = -1;
42+
}
43+
if (write_fd_ != -1) {
44+
close(write_fd_);
45+
write_fd_ = -1;
46+
}
3547
}

src/handler/error_handler.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ class ErrorHandler : public Handler {
1111
explicit ErrorHandler(HttpResponse::Status code);
1212
virtual ~ErrorHandler();
1313

14+
virtual int get_read_fd() const { return read_fd_; };
15+
virtual int get_write_fd() const { return write_fd_; };
16+
1417
virtual size_t read_data(char* buf, size_t n);
1518
virtual size_t write_data(const char* buf, size_t n);
1619

@@ -27,6 +30,9 @@ class ErrorHandler : public Handler {
2730
// NOTE: HttpResponse is default constructed with HTTP/1.1 + keep-alive
2831
HttpResponse res_;
2932
bool res_sent_;
33+
34+
int read_fd_;
35+
int write_fd_;
3036
};
3137

3238
#endif

src/handler/handler.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ class Handler {
77
public:
88
virtual ~Handler() {}
99

10+
virtual int get_read_fd() const = 0;
11+
virtual int get_write_fd() const = 0;
12+
1013
virtual size_t read_data(char* buf, size_t n) = 0;
1114
virtual size_t write_data(const char* buf, size_t n) = 0;
1215

src/handler/static_file_handler.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ static const char* derive_file_type(const std::string& file_path)
7474
StaticFileHandler::StaticFileHandler(const std::string& path,
7575
const RouteConfig& rc,
7676
const HttpRequest& saved_request)
77-
: fd_(-1),
78-
file_size_(0),
77+
: file_size_(0),
7978
eof_reached_(false),
80-
headers_off_(0)
79+
headers_off_(0),
80+
read_fd_(-1),
81+
write_fd_(-1)
8182
{
8283
HttpResponse res(saved_request.http_version);
8384
std::string full_path = resolve_path(path, rc.config.index_files);
@@ -93,7 +94,7 @@ StaticFileHandler::StaticFileHandler(const std::string& path,
9394

9495
struct stat file_stat;
9596

96-
fd_ = open(full_path.c_str(), O_RDONLY);
97+
read_fd_ = open(full_path.c_str(), O_RDONLY);
9798
stat(full_path.c_str(), &file_stat);
9899
file_size_ = file_stat.st_size;
99100

@@ -107,8 +108,14 @@ StaticFileHandler::StaticFileHandler(const std::string& path,
107108

108109
StaticFileHandler::~StaticFileHandler()
109110
{
110-
if (fd_ != -1)
111-
close(fd_);
111+
if (read_fd_ != -1) {
112+
close(read_fd_);
113+
read_fd_ = -1;
114+
}
115+
if (write_fd_ != -1) {
116+
close(write_fd_);
117+
write_fd_ = -1;
118+
}
112119
}
113120

114121
size_t StaticFileHandler::read_data(char* buf, size_t n)
@@ -128,7 +135,7 @@ size_t StaticFileHandler::read_data(char* buf, size_t n)
128135
}
129136
}
130137
if (has_body() && !body_sent()) {
131-
int body_bytes = read(fd_, buf + bytes_written, n - bytes_written);
138+
int body_bytes = read(read_fd_, buf + bytes_written, n - bytes_written);
132139
if (body_bytes == -1) {
133140
throw std::runtime_error("read failed");
134141
}

src/handler/static_file_handler.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class StaticFileHandler : public Handler {
1717

1818
virtual ~StaticFileHandler();
1919

20+
virtual int get_read_fd() const { return read_fd_; };
21+
virtual int get_write_fd() const { return write_fd_; };
22+
2023
virtual size_t read_data(char* buf, size_t n);
2124
virtual size_t write_data(const char* buf, size_t n);
2225

@@ -28,15 +31,17 @@ class StaticFileHandler : public Handler {
2831
StaticFileHandler(const StaticFileHandler&);
2932
StaticFileHandler& operator=(const StaticFileHandler&);
3033

31-
bool has_body() const { return fd_ != -1; }
34+
bool has_body() const { return read_fd_ != -1; }
3235
bool headers_sent() const { return headers_off_ == headers_.size(); }
3336
bool body_sent() const { return eof_reached_; }
3437

35-
int fd_;
3638
off_t file_size_;
3739
bool eof_reached_;
3840
std::string headers_;
3941
size_t headers_off_;
42+
43+
int read_fd_;
44+
int write_fd_;
4045
};
4146

4247
#endif

src/handler/upload_handler.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ Also, the UploadHandler could handle, both POST and PUT requests
2929

3030
UploadHandler::UploadHandler(const std::string& path, size_t content_length)
3131
: file_path_(path),
32-
fd_(-1),
3332
bytes_written_(0),
3433
content_length_(content_length),
3534
eob_reached_(false),
36-
headers_off_(0)
35+
headers_off_(0),
36+
read_fd_(-1),
37+
write_fd_(-1)
3738
{
3839
struct stat file_stat;
3940
if (stat(path.c_str(), &file_stat) == 0 && S_ISREG(file_stat.st_mode)) {
@@ -44,8 +45,8 @@ UploadHandler::UploadHandler(const std::string& path, size_t content_length)
4445
headers_ = res.to_string();
4546
return;
4647
}
47-
fd_ = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
48-
if (fd_ == -1) {
48+
write_fd_ = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
49+
if (write_fd_ == -1) {
4950
HttpResponse res;
5051
res.code = HttpResponse::kStatusDiskFull;
5152
res.inline_body = "<h1> 507 Disk Full </h1>"; // to display a message during testing
@@ -56,8 +57,14 @@ UploadHandler::UploadHandler(const std::string& path, size_t content_length)
5657

5758
UploadHandler::~UploadHandler()
5859
{
59-
if (fd_ != -1)
60-
close(fd_);
60+
if (read_fd_ != -1) {
61+
close(read_fd_);
62+
read_fd_ = -1;
63+
}
64+
if (write_fd_ != -1) {
65+
close(write_fd_);
66+
write_fd_ = -1;
67+
}
6168
}
6269

6370
bool UploadHandler::needs_input() const
@@ -81,7 +88,7 @@ size_t UploadHandler::read_data(char* buf, size_t n)
8188

8289
size_t UploadHandler::write_data(const char* buf, size_t n)
8390
{
84-
ssize_t bytes = write(fd_, buf, n);
91+
ssize_t bytes = write(write_fd_, buf, n);
8592
if (bytes == -1) {
8693
// error occured
8794
eob_reached_ = true;

src/handler/upload_handler.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ class UploadHandler : public Handler {
1212
explicit UploadHandler(const std::string& path, size_t content_lenght);
1313
virtual ~UploadHandler();
1414

15+
virtual int get_read_fd() const { return read_fd_; };
16+
virtual int get_write_fd() const { return write_fd_; };
17+
1518
virtual size_t read_data(char* buf, size_t n); // we should not read from this handler
1619
virtual size_t write_data(const char* buf, size_t n);
1720

@@ -29,12 +32,14 @@ class UploadHandler : public Handler {
2932

3033
const std::string file_path_;
3134

32-
int fd_;
3335
size_t bytes_written_;
3436
size_t content_length_;
3537
bool eob_reached_;
3638
std::string headers_;
3739
size_t headers_off_;
40+
41+
int read_fd_;
42+
int write_fd_;
3843
};
3944

4045
#endif

0 commit comments

Comments
 (0)