http.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #include <iostream>
  12. using std::cout;
  13. using std::endl;
  14. class HttpMethod;
  15. class CircleBuf
  16. {
  17. unsigned char *Buf;
  18. unsigned long Size;
  19. unsigned long InP;
  20. unsigned long OutP;
  21. string OutQueue;
  22. unsigned long StrPos;
  23. unsigned long MaxGet;
  24. struct timeval Start;
  25. static unsigned long BwReadLimit;
  26. static unsigned long BwTickReadData;
  27. static struct timeval BwReadTick;
  28. static const unsigned int BW_HZ;
  29. unsigned long LeftRead()
  30. {
  31. unsigned long Sz = Size - (InP - OutP);
  32. if (Sz > Size - (InP%Size))
  33. Sz = Size - (InP%Size);
  34. return Sz;
  35. }
  36. unsigned long LeftWrite()
  37. {
  38. unsigned long Sz = InP - OutP;
  39. if (InP > MaxGet)
  40. Sz = MaxGet - OutP;
  41. if (Sz > Size - (OutP%Size))
  42. Sz = Size - (OutP%Size);
  43. return Sz;
  44. }
  45. void FillOut();
  46. public:
  47. Hashes *Hash;
  48. // Read data in
  49. bool Read(int Fd);
  50. bool Read(string Data);
  51. // Write data out
  52. bool Write(int Fd);
  53. bool WriteTillEl(string &Data,bool Single = false);
  54. // Control the write limit
  55. void Limit(long Max) {if (Max == -1) MaxGet = 0-1; else MaxGet = OutP + Max;}
  56. bool IsLimit() {return MaxGet == OutP;};
  57. void Print() {cout << MaxGet << ',' << OutP << endl;};
  58. // Test for free space in the buffer
  59. bool ReadSpace() {return Size - (InP - OutP) > 0;};
  60. bool WriteSpace() {return InP - OutP > 0;};
  61. // Dump everything
  62. void Reset();
  63. void Stats();
  64. CircleBuf(unsigned long Size);
  65. ~CircleBuf() {delete [] Buf; delete Hash;};
  66. };
  67. struct ServerState
  68. {
  69. // This is the last parsed Header Line
  70. unsigned int Major;
  71. unsigned int Minor;
  72. unsigned int Result;
  73. char Code[MAXLEN];
  74. // These are some statistics from the last parsed header lines
  75. unsigned long Size;
  76. signed long StartPos;
  77. time_t Date;
  78. bool HaveContent;
  79. enum {Chunked,Stream,Closes} Encoding;
  80. enum {Header, Data} State;
  81. bool Persistent;
  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. int RunHeaders();
  96. bool RunData();
  97. bool Open();
  98. bool Close();
  99. ServerState(URI Srv,HttpMethod *Owner);
  100. ~ServerState() {Close();};
  101. };
  102. class HttpMethod : public pkgAcqMethod
  103. {
  104. void SendReq(FetchItem *Itm,CircleBuf &Out);
  105. bool Go(bool ToFile,ServerState *Srv);
  106. bool Flush(ServerState *Srv);
  107. bool ServerDie(ServerState *Srv);
  108. int DealWithHeaders(FetchResult &Res,ServerState *Srv);
  109. virtual bool Fetch(FetchItem *);
  110. virtual bool Configuration(string Message);
  111. // In the event of a fatal signal this file will be closed and timestamped.
  112. static string FailFile;
  113. static int FailFd;
  114. static time_t FailTime;
  115. static void SigTerm(int);
  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