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 /* ######################################################################
6 HTTP Acquire Method - This is the HTTP aquire method for APT.
8 ##################################################################### */
16 #include <apt-pkg/strutl.h>
29 unsigned long long Size
;
30 unsigned long long InP
;
31 unsigned long long OutP
;
33 unsigned long long StrPos
;
34 unsigned long long MaxGet
;
37 static unsigned long long BwReadLimit
;
38 static unsigned long long BwTickReadData
;
39 static struct timeval BwReadTick
;
40 static const unsigned int BW_HZ
;
42 unsigned long long LeftRead() const
44 unsigned long long Sz
= Size
- (InP
- OutP
);
45 if (Sz
> Size
- (InP%Size
))
46 Sz
= Size
- (InP%Size
);
49 unsigned long long LeftWrite() const
51 unsigned long long Sz
= InP
- OutP
;
54 if (Sz
> Size
- (OutP%Size
))
55 Sz
= Size
- (OutP%Size
);
66 bool Read(std::string Data
);
70 bool WriteTillEl(std::string
&Data
,bool Single
= false);
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
;};
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;};
85 CircleBuf(unsigned long long Size
);
91 // This is the last parsed Header Line
97 // These are some statistics from the last parsed header lines
98 unsigned long long Size
;
99 signed long long StartPos
;
102 enum {Chunked
,Stream
,Closes
} Encoding
;
103 enum {Header
, Data
} State
;
105 std::string Location
;
107 // This is a Persistent attribute of the server itself.
112 // This is the connection itself. Output is data FROM the server
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;
125 /** \brief Result of the header acquire */
126 enum RunHeadersResult
{
127 /** \brief Header ok */
129 /** \brief IO error while retrieving */
130 RUN_HEADERS_IO_ERROR
,
131 /** \brief Parse error after retrieving */
132 RUN_HEADERS_PARSE_ERROR
,
134 /** \brief Get the headers before the data */
135 RunHeadersResult
RunHeaders();
136 /** \brief Transfer the data from the socket */
142 ServerState(URI Srv
,HttpMethod
*Owner
);
143 ~ServerState() {Close();};
146 class HttpMethod
: public pkgAcqMethod
148 void SendReq(FetchItem
*Itm
,CircleBuf
&Out
);
149 bool Go(bool ToFile
,ServerState
*Srv
);
150 bool Flush(ServerState
*Srv
);
151 bool ServerDie(ServerState
*Srv
);
153 /** \brief Result of the header parsing */
154 enum DealWithHeadersResult
{
155 /** \brief The file is open and ready */
157 /** \brief We got a IMS hit, the file has not changed */
159 /** \brief The server reported a unrecoverable error */
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
168 /** \brief Handle the retrieved header data */
169 DealWithHeadersResult
DealWithHeaders(FetchResult
&Res
,ServerState
*Srv
);
171 /** \brief Try to AutoDetect the proxy */
172 bool AutoDetectProxy();
174 virtual bool Configuration(std::string Message
);
176 // In the event of a fatal signal this file will be closed and timestamped.
177 static std::string FailFile
;
179 static time_t FailTime
;
180 static void SigTerm(int);
183 virtual bool Fetch(FetchItem
*);
186 std::string AutoDetectProxyCmd
;
189 friend struct ServerState
;
196 HttpMethod() : pkgAcqMethod("1.2",Pipeline
| SendConfig
)