]> git.saurik.com Git - apt.git/blame - methods/http.h
s/Saftey/Safety/
[apt.git] / methods / http.h
CommitLineData
be4401bf 1// -*- mode: cpp; mode: fold -*-
a305f593
AL
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 $
be4401bf
AL
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
42195eb2
AL
16#include <iostream>
17
18using std::cout;
19using std::endl;
20
be4401bf
AL
21class HttpMethod;
22
23class 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
63b1700f 54 Hashes *Hash;
be4401bf
AL
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);
a305f593 78 ~CircleBuf() {delete [] Buf; delete Hash;};
be4401bf
AL
79};
80
81struct 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
92e889c8 89 // These are some statistics from the last parsed header lines
be4401bf
AL
90 unsigned long Size;
91 signed long StartPos;
92 time_t Date;
92e889c8 93 bool HaveContent;
be4401bf
AL
94 enum {Chunked,Stream,Closes} Encoding;
95 enum {Header, Data} State;
e836f356
AL
96 bool Persistent;
97
98 // This is a Persistent attribute of the server itself.
f93d1355 99 bool Pipeline;
be4401bf
AL
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;
f93d1355
AL
112 Encoding = Closes; time(&Date); ServerFd = -1;
113 Pipeline = true;};
92e889c8 114 int RunHeaders();
be4401bf
AL
115 bool RunData();
116
117 bool Open();
118 bool Close();
119
120 ServerState(URI Srv,HttpMethod *Owner);
121 ~ServerState() {Close();};
122};
123
124class 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);
5cb5d8dc 131
85f72a56
AL
132 virtual bool Fetch(FetchItem *);
133 virtual bool Configuration(string Message);
be4401bf 134
492f957a
AL
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
be4401bf 141 public:
b2e465d6 142 friend class ServerState;
be4401bf 143
be4401bf 144 FileFd *File;
5cb5d8dc 145 ServerState *Server;
be4401bf
AL
146
147 int Loop();
148
b98f2859 149 HttpMethod() : pkgAcqMethod("1.2",Pipeline | SendConfig)
be4401bf 150 {
be4401bf 151 File = 0;
5cb5d8dc 152 Server = 0;
be4401bf
AL
153 };
154};
155
156URI Proxy;
157
158#endif