http.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <sys/time.h>
  13. #include <iostream>
  14. #include "server.h"
  15. using std::cout;
  16. using std::endl;
  17. class FileFd;
  18. class HttpMethod;
  19. class Hashes;
  20. class CircleBuf
  21. {
  22. unsigned char *Buf;
  23. unsigned long long Size;
  24. unsigned long long InP;
  25. unsigned long long OutP;
  26. std::string OutQueue;
  27. unsigned long long StrPos;
  28. unsigned long long MaxGet;
  29. struct timeval Start;
  30. static unsigned long long BwReadLimit;
  31. static unsigned long long BwTickReadData;
  32. static struct timeval BwReadTick;
  33. static const unsigned int BW_HZ;
  34. unsigned long long LeftRead() const
  35. {
  36. unsigned long long Sz = Size - (InP - OutP);
  37. if (Sz > Size - (InP%Size))
  38. Sz = Size - (InP%Size);
  39. return Sz;
  40. }
  41. unsigned long long LeftWrite() const
  42. {
  43. unsigned long long Sz = InP - OutP;
  44. if (InP > MaxGet)
  45. Sz = MaxGet - OutP;
  46. if (Sz > Size - (OutP%Size))
  47. Sz = Size - (OutP%Size);
  48. return Sz;
  49. }
  50. void FillOut();
  51. public:
  52. Hashes *Hash;
  53. // total amount of data that got written so far
  54. unsigned long long TotalWriten;
  55. // Read data in
  56. bool Read(int Fd);
  57. bool Read(std::string const &Data);
  58. // Write data out
  59. bool Write(int Fd);
  60. bool WriteTillEl(std::string &Data,bool Single = false);
  61. // Control the write limit
  62. void Limit(long long Max) {if (Max == -1) MaxGet = 0-1; else MaxGet = OutP + Max;}
  63. bool IsLimit() const {return MaxGet == OutP;};
  64. void Print() const {cout << MaxGet << ',' << OutP << endl;};
  65. // Test for free space in the buffer
  66. bool ReadSpace() const {return Size - (InP - OutP) > 0;};
  67. bool WriteSpace() const {return InP - OutP > 0;};
  68. void Reset();
  69. // Dump everything
  70. void Stats();
  71. CircleBuf(HttpMethod const * const Owner, unsigned long long Size);
  72. ~CircleBuf();
  73. };
  74. struct HttpServerState: public ServerState
  75. {
  76. // This is the connection itself. Output is data FROM the server
  77. CircleBuf In;
  78. CircleBuf Out;
  79. int ServerFd;
  80. protected:
  81. virtual bool ReadHeaderLines(std::string &Data) APT_OVERRIDE;
  82. virtual bool LoadNextResponse(bool const ToFile, FileFd * const File) APT_OVERRIDE;
  83. virtual bool WriteResponse(std::string const &Data) APT_OVERRIDE;
  84. public:
  85. virtual void Reset(bool const Everything = true) APT_OVERRIDE;
  86. virtual bool RunData(FileFd * const File) APT_OVERRIDE;
  87. virtual bool RunDataToDevNull() 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 * const 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 std::unique_ptr<ServerState> CreateServerState(URI const &uri) APT_OVERRIDE;
  104. virtual void RotateDNS() APT_OVERRIDE;
  105. virtual DealWithHeadersResult DealWithHeaders(FetchResult &Res) APT_OVERRIDE;
  106. protected:
  107. std::string AutoDetectProxyCmd;
  108. public:
  109. friend struct HttpServerState;
  110. explicit HttpMethod(std::string &&pProg);
  111. };
  112. #endif