]> git.saurik.com Git - apt.git/blob - methods/http.h
New http method
[apt.git] / methods / http.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: http.h,v 1.1 1998/11/01 05:30:47 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 class HttpMethod;
17
18 class CircleBuf
19 {
20 unsigned char *Buf;
21 unsigned long Size;
22 unsigned long InP;
23 unsigned long OutP;
24 string OutQueue;
25 unsigned long StrPos;
26 unsigned long MaxGet;
27 struct timeval Start;
28
29 unsigned long LeftRead()
30 {
31 unsigned long Sz = Size - (InP - OutP);
32 if (Sz > Size - (InP%Size))
33 Sz = Size - (InP%Size);
34 return Sz;
35 }
36 unsigned long LeftWrite()
37 {
38 unsigned long Sz = InP - OutP;
39 if (InP > MaxGet)
40 Sz = MaxGet - OutP;
41 if (Sz > Size - (OutP%Size))
42 Sz = Size - (OutP%Size);
43 return Sz;
44 }
45 void FillOut();
46
47 public:
48
49 MD5Summation *MD5;
50
51 // Read data in
52 bool Read(int Fd);
53 bool Read(string Data);
54
55 // Write data out
56 bool Write(int Fd);
57 bool WriteTillEl(string &Data,bool Single = false);
58
59 // Control the write limit
60 void Limit(long Max) {if (Max == -1) MaxGet = 0-1; else MaxGet = OutP + Max;}
61 bool IsLimit() {return MaxGet == OutP;};
62 void Print() {cout << MaxGet << ',' << OutP << endl;};
63
64 // Test for free space in the buffer
65 bool ReadSpace() {return Size - (InP - OutP) > 0;};
66 bool WriteSpace() {return InP - OutP > 0;};
67
68 // Dump everything
69 void Reset();
70 void Stats();
71
72 CircleBuf(unsigned long Size);
73 ~CircleBuf() {delete [] Buf;};
74 };
75
76 struct ServerState
77 {
78 // This is the last parsed Header Line
79 unsigned int Major;
80 unsigned int Minor;
81 unsigned int Result;
82 char Code[MAXLEN];
83
84 // These are some statistics from the last parsed get line
85 unsigned long Size;
86 signed long StartPos;
87 time_t Date;
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;};
103 bool RunHeaders();
104 bool RunData();
105
106 bool Open();
107 bool Close();
108
109 ServerState(URI Srv,HttpMethod *Owner);
110 ~ServerState() {Close();};
111 };
112
113 class 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);
120
121 public:
122 friend ServerState;
123
124 ServerState *Srv;
125 int Depth;
126 FileFd *File;
127
128 int Loop();
129
130 HttpMethod() : pkgAcqMethod("1.2",SingleInstance | Pipeline | SendConfig)
131 {
132 Depth = 0;
133 Srv = 0;
134 File = 0;
135 Depth = 0;
136 };
137 };
138
139 URI Proxy;
140
141 #endif