http.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: http.h,v 1.4 1998/11/11 06:54:22 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. MD5Summation *MD5;
  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. 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. int RunHeaders();
  85. bool RunData();
  86. bool Open();
  87. bool Close();
  88. ServerState(URI Srv,HttpMethod *Owner);
  89. ~ServerState() {Close();};
  90. };
  91. class HttpMethod : public pkgAcqMethod
  92. {
  93. void SendReq(FetchItem *Itm,CircleBuf &Out);
  94. bool Go(bool ToFile,ServerState *Srv);
  95. bool Flush(ServerState *Srv);
  96. bool ServerDie(ServerState *Srv);
  97. int DealWithHeaders(FetchResult &Res,ServerState *Srv);
  98. // In the event of a fatal signal this file will be closed and timestamped.
  99. static string FailFile;
  100. static int FailFd;
  101. static time_t FailTime;
  102. static void SigTerm(int);
  103. public:
  104. friend ServerState;
  105. int Depth;
  106. FileFd *File;
  107. int Loop();
  108. HttpMethod() : pkgAcqMethod("1.2",Pipeline | SendConfig)
  109. {
  110. Depth = 0;
  111. File = 0;
  112. Depth = 0;
  113. };
  114. };
  115. URI Proxy;
  116. #endif