http.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <apt-pkg/acquire-method.h>
  12. #include <string>
  13. #include <sys/time.h>
  14. #include <iostream>
  15. #include "server.h"
  16. using std::cout;
  17. using std::endl;
  18. class FileFd;
  19. class HttpMethod;
  20. class Hashes;
  21. class CircleBuf
  22. {
  23. unsigned char *Buf;
  24. unsigned long long Size;
  25. unsigned long long InP;
  26. unsigned long long OutP;
  27. std::string OutQueue;
  28. unsigned long long StrPos;
  29. unsigned long long MaxGet;
  30. struct timeval Start;
  31. static unsigned long long BwReadLimit;
  32. static unsigned long long BwTickReadData;
  33. static struct timeval BwReadTick;
  34. static const unsigned int BW_HZ;
  35. unsigned long long LeftRead() const
  36. {
  37. unsigned long long Sz = Size - (InP - OutP);
  38. if (Sz > Size - (InP%Size))
  39. Sz = Size - (InP%Size);
  40. return Sz;
  41. }
  42. unsigned long long LeftWrite() const
  43. {
  44. unsigned long long Sz = InP - OutP;
  45. if (InP > MaxGet)
  46. Sz = MaxGet - OutP;
  47. if (Sz > Size - (OutP%Size))
  48. Sz = Size - (OutP%Size);
  49. return Sz;
  50. }
  51. void FillOut();
  52. public:
  53. Hashes *Hash;
  54. // total amount of data that got written so far
  55. unsigned long long TotalWriten;
  56. // Read data in
  57. bool Read(int Fd);
  58. bool Read(std::string Data);
  59. // Write data out
  60. bool Write(int Fd);
  61. bool WriteTillEl(std::string &Data,bool Single = false);
  62. // Control the write limit
  63. void Limit(long long Max) {if (Max == -1) MaxGet = 0-1; else MaxGet = OutP + Max;}
  64. bool IsLimit() const {return MaxGet == OutP;};
  65. void Print() const {cout << MaxGet << ',' << OutP << endl;};
  66. // Test for free space in the buffer
  67. bool ReadSpace() const {return Size - (InP - OutP) > 0;};
  68. bool WriteSpace() const {return InP - OutP > 0;};
  69. void Reset();
  70. // Dump everything
  71. void Stats();
  72. explicit CircleBuf(unsigned long long Size);
  73. ~CircleBuf();
  74. };
  75. struct HttpServerState: public ServerState
  76. {
  77. // This is the connection itself. Output is data FROM the server
  78. CircleBuf In;
  79. CircleBuf Out;
  80. int ServerFd;
  81. protected:
  82. virtual bool ReadHeaderLines(std::string &Data) APT_OVERRIDE;
  83. virtual bool LoadNextResponse(bool const ToFile, FileFd * const File) APT_OVERRIDE;
  84. virtual bool WriteResponse(std::string const &Data) APT_OVERRIDE;
  85. public:
  86. virtual void Reset() APT_OVERRIDE { ServerState::Reset(); ServerFd = -1; };
  87. virtual bool RunData(FileFd * const File) APT_OVERRIDE;
  88. virtual bool Open() APT_OVERRIDE;
  89. virtual bool IsOpen() APT_OVERRIDE;
  90. virtual bool Close() APT_OVERRIDE;
  91. virtual bool InitHashes(HashStringList const &ExpectedHashes) APT_OVERRIDE;
  92. virtual Hashes * GetHashes() APT_OVERRIDE;
  93. virtual bool Die(FileFd &File) APT_OVERRIDE;
  94. virtual bool Flush(FileFd * const File) APT_OVERRIDE;
  95. virtual bool Go(bool ToFile, FileFd * const File) APT_OVERRIDE;
  96. HttpServerState(URI Srv, HttpMethod *Owner);
  97. virtual ~HttpServerState() {Close();};
  98. };
  99. class HttpMethod : public ServerMethod
  100. {
  101. public:
  102. virtual void SendReq(FetchItem *Itm) APT_OVERRIDE;
  103. virtual bool Configuration(std::string Message) APT_OVERRIDE;
  104. virtual std::unique_ptr<ServerState> CreateServerState(URI const &uri) APT_OVERRIDE;
  105. virtual void RotateDNS() APT_OVERRIDE;
  106. protected:
  107. std::string AutoDetectProxyCmd;
  108. public:
  109. friend struct HttpServerState;
  110. HttpMethod() : ServerMethod("http", "1.2",Pipeline | SendConfig)
  111. {
  112. File = 0;
  113. Server = 0;
  114. };
  115. };
  116. #endif