server.h 4.1 KB

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