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