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