]> git.saurik.com Git - apt.git/blame - methods/server.h
derive more of https from http method
[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;
905fba60 40 bool ReceivedData;
7330f4df
DK
41 time_t Date;
42 bool HaveContent;
43 enum {Chunked,Stream,Closes} Encoding;
44 enum {Header, Data} State;
45 bool Persistent;
46 std::string Location;
47
48 // This is a Persistent attribute of the server itself.
49 bool Pipeline;
50 URI ServerName;
51 URI Proxy;
52 unsigned long TimeOut;
53
c48eea97 54 unsigned long long MaximumSize;
dcd5856b 55
7330f4df
DK
56 protected:
57 ServerMethod *Owner;
58
7330f4df
DK
59 virtual bool ReadHeaderLines(std::string &Data) = 0;
60 virtual bool LoadNextResponse(bool const ToFile, FileFd * const File) = 0;
61
62 public:
fd46d305
DK
63 bool HeaderLine(std::string Line);
64
7330f4df
DK
65 /** \brief Result of the header acquire */
66 enum RunHeadersResult {
67 /** \brief Header ok */
68 RUN_HEADERS_OK,
69 /** \brief IO error while retrieving */
70 RUN_HEADERS_IO_ERROR,
71 /** \brief Parse error after retrieving */
d3e8fbb3 72 RUN_HEADERS_PARSE_ERROR
7330f4df
DK
73 };
74 /** \brief Get the headers before the data */
9622b211 75 RunHeadersResult RunHeaders(FileFd * const File, const std::string &Uri);
7330f4df
DK
76
77 bool Comp(URI Other) const {return Other.Host == ServerName.Host && Other.Port == ServerName.Port;};
ed793a19 78 virtual void Reset() {Major = 0; Minor = 0; Result = 0; Code[0] = '\0'; Size = 0; JunkSize = 0;
905fba60 79 StartPos = 0; ReceivedData = false; Encoding = Closes; time(&Date); HaveContent = false;
c48eea97 80 State = Header; Persistent = false; Pipeline = true; MaximumSize = 0;};
7330f4df
DK
81 virtual bool WriteResponse(std::string const &Data) = 0;
82
83 /** \brief Transfer the data from the socket */
84 virtual bool RunData(FileFd * const File) = 0;
85
86 virtual bool Open() = 0;
87 virtual bool IsOpen() = 0;
88 virtual bool Close() = 0;
89 virtual bool InitHashes(FileFd &File) = 0;
90 virtual Hashes * GetHashes() = 0;
91 virtual bool Die(FileFd &File) = 0;
92 virtual bool Flush(FileFd * const File) = 0;
93 virtual bool Go(bool ToFile, FileFd * const File) = 0;
94
95 ServerState(URI Srv, ServerMethod *Owner);
96 virtual ~ServerState() {};
97};
98
99class ServerMethod : public pkgAcqMethod
100{
101 protected:
102 virtual bool Fetch(FetchItem *);
103
104 ServerState *Server;
105 std::string NextURI;
106 FileFd *File;
107
108 unsigned long PipelineDepth;
109 bool AllowRedirect;
110
f2b47ba2
MV
111 // Find the biggest item in the fetch queue for the checking of the maximum
112 // size
113 unsigned long long FindMaximumObjectSizeInQueue() const APT_PURE;
114
7330f4df
DK
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;
a02db58f 140 static APT_NORETURN void SigTerm(int);
7330f4df
DK
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;
fd46d305 149 virtual void RotateDNS() = 0;
7330f4df 150
895417ef 151 ServerMethod(const char *Ver,unsigned long Flags = 0) : pkgAcqMethod(Ver, Flags), Server(NULL), File(NULL), PipelineDepth(10), AllowRedirect(false), Debug(false) {};
7330f4df
DK
152 virtual ~ServerMethod() {};
153};
154
155#endif