]>
Commit | Line | Data |
---|---|---|
7330f4df DK |
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> | |
453b82a3 | 15 | #include <apt-pkg/acquire-method.h> |
7330f4df | 16 | |
453b82a3 DK |
17 | #include <time.h> |
18 | #include <iostream> | |
7330f4df | 19 | #include <string> |
830a1b8c | 20 | #include <memory> |
7330f4df DK |
21 | |
22 | using std::cout; | |
23 | using std::endl; | |
24 | ||
25 | class Hashes; | |
26 | class ServerMethod; | |
27 | class FileFd; | |
28 | ||
29 | struct ServerState | |
30 | { | |
31 | // This is the last parsed Header Line | |
32 | unsigned int Major; | |
33 | unsigned int Minor; | |
34 | unsigned int Result; | |
35 | char Code[360]; | |
36 | ||
37 | // These are some statistics from the last parsed header lines | |
6291f60e MV |
38 | |
39 | // total size of the usable content (aka: the file) | |
40 | unsigned long long TotalFileSize; | |
41 | // size we actually download (can be smaller than Size if we have partial content) | |
42 | unsigned long long DownloadSize; | |
43 | // size of junk content (aka: server error pages) | |
44 | unsigned long long JunkSize; | |
45 | // The start of the data (for partial content) | |
3de8f956 | 46 | unsigned long long StartPos; |
6291f60e | 47 | |
7330f4df DK |
48 | time_t Date; |
49 | bool HaveContent; | |
50 | enum {Chunked,Stream,Closes} Encoding; | |
51 | enum {Header, Data} State; | |
52 | bool Persistent; | |
53 | std::string Location; | |
54 | ||
55 | // This is a Persistent attribute of the server itself. | |
56 | bool Pipeline; | |
57 | URI ServerName; | |
58 | URI Proxy; | |
59 | unsigned long TimeOut; | |
60 | ||
c48eea97 | 61 | unsigned long long MaximumSize; |
dcd5856b | 62 | |
7330f4df DK |
63 | protected: |
64 | ServerMethod *Owner; | |
65 | ||
7330f4df DK |
66 | virtual bool ReadHeaderLines(std::string &Data) = 0; |
67 | virtual bool LoadNextResponse(bool const ToFile, FileFd * const File) = 0; | |
68 | ||
69 | public: | |
fd46d305 DK |
70 | bool HeaderLine(std::string Line); |
71 | ||
7330f4df DK |
72 | /** \brief Result of the header acquire */ |
73 | enum RunHeadersResult { | |
74 | /** \brief Header ok */ | |
75 | RUN_HEADERS_OK, | |
76 | /** \brief IO error while retrieving */ | |
77 | RUN_HEADERS_IO_ERROR, | |
78 | /** \brief Parse error after retrieving */ | |
d3e8fbb3 | 79 | RUN_HEADERS_PARSE_ERROR |
7330f4df DK |
80 | }; |
81 | /** \brief Get the headers before the data */ | |
9622b211 | 82 | RunHeadersResult RunHeaders(FileFd * const File, const std::string &Uri); |
34faa8f7 | 83 | bool AddPartialFileToHashes(FileFd &File); |
7330f4df DK |
84 | |
85 | bool Comp(URI Other) const {return Other.Host == ServerName.Host && Other.Port == ServerName.Port;}; | |
6291f60e | 86 | virtual void Reset() {Major = 0; Minor = 0; Result = 0; Code[0] = '\0'; TotalFileSize = 0; JunkSize = 0; |
27925d82 | 87 | StartPos = 0; Encoding = Closes; time(&Date); HaveContent = false; |
c48eea97 | 88 | State = Header; Persistent = false; Pipeline = true; MaximumSize = 0;}; |
7330f4df DK |
89 | virtual bool WriteResponse(std::string const &Data) = 0; |
90 | ||
91 | /** \brief Transfer the data from the socket */ | |
92 | virtual bool RunData(FileFd * const File) = 0; | |
93 | ||
94 | virtual bool Open() = 0; | |
95 | virtual bool IsOpen() = 0; | |
96 | virtual bool Close() = 0; | |
34faa8f7 | 97 | virtual bool InitHashes(HashStringList const &ExpectedHashes) = 0; |
7330f4df DK |
98 | virtual Hashes * GetHashes() = 0; |
99 | virtual bool Die(FileFd &File) = 0; | |
100 | virtual bool Flush(FileFd * const File) = 0; | |
101 | virtual bool Go(bool ToFile, FileFd * const File) = 0; | |
102 | ||
103 | ServerState(URI Srv, ServerMethod *Owner); | |
104 | virtual ~ServerState() {}; | |
105 | }; | |
106 | ||
107 | class ServerMethod : public pkgAcqMethod | |
108 | { | |
109 | protected: | |
3b302846 | 110 | virtual bool Fetch(FetchItem *) APT_OVERRIDE; |
7330f4df | 111 | |
830a1b8c | 112 | std::unique_ptr<ServerState> Server; |
7330f4df DK |
113 | std::string NextURI; |
114 | FileFd *File; | |
115 | ||
116 | unsigned long PipelineDepth; | |
117 | bool AllowRedirect; | |
118 | ||
f2b47ba2 MV |
119 | // Find the biggest item in the fetch queue for the checking of the maximum |
120 | // size | |
121 | unsigned long long FindMaximumObjectSizeInQueue() const APT_PURE; | |
122 | ||
7330f4df DK |
123 | public: |
124 | bool Debug; | |
125 | ||
126 | /** \brief Result of the header parsing */ | |
127 | enum DealWithHeadersResult { | |
128 | /** \brief The file is open and ready */ | |
129 | FILE_IS_OPEN, | |
130 | /** \brief We got a IMS hit, the file has not changed */ | |
131 | IMS_HIT, | |
132 | /** \brief The server reported a unrecoverable error */ | |
133 | ERROR_UNRECOVERABLE, | |
134 | /** \brief The server reported a error with a error content page */ | |
135 | ERROR_WITH_CONTENT_PAGE, | |
136 | /** \brief An error on the client side */ | |
137 | ERROR_NOT_FROM_SERVER, | |
138 | /** \brief A redirect or retry request */ | |
139 | TRY_AGAIN_OR_REDIRECT | |
140 | }; | |
141 | /** \brief Handle the retrieved header data */ | |
142 | DealWithHeadersResult DealWithHeaders(FetchResult &Res); | |
143 | ||
144 | // In the event of a fatal signal this file will be closed and timestamped. | |
145 | static std::string FailFile; | |
146 | static int FailFd; | |
147 | static time_t FailTime; | |
a02db58f | 148 | static APT_NORETURN void SigTerm(int); |
7330f4df | 149 | |
3b302846 | 150 | virtual bool Configuration(std::string Message) APT_OVERRIDE; |
7330f4df DK |
151 | virtual bool Flush() { return Server->Flush(File); }; |
152 | ||
153 | int Loop(); | |
154 | ||
155 | virtual void SendReq(FetchItem *Itm) = 0; | |
830a1b8c | 156 | virtual std::unique_ptr<ServerState> CreateServerState(URI const &uri) = 0; |
fd46d305 | 157 | virtual void RotateDNS() = 0; |
7330f4df | 158 | |
830a1b8c | 159 | ServerMethod(const char *Ver,unsigned long Flags = 0); |
7330f4df DK |
160 | virtual ~ServerMethod() {}; |
161 | }; | |
162 | ||
163 | #endif |