apt  2.2.4
About: Apt (Advanced Package Tool) is a management system for software packages (Debian/Ubuntu). Release series 2.2.
  Fossies Dox: apt-2.2.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

basehttp.h
Go to the documentation of this file.
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4 
5  Classes dealing with the abstraction of talking to a end via a text
6  protocol like HTTP (which is used by the http and https methods)
7 
8  ##################################################################### */
9  /*}}}*/
10 
11 #ifndef APT_SERVER_H
12 #define APT_SERVER_H
13 
14 #include "aptmethod.h"
15 #include <apt-pkg/fileutl.h>
16 #include <apt-pkg/strutl.h>
17 
18 #include <iostream>
19 #include <memory>
20 #include <string>
21 #include <time.h>
22 
23 using std::cout;
24 using std::endl;
25 
26 class Hashes;
27 class BaseHttpMethod;
28 struct ServerState;
29 
31 {
32  unsigned int Major = 0;
33  unsigned int Minor = 0;
34  unsigned int Result = 0;
35  char Code[360];
36 
37  // total size of the usable content (aka: the file)
38  unsigned long long TotalFileSize = 0;
39  // size we actually download (can be smaller than Size if we have partial content)
40  unsigned long long DownloadSize = 0;
41  // size of junk content (aka: server error pages)
42  unsigned long long JunkSize = 0;
43  // The start of the data (for partial content)
44  unsigned long long StartPos = 0;
45 
46  unsigned long long MaximumSize = 0;
47 
48  time_t Date;
49  bool HaveContent = false;
51  enum {Header, Data} State = Header;
52  std::string Location;
53 
55 
58 
59  bool HeaderLine(std::string const &Line);
61 
63  Owner(Owner), Server(Server) { time(&Date); Code[0] = '\0'; }
64 };
66 {
67  bool Persistent;
70  unsigned long PipelineAnswersReceived;
71 
72  bool Pipeline;
75  unsigned long TimeOut;
76 
77  protected:
79 
80  virtual bool ReadHeaderLines(std::string &Data) = 0;
81  virtual ResultState LoadNextResponse(bool const ToFile, RequestState &Req) = 0;
82 
83  public:
84 
85  /** \brief Result of the header acquire */
87  /** \brief Header ok */
89  /** \brief IO error while retrieving */
91  /** \brief Parse error after retrieving */
93  };
94  /** \brief Get the headers before the data */
95  RunHeadersResult RunHeaders(RequestState &Req, const std::string &Uri);
96 
97  bool Comp(URI Other) const {return Other.Access == ServerName.Access && Other.Host == ServerName.Host && Other.Port == ServerName.Port;};
98  virtual void Reset();
99  virtual bool WriteResponse(std::string const &Data) = 0;
100 
101  /** \brief Transfer the data from the socket */
102  virtual ResultState RunData(RequestState &Req) = 0;
104 
105  virtual ResultState Open() = 0;
106  virtual bool IsOpen() = 0;
107  virtual bool Close() = 0;
108  virtual bool InitHashes(HashStringList const &ExpectedHashes) = 0;
109  virtual ResultState Die(RequestState &Req) = 0;
110  virtual bool Flush(FileFd *const File, bool MustComplete = false) = 0;
111  virtual ResultState Go(bool ToFile, RequestState &Req) = 0;
112  virtual Hashes * GetHashes() = 0;
113 
115  virtual ~ServerState() {};
116 };
117 
119 {
120  protected:
121  virtual bool Fetch(FetchItem *) APT_OVERRIDE;
122 
123  std::unique_ptr<ServerState> Server;
124  std::string NextURI;
125 
127 
128  // Find the biggest item in the fetch queue for the checking of the maximum
129  // size
130  unsigned long long FindMaximumObjectSizeInQueue() const APT_PURE;
131 
132  public:
133  bool Debug;
134  unsigned long PipelineDepth;
135 
136  /** \brief Result of the header parsing */
138  /** \brief The file is open and ready */
140  /** \brief We got a IMS hit, the file has not changed */
142  /** \brief The server reported a unrecoverable error */
144  /** \brief The server reported a error with a error content page */
146  /** \brief An error on the client side */
148  /** \brief A redirect or retry request */
150  };
151  /** \brief Handle the retrieved header data */
152  virtual DealWithHeadersResult DealWithHeaders(FetchResult &Res, RequestState &Req);
153 
154  // In the event of a fatal signal this file will be closed and timestamped.
155  static std::string FailFile;
156  static int FailFd;
157  static time_t FailTime;
158  static APT_NORETURN void SigTerm(int);
159 
160  int Loop();
161 
162  virtual void SendReq(FetchItem *Itm) = 0;
163  virtual std::unique_ptr<ServerState> CreateServerState(URI const &uri) = 0;
164  virtual void RotateDNS() = 0;
165  virtual bool Configuration(std::string Message) APT_OVERRIDE;
166 
167  bool AddProxyAuth(URI &Proxy, URI const &Server);
168 
169  BaseHttpMethod(std::string &&Binary, char const * const Ver,unsigned long const Flags);
170  virtual ~BaseHttpMethod() {};
171 };
172 
173 #endif
ResultState
Definition: aptmethod.h:35
BaseHttpMethod(std::string &&Binary, char const *const Ver, unsigned long const Flags)
Definition: basehttp.cc:893
std::string NextURI
Definition: basehttp.h:124
static time_t FailTime
Definition: basehttp.h:157
static APT_NORETURN void SigTerm(int)
Definition: basehttp.cc:479
std::unique_ptr< ServerState > Server
Definition: basehttp.h:123
virtual DealWithHeadersResult DealWithHeaders(FetchResult &Res, RequestState &Req)
Handle the retrieved header data.
Definition: basehttp.cc:298
DealWithHeadersResult
Result of the header parsing.
Definition: basehttp.h:137
@ ERROR_UNRECOVERABLE
The server reported a unrecoverable error.
Definition: basehttp.h:143
@ FILE_IS_OPEN
The file is open and ready.
Definition: basehttp.h:139
@ ERROR_WITH_CONTENT_PAGE
The server reported a error with a error content page.
Definition: basehttp.h:145
@ ERROR_NOT_FROM_SERVER
An error on the client side.
Definition: basehttp.h:147
@ IMS_HIT
We got a IMS hit, the file has not changed.
Definition: basehttp.h:141
@ TRY_AGAIN_OR_REDIRECT
A redirect or retry request.
Definition: basehttp.h:149
virtual bool Configuration(std::string Message) APT_OVERRIDE
Definition: basehttp.cc:899
virtual void RotateDNS()=0
static int FailFd
Definition: basehttp.h:156
virtual std::unique_ptr< ServerState > CreateServerState(URI const &uri)=0
virtual bool Fetch(FetchItem *) APT_OVERRIDE
Definition: basehttp.cc:498
static std::string FailFile
Definition: basehttp.h:155
bool AddProxyAuth(URI &Proxy, URI const &Server)
Definition: basehttp.cc:909
unsigned long PipelineDepth
Definition: basehttp.h:134
unsigned long long FindMaximumObjectSizeInQueue() const APT_PURE
Definition: basehttp.cc:881
virtual void SendReq(FetchItem *Itm)=0
bool AllowRedirect
Definition: basehttp.h:126
virtual ~BaseHttpMethod()
Definition: basehttp.h:170
Definition: fileutl.h:39
Definition: hashes.h:170
Definition: strutl.h:193
std::string Access
Definition: strutl.h:198
unsigned int Port
Definition: strutl.h:203
std::string Host
Definition: strutl.h:201
std::string const Binary
Definition: aptmethod.h:49
const char va_list &args const
URI Proxy
Definition: ftp.cc:66
#define APT_OVERRIDE
Definition: macros.h:111
#define APT_PURE
Definition: macros.h:56
#define APT_NORETURN
Definition: macros.h:57
enum RequestState::@6 Encoding
unsigned int Result
Definition: basehttp.h:34
unsigned long long StartPos
Definition: basehttp.h:44
char Code[360]
Definition: basehttp.h:35
unsigned long long MaximumSize
Definition: basehttp.h:46
unsigned long long TotalFileSize
Definition: basehttp.h:38
RequestState(BaseHttpMethod *const Owner, ServerState *const Server)
Definition: basehttp.h:62
FileFd File
Definition: basehttp.h:54
unsigned long long DownloadSize
Definition: basehttp.h:40
unsigned long long JunkSize
Definition: basehttp.h:42
ServerState *const Server
Definition: basehttp.h:57
BaseHttpMethod *const Owner
Definition: basehttp.h:56
bool AddPartialFileToHashes(FileFd &File)
Definition: basehttp.cc:268
enum RequestState::@7 State
time_t Date
Definition: basehttp.h:48
bool HeaderLine(std::string const &Line)
Definition: basehttp.cc:86
unsigned int Major
Definition: basehttp.h:32
std::string Location
Definition: basehttp.h:52
unsigned int Minor
Definition: basehttp.h:33
bool HaveContent
Definition: basehttp.h:49
virtual bool IsOpen()=0
virtual ResultState Die(RequestState &Req)=0
virtual bool Flush(FileFd *const File, bool MustComplete=false)=0
RunHeadersResult
Result of the header acquire.
Definition: basehttp.h:86
@ RUN_HEADERS_PARSE_ERROR
Parse error after retrieving.
Definition: basehttp.h:92
@ RUN_HEADERS_IO_ERROR
IO error while retrieving.
Definition: basehttp.h:90
@ RUN_HEADERS_OK
Header ok.
Definition: basehttp.h:88
virtual ~ServerState()
Definition: basehttp.h:115
bool Pipeline
Definition: basehttp.h:72
bool Comp(URI Other) const
Definition: basehttp.h:97
RunHeadersResult RunHeaders(RequestState &Req, const std::string &Uri)
Get the headers before the data.
Definition: basehttp.cc:50
virtual bool ReadHeaderLines(std::string &Data)=0
ServerState(URI Srv, BaseHttpMethod *Owner)
Definition: basehttp.cc:262
virtual void Reset()
Definition: basehttp.cc:274
virtual bool WriteResponse(std::string const &Data)=0
virtual ResultState RunData(RequestState &Req)=0
Transfer the data from the socket.
URI ServerName
Definition: basehttp.h:73
virtual ResultState Open()=0
BaseHttpMethod * Owner
Definition: basehttp.h:78
virtual bool Close()=0
bool PipelineAllowed
Definition: basehttp.h:68
virtual ResultState LoadNextResponse(bool const ToFile, RequestState &Req)=0
unsigned long TimeOut
Definition: basehttp.h:75
URI Proxy
Definition: basehttp.h:74
virtual bool InitHashes(HashStringList const &ExpectedHashes)=0
virtual ResultState Go(bool ToFile, RequestState &Req)=0
bool RangesAllowed
Definition: basehttp.h:69
bool Persistent
Definition: basehttp.h:67
virtual Hashes * GetHashes()=0
virtual ResultState RunDataToDevNull(RequestState &Req)=0
unsigned long PipelineAnswersReceived
Definition: basehttp.h:70