]> git.saurik.com Git - apt.git/blob - methods/http.h
CMake: debian: Switch packaging over to CMake and dh 9
[apt.git] / methods / http.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/// $Id: http.h,v 1.12 2002/04/18 05:09:38 jgg Exp $
3 // $Id: http.h,v 1.12 2002/04/18 05:09:38 jgg Exp $
4 /* ######################################################################
5
6 HTTP Acquire Method - This is the HTTP acquire method for APT.
7
8 ##################################################################### */
9 /*}}}*/
10
11 #ifndef APT_HTTP_H
12 #define APT_HTTP_H
13
14 #include <apt-pkg/strutl.h>
15
16 #include <string>
17 #include <sys/time.h>
18 #include <iostream>
19
20 #include "server.h"
21
22 using std::cout;
23 using std::endl;
24
25 class FileFd;
26 class HttpMethod;
27 class Hashes;
28
29 class CircleBuf
30 {
31 unsigned char *Buf;
32 unsigned long long Size;
33 unsigned long long InP;
34 unsigned long long OutP;
35 std::string OutQueue;
36 unsigned long long StrPos;
37 unsigned long long MaxGet;
38 struct timeval Start;
39
40 static unsigned long long BwReadLimit;
41 static unsigned long long BwTickReadData;
42 static struct timeval BwReadTick;
43 static const unsigned int BW_HZ;
44
45 unsigned long long LeftRead() const
46 {
47 unsigned long long Sz = Size - (InP - OutP);
48 if (Sz > Size - (InP%Size))
49 Sz = Size - (InP%Size);
50 return Sz;
51 }
52 unsigned long long LeftWrite() const
53 {
54 unsigned long long Sz = InP - OutP;
55 if (InP > MaxGet)
56 Sz = MaxGet - OutP;
57 if (Sz > Size - (OutP%Size))
58 Sz = Size - (OutP%Size);
59 return Sz;
60 }
61 void FillOut();
62
63 public:
64 Hashes *Hash;
65 // total amount of data that got written so far
66 unsigned long long TotalWriten;
67
68 // Read data in
69 bool Read(int Fd);
70 bool Read(std::string Data);
71
72 // Write data out
73 bool Write(int Fd);
74 bool WriteTillEl(std::string &Data,bool Single = false);
75
76 // Control the write limit
77 void Limit(long long Max) {if (Max == -1) MaxGet = 0-1; else MaxGet = OutP + Max;}
78 bool IsLimit() const {return MaxGet == OutP;};
79 void Print() const {cout << MaxGet << ',' << OutP << endl;};
80
81 // Test for free space in the buffer
82 bool ReadSpace() const {return Size - (InP - OutP) > 0;};
83 bool WriteSpace() const {return InP - OutP > 0;};
84
85 void Reset();
86 // Dump everything
87 void Stats();
88
89 explicit CircleBuf(unsigned long long Size);
90 ~CircleBuf();
91 };
92
93 struct HttpServerState: public ServerState
94 {
95 // This is the connection itself. Output is data FROM the server
96 CircleBuf In;
97 CircleBuf Out;
98 int ServerFd;
99
100 protected:
101 virtual bool ReadHeaderLines(std::string &Data) APT_OVERRIDE;
102 virtual bool LoadNextResponse(bool const ToFile, FileFd * const File) APT_OVERRIDE;
103 virtual bool WriteResponse(std::string const &Data) APT_OVERRIDE;
104
105 public:
106 virtual void Reset() APT_OVERRIDE { ServerState::Reset(); ServerFd = -1; };
107
108 virtual bool RunData(FileFd * const File) APT_OVERRIDE;
109
110 virtual bool Open() APT_OVERRIDE;
111 virtual bool IsOpen() APT_OVERRIDE;
112 virtual bool Close() APT_OVERRIDE;
113 virtual bool InitHashes(HashStringList const &ExpectedHashes) APT_OVERRIDE;
114 virtual Hashes * GetHashes() APT_OVERRIDE;
115 virtual bool Die(FileFd * const File) APT_OVERRIDE;
116 virtual bool Flush(FileFd * const File) APT_OVERRIDE;
117 virtual bool Go(bool ToFile, FileFd * const File) APT_OVERRIDE;
118
119 HttpServerState(URI Srv, HttpMethod *Owner);
120 virtual ~HttpServerState() {Close();};
121 };
122
123 class HttpMethod : public ServerMethod
124 {
125 public:
126 virtual void SendReq(FetchItem *Itm) APT_OVERRIDE;
127
128 virtual bool Configuration(std::string Message) APT_OVERRIDE;
129
130 virtual std::unique_ptr<ServerState> CreateServerState(URI const &uri) APT_OVERRIDE;
131 virtual void RotateDNS() APT_OVERRIDE;
132
133 protected:
134 std::string AutoDetectProxyCmd;
135
136 public:
137 friend struct HttpServerState;
138
139 HttpMethod() : ServerMethod("http", "1.2",Pipeline | SendConfig)
140 {
141 File = 0;
142 Server = 0;
143 };
144 };
145
146 #endif