]>
Commit | Line | Data |
---|---|---|
be4401bf | 1 | // -*- mode: cpp; mode: fold -*- |
a305f593 AL |
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 $ | |
be4401bf AL |
4 | /* ###################################################################### |
5 | ||
ae58a985 | 6 | HTTP Acquire Method - This is the HTTP aquire method for APT. |
be4401bf AL |
7 | |
8 | ##################################################################### */ | |
9 | /*}}}*/ | |
10 | ||
11 | #ifndef APT_HTTP_H | |
12 | #define APT_HTTP_H | |
13 | ||
14 | #define MAXLEN 360 | |
15 | ||
472ff00e DK |
16 | #include <apt-pkg/strutl.h> |
17 | ||
18 | #include <string> | |
42195eb2 AL |
19 | |
20 | using std::cout; | |
21 | using std::endl; | |
22 | ||
be4401bf | 23 | class HttpMethod; |
472ff00e | 24 | class Hashes; |
be4401bf AL |
25 | |
26 | class CircleBuf | |
27 | { | |
28 | unsigned char *Buf; | |
650faab0 DK |
29 | unsigned long long Size; |
30 | unsigned long long InP; | |
31 | unsigned long long OutP; | |
8f3ba4e8 | 32 | std::string OutQueue; |
650faab0 DK |
33 | unsigned long long StrPos; |
34 | unsigned long long MaxGet; | |
be4401bf AL |
35 | struct timeval Start; |
36 | ||
650faab0 DK |
37 | static unsigned long long BwReadLimit; |
38 | static unsigned long long BwTickReadData; | |
7c6e2dc7 MV |
39 | static struct timeval BwReadTick; |
40 | static const unsigned int BW_HZ; | |
41 | ||
74b22002 | 42 | unsigned long long LeftRead() const |
be4401bf | 43 | { |
650faab0 | 44 | unsigned long long Sz = Size - (InP - OutP); |
be4401bf AL |
45 | if (Sz > Size - (InP%Size)) |
46 | Sz = Size - (InP%Size); | |
47 | return Sz; | |
48 | } | |
74b22002 | 49 | unsigned long long LeftWrite() const |
be4401bf | 50 | { |
650faab0 | 51 | unsigned long long Sz = InP - OutP; |
be4401bf AL |
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 | ||
63b1700f | 62 | Hashes *Hash; |
be4401bf AL |
63 | |
64 | // Read data in | |
65 | bool Read(int Fd); | |
8f3ba4e8 | 66 | bool Read(std::string Data); |
be4401bf AL |
67 | |
68 | // Write data out | |
69 | bool Write(int Fd); | |
8f3ba4e8 | 70 | bool WriteTillEl(std::string &Data,bool Single = false); |
be4401bf AL |
71 | |
72 | // Control the write limit | |
650faab0 | 73 | void Limit(long long Max) {if (Max == -1) MaxGet = 0-1; else MaxGet = OutP + Max;} |
f5a34606 DK |
74 | bool IsLimit() const {return MaxGet == OutP;}; |
75 | void Print() const {cout << MaxGet << ',' << OutP << endl;}; | |
be4401bf AL |
76 | |
77 | // Test for free space in the buffer | |
f5a34606 DK |
78 | bool ReadSpace() const {return Size - (InP - OutP) > 0;}; |
79 | bool WriteSpace() const {return InP - OutP > 0;}; | |
be4401bf AL |
80 | |
81 | // Dump everything | |
82 | void Reset(); | |
83 | void Stats(); | |
84 | ||
650faab0 | 85 | CircleBuf(unsigned long long Size); |
472ff00e | 86 | ~CircleBuf(); |
be4401bf AL |
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 | ||
92e889c8 | 97 | // These are some statistics from the last parsed header lines |
650faab0 DK |
98 | unsigned long long Size; |
99 | signed long long StartPos; | |
be4401bf | 100 | time_t Date; |
92e889c8 | 101 | bool HaveContent; |
be4401bf AL |
102 | enum {Chunked,Stream,Closes} Encoding; |
103 | enum {Header, Data} State; | |
e836f356 | 104 | bool Persistent; |
8f3ba4e8 | 105 | std::string Location; |
e836f356 AL |
106 | |
107 | // This is a Persistent attribute of the server itself. | |
f93d1355 | 108 | bool Pipeline; |
be4401bf AL |
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 | ||
8f3ba4e8 | 118 | bool HeaderLine(std::string Line); |
f5a34606 | 119 | bool Comp(URI Other) const {return Other.Host == ServerName.Host && Other.Port == ServerName.Port;}; |
be4401bf | 120 | void Reset() {Major = 0; Minor = 0; Result = 0; Size = 0; StartPos = 0; |
f93d1355 AL |
121 | Encoding = Closes; time(&Date); ServerFd = -1; |
122 | Pipeline = true;}; | |
7273e494 MV |
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 */ | |
be4401bf AL |
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); | |
7273e494 MV |
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 */ | |
bd49b02c | 171 | bool AutoDetectProxy(); |
5cb5d8dc | 172 | |
8f3ba4e8 | 173 | virtual bool Configuration(std::string Message); |
be4401bf | 174 | |
492f957a | 175 | // In the event of a fatal signal this file will be closed and timestamped. |
8f3ba4e8 | 176 | static std::string FailFile; |
492f957a AL |
177 | static int FailFd; |
178 | static time_t FailTime; | |
179 | static void SigTerm(int); | |
5f6b130d MV |
180 | |
181 | protected: | |
182 | virtual bool Fetch(FetchItem *); | |
492f957a | 183 | |
8f3ba4e8 DK |
184 | std::string NextURI; |
185 | std::string AutoDetectProxyCmd; | |
7273e494 | 186 | |
be4401bf | 187 | public: |
69718ced | 188 | friend struct ServerState; |
be4401bf | 189 | |
be4401bf | 190 | FileFd *File; |
5cb5d8dc | 191 | ServerState *Server; |
be4401bf AL |
192 | |
193 | int Loop(); | |
194 | ||
b98f2859 | 195 | HttpMethod() : pkgAcqMethod("1.2",Pipeline | SendConfig) |
be4401bf | 196 | { |
be4401bf | 197 | File = 0; |
5cb5d8dc | 198 | Server = 0; |
be4401bf AL |
199 | }; |
200 | }; | |
201 | ||
be4401bf | 202 | #endif |