http.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: http.h,v 1.1 1998/11/01 05:30:47 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 get line
  68. unsigned long Size;
  69. signed long StartPos;
  70. time_t Date;
  71. enum {Chunked,Stream,Closes} Encoding;
  72. enum {Header, Data} State;
  73. HttpMethod *Owner;
  74. // This is the connection itself. Output is data FROM the server
  75. CircleBuf In;
  76. CircleBuf Out;
  77. int ServerFd;
  78. URI ServerName;
  79. bool HeaderLine(string Line);
  80. bool Comp(URI Other) {return Other.Host == ServerName.Host && Other.Port == ServerName.Port;};
  81. void Reset() {Major = 0; Minor = 0; Result = 0; Size = 0; StartPos = 0;
  82. Encoding = Closes; time(&Date); ServerFd = -1;};
  83. bool RunHeaders();
  84. bool RunData();
  85. bool Open();
  86. bool Close();
  87. ServerState(URI Srv,HttpMethod *Owner);
  88. ~ServerState() {Close();};
  89. };
  90. class HttpMethod : public pkgAcqMethod
  91. {
  92. void SendReq(FetchItem *Itm,CircleBuf &Out);
  93. bool Go(bool ToFile,ServerState *Srv);
  94. bool Flush(ServerState *Srv);
  95. bool ServerDie(ServerState *Srv);
  96. int DealWithHeaders(FetchResult &Res,ServerState *Srv);
  97. public:
  98. friend ServerState;
  99. ServerState *Srv;
  100. int Depth;
  101. FileFd *File;
  102. int Loop();
  103. HttpMethod() : pkgAcqMethod("1.2",SingleInstance | Pipeline | SendConfig)
  104. {
  105. Depth = 0;
  106. Srv = 0;
  107. File = 0;
  108. Depth = 0;
  109. };
  110. };
  111. URI Proxy;
  112. #endif