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