oops
[ntk/apt.git] / methods / http.h
CommitLineData
be4401bf 1// -*- mode: cpp; mode: fold -*-
5cb5d8dc 2// Description /*{{{*/// $Id: http.h,v 1.5 1998/12/05 04:19:06 jgg Exp $
be4401bf
AL
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
15class HttpMethod;
16
17class 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
75struct 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
92e889c8 83 // These are some statistics from the last parsed header lines
be4401bf
AL
84 unsigned long Size;
85 signed long StartPos;
86 time_t Date;
92e889c8 87 bool HaveContent;
be4401bf
AL
88 enum {Chunked,Stream,Closes} Encoding;
89 enum {Header, Data} State;
90
91 HttpMethod *Owner;
92
93 // This is the connection itself. Output is data FROM the server
94 CircleBuf In;
95 CircleBuf Out;
96 int ServerFd;
97 URI ServerName;
98
99 bool HeaderLine(string Line);
100 bool Comp(URI Other) {return Other.Host == ServerName.Host && Other.Port == ServerName.Port;};
101 void Reset() {Major = 0; Minor = 0; Result = 0; Size = 0; StartPos = 0;
102 Encoding = Closes; time(&Date); ServerFd = -1;};
92e889c8 103 int RunHeaders();
be4401bf
AL
104 bool RunData();
105
106 bool Open();
107 bool Close();
108
109 ServerState(URI Srv,HttpMethod *Owner);
110 ~ServerState() {Close();};
111};
112
113class HttpMethod : public pkgAcqMethod
114{
115 void SendReq(FetchItem *Itm,CircleBuf &Out);
116 bool Go(bool ToFile,ServerState *Srv);
117 bool Flush(ServerState *Srv);
118 bool ServerDie(ServerState *Srv);
119 int DealWithHeaders(FetchResult &Res,ServerState *Srv);
5cb5d8dc
AL
120
121 bool Fetch(FetchItem *);
be4401bf 122
492f957a
AL
123 // In the event of a fatal signal this file will be closed and timestamped.
124 static string FailFile;
125 static int FailFd;
126 static time_t FailTime;
127 static void SigTerm(int);
128
be4401bf
AL
129 public:
130 friend ServerState;
131
be4401bf 132 FileFd *File;
5cb5d8dc 133 ServerState *Server;
be4401bf
AL
134
135 int Loop();
136
b98f2859 137 HttpMethod() : pkgAcqMethod("1.2",Pipeline | SendConfig)
be4401bf 138 {
be4401bf 139 File = 0;
5cb5d8dc 140 Server = 0;
be4401bf
AL
141 };
142};
143
144URI Proxy;
145
146#endif