http.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/// $Id: http.h,v 1.7 1999/12/09 03:45:56 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 Pipeline;
  74. HttpMethod *Owner;
  75. // This is the connection itself. Output is data FROM the server
  76. CircleBuf In;
  77. CircleBuf Out;
  78. int ServerFd;
  79. URI ServerName;
  80. bool HeaderLine(string Line);
  81. bool Comp(URI Other) {return Other.Host == ServerName.Host && Other.Port == ServerName.Port;};
  82. void Reset() {Major = 0; Minor = 0; Result = 0; Size = 0; StartPos = 0;
  83. Encoding = Closes; time(&Date); ServerFd = -1;
  84. Pipeline = true;};
  85. int RunHeaders();
  86. bool RunData();
  87. bool Open();
  88. bool Close();
  89. ServerState(URI Srv,HttpMethod *Owner);
  90. ~ServerState() {Close();};
  91. };
  92. class HttpMethod : public pkgAcqMethod
  93. {
  94. void SendReq(FetchItem *Itm,CircleBuf &Out);
  95. bool Go(bool ToFile,ServerState *Srv);
  96. bool Flush(ServerState *Srv);
  97. bool ServerDie(ServerState *Srv);
  98. int DealWithHeaders(FetchResult &Res,ServerState *Srv);
  99. virtual bool Fetch(FetchItem *);
  100. virtual bool Configuration(string Message);
  101. // In the event of a fatal signal this file will be closed and timestamped.
  102. static string FailFile;
  103. static int FailFd;
  104. static time_t FailTime;
  105. static void SigTerm(int);
  106. public:
  107. friend ServerState;
  108. FileFd *File;
  109. ServerState *Server;
  110. int Loop();
  111. HttpMethod() : pkgAcqMethod("1.2",Pipeline | SendConfig)
  112. {
  113. File = 0;
  114. Server = 0;
  115. };
  116. };
  117. URI Proxy;
  118. #endif