server.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <apt-pkg/acquire-method.h>
  12. #include "aptmethod.h"
  13. #include <time.h>
  14. #include <iostream>
  15. #include <string>
  16. #include <memory>
  17. using std::cout;
  18. using std::endl;
  19. class Hashes;
  20. class ServerMethod;
  21. class FileFd;
  22. struct ServerState
  23. {
  24. // This is the last parsed Header Line
  25. unsigned int Major;
  26. unsigned int Minor;
  27. unsigned int Result;
  28. char Code[360];
  29. // These are some statistics from the last parsed header lines
  30. // total size of the usable content (aka: the file)
  31. unsigned long long TotalFileSize;
  32. // size we actually download (can be smaller than Size if we have partial content)
  33. unsigned long long DownloadSize;
  34. // size of junk content (aka: server error pages)
  35. unsigned long long JunkSize;
  36. // The start of the data (for partial content)
  37. unsigned long long StartPos;
  38. time_t Date;
  39. bool HaveContent;
  40. enum {Chunked,Stream,Closes} Encoding;
  41. enum {Header, Data} State;
  42. bool Persistent;
  43. bool PipelineAllowed;
  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() {Major = 0; Minor = 0; Result = 0; Code[0] = '\0'; TotalFileSize = 0; JunkSize = 0;
  71. StartPos = 0; Encoding = Closes; time(&Date); HaveContent = false;
  72. State = Header; Persistent = false; Pipeline = false; MaximumSize = 0; PipelineAllowed = true;};
  73. virtual bool WriteResponse(std::string const &Data) = 0;
  74. /** \brief Transfer the data from the socket */
  75. virtual bool RunData(FileFd * const File) = 0;
  76. virtual bool Open() = 0;
  77. virtual bool IsOpen() = 0;
  78. virtual bool Close() = 0;
  79. virtual bool InitHashes(HashStringList const &ExpectedHashes) = 0;
  80. virtual Hashes * GetHashes() = 0;
  81. virtual bool Die(FileFd &File) = 0;
  82. virtual bool Flush(FileFd * const File) = 0;
  83. virtual bool Go(bool ToFile, FileFd * const File) = 0;
  84. ServerState(URI Srv, ServerMethod *Owner);
  85. virtual ~ServerState() {};
  86. };
  87. class ServerMethod : public aptMethod
  88. {
  89. protected:
  90. virtual bool Fetch(FetchItem *) APT_OVERRIDE;
  91. std::unique_ptr<ServerState> Server;
  92. std::string NextURI;
  93. FileFd *File;
  94. unsigned long PipelineDepth;
  95. bool AllowRedirect;
  96. // Find the biggest item in the fetch queue for the checking of the maximum
  97. // size
  98. unsigned long long FindMaximumObjectSizeInQueue() const APT_PURE;
  99. public:
  100. bool Debug;
  101. /** \brief Result of the header parsing */
  102. enum DealWithHeadersResult {
  103. /** \brief The file is open and ready */
  104. FILE_IS_OPEN,
  105. /** \brief We got a IMS hit, the file has not changed */
  106. IMS_HIT,
  107. /** \brief The server reported a unrecoverable error */
  108. ERROR_UNRECOVERABLE,
  109. /** \brief The server reported a error with a error content page */
  110. ERROR_WITH_CONTENT_PAGE,
  111. /** \brief An error on the client side */
  112. ERROR_NOT_FROM_SERVER,
  113. /** \brief A redirect or retry request */
  114. TRY_AGAIN_OR_REDIRECT
  115. };
  116. /** \brief Handle the retrieved header data */
  117. DealWithHeadersResult DealWithHeaders(FetchResult &Res);
  118. // In the event of a fatal signal this file will be closed and timestamped.
  119. static std::string FailFile;
  120. static int FailFd;
  121. static time_t FailTime;
  122. static APT_NORETURN void SigTerm(int);
  123. virtual bool Flush() { return Server->Flush(File); };
  124. int Loop();
  125. virtual void SendReq(FetchItem *Itm) = 0;
  126. virtual std::unique_ptr<ServerState> CreateServerState(URI const &uri) = 0;
  127. virtual void RotateDNS() = 0;
  128. ServerMethod(char const * const Binary, char const * const Ver,unsigned long const Flags);
  129. virtual ~ServerMethod() {};
  130. };
  131. #endif