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 ##################################################################### */
14 #include <apt-pkg/strutl.h>
27 unsigned long long Size
;
28 unsigned long long InP
;
29 unsigned long long OutP
;
31 unsigned long long StrPos
;
32 unsigned long long MaxGet
;
35 static unsigned long long BwReadLimit
;
36 static unsigned long long BwTickReadData
;
37 static struct timeval BwReadTick
;
38 static const unsigned int BW_HZ
;
40 unsigned long long LeftRead() const
42 unsigned long long Sz
= Size
- (InP
- OutP
);
43 if (Sz
> Size
- (InP%Size
))
44 Sz
= Size
- (InP%Size
);
47 unsigned long long LeftWrite() const
49 unsigned long long Sz
= InP
- OutP
;
52 if (Sz
> Size
- (OutP%Size
))
53 Sz
= Size
- (OutP%Size
);
64 bool Read(std::string Data
);
68 bool WriteTillEl(std::string
&Data
,bool Single
= false);
70 // Control the write limit
71 void Limit(long long Max
) {if (Max
== -1) MaxGet
= 0-1; else MaxGet
= OutP
+ Max
;}
72 bool IsLimit() const {return MaxGet
== OutP
;};
73 void Print() const {cout
<< MaxGet
<< ',' << OutP
<< endl
;};
75 // Test for free space in the buffer
76 bool ReadSpace() const {return Size
- (InP
- OutP
) > 0;};
77 bool WriteSpace() const {return InP
- OutP
> 0;};
83 CircleBuf(unsigned long long Size
);
89 // This is the last parsed Header Line
95 // These are some statistics from the last parsed header lines
96 unsigned long long Size
;
97 signed long long StartPos
;
100 enum {Chunked
,Stream
,Closes
} Encoding
;
101 enum {Header
, Data
} State
;
103 std::string Location
;
105 // This is a Persistent attribute of the server itself.
110 // This is the connection itself. Output is data FROM the server
116 bool HeaderLine(std::string Line
);
117 bool Comp(URI Other
) const {return Other
.Host
== ServerName
.Host
&& Other
.Port
== ServerName
.Port
;};
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;
123 /** \brief Result of the header acquire */
124 enum RunHeadersResult
{
125 /** \brief Header ok */
127 /** \brief IO error while retrieving */
128 RUN_HEADERS_IO_ERROR
,
129 /** \brief Parse error after retrieving */
130 RUN_HEADERS_PARSE_ERROR
,
132 /** \brief Get the headers before the data */
133 RunHeadersResult
RunHeaders();
134 /** \brief Transfer the data from the socket */
140 ServerState(URI Srv
,HttpMethod
*Owner
);
141 ~ServerState() {Close();};
144 class HttpMethod
: public pkgAcqMethod
146 void SendReq(FetchItem
*Itm
,CircleBuf
&Out
);
147 bool Go(bool ToFile
,ServerState
*Srv
);
148 bool Flush(ServerState
*Srv
);
149 bool ServerDie(ServerState
*Srv
);
151 /** \brief Result of the header parsing */
152 enum DealWithHeadersResult
{
153 /** \brief The file is open and ready */
155 /** \brief We got a IMS hit, the file has not changed */
157 /** \brief The server reported a unrecoverable error */
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
166 /** \brief Handle the retrieved header data */
167 DealWithHeadersResult
DealWithHeaders(FetchResult
&Res
,ServerState
*Srv
);
169 /** \brief Try to AutoDetect the proxy */
170 bool AutoDetectProxy();
172 virtual bool Configuration(std::string Message
);
174 // In the event of a fatal signal this file will be closed and timestamped.
175 static std::string FailFile
;
177 static time_t FailTime
;
178 static void SigTerm(int);
181 virtual bool Fetch(FetchItem
*);
184 std::string AutoDetectProxyCmd
;
187 friend struct ServerState
;
194 HttpMethod() : pkgAcqMethod("1.2",Pipeline
| SendConfig
)