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