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