优化文件上传速度,增加请求头预校验功能
This commit is contained in:
+64
-43
@@ -1,70 +1,91 @@
|
||||
#ifndef WEBCC_REQUEST_PARSER_H_
|
||||
#ifndef WEBCC_REQUEST_PARSER_H_
|
||||
#define WEBCC_REQUEST_PARSER_H_
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#include "webcc/parser.h"
|
||||
#include "webcc/view.h"
|
||||
|
||||
namespace webcc {
|
||||
namespace webcc
|
||||
{
|
||||
|
||||
using ViewMatcher =
|
||||
std::function<bool(const std::string&, const std::string&, bool*)>;
|
||||
using ViewMatcher =
|
||||
std::function<bool(const std::string&, const std::string&, bool*, ViewPtr*)>;
|
||||
|
||||
class Request;
|
||||
class Request;
|
||||
|
||||
class RequestParser : public Parser {
|
||||
public:
|
||||
RequestParser();
|
||||
class RequestParser : public Parser
|
||||
{
|
||||
public:
|
||||
RequestParser();
|
||||
|
||||
~RequestParser() override = default;
|
||||
~RequestParser() override = default;
|
||||
|
||||
void Init(Request* request, ViewMatcher view_matcher);
|
||||
void Init(Request* request, ViewMatcher view_matcher);
|
||||
|
||||
private:
|
||||
// Override to match the URL against views and check if the matched view
|
||||
// asks for data streaming.
|
||||
bool OnHeadersEnd() override;
|
||||
// 【新增】:检查 Header 是否已解析完毕
|
||||
bool IsHeaderParsed() const
|
||||
{
|
||||
return header_parsed_;
|
||||
}
|
||||
|
||||
bool ParseStartLine(const std::string& line) override;
|
||||
ViewPtr MatchedView() const
|
||||
{
|
||||
return matched_view_;
|
||||
}
|
||||
|
||||
// Override to handle multipart form data which is request only.
|
||||
bool ParseContent(const char* data, std::size_t length) override;
|
||||
private:
|
||||
// Override to match the URL against views and check if the matched view
|
||||
// asks for data streaming.
|
||||
bool OnHeadersEnd() override;
|
||||
|
||||
// Multipart specific parsing helpers.
|
||||
bool ParseStartLine(const std::string& line) override;
|
||||
|
||||
bool ParseMultipartContent(const char* data, std::size_t length);
|
||||
bool ParsePartHeaders(bool* need_more_data);
|
||||
bool GetNextBoundaryLine(std::size_t* b_off, std::size_t* b_len, bool* ended);
|
||||
// Override to handle multipart form data which is request only.
|
||||
bool ParseContent(const char* data, std::size_t length) override;
|
||||
|
||||
// Check if the str.substr(off, count) is a boundary.
|
||||
bool IsBoundary(const std::string& str, std::size_t off,
|
||||
std::size_t count, bool* end = nullptr) const;
|
||||
// Multipart specific parsing helpers.
|
||||
|
||||
private:
|
||||
// The result request message.
|
||||
Request* request_ = nullptr;
|
||||
bool ParseMultipartContent(const char* data, std::size_t length);
|
||||
bool ParsePartHeaders(bool* need_more_data);
|
||||
bool GetNextBoundaryLine(std::size_t* b_off, std::size_t* b_len, bool* ended);
|
||||
|
||||
// A function for matching view once the headers of a request has been
|
||||
// received. The parsing will stop and fail if no view can be matched.
|
||||
ViewMatcher view_matcher_;
|
||||
// Check if the str.substr(off, count) is a boundary.
|
||||
bool IsBoundary(const std::string& str, std::size_t off,
|
||||
std::size_t count, bool* end = nullptr) const;
|
||||
|
||||
// Form data parsing steps.
|
||||
enum class Step {
|
||||
kStart,
|
||||
kBoundaryParsed,
|
||||
kHeadersParsed,
|
||||
kEnded,
|
||||
};
|
||||
private:
|
||||
// The result request message.
|
||||
Request* request_ = nullptr;
|
||||
|
||||
Step step_ = Step::kStart;
|
||||
// A function for matching view once the headers of a request has been
|
||||
// received. The parsing will stop and fail if no view can be matched.
|
||||
ViewMatcher view_matcher_;
|
||||
|
||||
// The current form part being parsed.
|
||||
FormPartPtr part_;
|
||||
// Form data parsing steps.
|
||||
enum class Step
|
||||
{
|
||||
kStart,
|
||||
kBoundaryParsed,
|
||||
kHeadersParsed,
|
||||
kEnded,
|
||||
};
|
||||
|
||||
// All form parts parsed.
|
||||
std::vector<FormPartPtr> form_parts_;
|
||||
};
|
||||
Step step_ = Step::kStart;
|
||||
|
||||
// The current form part being parsed.
|
||||
FormPartPtr part_;
|
||||
|
||||
// All form parts parsed.
|
||||
std::vector<FormPartPtr> form_parts_;
|
||||
|
||||
// 【新增】:Header 解析完成标志
|
||||
bool header_parsed_ = false;
|
||||
|
||||
// View映射(用于请求头检查)
|
||||
ViewPtr matched_view_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
|
||||
Reference in New Issue
Block a user