]> git.saurik.com Git - apt.git/blob - methods/server.h
use the same redirection handling for http and https
[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 "aptmethod.h"
16
17 #include <time.h>
18 #include <iostream>
19 #include <string>
20 #include <memory>
21
22 using std::cout;
23 using std::endl;
24
25 class Hashes;
26 class ServerMethod;
27 class FileFd;
28
29 struct 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
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)
46 unsigned long long StartPos;
47
48 time_t Date;
49 bool HaveContent;
50 enum {Chunked,Stream,Closes} Encoding;
51 enum {Header, Data} State;
52 bool Persistent;
53 bool PipelineAllowed;
54 std::string Location;
55
56 // This is a Persistent attribute of the server itself.
57 bool Pipeline;
58 URI ServerName;
59 URI Proxy;
60 unsigned long TimeOut;
61
62 unsigned long long MaximumSize;
63
64 protected:
65 ServerMethod *Owner;
66
67 virtual bool ReadHeaderLines(std::string &Data) = 0;
68 virtual bool LoadNextResponse(bool const ToFile, FileFd * const File) = 0;
69
70 public:
71 bool HeaderLine(std::string Line);
72
73 /** \brief Result of the header acquire */
74 enum RunHeadersResult {
75 /** \brief Header ok */
76 RUN_HEADERS_OK,
77 /** \brief IO error while retrieving */
78 RUN_HEADERS_IO_ERROR,
79 /** \brief Parse error after retrieving */
80 RUN_HEADERS_PARSE_ERROR
81 };
82 /** \brief Get the headers before the data */
83 RunHeadersResult RunHeaders(FileFd * const File, const std::string &Uri);
84 bool AddPartialFileToHashes(FileFd &File);
85
86 bool Comp(URI Other) const {return Other.Host == ServerName.Host && Other.Port == ServerName.Port;};
87 virtual void Reset() {Major = 0; Minor = 0; Result = 0; Code[0] = '\0'; TotalFileSize = 0; JunkSize = 0;
88 StartPos = 0; Encoding = Closes; time(&Date); HaveContent = false;
89 State = Header; Persistent = false; Pipeline = false; MaximumSize = 0; PipelineAllowed = true;};
90 virtual bool WriteResponse(std::string const &Data) = 0;
91
92 /** \brief Transfer the data from the socket */
93 virtual bool RunData(FileFd * const File) = 0;
94 virtual bool RunDataToDevNull() = 0;
95
96 virtual bool Open() = 0;
97 virtual bool IsOpen() = 0;
98 virtual bool Close() = 0;
99 virtual bool InitHashes(HashStringList const &ExpectedHashes) = 0;
100 virtual Hashes * GetHashes() = 0;
101 virtual bool Die(FileFd * const 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
109 class ServerMethod : public aptMethod
110 {
111 protected:
112 virtual bool Fetch(FetchItem *) APT_OVERRIDE;
113
114 std::unique_ptr<ServerState> Server;
115 std::string NextURI;
116 FileFd *File;
117
118 unsigned long PipelineDepth;
119 bool AllowRedirect;
120
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
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 virtual 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;
150 static APT_NORETURN void SigTerm(int);
151
152 virtual bool Flush() { return Server->Flush(File); };
153
154 int Loop();
155
156 virtual void SendReq(FetchItem *Itm) = 0;
157 virtual std::unique_ptr<ServerState> CreateServerState(URI const &uri) = 0;
158 virtual void RotateDNS() = 0;
159
160 ServerMethod(char const * const Binary, char const * const Ver,unsigned long const Flags);
161 virtual ~ServerMethod() {};
162 };
163
164 #endif