voidget_URL(const string &host, const string &path){ // Your code here.
// You will need to connect to the "http" service on // the computer whose name is in the "host" string, // then request the URL path given in the "path" string.
// Then you'll need to print out everything the server sends back, // (not just one call to read() -- everything) until you reach // the "eof" (end of file). TCPSocket sock; sock.connect(Address(host, "http")); sock.write("GET " + path + " HTTP/1.1\r\nHost: " + host + "\r\n\r\n\r\n"); sock.shutdown(SHUT_WR); while(!sock.eof()) std::cout << sock.read(); sock.close(); }
#include<string> #include<queue> //! \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. classByteStream { private: // Your code here -- add private members as necessary. size_t stream_capacity;//容量大小 size_t byte_written;//写入 size_t byte_read;//读取 std::deque<char> byte_stream;//快速插入和删除 bool _end;//结束标志 // Hint: This doesn't need to be a sophisticated data structure at // all, but if any of your tests are taking longer than a second, // that's a sign that you probably want to keep exploring // different approaches.
bool _error{}; //!< Flag indicating that the stream suffered an error.
public: //! Construct a stream with room for `capacity` bytes. //构造函数,构造一个带有容量字节空间的流。 ByteStream(constsize_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 stream //写入字节,返回字节的数量 size_twrite(const std::string &data);
//! \returns the number of additional bytes that the stream has space for // size_tremaining_capacity()const;
//! Signal that the byte stream has reached its ending //结束输入 voidend_input();
//! Indicate that the stream suffered an error. voidset_error(){ _error = true; } //!@}
//! \name "Output" interface for the reader //!@{
//! Peek at next "len" bytes of the stream //! \returns a string std::string peek_output(constsize_t len)const;
//! Remove bytes from the buffer voidpop_output(constsize_t len);
//! Read (i.e., copy and then pop) the next "len" bytes of the stream //! \returns a string std::string read(constsize_t len);
//! \returns `true` if the stream input has ended boolinput_ended()const;
//! \returns `true` if the stream has suffered an error boolerror()const{ return _error; }
//! \returns the maximum amount that can currently be read from the stream size_tbuffer_size()const;
//! \returns `true` if the buffer is empty boolbuffer_empty()const;
//! \returns `true` if the output has reached the ending booleof()const; //!@}
//! \name General accounting //!@{
//! Total number of bytes written size_tbytes_written()const;
//! Total number of bytes popped size_tbytes_read()const; //!@} };
//! \param[in] len bytes will be copied from the output side of the buffer string ByteStream::peek_output(constsize_t len)const{ DUMMY_CODE(len); return std::string(byte_stream.begin(),byte_stream.begin()+std::min(len,byte_stream.size())); }
//! \param[in] len bytes will be removed from the output side of the buffer voidByteStream::pop_output(constsize_t len){ DUMMY_CODE(len); size_t lens_byte = std::min(len,byte_stream.size()); byte_stream.erase(byte_stream.begin(),byte_stream.begin()+lens_byte); byte_read+=lens_byte; }
//! 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 string std::string ByteStream::read(constsize_t len){ DUMMY_CODE(len); size_t lens_byte = std::min(len,byte_stream.size()); std::string n(byte_stream.begin(),byte_stream.begin()+lens_byte); pop_output(len); return n; }
//! \param[in] len bytes will be copied from the output side of the buffer string ByteStream::peek_output(constsize_t len)const{ DUMMY_CODE(len); return std::string(byte_stream.begin(),byte_stream.begin()+std::min(len,byte_stream.size())); }
//! \param[in] len bytes will be removed from the output side of the buffer voidByteStream::pop_output(constsize_t len){ DUMMY_CODE(len); //其实在实际情况下并不需要求较小值,但是为了通过单个测试,必须要求较小值 size_t lens_byte = std::min(len,byte_stream.size()); //删除这些数据 byte_stream.erase(byte_stream.begin(),byte_stream.begin()+lens_byte); //已经读取的数据增加 byte_read+=lens_byte; }
//! 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 string std::string ByteStream::read(constsize_t len){ DUMMY_CODE(len); //找到要read的长度byte_stream长度的较小值,尽可能地读 size_t lens_byte = std::min(len,byte_stream.size()); //读取数据 std::string n(byte_stream.begin(),byte_stream.begin()+lens_byte); //将读取后的数据弹出 pop_output(len); return n; }