HTTPStream.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 <platform/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. size_t contentLength = -1;
  32. size_t currentPos = -1;
  33. StreamStatus status = StreamStatus::OPENING;
  34. /*
  35. * opens connection to given url and reads header
  36. *
  37. * @param url the http url to connect to
  38. */
  39. void connectToUrl(std::string url, bool disableSSL = false);
  40. /*
  41. * Reads data from the stream.
  42. *
  43. * @param buf The buffer to read data into.
  44. * @param nbytes The size of the buffer.
  45. * @return The number of bytes read.
  46. * @throws std::runtime_error if the stream is closed.
  47. */
  48. size_t read(uint8_t *buf, size_t nbytes);
  49. /*
  50. * Skips nbytes bytes in the stream.
  51. */
  52. size_t skip(size_t nbytes);
  53. size_t position() { return currentPos; }
  54. size_t size() { return contentLength; }
  55. // Closes the connection
  56. void close();
  57. };
  58. }
  59. #endif