]> git.saurik.com Git - apt.git/blob - methods/server.h
Rename "Size" in ServerState to TotalFileSize
[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 protected:
61 ServerMethod *Owner;
62
63 virtual bool ReadHeaderLines(std::string &Data) = 0;
64 virtual bool LoadNextResponse(bool const ToFile, FileFd * const File) = 0;
65
66 public:
67 bool HeaderLine(std::string Line);
68
69 /** \brief Result of the header acquire */
70 enum RunHeadersResult {
71 /** \brief Header ok */
72 RUN_HEADERS_OK,
73 /** \brief IO error while retrieving */
74 RUN_HEADERS_IO_ERROR,
75 /** \brief Parse error after retrieving */
76 RUN_HEADERS_PARSE_ERROR
77 };
78 /** \brief Get the headers before the data */
79 RunHeadersResult RunHeaders(FileFd * const File, const std::string &Uri);
80
81 bool Comp(URI Other) const {return Other.Host == ServerName.Host && Other.Port == ServerName.Port;};
82 virtual void Reset() {Major = 0; Minor = 0; Result = 0; Code[0] = '\0'; TotalFileSize = 0; JunkSize = 0;
83 StartPos = 0; Encoding = Closes; time(&Date); HaveContent = false;
84 State = Header; Persistent = false; Pipeline = true;};
85 virtual bool WriteResponse(std::string const &Data) = 0;
86
87 /** \brief Transfer the data from the socket */
88 virtual bool RunData(FileFd * const File) = 0;
89
90 virtual bool Open() = 0;
91 virtual bool IsOpen() = 0;
92 virtual bool Close() = 0;
93 virtual bool InitHashes(FileFd &File) = 0;
94 virtual Hashes * GetHashes() = 0;
95 virtual bool Die(FileFd &File) = 0;
96 virtual bool Flush(FileFd * const File) = 0;
97 virtual bool Go(bool ToFile, FileFd * const File) = 0;
98
99 ServerState(URI Srv, ServerMethod *Owner);
100 virtual ~ServerState() {};
101 };
102
103 class ServerMethod : public pkgAcqMethod
104 {
105 protected:
106 virtual bool Fetch(FetchItem *);
107
108 ServerState *Server;
109 std::string NextURI;
110 FileFd *File;
111
112 unsigned long PipelineDepth;
113 bool AllowRedirect;
114
115 public:
116 bool Debug;
117
118 /** \brief Result of the header parsing */
119 enum DealWithHeadersResult {
120 /** \brief The file is open and ready */
121 FILE_IS_OPEN,
122 /** \brief We got a IMS hit, the file has not changed */
123 IMS_HIT,
124 /** \brief The server reported a unrecoverable error */
125 ERROR_UNRECOVERABLE,
126 /** \brief The server reported a error with a error content page */
127 ERROR_WITH_CONTENT_PAGE,
128 /** \brief An error on the client side */
129 ERROR_NOT_FROM_SERVER,
130 /** \brief A redirect or retry request */
131 TRY_AGAIN_OR_REDIRECT
132 };
133 /** \brief Handle the retrieved header data */
134 DealWithHeadersResult DealWithHeaders(FetchResult &Res);
135
136 // In the event of a fatal signal this file will be closed and timestamped.
137 static std::string FailFile;
138 static int FailFd;
139 static time_t FailTime;
140 static APT_NORETURN void SigTerm(int);
141
142 virtual bool Configuration(std::string Message);
143 virtual bool Flush() { return Server->Flush(File); };
144
145 int Loop();
146
147 virtual void SendReq(FetchItem *Itm) = 0;
148 virtual ServerState * CreateServerState(URI uri) = 0;
149 virtual void RotateDNS() = 0;
150
151 ServerMethod(const char *Ver,unsigned long Flags = 0) : pkgAcqMethod(Ver, Flags), Server(NULL), File(NULL), PipelineDepth(0), AllowRedirect(false), Debug(false) {};
152 virtual ~ServerMethod() {};
153 };
154
155 #endif