http.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/// $Id: http.h,v 1.12 2002/04/18 05:09:38 jgg Exp $
  3. // $Id: http.h,v 1.12 2002/04/18 05:09:38 jgg Exp $
  4. /* ######################################################################
  5. HTTP Acquire Method - This is the HTTP aquire method for APT.
  6. ##################################################################### */
  7. /*}}}*/
  8. #ifndef APT_HTTP_H
  9. #define APT_HTTP_H
  10. #include <apt-pkg/strutl.h>
  11. #include <string>
  12. using std::cout;
  13. using std::endl;
  14. class HttpMethod;
  15. class Hashes;
  16. class CircleBuf
  17. {
  18. unsigned char *Buf;
  19. unsigned long long Size;
  20. unsigned long long InP;
  21. unsigned long long OutP;
  22. std::string OutQueue;
  23. unsigned long long StrPos;
  24. unsigned long long MaxGet;
  25. struct timeval Start;
  26. static unsigned long long BwReadLimit;
  27. static unsigned long long BwTickReadData;
  28. static struct timeval BwReadTick;
  29. static const unsigned int BW_HZ;
  30. unsigned long long LeftRead() const
  31. {
  32. unsigned long long Sz = Size - (InP - OutP);
  33. if (Sz > Size - (InP%Size))
  34. Sz = Size - (InP%Size);
  35. return Sz;
  36. }
  37. unsigned long long LeftWrite() const
  38. {
  39. unsigned long long Sz = InP - OutP;
  40. if (InP > MaxGet)
  41. Sz = MaxGet - OutP;
  42. if (Sz > Size - (OutP%Size))
  43. Sz = Size - (OutP%Size);
  44. return Sz;
  45. }
  46. void FillOut();
  47. public:
  48. Hashes *Hash;
  49. // Read data in
  50. bool Read(int Fd);
  51. bool Read(std::string Data);
  52. // Write data out
  53. bool Write(int Fd);
  54. bool WriteTillEl(std::string &Data,bool Single = false);
  55. // Control the write limit
  56. void Limit(long long Max) {if (Max == -1) MaxGet = 0-1; else MaxGet = OutP + Max;}
  57. bool IsLimit() const {return MaxGet == OutP;};
  58. void Print() const {cout << MaxGet << ',' << OutP << endl;};
  59. // Test for free space in the buffer
  60. bool ReadSpace() const {return Size - (InP - OutP) > 0;};
  61. bool WriteSpace() const {return InP - OutP > 0;};
  62. // Dump everything
  63. void Reset();
  64. void Stats();
  65. CircleBuf(unsigned long long Size);
  66. ~CircleBuf();
  67. };
  68. struct ServerState
  69. {
  70. // This is the last parsed Header Line
  71. unsigned int Major;
  72. unsigned int Minor;
  73. unsigned int Result;
  74. char Code[360];
  75. // These are some statistics from the last parsed header lines
  76. unsigned long long Size;
  77. signed long long StartPos;
  78. time_t Date;
  79. bool HaveContent;
  80. enum {Chunked,Stream,Closes} Encoding;
  81. enum {Header, Data} State;
  82. bool Persistent;
  83. std::string Location;
  84. // This is a Persistent attribute of the server itself.
  85. bool Pipeline;
  86. HttpMethod *Owner;
  87. // This is the connection itself. Output is data FROM the server
  88. CircleBuf In;
  89. CircleBuf Out;
  90. int ServerFd;
  91. URI ServerName;
  92. bool HeaderLine(std::string Line);
  93. bool Comp(URI Other) const {return Other.Host == ServerName.Host && Other.Port == ServerName.Port;};
  94. void Reset() {Major = 0; Minor = 0; Result = 0; Code[0] = '\0'; Size = 0;
  95. StartPos = 0; Encoding = Closes; time(&Date); HaveContent = false;
  96. State = Header; Persistent = false; ServerFd = -1;
  97. Pipeline = true;};
  98. /** \brief Result of the header acquire */
  99. enum RunHeadersResult {
  100. /** \brief Header ok */
  101. RUN_HEADERS_OK,
  102. /** \brief IO error while retrieving */
  103. RUN_HEADERS_IO_ERROR,
  104. /** \brief Parse error after retrieving */
  105. RUN_HEADERS_PARSE_ERROR,
  106. };
  107. /** \brief Get the headers before the data */
  108. RunHeadersResult RunHeaders();
  109. /** \brief Transfer the data from the socket */
  110. bool RunData();
  111. bool Open();
  112. bool Close();
  113. ServerState(URI Srv,HttpMethod *Owner);
  114. ~ServerState() {Close();};
  115. };
  116. class HttpMethod : public pkgAcqMethod
  117. {
  118. void SendReq(FetchItem *Itm,CircleBuf &Out);
  119. bool Go(bool ToFile,ServerState *Srv);
  120. bool Flush(ServerState *Srv);
  121. bool ServerDie(ServerState *Srv);
  122. /** \brief Result of the header parsing */
  123. enum DealWithHeadersResult {
  124. /** \brief The file is open and ready */
  125. FILE_IS_OPEN,
  126. /** \brief We got a IMS hit, the file has not changed */
  127. IMS_HIT,
  128. /** \brief The server reported a unrecoverable error */
  129. ERROR_UNRECOVERABLE,
  130. /** \brief The server reported a error with a error content page */
  131. ERROR_WITH_CONTENT_PAGE,
  132. /** \brief A error on the client side */
  133. ERROR_NOT_FROM_SERVER,
  134. /** \brief A redirect or retry request */
  135. TRY_AGAIN_OR_REDIRECT
  136. };
  137. /** \brief Handle the retrieved header data */
  138. DealWithHeadersResult DealWithHeaders(FetchResult &Res,ServerState *Srv);
  139. /** \brief Try to AutoDetect the proxy */
  140. bool AutoDetectProxy();
  141. virtual bool Configuration(std::string Message);
  142. // In the event of a fatal signal this file will be closed and timestamped.
  143. static std::string FailFile;
  144. static int FailFd;
  145. static time_t FailTime;
  146. static void SigTerm(int);
  147. protected:
  148. virtual bool Fetch(FetchItem *);
  149. std::string NextURI;
  150. std::string AutoDetectProxyCmd;
  151. public:
  152. friend struct ServerState;
  153. FileFd *File;
  154. ServerState *Server;
  155. int Loop();
  156. HttpMethod() : pkgAcqMethod("1.2",Pipeline | SendConfig)
  157. {
  158. File = 0;
  159. Server = 0;
  160. };
  161. };
  162. #endif