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