webget的get_url方法

  1. void get_URL(const string &host, const string &path) {
  2. TCPSocket sock{};
  3. sock.connect(Address(host,"http"));
  4. sock.write("GET "+path+" HTTP/1.1\r\nHost: "+host+"\r\n\r\n");
  5. sock.shutdown(SHUT_WR);
  6. while(!sock.eof()){
  7. cout<<sock.read();
  8. }
  9. sock.close();
  10. return;
  11. }
  • 通过make check_webget测试
  • 在虚拟机中生成SSH,配置到github上

    An in-memory reliable byte stream

    byte_stream.hh ```cpp

    ifndef 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 _data_q; // 字节流 bool is_input_end; // 输入端是否关闭 size_t _bytes_written; size_t _bytes_read; // 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.

  1. bool _error{}; //!< Flag indicating that the stream suffered an error.

public: //! Construct a stream with room for capacity bytes. ByteStream(const size_t capacity);

  1. //! \name "Input" interface for the writer
  2. //!@{
  3. //! Write a string of bytes into the stream. Write as many
  4. //! as will fit, and return how many were written.
  5. //! \returns the number of bytes accepted into the stream
  6. size_t write(const std::string &data);
  7. //! \returns the number of additional bytes that the stream has space for
  8. size_t remaining_capacity() const;
  9. //! Signal that the byte stream has reached its ending
  10. void end_input();
  11. //! Indicate that the stream suffered an error.
  12. void set_error() { _error = true; }
  13. //!@}
  14. //! \name "Output" interface for the reader
  15. //!@{
  16. //! Peek at next "len" bytes of the stream
  17. //! \returns a string
  18. std::string peek_output(const size_t len) const;
  19. //! Remove bytes from the buffer
  20. void pop_output(const size_t len);
  21. //! Read (i.e., copy and then pop) the next "len" bytes of the stream
  22. //! \returns a string
  23. std::string read(const size_t len);
  24. //! \returns `true` if the stream input has ended
  25. bool input_ended() const;
  26. //! \returns `true` if the stream has suffered an error
  27. bool error() const { return _error; }
  28. //! \returns the maximum amount that can currently be read from the stream
  29. size_t buffer_size() const;
  30. //! \returns `true` if the buffer is empty
  31. bool buffer_empty() const;
  32. //! \returns `true` if the output has reached the ending
  33. bool eof() const;
  34. //!@}
  35. //! \name General accounting
  36. //!@{
  37. //! Total number of bytes written
  38. size_t bytes_written() const;
  39. //! Total number of bytes popped
  40. size_t bytes_read() const;
  41. //!@}

};

endif // SPONGE_LIBSPONGE_BYTE_STREAM_HH

  1. byte_stream.cc
  2. ```cpp
  3. #include "byte_stream.hh"
  4. // Dummy implementation of a flow-controlled in-memory byte stream.
  5. // For Lab 0, please replace with a real implementation that passes the
  6. // automated checks run by `make check_lab0`.
  7. // You will need to add private members to the class declaration in `byte_stream.hh`
  8. template <typename... Targs>
  9. void DUMMY_CODE(Targs &&... /* unused */) {}
  10. using namespace std;
  11. // 需要列表初始化成员
  12. ByteStream::ByteStream(const size_t capacity):_capacity(capacity),_data_q(),is_input_end(false),_bytes_written(0),_bytes_read(0) {
  13. }
  14. // 返回实际写入的字节数量
  15. // 如果写入端关闭,则返回0
  16. size_t ByteStream::write(const string &data) {
  17. if (is_input_end) {
  18. return 0;
  19. }
  20. size_t toAddSize = data.size();
  21. if (toAddSize>remaining_capacity()) {
  22. toAddSize=remaining_capacity();
  23. }
  24. for (size_t i=0; i<toAddSize; i++) {
  25. _data_q.emplace_back(data[i]);
  26. }
  27. _bytes_written+=toAddSize;
  28. return toAddSize;
  29. }
  30. // 返回接下来读出的len个字节
  31. // 如果字节流中的字节个数不足,则返回全部
  32. // 这里不改变_bytes_read
  33. //! \param[in] len bytes will be copied from the output side of the buffer
  34. string ByteStream::peek_output(const size_t len) const {
  35. string res;
  36. for (size_t i=0; i<len&&i<_data_q.size(); i++) {
  37. res+=_data_q[i];
  38. }
  39. return res;
  40. }
  41. // pop字节流接下来读出的len个字节
  42. // 没有显式处理len>字节流中字节个数的情况
  43. // 这里改变_bytes_read
  44. //! \param[in] len bytes will be removed from the output side of the buffer
  45. void ByteStream::pop_output(const size_t len) {
  46. for (size_t i=0; i<len&&_data_q.size()>0; i++) {
  47. _data_q.pop_front();
  48. _bytes_read++;
  49. }
  50. }
  51. // 读出字节流接下来的len个字节,并pop
  52. // 如果字节流中的字节数不足,则返回并pop全部
  53. //! Read (i.e., copy and then pop) the next "len" bytes of the stream
  54. //! \param[in] len bytes will be popped and returned
  55. //! \returns a string
  56. std::string ByteStream::read(const size_t len) {
  57. string res;
  58. for (size_t i=0; i<len&&i<_data_q.size(); i++) {
  59. res+=_data_q.front();
  60. _data_q.pop_front();
  61. _bytes_read++;
  62. }
  63. return res;
  64. }
  65. void ByteStream::end_input() { is_input_end = true; }
  66. bool ByteStream::input_ended() const { return is_input_end; }
  67. // 当前字节流中的字节个数
  68. size_t ByteStream::buffer_size() const { return _data_q.size(); }
  69. // 当前字节流是否为空
  70. bool ByteStream::buffer_empty() const { return _data_q.size()==0; }
  71. // eof的条件是写入端关闭且字节流为空
  72. bool ByteStream::eof() const { return is_input_end&&_data_q.size()==0; }
  73. size_t ByteStream::bytes_written() const { return _bytes_written; }
  74. size_t ByteStream::bytes_read() const { return _bytes_read; }
  75. size_t ByteStream::remaining_capacity() const { return _capacity-_data_q.size(); }

测试

webget的测试样例可能会超时,这可能是因为国内网的原因,多运行几遍就好