server.h 4.6 KB

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