* Translations:
[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 #define MAXLEN 360
15
16
17
18 using std::cout;
19 using std::endl;
20
21 class HttpMethod;
22
23 class CircleBuf
24 {
25 unsigned char *Buf;
26 unsigned long Size;
27 unsigned long InP;
28 unsigned long OutP;
29 string OutQueue;
30 unsigned long StrPos;
31 unsigned long MaxGet;
32 struct timeval Start;
33
34 static unsigned long BwReadLimit;
35 static unsigned long BwTickReadData;
36 static struct timeval BwReadTick;
37 static const unsigned int BW_HZ;
38
39 unsigned long LeftRead()
40 {
41 unsigned long Sz = Size - (InP - OutP);
42 if (Sz > Size - (InP%Size))
43 Sz = Size - (InP%Size);
44 return Sz;
45 }
46 unsigned long LeftWrite()
47 {
48 unsigned long Sz = InP - OutP;
49 if (InP > MaxGet)
50 Sz = MaxGet - OutP;
51 if (Sz > Size - (OutP%Size))
52 Sz = Size - (OutP%Size);
53 return Sz;
54 }
55 void FillOut();
56
57 public:
58
59 Hashes *Hash;
60
61 // Read data in
62 bool Read(int Fd);
63 bool Read(string Data);
64
65 // Write data out
66 bool Write(int Fd);
67 bool WriteTillEl(string &Data,bool Single = false);
68
69 // Control the write limit
70 void Limit(long Max) {if (Max == -1) MaxGet = 0-1; else MaxGet = OutP + Max;}
71 bool IsLimit() {return MaxGet == OutP;};
72 void Print() {cout << MaxGet << ',' << OutP << endl;};
73
74 // Test for free space in the buffer
75 bool ReadSpace() {return Size - (InP - OutP) > 0;};
76 bool WriteSpace() {return InP - OutP > 0;};
77
78 // Dump everything
79 void Reset();
80 void Stats();
81
82 CircleBuf(unsigned long Size);
83 ~CircleBuf() {delete [] Buf; delete Hash;};
84 };
85
86 struct ServerState
87 {
88 // This is the last parsed Header Line
89 unsigned int Major;
90 unsigned int Minor;
91 unsigned int Result;
92 char Code[MAXLEN];
93
94 // These are some statistics from the last parsed header lines
95 unsigned long Size;
96 signed long StartPos;
97 time_t Date;
98 bool HaveContent;
99 enum {Chunked,Stream,Closes} Encoding;
100 enum {Header, Data} State;
101 bool Persistent;
102
103 // This is a Persistent attribute of the server itself.
104 bool Pipeline;
105
106 HttpMethod *Owner;
107
108 // This is the connection itself. Output is data FROM the server
109 CircleBuf In;
110 CircleBuf Out;
111 int ServerFd;
112 URI ServerName;
113
114 bool HeaderLine(string Line);
115 bool Comp(URI Other) {return Other.Host == ServerName.Host && Other.Port == ServerName.Port;};
116 void Reset() {Major = 0; Minor = 0; Result = 0; Size = 0; StartPos = 0;
117 Encoding = Closes; time(&Date); ServerFd = -1;
118 Pipeline = true;};
119 int RunHeaders();
120 bool RunData();
121
122 bool Open();
123 bool Close();
124
125 ServerState(URI Srv,HttpMethod *Owner);
126 ~ServerState() {Close();};
127 };
128
129 class HttpMethod : public pkgAcqMethod
130 {
131 void SendReq(FetchItem *Itm,CircleBuf &Out);
132 bool Go(bool ToFile,ServerState *Srv);
133 bool Flush(ServerState *Srv);
134 bool ServerDie(ServerState *Srv);
135 int DealWithHeaders(FetchResult &Res,ServerState *Srv);
136
137 virtual bool Configuration(string Message);
138
139 // In the event of a fatal signal this file will be closed and timestamped.
140 static string FailFile;
141 static int FailFd;
142 static time_t FailTime;
143 static void SigTerm(int);
144
145 protected:
146 virtual bool Fetch(FetchItem *);
147
148 public:
149 friend class ServerState;
150
151 FileFd *File;
152 ServerState *Server;
153
154 int Loop();
155
156 HttpMethod() : pkgAcqMethod("1.2",Pipeline | SendConfig)
157 {
158 File = 0;
159 Server = 0;
160 };
161 };
162
163 #endif