webget的get_url方法
void get_URL(const string &host, const string &path) {TCPSocket sock{};sock.connect(Address(host,"http"));sock.write("GET "+path+" HTTP/1.1\r\nHost: "+host+"\r\n\r\n");sock.shutdown(SHUT_WR);while(!sock.eof()){cout<<sock.read();}sock.close();return;}
- 通过
make check_webget测试 - 在虚拟机中生成SSH,配置到github上
An in-memory reliable byte stream
byte_stream.hh ```cppifndef SPONGE_LIBSPONGE_BYTE_STREAM_HH
define SPONGE_LIBSPONGE_BYTE_STREAM_HH
include
include
//! \brief An in-order byte stream.
//! Bytes are written on the “input” side and read from the “output”
//! side. The byte stream is finite: the writer can end the input,
//! and then no more bytes can be written.
class ByteStream {
private:
// Your code here — add private members as necessary.
size_t _capacity; // 容量
std::deque
bool _error{}; //!< Flag indicating that the stream suffered an error.
public:
//! Construct a stream with room for capacity bytes.
ByteStream(const size_t capacity);
//! \name "Input" interface for the writer//!@{//! Write a string of bytes into the stream. Write as many//! as will fit, and return how many were written.//! \returns the number of bytes accepted into the streamsize_t write(const std::string &data);//! \returns the number of additional bytes that the stream has space forsize_t remaining_capacity() const;//! Signal that the byte stream has reached its endingvoid end_input();//! Indicate that the stream suffered an error.void set_error() { _error = true; }//!@}//! \name "Output" interface for the reader//!@{//! Peek at next "len" bytes of the stream//! \returns a stringstd::string peek_output(const size_t len) const;//! Remove bytes from the buffervoid pop_output(const size_t len);//! Read (i.e., copy and then pop) the next "len" bytes of the stream//! \returns a stringstd::string read(const size_t len);//! \returns `true` if the stream input has endedbool input_ended() const;//! \returns `true` if the stream has suffered an errorbool error() const { return _error; }//! \returns the maximum amount that can currently be read from the streamsize_t buffer_size() const;//! \returns `true` if the buffer is emptybool buffer_empty() const;//! \returns `true` if the output has reached the endingbool eof() const;//!@}//! \name General accounting//!@{//! Total number of bytes writtensize_t bytes_written() const;//! Total number of bytes poppedsize_t bytes_read() const;//!@}
};
endif // SPONGE_LIBSPONGE_BYTE_STREAM_HH
byte_stream.cc```cpp#include "byte_stream.hh"// Dummy implementation of a flow-controlled in-memory byte stream.// For Lab 0, please replace with a real implementation that passes the// automated checks run by `make check_lab0`.// You will need to add private members to the class declaration in `byte_stream.hh`template <typename... Targs>void DUMMY_CODE(Targs &&... /* unused */) {}using namespace std;// 需要列表初始化成员ByteStream::ByteStream(const size_t capacity):_capacity(capacity),_data_q(),is_input_end(false),_bytes_written(0),_bytes_read(0) {}// 返回实际写入的字节数量// 如果写入端关闭,则返回0size_t ByteStream::write(const string &data) {if (is_input_end) {return 0;}size_t toAddSize = data.size();if (toAddSize>remaining_capacity()) {toAddSize=remaining_capacity();}for (size_t i=0; i<toAddSize; i++) {_data_q.emplace_back(data[i]);}_bytes_written+=toAddSize;return toAddSize;}// 返回接下来读出的len个字节// 如果字节流中的字节个数不足,则返回全部// 这里不改变_bytes_read//! \param[in] len bytes will be copied from the output side of the bufferstring ByteStream::peek_output(const size_t len) const {string res;for (size_t i=0; i<len&&i<_data_q.size(); i++) {res+=_data_q[i];}return res;}// pop字节流接下来读出的len个字节// 没有显式处理len>字节流中字节个数的情况// 这里改变_bytes_read//! \param[in] len bytes will be removed from the output side of the buffervoid ByteStream::pop_output(const size_t len) {for (size_t i=0; i<len&&_data_q.size()>0; i++) {_data_q.pop_front();_bytes_read++;}}// 读出字节流接下来的len个字节,并pop// 如果字节流中的字节数不足,则返回并pop全部//! Read (i.e., copy and then pop) the next "len" bytes of the stream//! \param[in] len bytes will be popped and returned//! \returns a stringstd::string ByteStream::read(const size_t len) {string res;for (size_t i=0; i<len&&i<_data_q.size(); i++) {res+=_data_q.front();_data_q.pop_front();_bytes_read++;}return res;}void ByteStream::end_input() { is_input_end = true; }bool ByteStream::input_ended() const { return is_input_end; }// 当前字节流中的字节个数size_t ByteStream::buffer_size() const { return _data_q.size(); }// 当前字节流是否为空bool ByteStream::buffer_empty() const { return _data_q.size()==0; }// eof的条件是写入端关闭且字节流为空bool ByteStream::eof() const { return is_input_end&&_data_q.size()==0; }size_t ByteStream::bytes_written() const { return _bytes_written; }size_t ByteStream::bytes_read() const { return _bytes_read; }size_t ByteStream::remaining_capacity() const { return _capacity-_data_q.size(); }
测试
webget的测试样例可能会超时,这可能是因为国内网的原因,多运行几遍就好
