http.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 acquire 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. #include "server.h"
  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 HttpServerState: public ServerState
  70. {
  71. // This is the connection itself. Output is data FROM the server
  72. CircleBuf In;
  73. CircleBuf Out;
  74. int ServerFd;
  75. protected:
  76. virtual bool ReadHeaderLines(std::string &Data);
  77. virtual bool LoadNextResponse(bool const ToFile, FileFd * const File);
  78. virtual bool WriteResponse(std::string const &Data);
  79. public:
  80. virtual void Reset() { ServerState::Reset(); ServerFd = -1; };
  81. virtual bool RunData(FileFd * const File);
  82. virtual bool Open();
  83. virtual bool IsOpen();
  84. virtual bool Close();
  85. virtual bool InitHashes(FileFd &File);
  86. virtual Hashes * GetHashes();
  87. virtual bool Die(FileFd &File);
  88. virtual bool Flush(FileFd * const File);
  89. virtual bool Go(bool ToFile, FileFd * const File);
  90. HttpServerState(URI Srv, HttpMethod *Owner);
  91. virtual ~HttpServerState() {Close();};
  92. };
  93. class HttpMethod : public ServerMethod
  94. {
  95. public:
  96. virtual void SendReq(FetchItem *Itm);
  97. /** \brief Try to AutoDetect the proxy */
  98. bool AutoDetectProxy();
  99. virtual bool Configuration(std::string Message);
  100. virtual ServerState * CreateServerState(URI uri);
  101. virtual void RotateDNS();
  102. protected:
  103. std::string AutoDetectProxyCmd;
  104. public:
  105. friend struct HttpServerState;
  106. HttpMethod() : ServerMethod("1.2",Pipeline | SendConfig)
  107. {
  108. File = 0;
  109. Server = 0;
  110. };
  111. };
  112. #endif