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