Buffer ChunkedEncoder::process(const BufferRef& input) { #if 0 if (input.empty()) ERROR("proc: EOF"); else TRACE("proc: inputLen=%ld", input.size()); #endif if (finished_) return Buffer(); if (input.empty()) finished_ = true; Buffer output; if (input.size()) { char buf[12]; int size = snprintf(buf, sizeof(buf), "%zx\r\n", input.size()); output.push_back(buf, size); output.push_back(input); output.push_back("\r\n"); } else output.push_back("0\r\n\r\n"); //! \todo a filter might create multiple output-chunks, though, we could improve process() to not return the output but append them directly. // virtual void process(const BufferRef& input, composite_source& output) = 0; return output; }
bool HttpFileMgr::Settings::openMimeTypes(const std::string& path) { Buffer input; if (!readFile(path, &input)) return false; auto lines = Tokenizer<BufferRef, Buffer>::tokenize(input, "\n"); mimetypes.clear(); for (auto line: lines) { line = line.trim(); auto columns = Tokenizer<BufferRef, BufferRef>::tokenize(line, " \t"); auto ci = columns.begin(), ce = columns.end(); BufferRef mime = ci != ce ? *ci++ : BufferRef(); if (!mime.empty() && mime[0] != '#') { for (; ci != ce; ++ci) { mimetypes[ci->str()] = mime.str(); } } } return true; }
Buffer DeflateFilter::process(const BufferRef& input) { //FIXME bool eof = input.empty(); /* if (input.empty()) { TRACE("process received empty input"); return Buffer(); } */ z_.total_out = 0; z_.next_in = reinterpret_cast<Bytef *>(input.begin()); z_.avail_in = input.size(); Buffer output(input.size() * 1.1 + 12 + 18); z_.next_out = reinterpret_cast<Bytef *>(output.end()); z_.avail_out = output.capacity(); do { if (output.size() > output.capacity() / 2) { z_.avail_out = Buffer::CHUNK_SIZE; output.reserve(output.capacity() + z_.avail_out); } int flushMethod; int expected; if (!eof) { flushMethod = Z_SYNC_FLUSH; // Z_NO_FLUSH expected = Z_OK; } else { flushMethod = Z_FINISH; expected = Z_STREAM_END; } int rv = deflate(&z_, flushMethod); //TRACE("deflate(): rv=%d, avail_in=%d, avail_out=%d, total_out=%ld", rv, z_.avail_in, z_.avail_out, z_.total_out); if (rv != expected) { TRACE("process: deflate() error (%d)", rv); return Buffer(); } } while (z_.avail_out == 0); assert(z_.avail_in == 0); output.resize(z_.total_out); //TRACE("process(%ld bytes, eof=%d) -> %ld", input.size(), eof, z_.total_out); return output; }
Buffer ObjectCache::Builder::process(const BufferRef& chunk) { if (object_) { TRACE(object_->request_, 3, "ObjectCache.Builder.process(): %zu bytes", chunk.size()); if (!chunk.empty()) { object_->backBuffer().body.push_back(chunk); } } return Buffer(chunk); }
void ApiRequest::onBodyChunk(const BufferRef& chunk) { body_ << chunk; if (chunk.empty()) { parseBody(); if (!process()) { request_->log(Severity::error, "Error parsing request body."); if (!request_->status) request_->status = HttpStatus::BadRequest; request_->finish(); } } }
Buffer BZip2Filter::process(const BufferRef& input) { if (input.empty()) return Buffer(); int rv = BZ2_bzCompressInit(&bz_, level(), // compression level 0, // no output 0 // work factor ); if (rv != BZ_OK) return Buffer(); bz_.next_in = input.begin(); bz_.avail_in = input.size(); bz_.total_in_lo32 = 0; bz_.total_in_hi32 = 0; Buffer output(input.size() * 1.1 + 12); bz_.next_out = output.end(); bz_.avail_out = output.capacity(); bz_.total_out_lo32 = 0; bz_.total_out_hi32 = 0; rv = BZ2_bzCompress(&bz_, BZ_FINISH); if (rv != BZ_STREAM_END) { BZ2_bzCompressEnd(&bz_); return Buffer(); } if (bz_.total_out_hi32) return Buffer(); // file too large output.resize(bz_.total_out_lo32); rv = BZ2_bzCompressEnd(&bz_); if (rv != BZ_OK) return Buffer(); return output; }
void FileInfoService::Config::loadMimetypes(const std::string& filename) { Buffer input(x0::read_file(filename)); auto lines = Tokenizer<BufferRef, Buffer>::tokenize(input, "\n"); mimetypes.clear(); for (auto line: lines) { line = line.trim(); auto columns = Tokenizer<BufferRef, BufferRef>::tokenize(line, " \t"); auto ci = columns.begin(), ce = columns.end(); BufferRef mime = ci != ce ? *ci++ : BufferRef(); if (!mime.empty() && mime[0] != '#') { for (; ci != ce; ++ci) { mimetypes[ci->str()] = mime.str(); } } } }