MCPcopy Create free account
hub / github.com/PABannier/bark.cpp / parse_range_header

Function parse_range_header

examples/server/httplib.h:4229–5041  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4227
4228#ifdef CPPHTTPLIB_NO_EXCEPTIONS
4229inline bool parse_range_header(const std::string &s, Ranges &ranges) {
4230#else
4231inline bool parse_range_header(const std::string &s, Ranges &ranges) try {
4232#endif
4233 static auto re_first_range = std::regex(R"(bytes=(\d*-\d*(?:,\s*\d*-\d*)*))");
4234 std::smatch m;
4235 if (std::regex_match(s, m, re_first_range)) {
4236 auto pos = static_cast<size_t>(m.position(1));
4237 auto len = static_cast<size_t>(m.length(1));
4238 auto all_valid_ranges = true;
4239 split(&s[pos], &s[pos + len], ',', [&](const char *b, const char *e) {
4240 if (!all_valid_ranges) return;
4241 static auto re_another_range = std::regex(R"(\s*(\d*)-(\d*))");
4242 std::cmatch cm;
4243 if (std::regex_match(b, e, cm, re_another_range)) {
4244 ssize_t first = -1;
4245 if (!cm.str(1).empty()) {
4246 first = static_cast<ssize_t>(std::stoll(cm.str(1)));
4247 }
4248
4249 ssize_t last = -1;
4250 if (!cm.str(2).empty()) {
4251 last = static_cast<ssize_t>(std::stoll(cm.str(2)));
4252 }
4253
4254 if (first != -1 && last != -1 && first > last) {
4255 all_valid_ranges = false;
4256 return;
4257 }
4258 ranges.emplace_back(std::make_pair(first, last));
4259 }
4260 });
4261 return all_valid_ranges;
4262 }
4263 return false;
4264#ifdef CPPHTTPLIB_NO_EXCEPTIONS
4265}
4266#else
4267} catch (...) { return false; }
4268#endif
4269
4270class MultipartFormDataParser {
4271public:
4272 MultipartFormDataParser() = default;
4273
4274 void set_boundary(std::string &&boundary) {
4275 boundary_ = boundary;
4276 dash_boundary_crlf_ = dash_ + boundary_ + crlf_;
4277 crlf_dash_boundary_ = crlf_ + dash_ + boundary_;
4278 }
4279
4280 bool is_valid() const { return is_valid_; }
4281
4282 bool parse(const char *buf, size_t n, const ContentReceiver &content_callback,
4283 const MultipartContentHeader &header_callback) {
4284
4285 buf_append(buf, n);
4286

Callers 1

process_requestMethod · 0.85

Calls 2

emptyMethod · 0.80
splitFunction · 0.70

Tested by

no test coverage detected