http.h 5.0 KB

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