]> git.saurik.com Git - apt.git/blob - methods/http.h
apt-pkg/packagemanager.cc: make the MaxLoopCount really big to avoid potential regres...
[apt.git] / methods / http.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/// $Id: http.h,v 1.12 2002/04/18 05:09:38 jgg Exp $
3 // $Id: http.h,v 1.12 2002/04/18 05:09:38 jgg Exp $
4 /* ######################################################################
5
6 HTTP Acquire Method - This is the HTTP aquire method for APT.
7
8 ##################################################################### */
9 /*}}}*/
10
11 #ifndef APT_HTTP_H
12 #define APT_HTTP_H
13
14 #define MAXLEN 360
15
16 #include <apt-pkg/strutl.h>
17
18 #include <string>
19
20 using std::cout;
21 using std::endl;
22
23 class HttpMethod;
24 class Hashes;
25
26 class CircleBuf
27 {
28 unsigned char *Buf;
29 unsigned long long Size;
30 unsigned long long InP;
31 unsigned long long OutP;
32 std::string OutQueue;
33 unsigned long long StrPos;
34 unsigned long long MaxGet;
35 struct timeval Start;
36
37 static unsigned long long BwReadLimit;
38 static unsigned long long BwTickReadData;
39 static struct timeval BwReadTick;
40 static const unsigned int BW_HZ;
41
42 unsigned long long LeftRead() const
43 {
44 unsigned long long Sz = Size - (InP - OutP);
45 if (Sz > Size - (InP%Size))
46 Sz = Size - (InP%Size);
47 return Sz;
48 }
49 unsigned long long LeftWrite() const
50 {
51 unsigned long long Sz = InP - OutP;
52 if (InP > MaxGet)
53 Sz = MaxGet - OutP;
54 if (Sz > Size - (OutP%Size))
55 Sz = Size - (OutP%Size);
56 return Sz;
57 }
58 void FillOut();
59
60 public:
61
62 Hashes *Hash;
63
64 // Read data in
65 bool Read(int Fd);
66 bool Read(std::string Data);
67
68 // Write data out
69 bool Write(int Fd);
70 bool WriteTillEl(std::string &Data,bool Single = false);
71
72 // Control the write limit
73 void Limit(long long Max) {if (Max == -1) MaxGet = 0-1; else MaxGet = OutP + Max;}
74 bool IsLimit() const {return MaxGet == OutP;};
75 void Print() const {cout << MaxGet << ',' << OutP << endl;};
76
77 // Test for free space in the buffer
78 bool ReadSpace() const {return Size - (InP - OutP) > 0;};
79 bool WriteSpace() const {return InP - OutP > 0;};
80
81 // Dump everything
82 void Reset();
83 void Stats();
84
85 CircleBuf(unsigned long long Size);
86 ~CircleBuf();
87 };
88
89 struct ServerState
90 {
91 // This is the last parsed Header Line
92 unsigned int Major;
93 unsigned int Minor;
94 unsigned int Result;
95 char Code[MAXLEN];
96
97 // These are some statistics from the last parsed header lines
98 unsigned long long Size;
99 signed long long StartPos;
100 time_t Date;
101 bool HaveContent;
102 enum {Chunked,Stream,Closes} Encoding;
103 enum {Header, Data} State;
104 bool Persistent;
105 std::string Location;
106
107 // This is a Persistent attribute of the server itself.
108 bool Pipeline;
109
110 HttpMethod *Owner;
111
112 // This is the connection itself. Output is data FROM the server
113 CircleBuf In;
114 CircleBuf Out;
115 int ServerFd;
116 URI ServerName;
117
118 bool HeaderLine(std::string Line);
119 bool Comp(URI Other) const {return Other.Host == ServerName.Host && Other.Port == ServerName.Port;};
120 void Reset() {Major = 0; Minor = 0; Result = 0; Code[0] = '\0'; Size = 0;
121 StartPos = 0; Encoding = Closes; time(&Date); HaveContent = false;
122 State = Header; Persistent = false; ServerFd = -1;
123 Pipeline = true;};
124
125 /** \brief Result of the header acquire */
126 enum RunHeadersResult {
127 /** \brief Header ok */
128 RUN_HEADERS_OK,
129 /** \brief IO error while retrieving */
130 RUN_HEADERS_IO_ERROR,
131 /** \brief Parse error after retrieving */
132 RUN_HEADERS_PARSE_ERROR,
133 };
134 /** \brief Get the headers before the data */
135 RunHeadersResult RunHeaders();
136 /** \brief Transfer the data from the socket */
137 bool RunData();
138
139 bool Open();
140 bool Close();
141
142 ServerState(URI Srv,HttpMethod *Owner);
143 ~ServerState() {Close();};
144 };
145
146 class HttpMethod : public pkgAcqMethod
147 {
148 void SendReq(FetchItem *Itm,CircleBuf &Out);
149 bool Go(bool ToFile,ServerState *Srv);
150 bool Flush(ServerState *Srv);
151 bool ServerDie(ServerState *Srv);
152
153 /** \brief Result of the header parsing */
154 enum DealWithHeadersResult {
155 /** \brief The file is open and ready */
156 FILE_IS_OPEN,
157 /** \brief We got a IMS hit, the file has not changed */
158 IMS_HIT,
159 /** \brief The server reported a unrecoverable error */
160 ERROR_UNRECOVERABLE,
161 /** \brief The server reported a error with a error content page */
162 ERROR_WITH_CONTENT_PAGE,
163 /** \brief A error on the client side */
164 ERROR_NOT_FROM_SERVER,
165 /** \brief A redirect or retry request */
166 TRY_AGAIN_OR_REDIRECT
167 };
168 /** \brief Handle the retrieved header data */
169 DealWithHeadersResult DealWithHeaders(FetchResult &Res,ServerState *Srv);
170
171 /** \brief Try to AutoDetect the proxy */
172 bool AutoDetectProxy();
173
174 virtual bool Configuration(std::string Message);
175
176 // In the event of a fatal signal this file will be closed and timestamped.
177 static std::string FailFile;
178 static int FailFd;
179 static time_t FailTime;
180 static void SigTerm(int);
181
182 protected:
183 virtual bool Fetch(FetchItem *);
184
185 std::string NextURI;
186 std::string AutoDetectProxyCmd;
187
188 public:
189 friend struct ServerState;
190
191 FileFd *File;
192 ServerState *Server;
193
194 int Loop();
195
196 HttpMethod() : pkgAcqMethod("1.2",Pipeline | SendConfig)
197 {
198 File = 0;
199 Server = 0;
200 };
201 };
202
203 #endif