http.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/// $Id: http.h,v 1.10 2001/03/06 07:15:29 jgg Exp $
  3. // $Id: http.h,v 1.10 2001/03/06 07:15:29 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. class HttpMethod;
  12. class CircleBuf
  13. {
  14. unsigned char *Buf;
  15. unsigned long Size;
  16. unsigned long InP;
  17. unsigned long OutP;
  18. string OutQueue;
  19. unsigned long StrPos;
  20. unsigned long MaxGet;
  21. struct timeval Start;
  22. unsigned long LeftRead()
  23. {
  24. unsigned long Sz = Size - (InP - OutP);
  25. if (Sz > Size - (InP%Size))
  26. Sz = Size - (InP%Size);
  27. return Sz;
  28. }
  29. unsigned long LeftWrite()
  30. {
  31. unsigned long Sz = InP - OutP;
  32. if (InP > MaxGet)
  33. Sz = MaxGet - OutP;
  34. if (Sz > Size - (OutP%Size))
  35. Sz = Size - (OutP%Size);
  36. return Sz;
  37. }
  38. void FillOut();
  39. public:
  40. Hashes *Hash;
  41. // Read data in
  42. bool Read(int Fd);
  43. bool Read(string Data);
  44. // Write data out
  45. bool Write(int Fd);
  46. bool WriteTillEl(string &Data,bool Single = false);
  47. // Control the write limit
  48. void Limit(long Max) {if (Max == -1) MaxGet = 0-1; else MaxGet = OutP + Max;}
  49. bool IsLimit() {return MaxGet == OutP;};
  50. void Print() {cout << MaxGet << ',' << OutP << endl;};
  51. // Test for free space in the buffer
  52. bool ReadSpace() {return Size - (InP - OutP) > 0;};
  53. bool WriteSpace() {return InP - OutP > 0;};
  54. // Dump everything
  55. void Reset();
  56. void Stats();
  57. CircleBuf(unsigned long Size);
  58. ~CircleBuf() {delete [] Buf;};
  59. };
  60. struct ServerState
  61. {
  62. // This is the last parsed Header Line
  63. unsigned int Major;
  64. unsigned int Minor;
  65. unsigned int Result;
  66. char Code[MAXLEN];
  67. // These are some statistics from the last parsed header lines
  68. unsigned long Size;
  69. signed long StartPos;
  70. time_t Date;
  71. bool HaveContent;
  72. enum {Chunked,Stream,Closes} Encoding;
  73. enum {Header, Data} State;
  74. bool Persistent;
  75. // This is a Persistent attribute of the server itself.
  76. bool Pipeline;
  77. HttpMethod *Owner;
  78. // This is the connection itself. Output is data FROM the server
  79. CircleBuf In;
  80. CircleBuf Out;
  81. int ServerFd;
  82. URI ServerName;
  83. bool HeaderLine(string Line);
  84. bool Comp(URI Other) {return Other.Host == ServerName.Host && Other.Port == ServerName.Port;};
  85. void Reset() {Major = 0; Minor = 0; Result = 0; Size = 0; StartPos = 0;
  86. Encoding = Closes; time(&Date); ServerFd = -1;
  87. Pipeline = true;};
  88. int RunHeaders();
  89. bool RunData();
  90. bool Open();
  91. bool Close();
  92. ServerState(URI Srv,HttpMethod *Owner);
  93. ~ServerState() {Close();};
  94. };
  95. class HttpMethod : public pkgAcqMethod
  96. {
  97. void SendReq(FetchItem *Itm,CircleBuf &Out);
  98. bool Go(bool ToFile,ServerState *Srv);
  99. bool Flush(ServerState *Srv);
  100. bool ServerDie(ServerState *Srv);
  101. int DealWithHeaders(FetchResult &Res,ServerState *Srv);
  102. virtual bool Fetch(FetchItem *);
  103. virtual bool Configuration(string Message);
  104. // In the event of a fatal signal this file will be closed and timestamped.
  105. static string FailFile;
  106. static int FailFd;
  107. static time_t FailTime;
  108. static void SigTerm(int);
  109. public:
  110. friend class ServerState;
  111. FileFd *File;
  112. ServerState *Server;
  113. int Loop();
  114. HttpMethod() : pkgAcqMethod("1.2",Pipeline | SendConfig)
  115. {
  116. File = 0;
  117. Server = 0;
  118. };
  119. };
  120. URI Proxy;
  121. #endif