Skip to content

Commit 475a595

Browse files
committed
fix: apply suggestions for http dir from ai code review
1 parent 9a7b6b7 commit 475a595

3 files changed

Lines changed: 17 additions & 8 deletions

File tree

http/HttpMessage.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@ class HV_EXPORT HttpMessage {
219219
if (file.open(filepath.c_str(), "wb") != 0) {
220220
return HTTP_STATUS_INTERNAL_SERVER_ERROR;
221221
}
222-
file.write(formdata.content.data(), formdata.content.size());
222+
if (file.write(formdata.content.data(), formdata.content.size()) != formdata.content.size()) {
223+
return HTTP_STATUS_INTERNAL_SERVER_ERROR;
224+
}
223225
return 200;
224226
}
225227

http/server/HttpHandler.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,11 @@ int HttpHandler::defaultStaticHandler() {
586586
}
587587
long total = file->size();
588588
if (to == 0 || to >= total) to = total - 1;
589+
if (from < 0 || from >= total || to < from) {
590+
closeFile();
591+
resp->SetHeader("Content-Range", hv::asprintf("bytes */%ld", total));
592+
return HTTP_STATUS_RANGE_NOT_SATISFIABLE;
593+
}
589594
file->seek(from);
590595
status_code = HTTP_STATUS_PARTIAL_CONTENT;
591596
resp->status_code = HTTP_STATUS_PARTIAL_CONTENT;
@@ -1046,9 +1051,8 @@ int HttpHandler::handleForwardProxy() {
10461051
return connectProxy(req->url);
10471052
} else {
10481053
hlogw("[%s:%d] Forbidden to forward proxy %s", ip, port, req->url.c_str());
1049-
SetError(HTTP_STATUS_FORBIDDEN, HTTP_STATUS_FORBIDDEN);
1054+
return SendHttpStatusResponse(HTTP_STATUS_FORBIDDEN);
10501055
}
1051-
return 0;
10521056
}
10531057

10541058
int HttpHandler::handleReverseProxy() {
@@ -1079,8 +1083,7 @@ int HttpHandler::connectProxy(const std::string& strUrl) {
10791083

10801084
if (forward_proxy && !service->IsTrustProxy(url.host.c_str())) {
10811085
hlogw("[%s:%d] Forbidden to proxy %s", ip, port, url.host.c_str());
1082-
SetError(HTTP_STATUS_FORBIDDEN, HTTP_STATUS_FORBIDDEN);
1083-
return 0;
1086+
return SendHttpStatusResponse(HTTP_STATUS_FORBIDDEN);
10841087
}
10851088

10861089
hloop_t* loop = hevent_loop(io);
@@ -1102,10 +1105,10 @@ int HttpHandler::connectProxy(const std::string& strUrl) {
11021105
hio_set_connect_timeout(upstream_io, service->proxy_connect_timeout);
11031106
}
11041107
if (service->proxy_read_timeout > 0) {
1105-
hio_set_read_timeout(io, service->proxy_read_timeout);
1108+
hio_set_read_timeout(upstream_io, service->proxy_read_timeout);
11061109
}
11071110
if (service->proxy_write_timeout > 0) {
1108-
hio_set_write_timeout(io, service->proxy_write_timeout);
1111+
hio_set_write_timeout(upstream_io, service->proxy_write_timeout);
11091112
}
11101113
hio_connect(upstream_io);
11111114
// NOTE: wait upstream_io connected then start read

http/server/HttpMiddleware.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
BEGIN_NAMESPACE_HV
55

66
int HttpMiddleware::CORS(HttpRequest* req, HttpResponse* resp) {
7-
resp->headers["Access-Control-Allow-Origin"] = req->GetHeader("Origin", "*");
7+
std::string origin = req->GetHeader("Origin", "*");
8+
resp->headers["Access-Control-Allow-Origin"] = origin;
9+
if (origin != "*") {
10+
resp->headers["Vary"] = "Origin";
11+
}
812
if (req->method == HTTP_OPTIONS) {
913
resp->headers["Access-Control-Allow-Methods"] = req->GetHeader("Access-Control-Request-Method", "OPTIONS, HEAD, GET, POST, PUT, DELETE, PATCH");
1014
resp->headers["Access-Control-Allow-Headers"] = req->GetHeader("Access-Control-Request-Headers", "Content-Type");

0 commit comments

Comments
 (0)