]> git.saurik.com Git - apt.git/blob - methods/basehttp.h
rename ServerMethod to BaseHttpMethod
[apt.git] / methods / basehttp.h
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 <apt-pkg/strutl.h>
15 #include <apt-pkg/fileutl.h>
16 #include "aptmethod.h"
17
18 #include <time.h>
19 #include <iostream>
20 #include <string>
21 #include <memory>
22
23 using std::cout;
24 using std::endl;
25
26 class Hashes;
27 class BaseHttpMethod;
28 struct ServerState;
29
30 struct RequestState
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;
50 enum {Chunked,Stream,Closes} Encoding = Closes;
51 enum {Header, Data} State = Header;
52 std::string Location;
53
54 FileFd File;
55
56 BaseHttpMethod * const Owner;
57 ServerState * const Server;
58
59 bool HeaderLine(std::string const &Line);
60 bool AddPartialFileToHashes(FileFd &File);
61
62 RequestState(BaseHttpMethod * const Owner, ServerState * const Server) :
63 Owner(Owner), Server(Server) { time(&Date); }
64 };
65
66 struct ServerState
67 {
68 bool Persistent;
69 bool PipelineAllowed;
70 bool RangesAllowed;
71
72 bool Pipeline;
73 URI ServerName;
74 URI Proxy;
75 unsigned long TimeOut;
76
77 protected:
78 BaseHttpMethod *Owner;
79
80 virtual bool ReadHeaderLines(std::string &Data) = 0;
81 virtual bool LoadNextResponse(bool const ToFile, RequestState &Req) = 0;
82
83 public:
84
85 /** \brief Result of the header acquire */
86 enum RunHeadersResult {
87 /** \brief Header ok */
88 RUN_HEADERS_OK,
89 /** \brief IO error while retrieving */
90 RUN_HEADERS_IO_ERROR,
91 /** \brief Parse error after retrieving */
92 RUN_HEADERS_PARSE_ERROR
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.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 bool RunData(RequestState &Req) = 0;
103 virtual bool RunDataToDevNull(RequestState &Req) = 0;
104
105 virtual bool Open() = 0;
106 virtual bool IsOpen() = 0;
107 virtual bool Close() = 0;
108 virtual bool InitHashes(HashStringList const &ExpectedHashes) = 0;
109 virtual bool Die(RequestState &Req) = 0;
110 virtual bool Flush(FileFd * const File) = 0;
111 virtual bool Go(bool ToFile, RequestState &Req) = 0;
112 virtual Hashes * GetHashes() = 0;
113
114 ServerState(URI Srv, BaseHttpMethod *Owner);
115 virtual ~ServerState() {};
116 };
117
118 class BaseHttpMethod : public aptMethod
119 {
120 protected:
121 virtual bool Fetch(FetchItem *) APT_OVERRIDE;
122
123 std::unique_ptr<ServerState> Server;
124 std::string NextURI;
125
126 unsigned long PipelineDepth;
127 bool AllowRedirect;
128
129 // Find the biggest item in the fetch queue for the checking of the maximum
130 // size
131 unsigned long long FindMaximumObjectSizeInQueue() const APT_PURE;
132
133 public:
134 bool Debug;
135
136 /** \brief Result of the header parsing */
137 enum DealWithHeadersResult {
138 /** \brief The file is open and ready */
139 FILE_IS_OPEN,
140 /** \brief We got a IMS hit, the file has not changed */
141 IMS_HIT,
142 /** \brief The server reported a unrecoverable error */
143 ERROR_UNRECOVERABLE,
144 /** \brief The server reported a error with a error content page */
145 ERROR_WITH_CONTENT_PAGE,
146 /** \brief An error on the client side */
147 ERROR_NOT_FROM_SERVER,
148 /** \brief A redirect or retry request */
149 TRY_AGAIN_OR_REDIRECT
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) const;
168
169 BaseHttpMethod(std::string &&Binary, char const * const Ver,unsigned long const Flags);
170 virtual ~BaseHttpMethod() {};
171 };
172
173 #endif