Browse Source

Performance improvements in multipart parsing

master
Artyom Beilis 9 years ago
parent
commit
6c2790b7ac
2 changed files with 37 additions and 31 deletions
  1. +36
    -25
      private/multipart_parser.h
  2. +1
    -6
      src/http_file.cpp

+ 36
- 25
private/multipart_parser.h View File

@@ -138,31 +138,42 @@ namespace cppcms {
}
break;
case expecting_separator_boundary:
if(*buffer == boundary_[position_])
position_++;
else if(position_ > 0) {
file_->write_data().write(boundary_.c_str(),position_);
position_ = 0;
if(*buffer == boundary_[0])
position_=1;
if(!file_->write_data())
return no_room_left;
}
if(position_ == 0) {
file_->write_data() << *buffer;
if(!file_->write_data())
return no_room_left;
}
else if(position_ == boundary_.size()) {
state_ = expecting_one_crlf_or_eof;
position_ = 0;
file_->data().seekg(0);
files_.push_back(file_);
file_.reset(new http::file());
file_->set_temporary_directory(temp_dir_);
if(memory_limit_ != -1) {
file_->set_memory_limit(memory_limit_);
}
{
std::streambuf *out=file_->write_data().rdbuf();
char const *this_boundary = boundary_.c_str();
size_t boundary_size = boundary_.size();
while(size > 0) {
char c=*buffer;
if(c == this_boundary[position_])
position_++;
else if(position_ > 0) {
std::streamsize expected = position_;
std::streamsize s=out->sputn(this_boundary,position_);
position_ = 0;
if(c == boundary_[0])
position_=1;
if(s!=expected)
return no_room_left;
}
if(position_ == 0) {
if(out->sputc(c)==EOF)
return no_room_left;
}
else if(position_ == boundary_size) {
state_ = expecting_one_crlf_or_eof;
position_ = 0;
file_->data().seekg(0);
files_.push_back(file_);
file_.reset(new http::file());
file_->set_temporary_directory(temp_dir_);
if(memory_limit_ != -1) {
file_->set_memory_limit(memory_limit_);
}
break;
}
buffer++;
size--;
} // end while
}
break;
}


+ 1
- 6
src/http_file.cpp View File

@@ -12,7 +12,6 @@
#include <booster/nowide/cstdio.h>
#include <booster/nowide/fstream.h>
#include <stdlib.h>
#include <vector>

#include "tohex.h"

@@ -77,11 +76,7 @@ std::ostream &file::write_data()

void file::copy_stream(std::istream &in,std::ostream &out)
{
std::vector<char> v(1024,0);
while(!in.eof()) {
in.read(&v.front(),1024);
out.write(&v.front(),in.gcount());
}
out << in.rdbuf();
}

void file::save_to(std::string const &filename)


Loading…
Cancel
Save