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