Skip to content

Commit 0ae7037

Browse files
committed
Fixed OPTIONS asterisk-form uri request
1 parent 45d00ab commit 0ae7037

File tree

4 files changed

+13
-11
lines changed

4 files changed

+13
-11
lines changed

include/http/Request.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Request {
3636
bool isMethod(const std::string &method);
3737
bool hasHeaderFieldValue(std::string key, const std::string &value);
3838

39-
int parseStatus(std::string line);
39+
int parseRequestLine(std::string line);
4040
int parseHeaderFields(std::string fields);
4141
};
4242

src/http/Http.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,22 @@ void Http::OnRequestRecv(std::string msg) {
3030
_request = Request();
3131

3232
msg.erase(msg.size() - delim.size(), delim.size());
33-
bool parseRet = _request.parseStatus(msg);
33+
bool parseRet = _request.parseRequestLine(msg);
3434

3535
_log = toString<Address &>(client) + ": " + _request.getMethod() + " " +
3636
_request.getUri().generate() + " " + _request.getVersion() + " -> " +
3737
toString<Address &>(host);
3838
parseRet |= _request.getUri().decode();
3939
accessLog_g.write("Decoded URI: " + _request.getUri().generate(), DEBUG);
40-
parseRet |= _request.getUri().resolveDots();
40+
41+
bool validPath = !_request.getUri().resolveDots();
4142
accessLog_g.write("Resolved URI: " + _request.getUri().generate(), DEBUG);
4243

43-
if (parseRet)
44+
const Uri &uriRef = _request.getUri();
45+
validPath &= startsWith(uriRef.getPath(), "/") ||
46+
(_request.isMethod("OPTIONS") && uriRef.getPath() == "*");
47+
48+
if (parseRet || !validPath)
4449
processError("400", "Bad Request", true);
4550
else if (isHttpVersionValid(_request.getVersion()) == false) {
4651
processError("505", "HTTP Version Not Supported");
@@ -177,8 +182,7 @@ void Http::processRequest() {
177182
if (_context == NULL) return processError("500", "Internal Server Error");
178183

179184
std::string contextUri = getContextArgs();
180-
if (Log::getLevel() >= DEBUG)
181-
accessLog_g.write("Context URI: '" + contextUri + "'", DEBUG);
185+
accessLog_g.write("Context URI: '" + contextUri + "'", DEBUG);
182186

183187
if (isMethodValid() == false) {
184188
bool bodyRequest = _request.isMethod("POST") || _request.isMethod("PUT");
@@ -385,7 +389,7 @@ void Http::getPutResponse(std::string uri) {
385389
void Http::processOptions(std::string uri) {
386390
_response.init("HTTP/1.1", "200", "OK");
387391
_response.setHeader("Allow",
388-
concatenate(getAllowedMethods(uri != "/*"), ", "));
392+
concatenate(getAllowedMethods(uri != "*"), ", "));
389393
_response.setReady();
390394
}
391395

src/http/Request.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ bool Request::hasHeaderFieldValue(std::string key, const std::string &value) {
6868
it->second.end();
6969
}
7070

71-
int Request::parseStatus(std::string line) {
71+
int Request::parseRequestLine(std::string line) {
7272
std::vector<std::string> requestLineTokens =
7373
split<std::vector<std::string> >(line, " ", true);
7474
if (requestLineTokens.size() != 3) return 1;

src/http/Uri.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ int Uri::load(std::string uri) {
4848
return 1;
4949
}
5050
}
51-
pos = uri.find("/");
52-
if (pos != 0) return 1;
5351

5452
// Find query start
5553
pos = uri.find("?");
@@ -136,8 +134,8 @@ int Uri::resolveDots() {
136134
it++;
137135
}
138136

139-
std::string newPath;
140137
it = blocks.begin();
138+
std::string newPath = *it++;
141139
for (; it != blocks.end(); it++) {
142140
if (it->empty() && it != --blocks.end()) continue;
143141
newPath += "/" + *it;

0 commit comments

Comments
 (0)