http.h 3.5 KB

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