]> git.saurik.com Git - apt.git/blob - methods/server.h
ed3cb456aae4898f780733c1b4552302cfe56651
[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 unsigned long long Size; // total size of the usable content (aka: the file)
38 unsigned long long DownloadSize; // size we actually download (can be smaller than Size if we have partial content)
39 unsigned long long JunkSize; // size of junk content (aka: server error pages)
40 unsigned long long StartPos;
41 time_t Date;
42 bool HaveContent;
43 enum {Chunked,Stream,Closes} Encoding;
44 enum {Header, Data} State;
45 bool Persistent;
46 std::string Location;
47
48 // This is a Persistent attribute of the server itself.
49 bool Pipeline;
50 URI ServerName;
51 URI Proxy;
52 unsigned long TimeOut;
53
54 protected:
55 ServerMethod *Owner;
56
57 virtual bool ReadHeaderLines(std::string &Data) = 0;
58 virtual bool LoadNextResponse(bool const ToFile, FileFd * const File) = 0;
59
60 public:
61 bool HeaderLine(std::string Line);
62
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 */
70 RUN_HEADERS_PARSE_ERROR
71 };
72 /** \brief Get the headers before the data */
73 RunHeadersResult RunHeaders(FileFd * const File, const std::string &Uri);
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; JunkSize = 0;
77 StartPos = 0; Encoding = Closes; time(&Date); HaveContent = false;
78 State = Header; Persistent = false; Pipeline = true;};
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
109 public:
110 bool Debug;
111
112 /** \brief Result of the header parsing */
113 enum DealWithHeadersResult {
114 /** \brief The file is open and ready */
115 FILE_IS_OPEN,
116 /** \brief We got a IMS hit, the file has not changed */
117 IMS_HIT,
118 /** \brief The server reported a unrecoverable error */
119 ERROR_UNRECOVERABLE,
120 /** \brief The server reported a error with a error content page */
121 ERROR_WITH_CONTENT_PAGE,
122 /** \brief An error on the client side */
123 ERROR_NOT_FROM_SERVER,
124 /** \brief A redirect or retry request */
125 TRY_AGAIN_OR_REDIRECT
126 };
127 /** \brief Handle the retrieved header data */
128 DealWithHeadersResult DealWithHeaders(FetchResult &Res);
129
130 // In the event of a fatal signal this file will be closed and timestamped.
131 static std::string FailFile;
132 static int FailFd;
133 static time_t FailTime;
134 static APT_NORETURN void SigTerm(int);
135
136 virtual bool Configuration(std::string Message);
137 virtual bool Flush() { return Server->Flush(File); };
138
139 int Loop();
140
141 virtual void SendReq(FetchItem *Itm) = 0;
142 virtual ServerState * CreateServerState(URI uri) = 0;
143 virtual void RotateDNS() = 0;
144
145 ServerMethod(const char *Ver,unsigned long Flags = 0) : pkgAcqMethod(Ver, Flags), Server(NULL), File(NULL), PipelineDepth(0), AllowRedirect(false), Debug(false) {};
146 virtual ~ServerMethod() {};
147 };
148
149 #endif