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