http.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 Aquire 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. // This is a Persistent attribute of the server itself.
  82. bool Pipeline;
  83. HttpMethod *Owner;
  84. // This is the connection itself. Output is data FROM the server
  85. CircleBuf In;
  86. CircleBuf Out;
  87. int ServerFd;
  88. URI ServerName;
  89. bool HeaderLine(string Line);
  90. bool Comp(URI Other) {return Other.Host == ServerName.Host && Other.Port == ServerName.Port;};
  91. void Reset() {Major = 0; Minor = 0; Result = 0; Size = 0; StartPos = 0;
  92. Encoding = Closes; time(&Date); ServerFd = -1;
  93. Pipeline = true;};
  94. int RunHeaders();
  95. bool RunData();
  96. bool Open();
  97. bool Close();
  98. ServerState(URI Srv,HttpMethod *Owner);
  99. ~ServerState() {Close();};
  100. };
  101. class HttpMethod : public pkgAcqMethod
  102. {
  103. void SendReq(FetchItem *Itm,CircleBuf &Out);
  104. bool Go(bool ToFile,ServerState *Srv);
  105. bool Flush(ServerState *Srv);
  106. bool ServerDie(ServerState *Srv);
  107. int DealWithHeaders(FetchResult &Res,ServerState *Srv);
  108. virtual bool Configuration(string Message);
  109. // In the event of a fatal signal this file will be closed and timestamped.
  110. static string FailFile;
  111. static int FailFd;
  112. static time_t FailTime;
  113. static void SigTerm(int);
  114. protected:
  115. virtual bool Fetch(FetchItem *);
  116. public:
  117. friend class ServerState;
  118. FileFd *File;
  119. ServerState *Server;
  120. int Loop();
  121. HttpMethod() : pkgAcqMethod("1.2",Pipeline | SendConfig)
  122. {
  123. File = 0;
  124. Server = 0;
  125. };
  126. };
  127. #endif