Skip to content

Commit 2cdf177

Browse files
feat: fixing is_done() + tests on dir autoindexing and redirection
1 parent f095996 commit 2cdf177

File tree

3 files changed

+106
-10
lines changed

3 files changed

+106
-10
lines changed

src/handler/static_file_handler.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,10 @@ StaticFileHandler::StaticFileHandler(const std::string& path,
183183
req_(req),
184184
out_off_(0),
185185
file_size_(0),
186-
fd_(-1)
186+
fd_(-1),
187+
eof_(false)
187188

188189
{
189-
LOG(DEBUG) << "req_.path = " << req_.path;
190190
struct ResolveResult r;
191191
resolve_path(r);
192192
if (r.kind == kNotFound) {
@@ -207,7 +207,7 @@ StaticFileHandler::StaticFileHandler(const std::string& path,
207207
HttpResponse::kStatusOk, "text/html; charset=UTF-8", html, req_);
208208

209209
out_buf_ = res_.to_string();
210-
fd_ = -1;
210+
eof_ = true;
211211
return;
212212
}
213213

@@ -271,18 +271,14 @@ void StaticFileHandler::set_error(const HttpResponse::Status code)
271271
{
272272
res_ = HttpResponse::make_error(code, rc_.shared.error_pages, req_);
273273
out_buf_ = res_.to_string();
274-
if (fd_ != -1) {
275-
close(fd_);
276-
fd_ = -1;
277-
}
274+
eof_ = true;
278275
}
279276

280277
void StaticFileHandler::set_redirect(const HttpResponse::Status code,
281278
const std::string& redirect_path)
282279
{
283280
res_ = HttpResponse::make_response_headers_only(code, "", 0, req_);
284-
// LOG(DEBUG) << "redirect location = " << redirect_path;
285281
res_.location = redirect_path;
286282
out_buf_ = res_.to_string();
287-
fd_ = -1;
283+
eof_ = true;
288284
}

src/handler/static_file_handler.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class StaticFileHandler : public Handler {
2828
virtual bool is_regular_file() const { return true; }
2929
virtual bool has_output() const { return out_off_ < out_buf_.size() || fd_ != -1; }
3030
virtual bool needs_input() const { return false; };
31-
virtual bool is_done() const { return out_off_ >= out_buf_.size() && fd_ == -1; }
31+
virtual bool is_done() const { return eof_ && out_off_ >= out_buf_.size() && fd_ == -1; }
3232

3333
virtual int cgi_read_fd() const { return -1; };
3434
virtual int cgi_write_fd() const { return -1; };
@@ -52,6 +52,7 @@ class StaticFileHandler : public Handler {
5252
// other handler specifc variables
5353
off_t file_size_;
5454
int fd_;
55+
bool eof_;
5556
};
5657

5758
#endif

tests/static_file_handler_unittest.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,102 @@ UTEST(StaticFileHandlerTest, SmallBuffer)
8787
EXPECT_TRUE(str_contains(response, "<!DOCTYPE html>"
8888
"<html><head><title>Example</title></head></html>\n"));
8989
}
90+
91+
UTEST(StaticFileHandlerTest, is_done)
92+
{
93+
RouteConfig rc;
94+
HttpRequest req;
95+
StaticFileHandler test("www/example/index.html", rc, req);
96+
97+
char buf[1] = {0};
98+
std::string response;
99+
100+
while (!test.is_done() && test.has_output()) {
101+
test.read_output(buf, sizeof(buf));
102+
response.append(buf, 1);
103+
}
104+
105+
EXPECT_TRUE(str_contains(response, "HTTP/1.1 200 OK"));
106+
EXPECT_TRUE(str_contains(response, "Content-Length: 64"));
107+
EXPECT_TRUE(str_contains(response, "<!DOCTYPE html>"
108+
"<html><head><title>Example</title></head></html>\n"));
109+
}
110+
UTEST(StaticFileHandlerTest, needs_input)
111+
{
112+
RouteConfig rc;
113+
HttpRequest req;
114+
StaticFileHandler test("www/example/index.html", rc, req);
115+
EXPECT_FALSE(test.needs_input());
116+
}
117+
118+
UTEST(StaticFileHandlerTest, dir_autoindex_off_with_index_file)
119+
{
120+
RouteConfig rc;
121+
rc.shared.autoindex_enabled = false;
122+
HttpRequest req;
123+
StaticFileHandler test("www/example/", rc, req);
124+
125+
char buf[1] = {0};
126+
std::string response;
127+
128+
while (!test.is_done() && test.has_output()) {
129+
test.read_output(buf, sizeof(buf));
130+
response.append(buf, 1);
131+
}
132+
// std::cout << response << std::endl;
133+
EXPECT_TRUE(str_contains(response, "HTTP/1.1 200 OK"));
134+
}
135+
136+
UTEST(StaticFileHandlerTest, dir_autoindex_off_no_index_file)
137+
{
138+
RouteConfig rc;
139+
rc.shared.autoindex_enabled = false;
140+
HttpRequest req;
141+
StaticFileHandler test("www/example/bin/", rc, req);
142+
143+
char buf[1] = {0};
144+
std::string response;
145+
146+
while (!test.is_done() && test.has_output()) {
147+
test.read_output(buf, sizeof(buf));
148+
response.append(buf, 1);
149+
}
150+
// std::cout << response << std::endl;
151+
EXPECT_TRUE(str_contains(response, "HTTP/1.1 403 Forbidden"));
152+
}
153+
154+
UTEST(StaticFileHandlerTest, dir_autoindex_on_no_index_file)
155+
{
156+
RouteConfig rc;
157+
rc.shared.autoindex_enabled = true;
158+
HttpRequest req;
159+
StaticFileHandler test("www/example/bin/", rc, req);
160+
161+
char buf[1] = {0};
162+
std::string response;
163+
164+
while (!test.is_done() && test.has_output()) {
165+
test.read_output(buf, sizeof(buf));
166+
response.append(buf, 1);
167+
}
168+
// std::cout << response << std::endl;
169+
EXPECT_TRUE(str_contains(response, "HTTP/1.1 200 OK"));
170+
}
171+
172+
UTEST(StaticFileHandlerTest, dir_autoindex_on_no_index_file_redirection)
173+
{
174+
RouteConfig rc;
175+
rc.shared.autoindex_enabled = true;
176+
HttpRequest req;
177+
StaticFileHandler test("www/example/bin", rc, req);
178+
179+
char buf[1] = {0};
180+
std::string response;
181+
182+
while (!test.is_done() && test.has_output()) {
183+
test.read_output(buf, sizeof(buf));
184+
response.append(buf, 1);
185+
}
186+
std::cout << response << std::endl;
187+
EXPECT_TRUE(str_contains(response, "HTTP/1.1 301 Moved Permanently"));
188+
}

0 commit comments

Comments
 (0)