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