integrate Anthonys rred with POC for client-side merge
[ntk/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 aquire 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
18 #include "server.h"
19
20 using std::cout;
21 using std::endl;
22
23 class HttpMethod;
24 class Hashes;
25
26 class CircleBuf
27 {
28 unsigned char *Buf;
29 unsigned long long Size;
30 unsigned long long InP;
31 unsigned long long OutP;
32 std::string OutQueue;
33 unsigned long long StrPos;
34 unsigned long long MaxGet;
35 struct timeval Start;
36
37 static unsigned long long BwReadLimit;
38 static unsigned long long BwTickReadData;
39 static struct timeval BwReadTick;
40 static const unsigned int BW_HZ;
41
42 unsigned long long LeftRead() const
43 {
44 unsigned long long Sz = Size - (InP - OutP);
45 if (Sz > Size - (InP%Size))
46 Sz = Size - (InP%Size);
47 return Sz;
48 }
49 unsigned long long LeftWrite() const
50 {
51 unsigned long long Sz = InP - OutP;
52 if (InP > MaxGet)
53 Sz = MaxGet - OutP;
54 if (Sz > Size - (OutP%Size))
55 Sz = Size - (OutP%Size);
56 return Sz;
57 }
58 void FillOut();
59
60 public:
61 Hashes *Hash;
62
63 // Read data in
64 bool Read(int Fd);
65 bool Read(std::string Data);
66
67 // Write data out
68 bool Write(int Fd);
69 bool WriteTillEl(std::string &Data,bool Single = false);
70
71 // Control the write limit
72 void Limit(long long Max) {if (Max == -1) MaxGet = 0-1; else MaxGet = OutP + Max;}
73 bool IsLimit() const {return MaxGet == OutP;};
74 void Print() const {cout << MaxGet << ',' << OutP << endl;};
75
76 // Test for free space in the buffer
77 bool ReadSpace() const {return Size - (InP - OutP) > 0;};
78 bool WriteSpace() const {return InP - OutP > 0;};
79
80 // Dump everything
81 void Reset();
82 void Stats();
83
84 CircleBuf(unsigned long long Size);
85 ~CircleBuf();
86 };
87
88 struct HttpServerState: public ServerState
89 {
90 // This is the connection itself. Output is data FROM the server
91 CircleBuf In;
92 CircleBuf Out;
93 int ServerFd;
94
95 protected:
96 virtual bool ReadHeaderLines(std::string &Data);
97 virtual bool LoadNextResponse(bool const ToFile, FileFd * const File);
98 virtual bool WriteResponse(std::string const &Data);
99
100 public:
101 virtual void Reset() { ServerState::Reset(); ServerFd = -1; };
102
103 virtual bool RunData(FileFd * const File);
104
105 virtual bool Open();
106 virtual bool IsOpen();
107 virtual bool Close();
108 virtual bool InitHashes(FileFd &File);
109 virtual Hashes * GetHashes();
110 virtual bool Die(FileFd &File);
111 virtual bool Flush(FileFd * const File);
112 virtual bool Go(bool ToFile, FileFd * const File);
113
114 HttpServerState(URI Srv, HttpMethod *Owner);
115 virtual ~HttpServerState() {Close();};
116 };
117
118 class HttpMethod : public ServerMethod
119 {
120 public:
121 virtual void SendReq(FetchItem *Itm);
122
123 /** \brief Try to AutoDetect the proxy */
124 bool AutoDetectProxy();
125
126 virtual bool Configuration(std::string Message);
127
128 virtual ServerState * CreateServerState(URI uri);
129 virtual void RotateDNS();
130
131 protected:
132 std::string AutoDetectProxyCmd;
133
134 public:
135 friend struct HttpServerState;
136
137 HttpMethod() : ServerMethod("1.2",Pipeline | SendConfig)
138 {
139 File = 0;
140 Server = 0;
141 };
142 };
143
144 #endif