server.h 4.2 KB

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