std::shared_ptr<Response> request(const std::string& request_type, const std::string& path, std::iostream& content, const std::map<std::string, std::string>& header = std::map<std::string, std::string>()) { std::string corrected_path = path; if (corrected_path == "") corrected_path = "/"; content.seekp(0, std::ios::end); auto content_length = content.tellp(); content.seekp(0, std::ios::beg); boost::asio::streambuf write_buffer; std::ostream write_stream(&write_buffer); write_stream << request_type << " " << corrected_path << " HTTP/1.1\r\n"; write_stream << "Host: " << host << "\r\n"; for (auto& h : header) { write_stream << h.first << ": " << h.second << "\r\n"; } if (content_length>0) write_stream << "Content-Length: " << content_length << "\r\n"; write_stream << "\r\n"; if (content_length>0) write_stream << content.rdbuf(); try { connect(); boost::asio::write(*socket, write_buffer); } catch (const std::exception& e) { socket_error = true; throw std::invalid_argument(e.what()); } return request_read(); }
bool test_seekable_in_chars(std::iostream& io) { int i; // old 'for' scope workaround. // Test seeking with ios::cur for (i = 0; i < data_reps; ++i) { int j; for (j = 0; j < chunk_size; ++j) io.put(narrow_data()[j]); io.seekp(-chunk_size, BOOST_IOS::cur); for (j = 0; j < chunk_size; ++j) if (io.get() != narrow_data()[j]) return false; io.seekp(-chunk_size, BOOST_IOS::cur); for (j = 0; j < chunk_size; ++j) io.put(narrow_data()[j]); } // Test seeking with ios::beg std::streamoff off = 0; io.seekp(0, BOOST_IOS::beg); for (i = 0; i < data_reps; ++i, off += chunk_size) { int j; for (j = 0; j < chunk_size; ++j) io.put(narrow_data()[j]); io.seekp(off, BOOST_IOS::beg); for (j = 0; j < chunk_size; ++j) if (io.get() != narrow_data()[j]) return false; io.seekp(off, BOOST_IOS::beg); for (j = 0; j < chunk_size; ++j) io.put(narrow_data()[j]); } // Test seeking with ios::end io.seekp(0, BOOST_IOS::end); off = io.tellp(); io.seekp(-off, BOOST_IOS::end); for (i = 0; i < data_reps; ++i, off -= chunk_size) { int j; for (j = 0; j < chunk_size; ++j) io.put(narrow_data()[j]); io.seekp(-off, BOOST_IOS::end); for (j = 0; j < chunk_size; ++j) if (io.get() != narrow_data()[j]) return false; io.seekp(-off, BOOST_IOS::end); for (j = 0; j < chunk_size; ++j) io.put(narrow_data()[j]); } return true; }
bool test_seekable_in_chunks(std::iostream& io) { int i; // old 'for' scope workaround. // Test seeking with ios::cur for (i = 0; i < data_reps; ++i) { io.write(narrow_data(), chunk_size); io.seekp(-chunk_size, BOOST_IOS::cur); char buf[chunk_size]; io.read(buf, chunk_size); if (strncmp(buf, narrow_data(), chunk_size) != 0) return false; io.seekp(-chunk_size, BOOST_IOS::cur); io.write(narrow_data(), chunk_size); } // Test seeking with ios::beg std::streamoff off = 0; io.seekp(0, BOOST_IOS::beg); for (i = 0; i < data_reps; ++i, off += chunk_size) { io.write(narrow_data(), chunk_size); io.seekp(off, BOOST_IOS::beg); char buf[chunk_size]; io.read(buf, chunk_size); if (strncmp(buf, narrow_data(), chunk_size) != 0) return false; io.seekp(off, BOOST_IOS::beg); io.write(narrow_data(), chunk_size); } // Test seeking with ios::end io.seekp(0, BOOST_IOS::end); off = io.tellp(); io.seekp(-off, BOOST_IOS::end); for (i = 0; i < data_reps; ++i, off -= chunk_size) { io.write(narrow_data(), chunk_size); io.seekp(-off, BOOST_IOS::end); char buf[chunk_size]; io.read(buf, chunk_size); if (strncmp(buf, narrow_data(), chunk_size) != 0) return false; io.seekp(-off, BOOST_IOS::end); io.write(narrow_data(), chunk_size); } return true; }