server.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Classes dealing with the abstraction of talking to a end via a text
  5. protocol like HTTP (which is used by the http and https methods)
  6. ##################################################################### */
  7. /*}}}*/
  8. #ifndef APT_SERVER_H
  9. #define APT_SERVER_H
  10. #include <apt-pkg/strutl.h>
  11. #include "aptmethod.h"
  12. #include <time.h>
  13. #include <iostream>
  14. #include <string>
  15. #include <memory>
  16. using std::cout;
  17. using std::endl;
  18. class Hashes;
  19. class ServerMethod;
  20. class FileFd;
  21. struct ServerState
  22. {
  23. // This is the last parsed Header Line
  24. unsigned int Major;
  25. unsigned int Minor;
  26. unsigned int Result;
  27. char Code[360];
  28. // These are some statistics from the last parsed header lines
  29. // total size of the usable content (aka: the file)
  30. unsigned long long TotalFileSize;
  31. // size we actually download (can be smaller than Size if we have partial content)
  32. unsigned long long DownloadSize;
  33. // size of junk content (aka: server error pages)
  34. unsigned long long JunkSize;
  35. // The start of the data (for partial content)
  36. unsigned long long StartPos;
  37. time_t Date;
  38. bool HaveContent;
  39. enum {Chunked,Stream,Closes} Encoding;
  40. enum {Header, Data} State;
  41. bool Persistent;
  42. bool PipelineAllowed;
  43. bool RangesAllowed;
  44. std::string Location;
  45. // This is a Persistent attribute of the server itself.
  46. bool Pipeline;
  47. URI ServerName;
  48. URI Proxy;
  49. unsigned long TimeOut;
  50. unsigned long long MaximumSize;
  51. protected:
  52. ServerMethod *Owner;
  53. virtual bool ReadHeaderLines(std::string &Data) = 0;
  54. virtual bool LoadNextResponse(bool const ToFile, FileFd * const File) = 0;
  55. public:
  56. bool HeaderLine(std::string Line);
  57. /** \brief Result of the header acquire */
  58. enum RunHeadersResult {
  59. /** \brief Header ok */
  60. RUN_HEADERS_OK,
  61. /** \brief IO error while retrieving */
  62. RUN_HEADERS_IO_ERROR,
  63. /** \brief Parse error after retrieving */
  64. RUN_HEADERS_PARSE_ERROR
  65. };
  66. /** \brief Get the headers before the data */
  67. RunHeadersResult RunHeaders(FileFd * const File, const std::string &Uri);
  68. bool AddPartialFileToHashes(FileFd &File);
  69. bool Comp(URI Other) const {return Other.Host == ServerName.Host && Other.Port == ServerName.Port;};
  70. virtual void Reset(bool const Everything = true);
  71. virtual bool WriteResponse(std::string const &Data) = 0;
  72. /** \brief Transfer the data from the socket */
  73. virtual bool RunData(FileFd * const File) = 0;
  74. virtual bool RunDataToDevNull() = 0;
  75. virtual bool Open() = 0;
  76. virtual bool IsOpen() = 0;
  77. virtual bool Close() = 0;
  78. virtual bool InitHashes(HashStringList const &ExpectedHashes) = 0;
  79. virtual Hashes * GetHashes() = 0;
  80. virtual bool Die(FileFd * const File) = 0;
  81. virtual bool Flush(FileFd * const File) = 0;
  82. virtual bool Go(bool ToFile, FileFd * const File) = 0;
  83. ServerState(URI Srv, ServerMethod *Owner);
  84. virtual ~ServerState() {};
  85. };
  86. class ServerMethod : public aptMethod
  87. {
  88. protected:
  89. virtual bool Fetch(FetchItem *) APT_OVERRIDE;
  90. std::unique_ptr<ServerState> Server;
  91. std::string NextURI;
  92. FileFd *File;
  93. unsigned long PipelineDepth;
  94. bool AllowRedirect;
  95. // Find the biggest item in the fetch queue for the checking of the maximum
  96. // size
  97. unsigned long long FindMaximumObjectSizeInQueue() const APT_PURE;
  98. public:
  99. bool Debug;
  100. /** \brief Result of the header parsing */
  101. enum DealWithHeadersResult {
  102. /** \brief The file is open and ready */
  103. FILE_IS_OPEN,
  104. /** \brief We got a IMS hit, the file has not changed */
  105. IMS_HIT,
  106. /** \brief The server reported a unrecoverable error */
  107. ERROR_UNRECOVERABLE,
  108. /** \brief The server reported a error with a error content page */
  109. ERROR_WITH_CONTENT_PAGE,
  110. /** \brief An error on the client side */
  111. ERROR_NOT_FROM_SERVER,
  112. /** \brief A redirect or retry request */
  113. TRY_AGAIN_OR_REDIRECT
  114. };
  115. /** \brief Handle the retrieved header data */
  116. virtual DealWithHeadersResult DealWithHeaders(FetchResult &Res);
  117. // In the event of a fatal signal this file will be closed and timestamped.
  118. static std::string FailFile;
  119. static int FailFd;
  120. static time_t FailTime;
  121. static APT_NORETURN void SigTerm(int);
  122. virtual bool Flush() { return Server->Flush(File); };
  123. int Loop();
  124. virtual void SendReq(FetchItem *Itm) = 0;
  125. virtual std::unique_ptr<ServerState> CreateServerState(URI const &uri) = 0;
  126. virtual void RotateDNS() = 0;
  127. virtual bool Configuration(std::string Message) APT_OVERRIDE;
  128. bool AddProxyAuth(URI &Proxy, URI const &Server) const;
  129. ServerMethod(std::string &&Binary, char const * const Ver,unsigned long const Flags);
  130. virtual ~ServerMethod() {};
  131. };
  132. #endif