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