]> git.saurik.com Git - apt.git/blame - methods/server.h
methods/connect.cc: Only use AI_IDN if defined
[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>
23e64f6d 15#include "aptmethod.h"
7330f4df 16
453b82a3
DK
17#include <time.h>
18#include <iostream>
7330f4df 19#include <string>
830a1b8c 20#include <memory>
7330f4df
DK
21
22using std::cout;
23using std::endl;
24
25class Hashes;
26class ServerMethod;
27class FileFd;
28
29struct ServerState
30{
31 // This is the last parsed Header Line
32 unsigned int Major;
33 unsigned int Minor;
34 unsigned int Result;
35 char Code[360];
36
37 // These are some statistics from the last parsed header lines
6291f60e
MV
38
39 // total size of the usable content (aka: the file)
40 unsigned long long TotalFileSize;
41 // size we actually download (can be smaller than Size if we have partial content)
42 unsigned long long DownloadSize;
43 // size of junk content (aka: server error pages)
44 unsigned long long JunkSize;
45 // The start of the data (for partial content)
3de8f956 46 unsigned long long StartPos;
6291f60e 47
7330f4df
DK
48 time_t Date;
49 bool HaveContent;
50 enum {Chunked,Stream,Closes} Encoding;
51 enum {Header, Data} State;
52 bool Persistent;
b6d88f39 53 bool PipelineAllowed;
d94b1d80 54 bool RangesAllowed;
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;};
ebdb6f18 88 virtual void Reset(bool const Everything = true);
7330f4df
DK
89 virtual bool WriteResponse(std::string const &Data) = 0;
90
91 /** \brief Transfer the data from the socket */
92 virtual bool RunData(FileFd * const File) = 0;
57401c48 93 virtual bool RunDataToDevNull() = 0;
7330f4df
DK
94
95 virtual bool Open() = 0;
96 virtual bool IsOpen() = 0;
97 virtual bool Close() = 0;
34faa8f7 98 virtual bool InitHashes(HashStringList const &ExpectedHashes) = 0;
7330f4df 99 virtual Hashes * GetHashes() = 0;
44605518 100 virtual bool Die(FileFd * const File) = 0;
7330f4df
DK
101 virtual bool Flush(FileFd * const File) = 0;
102 virtual bool Go(bool ToFile, FileFd * const File) = 0;
103
104 ServerState(URI Srv, ServerMethod *Owner);
105 virtual ~ServerState() {};
106};
107
23e64f6d 108class ServerMethod : public aptMethod
7330f4df
DK
109{
110 protected:
3b302846 111 virtual bool Fetch(FetchItem *) APT_OVERRIDE;
7330f4df 112
830a1b8c 113 std::unique_ptr<ServerState> Server;
7330f4df
DK
114 std::string NextURI;
115 FileFd *File;
116
117 unsigned long PipelineDepth;
118 bool AllowRedirect;
119
f2b47ba2
MV
120 // Find the biggest item in the fetch queue for the checking of the maximum
121 // size
122 unsigned long long FindMaximumObjectSizeInQueue() const APT_PURE;
123
7330f4df
DK
124 public:
125 bool Debug;
126
127 /** \brief Result of the header parsing */
128 enum DealWithHeadersResult {
129 /** \brief The file is open and ready */
130 FILE_IS_OPEN,
131 /** \brief We got a IMS hit, the file has not changed */
132 IMS_HIT,
133 /** \brief The server reported a unrecoverable error */
134 ERROR_UNRECOVERABLE,
135 /** \brief The server reported a error with a error content page */
136 ERROR_WITH_CONTENT_PAGE,
137 /** \brief An error on the client side */
138 ERROR_NOT_FROM_SERVER,
139 /** \brief A redirect or retry request */
140 TRY_AGAIN_OR_REDIRECT
141 };
142 /** \brief Handle the retrieved header data */
4bba5a88 143 virtual DealWithHeadersResult DealWithHeaders(FetchResult &Res);
7330f4df
DK
144
145 // In the event of a fatal signal this file will be closed and timestamped.
146 static std::string FailFile;
147 static int FailFd;
148 static time_t FailTime;
a02db58f 149 static APT_NORETURN void SigTerm(int);
7330f4df 150
7330f4df
DK
151 virtual bool Flush() { return Server->Flush(File); };
152
153 int Loop();
154
155 virtual void SendReq(FetchItem *Itm) = 0;
830a1b8c 156 virtual std::unique_ptr<ServerState> CreateServerState(URI const &uri) = 0;
fd46d305 157 virtual void RotateDNS() = 0;
0568d325
DK
158 virtual bool Configuration(std::string Message) APT_OVERRIDE;
159
160 bool AddProxyAuth(URI &Proxy, URI const &Server) const;
7330f4df 161
30060442 162 ServerMethod(std::string &&Binary, char const * const Ver,unsigned long const Flags);
7330f4df
DK
163 virtual ~ServerMethod() {};
164};
165
166#endif