HTTPStream.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #ifndef BELL_HTTP_STREAM_H
  2. #define BELL_HTTP_STREAM_H
  3. #include <string>
  4. #include <BellLogger.h>
  5. #include <ByteStream.h>
  6. #include <BellSocket.h>
  7. #include <TCPSocket.h>
  8. #include <TLSSocket.h>
  9. /*
  10. * HTTPStream
  11. *
  12. * A class for reading and writing HTTP streams implementing the ByteStream interface.
  13. *
  14. */
  15. namespace bell
  16. {
  17. enum class StreamStatus
  18. {
  19. OPENING,
  20. READING_HEADERS,
  21. READING_DATA,
  22. CLOSED
  23. };
  24. class HTTPStream : public ByteStream
  25. {
  26. public:
  27. HTTPStream();
  28. ~HTTPStream();
  29. std::unique_ptr<bell::Socket> socket;
  30. bool hasFixedSize = false;
  31. std::vector<uint8_t> remainingData;
  32. size_t contentLength = -1;
  33. size_t currentPos = -1;
  34. StreamStatus status = StreamStatus::OPENING;
  35. /*
  36. * opens connection to given url and reads header
  37. *
  38. * @param url the http url to connect to
  39. */
  40. void connectToUrl(std::string url, bool disableSSL = false);
  41. /*
  42. * Reads data from the stream.
  43. *
  44. * @param buf The buffer to read data into.
  45. * @param nbytes The size of the buffer.
  46. * @return The number of bytes read.
  47. * @throws std::runtime_error if the stream is closed.
  48. */
  49. size_t read(uint8_t *buf, size_t nbytes);
  50. /*
  51. * Skips nbytes bytes in the stream.
  52. */
  53. size_t skip(size_t nbytes);
  54. size_t position() { return currentPos; }
  55. size_t size() { return contentLength; }
  56. // Closes the connection
  57. void close();
  58. };
  59. }
  60. #endif