@@ -29,11 +29,12 @@ Also, the UploadHandler could handle, both POST and PUT requests
2929
3030UploadHandler::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
5758UploadHandler::~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
6370bool UploadHandler::needs_input () const
@@ -81,7 +88,7 @@ size_t UploadHandler::read_data(char* buf, size_t n)
8188
8289size_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 ;
0 commit comments