http.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #define MAXLEN 360
  11. #include <apt-pkg/strutl.h>
  12. #include <string>
  13. using std::cout;
  14. using std::endl;
  15. class HttpMethod;
  16. class Hashes;
  17. class CircleBuf
  18. {
  19. unsigned char *Buf;
  20. unsigned long long Size;
  21. unsigned long long InP;
  22. unsigned long long OutP;
  23. std::string OutQueue;
  24. unsigned long long StrPos;
  25. unsigned long long MaxGet;
  26. struct timeval Start;
  27. static unsigned long long BwReadLimit;
  28. static unsigned long long BwTickReadData;
  29. static struct timeval BwReadTick;
  30. static const unsigned int BW_HZ;
  31. unsigned long long LeftRead() const
  32. {
  33. unsigned long long Sz = Size - (InP - OutP);
  34. if (Sz > Size - (InP%Size))
  35. Sz = Size - (InP%Size);
  36. return Sz;
  37. }
  38. unsigned long long LeftWrite() const
  39. {
  40. unsigned long long Sz = InP - OutP;
  41. if (InP > MaxGet)
  42. Sz = MaxGet - OutP;
  43. if (Sz > Size - (OutP%Size))
  44. Sz = Size - (OutP%Size);
  45. return Sz;
  46. }
  47. void FillOut();
  48. public:
  49. Hashes *Hash;
  50. // Read data in
  51. bool Read(int Fd);
  52. bool Read(std::string Data);
  53. // Write data out
  54. bool Write(int Fd);
  55. bool WriteTillEl(std::string &Data,bool Single = false);
  56. // Control the write limit
  57. void Limit(long long Max) {if (Max == -1) MaxGet = 0-1; else MaxGet = OutP + Max;}
  58. bool IsLimit() const {return MaxGet == OutP;};
  59. void Print() const {cout << MaxGet << ',' << OutP << endl;};
  60. // Test for free space in the buffer
  61. bool ReadSpace() const {return Size - (InP - OutP) > 0;};
  62. bool WriteSpace() const {return InP - OutP > 0;};
  63. // Dump everything
  64. void Reset();
  65. void Stats();
  66. CircleBuf(unsigned long long Size);
  67. ~CircleBuf();
  68. };
  69. struct ServerState
  70. {
  71. // This is the last parsed Header Line
  72. unsigned int Major;
  73. unsigned int Minor;
  74. unsigned int Result;
  75. char Code[MAXLEN];
  76. // These are some statistics from the last parsed header lines
  77. unsigned long long Size;
  78. signed long long StartPos;
  79. time_t Date;
  80. bool HaveContent;
  81. enum {Chunked,Stream,Closes} Encoding;
  82. enum {Header, Data} State;
  83. bool Persistent;
  84. std::string Location;
  85. // This is a Persistent attribute of the server itself.
  86. bool Pipeline;
  87. HttpMethod *Owner;
  88. // This is the connection itself. Output is data FROM the server
  89. CircleBuf In;
  90. CircleBuf Out;
  91. int ServerFd;
  92. URI ServerName;
  93. bool HeaderLine(std::string Line);
  94. bool Comp(URI Other) const {return Other.Host == ServerName.Host && Other.Port == ServerName.Port;};
  95. void Reset() {Major = 0; Minor = 0; Result = 0; Size = 0; StartPos = 0;
  96. Encoding = Closes; time(&Date); 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