]> git.saurik.com Git - apt.git/blob - methods/http.h
Allow the FileFd to use an external Compressor to uncompress a given file
[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; Size = 0; StartPos = 0;
121 Encoding = Closes; time(&Date); ServerFd = -1;
122 Pipeline = true;};
123
124 /** \brief Result of the header acquire */
125 enum RunHeadersResult {
126 /** \brief Header ok */
127 RUN_HEADERS_OK,
128 /** \brief IO error while retrieving */
129 RUN_HEADERS_IO_ERROR,
130 /** \brief Parse error after retrieving */
131 RUN_HEADERS_PARSE_ERROR,
132 };
133 /** \brief Get the headers before the data */
134 RunHeadersResult RunHeaders();
135 /** \brief Transfer the data from the socket */
136 bool RunData();
137
138 bool Open();
139 bool Close();
140
141 ServerState(URI Srv,HttpMethod *Owner);
142 ~ServerState() {Close();};
143 };
144
145 class HttpMethod : public pkgAcqMethod
146 {
147 void SendReq(FetchItem *Itm,CircleBuf &Out);
148 bool Go(bool ToFile,ServerState *Srv);
149 bool Flush(ServerState *Srv);
150 bool ServerDie(ServerState *Srv);
151
152 /** \brief Result of the header parsing */
153 enum DealWithHeadersResult {
154 /** \brief The file is open and ready */
155 FILE_IS_OPEN,
156 /** \brief We got a IMS hit, the file has not changed */
157 IMS_HIT,
158 /** \brief The server reported a unrecoverable error */
159 ERROR_UNRECOVERABLE,
160 /** \brief The server reported a error with a error content page */
161 ERROR_WITH_CONTENT_PAGE,
162 /** \brief A error on the client side */
163 ERROR_NOT_FROM_SERVER,
164 /** \brief A redirect or retry request */
165 TRY_AGAIN_OR_REDIRECT
166 };
167 /** \brief Handle the retrieved header data */
168 DealWithHeadersResult DealWithHeaders(FetchResult &Res,ServerState *Srv);
169
170 /** \brief Try to AutoDetect the proxy */
171 bool AutoDetectProxy();
172
173 virtual bool Configuration(std::string Message);
174
175 // In the event of a fatal signal this file will be closed and timestamped.
176 static std::string FailFile;
177 static int FailFd;
178 static time_t FailTime;
179 static void SigTerm(int);
180
181 protected:
182 virtual bool Fetch(FetchItem *);
183
184 std::string NextURI;
185 std::string AutoDetectProxyCmd;
186
187 public:
188 friend struct ServerState;
189
190 FileFd *File;
191 ServerState *Server;
192
193 int Loop();
194
195 HttpMethod() : pkgAcqMethod("1.2",Pipeline | SendConfig)
196 {
197 File = 0;
198 Server = 0;
199 };
200 };
201
202 #endif