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