voidget_URL(const string &host, const string &path){ // Your code here. TCPSocket socket; socket.connect(Address(host, "http")); socket.write("GET " + path + " HTTP/1.1\r\n"); socket.write("HOST: " + host + "\r\n"); socket.write("Connection: close\r\n"); socket.write("\r\n"); while (!socket.eof()) { cout << socket.read(); } socket.close(); // 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).
cerr << "Function called: get_URL(" << host << ", " << path << ").\n"; cerr << "Warning: get_URL() has not been implemented yet.\n"; }
2. The byte_stream
2.1 request
读入数据
输出数据
有容量限制
能控制是否结束
2.2 new members
数据的输入端和输出端是在不同的位置的,所以我们使用deqeue这种数据结构…
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classByteStream { private: // Your code here -- add private members as necessary.
// 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. std::deque<char> _data; // storage the data size_t _bytes_written; size_t _bytes_read; size_t _capacity; // the capacity bool _end_input; // make whether the stream has ended bool _error{}; //!< Flag indicating that the stream suffered an error.
size_tByteStream::write(const string &data){ if (_end_input) return0; size_t can_write = min(data.size(), _capacity - _data.size()); _bytes_written += can_write; for (size_t i = 0; i < can_write; i++) { _data.push_back(data[i]); } return can_write; }
//! \param[in] len bytes will be copied from the output side of the buffer string ByteStream::peek_output(constsize_t len)const{ int can_peek_output = min(len, _data.size()); returnstring(_data.begin(), _data.begin() + can_peek_output); }
//! \param[in] len bytes will be removed from the output side of the buffer voidByteStream::pop_output(constsize_t len){ size_t can_pop_output = min(len, _data.size()); _bytes_read += can_pop_output; for (size_t i = 0; i < can_pop_output; i++) _data.pop_front(); }
//! 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){ int can_read = min(_data.size(), len); string _read = peek_output(can_read); pop_output(can_read); return _read; }