http.h 3.6 KB

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