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