// 如果 write_size < data.length(),多余的部分会被丢弃 /* lab0 for (size_t i = 0; i < write_size; ++i) { _buffer.push_back(data[i]); } */ // optimization in lab4 string tmp = data.substr(0, write_size); _buffer.append(BufferList(std::move(tmp)));
return write_size; }
//! \param[in] len bytes will be copied from the output side of the buffer string ByteStream::peek_output(constsize_t len)const{ size_t peek_size = std::min(len, buffer_size()); /* lab0 return std::string(_buffer.begin(), _buffer.begin() + peek_size); */ // optimization in lab4 string s = _buffer.concatenate(); returnstring(s.begin(), s.begin() + peek_size); }
//! \param[in] len bytes will be removed from the output side of the buffer voidByteStream::pop_output(constsize_t len){ size_t pop_size = std::min(len, buffer_size()); _read_size += pop_size; /* lab0 while (pop_size--) { _buffer.pop_front(); } */ // optimization in lab4 _buffer.remove_prefix(pop_size); }
//! 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){ std::string data = peek_output(len); pop_output(len); return data; }
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.
// If you don’t shut down your outgoing byte stream, // the server will wait around for a while for you to send additional requests // and won’t end its outgoing byte stream either. sock.shutdown(SHUT_WR);
// 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).
$ make check_webget Scanning dependencies of target check_webget [100%] Testing webget... Test project /home/kasen/Documents/cs144-sponge/build Start 31: t_webget 1/1 Test #31: t_webget ......................... Passed 2.05 sec
100% tests passed, 0 tests failed out of 1
Total Test time (real) = 2.05 sec [100%] Built target check_webget
$ ./apps/webget cs144.keithw.org /hasher/xyzzy DEBUG: Connecting to 104.196.238.229:80... done. DEBUG: Outbound stream to 104.196.238.229:80 finished (74 bytes still in flight). DEBUG: Outbound stream to 104.196.238.229:80 has been fully acknowledged. HTTP/1.1 200 OK Date: Thu, 15 Dec 2022 03:59:03 GMT Server: Apache Content-Length: 44 Connection: close Content-Type: text/plain
QWx0NhMPkoM/bJr/ohvHXlviFhOyYrYb+qqdOnwLYo4 DEBUG: Inbound stream from 104.196.238.229:80 finished cleanly. DEBUG: Waiting for lingering segments (e.g. retransmissions of FIN) from peer... DEBUG: Waiting for clean shutdown... DEBUG: TCP connection finished cleanly. done.
$ ./apps/webget stanford.edu /class/cs144 DEBUG: Connecting to 171.67.215.200:80... done. DEBUG: Outbound stream to 171.67.215.200:80 finished (69 bytes still in flight). DEBUG: Outbound stream to 171.67.215.200:80 has been fully acknowledged. DEBUG: Inbound stream from 171.67.215.200:80 finished cleanly. DEBUG: Waiting for lingering segments (e.g. retransmissions of FIN) from peer... HTTP/1.1 301 Moved Permanently Date: Thu, 15 Dec 2022 04:02:16 GMT Server: Apache Location: http://stanford.edu/class/cs144/ Content-Length: 240 Connection: close Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>301 Moved Permanently</title> </head><body> <h1>Moved Permanently</h1> <p>The document has moved <a href="http://stanford.edu/class/cs144/">here</a>.</p> </body></html> DEBUG: Waiting for clean shutdown... DEBUG: TCP connection finished cleanly. done.