]>
Commit | Line | Data |
---|---|---|
be4401bf AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
2cbcabd8 | 3 | // $Id: http.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $ |
be4401bf AL |
4 | /* ###################################################################### |
5 | ||
ae58a985 | 6 | HTTP Acquire Method - This is the HTTP aquire method for APT. |
be4401bf AL |
7 | |
8 | It uses HTTP/1.1 and many of the fancy options there-in, such as | |
e836f356 AL |
9 | pipelining, range, if-range and so on. |
10 | ||
11 | It is based on a doubly buffered select loop. A groupe of requests are | |
be4401bf AL |
12 | fed into a single output buffer that is constantly fed out the |
13 | socket. This provides ideal pipelining as in many cases all of the | |
14 | requests will fit into a single packet. The input socket is buffered | |
e836f356 | 15 | the same way and fed into the fd for the file (may be a pipe in future). |
be4401bf AL |
16 | |
17 | This double buffering provides fairly substantial transfer rates, | |
18 | compared to wget the http method is about 4% faster. Most importantly, | |
19 | when HTTP is compared with FTP as a protocol the speed difference is | |
20 | huge. In tests over the internet from two sites to llug (via ATM) this | |
21 | program got 230k/s sustained http transfer rates. FTP on the other | |
22 | hand topped out at 170k/s. That combined with the time to setup the | |
23 | FTP connection makes HTTP a vastly superior protocol. | |
24 | ||
25 | ##################################################################### */ | |
26 | /*}}}*/ | |
27 | // Include Files /*{{{*/ | |
28 | #include <apt-pkg/fileutl.h> | |
29 | #include <apt-pkg/acquire-method.h> | |
30 | #include <apt-pkg/error.h> | |
63b1700f | 31 | #include <apt-pkg/hashes.h> |
be4401bf AL |
32 | |
33 | #include <sys/stat.h> | |
34 | #include <sys/time.h> | |
35 | #include <utime.h> | |
36 | #include <unistd.h> | |
492f957a | 37 | #include <signal.h> |
be4401bf | 38 | #include <stdio.h> |
65a1e968 | 39 | #include <errno.h> |
42195eb2 AL |
40 | #include <string.h> |
41 | #include <iostream> | |
d77559ac | 42 | #include <apti18n.h> |
be4401bf AL |
43 | |
44 | // Internet stuff | |
0837bd25 | 45 | #include <netdb.h> |
be4401bf | 46 | |
59b46c41 | 47 | #include "config.h" |
0837bd25 | 48 | #include "connect.h" |
934b6582 | 49 | #include "rfc2553emu.h" |
be4401bf | 50 | #include "http.h" |
934b6582 | 51 | |
be4401bf | 52 | /*}}}*/ |
42195eb2 | 53 | using namespace std; |
be4401bf | 54 | |
492f957a AL |
55 | string HttpMethod::FailFile; |
56 | int HttpMethod::FailFd = -1; | |
57 | time_t HttpMethod::FailTime = 0; | |
c37030c2 | 58 | unsigned long PipelineDepth = 10; |
3000ccea | 59 | unsigned long TimeOut = 120; |
c98b1307 | 60 | bool Debug = false; |
c37030c2 | 61 | URI Proxy; |
492f957a | 62 | |
7c6e2dc7 MV |
63 | unsigned long CircleBuf::BwReadLimit=0; |
64 | unsigned long CircleBuf::BwTickReadData=0; | |
65 | struct timeval CircleBuf::BwReadTick={0,0}; | |
66 | const unsigned int CircleBuf::BW_HZ=10; | |
67 | ||
be4401bf AL |
68 | // CircleBuf::CircleBuf - Circular input buffer /*{{{*/ |
69 | // --------------------------------------------------------------------- | |
70 | /* */ | |
63b1700f | 71 | CircleBuf::CircleBuf(unsigned long Size) : Size(Size), Hash(0) |
be4401bf AL |
72 | { |
73 | Buf = new unsigned char[Size]; | |
74 | Reset(); | |
7c6e2dc7 MV |
75 | |
76 | CircleBuf::BwReadLimit = _config->FindI("Acquire::http::Dl-Limit",0)*1024; | |
be4401bf AL |
77 | } |
78 | /*}}}*/ | |
79 | // CircleBuf::Reset - Reset to the default state /*{{{*/ | |
80 | // --------------------------------------------------------------------- | |
81 | /* */ | |
82 | void CircleBuf::Reset() | |
83 | { | |
84 | InP = 0; | |
85 | OutP = 0; | |
86 | StrPos = 0; | |
87 | MaxGet = (unsigned int)-1; | |
88 | OutQueue = string(); | |
63b1700f | 89 | if (Hash != 0) |
be4401bf | 90 | { |
63b1700f AL |
91 | delete Hash; |
92 | Hash = new Hashes; | |
be4401bf AL |
93 | } |
94 | }; | |
95 | /*}}}*/ | |
96 | // CircleBuf::Read - Read from a FD into the circular buffer /*{{{*/ | |
97 | // --------------------------------------------------------------------- | |
98 | /* This fills up the buffer with as much data as is in the FD, assuming it | |
99 | is non-blocking.. */ | |
100 | bool CircleBuf::Read(int Fd) | |
101 | { | |
7c6e2dc7 MV |
102 | unsigned long BwReadMax; |
103 | ||
be4401bf AL |
104 | while (1) |
105 | { | |
106 | // Woops, buffer is full | |
107 | if (InP - OutP == Size) | |
108 | return true; | |
7c6e2dc7 MV |
109 | |
110 | // what's left to read in this tick | |
111 | BwReadMax = CircleBuf::BwReadLimit/BW_HZ; | |
112 | ||
113 | if(CircleBuf::BwReadLimit) { | |
114 | struct timeval now; | |
115 | gettimeofday(&now,0); | |
116 | ||
117 | unsigned long d = (now.tv_sec-CircleBuf::BwReadTick.tv_sec)*1000000 + | |
118 | now.tv_usec-CircleBuf::BwReadTick.tv_usec; | |
119 | if(d > 1000000/BW_HZ) { | |
120 | CircleBuf::BwReadTick = now; | |
121 | CircleBuf::BwTickReadData = 0; | |
122 | } | |
123 | ||
124 | if(CircleBuf::BwTickReadData >= BwReadMax) { | |
125 | usleep(1000000/BW_HZ); | |
126 | return true; | |
127 | } | |
128 | } | |
129 | ||
be4401bf AL |
130 | // Write the buffer segment |
131 | int Res; | |
7c6e2dc7 MV |
132 | if(CircleBuf::BwReadLimit) { |
133 | Res = read(Fd,Buf + (InP%Size), | |
134 | BwReadMax > LeftRead() ? LeftRead() : BwReadMax); | |
135 | } else | |
136 | Res = read(Fd,Buf + (InP%Size),LeftRead()); | |
be4401bf | 137 | |
7c6e2dc7 MV |
138 | if(Res > 0 && BwReadLimit > 0) |
139 | CircleBuf::BwTickReadData += Res; | |
140 | ||
be4401bf AL |
141 | if (Res == 0) |
142 | return false; | |
143 | if (Res < 0) | |
144 | { | |
145 | if (errno == EAGAIN) | |
146 | return true; | |
147 | return false; | |
148 | } | |
149 | ||
150 | if (InP == 0) | |
151 | gettimeofday(&Start,0); | |
152 | InP += Res; | |
153 | } | |
154 | } | |
155 | /*}}}*/ | |
156 | // CircleBuf::Read - Put the string into the buffer /*{{{*/ | |
157 | // --------------------------------------------------------------------- | |
158 | /* This will hold the string in and fill the buffer with it as it empties */ | |
159 | bool CircleBuf::Read(string Data) | |
160 | { | |
161 | OutQueue += Data; | |
162 | FillOut(); | |
163 | return true; | |
164 | } | |
165 | /*}}}*/ | |
166 | // CircleBuf::FillOut - Fill the buffer from the output queue /*{{{*/ | |
167 | // --------------------------------------------------------------------- | |
168 | /* */ | |
169 | void CircleBuf::FillOut() | |
170 | { | |
171 | if (OutQueue.empty() == true) | |
172 | return; | |
173 | while (1) | |
174 | { | |
175 | // Woops, buffer is full | |
176 | if (InP - OutP == Size) | |
177 | return; | |
178 | ||
179 | // Write the buffer segment | |
180 | unsigned long Sz = LeftRead(); | |
181 | if (OutQueue.length() - StrPos < Sz) | |
182 | Sz = OutQueue.length() - StrPos; | |
42195eb2 | 183 | memcpy(Buf + (InP%Size),OutQueue.c_str() + StrPos,Sz); |
be4401bf AL |
184 | |
185 | // Advance | |
186 | StrPos += Sz; | |
187 | InP += Sz; | |
188 | if (OutQueue.length() == StrPos) | |
189 | { | |
190 | StrPos = 0; | |
191 | OutQueue = ""; | |
192 | return; | |
193 | } | |
194 | } | |
195 | } | |
196 | /*}}}*/ | |
197 | // CircleBuf::Write - Write from the buffer into a FD /*{{{*/ | |
198 | // --------------------------------------------------------------------- | |
199 | /* This empties the buffer into the FD. */ | |
200 | bool CircleBuf::Write(int Fd) | |
201 | { | |
202 | while (1) | |
203 | { | |
204 | FillOut(); | |
205 | ||
206 | // Woops, buffer is empty | |
207 | if (OutP == InP) | |
208 | return true; | |
209 | ||
210 | if (OutP == MaxGet) | |
211 | return true; | |
212 | ||
213 | // Write the buffer segment | |
214 | int Res; | |
215 | Res = write(Fd,Buf + (OutP%Size),LeftWrite()); | |
216 | ||
217 | if (Res == 0) | |
218 | return false; | |
219 | if (Res < 0) | |
220 | { | |
221 | if (errno == EAGAIN) | |
222 | return true; | |
223 | ||
224 | return false; | |
225 | } | |
226 | ||
63b1700f AL |
227 | if (Hash != 0) |
228 | Hash->Add(Buf + (OutP%Size),Res); | |
be4401bf AL |
229 | |
230 | OutP += Res; | |
231 | } | |
232 | } | |
233 | /*}}}*/ | |
234 | // CircleBuf::WriteTillEl - Write from the buffer to a string /*{{{*/ | |
235 | // --------------------------------------------------------------------- | |
236 | /* This copies till the first empty line */ | |
237 | bool CircleBuf::WriteTillEl(string &Data,bool Single) | |
238 | { | |
239 | // We cheat and assume it is unneeded to have more than one buffer load | |
240 | for (unsigned long I = OutP; I < InP; I++) | |
241 | { | |
242 | if (Buf[I%Size] != '\n') | |
243 | continue; | |
2cbcabd8 | 244 | ++I; |
be4401bf AL |
245 | |
246 | if (Single == false) | |
247 | { | |
2cbcabd8 AL |
248 | if (I < InP && Buf[I%Size] == '\r') |
249 | ++I; | |
927c393f MV |
250 | if (I >= InP || Buf[I%Size] != '\n') |
251 | continue; | |
252 | ++I; | |
be4401bf AL |
253 | } |
254 | ||
be4401bf AL |
255 | Data = ""; |
256 | while (OutP < I) | |
257 | { | |
258 | unsigned long Sz = LeftWrite(); | |
259 | if (Sz == 0) | |
260 | return false; | |
927c393f | 261 | if (I - OutP < Sz) |
be4401bf AL |
262 | Sz = I - OutP; |
263 | Data += string((char *)(Buf + (OutP%Size)),Sz); | |
264 | OutP += Sz; | |
265 | } | |
266 | return true; | |
267 | } | |
268 | return false; | |
269 | } | |
270 | /*}}}*/ | |
271 | // CircleBuf::Stats - Print out stats information /*{{{*/ | |
272 | // --------------------------------------------------------------------- | |
273 | /* */ | |
274 | void CircleBuf::Stats() | |
275 | { | |
276 | if (InP == 0) | |
277 | return; | |
278 | ||
279 | struct timeval Stop; | |
280 | gettimeofday(&Stop,0); | |
281 | /* float Diff = Stop.tv_sec - Start.tv_sec + | |
282 | (float)(Stop.tv_usec - Start.tv_usec)/1000000; | |
283 | clog << "Got " << InP << " in " << Diff << " at " << InP/Diff << endl;*/ | |
284 | } | |
285 | /*}}}*/ | |
286 | ||
287 | // ServerState::ServerState - Constructor /*{{{*/ | |
288 | // --------------------------------------------------------------------- | |
289 | /* */ | |
290 | ServerState::ServerState(URI Srv,HttpMethod *Owner) : Owner(Owner), | |
3000ccea | 291 | In(64*1024), Out(4*1024), |
be4401bf AL |
292 | ServerName(Srv) |
293 | { | |
294 | Reset(); | |
295 | } | |
296 | /*}}}*/ | |
297 | // ServerState::Open - Open a connection to the server /*{{{*/ | |
298 | // --------------------------------------------------------------------- | |
299 | /* This opens a connection to the server. */ | |
be4401bf AL |
300 | bool ServerState::Open() |
301 | { | |
92e889c8 AL |
302 | // Use the already open connection if possible. |
303 | if (ServerFd != -1) | |
304 | return true; | |
305 | ||
be4401bf | 306 | Close(); |
492f957a AL |
307 | In.Reset(); |
308 | Out.Reset(); | |
e836f356 AL |
309 | Persistent = true; |
310 | ||
492f957a | 311 | // Determine the proxy setting |
52e7839a | 312 | if (getenv("http_proxy") == 0) |
492f957a | 313 | { |
352c2768 AL |
314 | string DefProxy = _config->Find("Acquire::http::Proxy"); |
315 | string SpecificProxy = _config->Find("Acquire::http::Proxy::" + ServerName.Host); | |
316 | if (SpecificProxy.empty() == false) | |
317 | { | |
318 | if (SpecificProxy == "DIRECT") | |
319 | Proxy = ""; | |
320 | else | |
321 | Proxy = SpecificProxy; | |
322 | } | |
492f957a | 323 | else |
352c2768 AL |
324 | Proxy = DefProxy; |
325 | } | |
492f957a | 326 | else |
352c2768 AL |
327 | Proxy = getenv("http_proxy"); |
328 | ||
f8081133 | 329 | // Parse no_proxy, a , separated list of domains |
9e2a06ff AL |
330 | if (getenv("no_proxy") != 0) |
331 | { | |
f8081133 AL |
332 | if (CheckDomainList(ServerName.Host,getenv("no_proxy")) == true) |
333 | Proxy = ""; | |
334 | } | |
335 | ||
492f957a | 336 | // Determine what host and port to use based on the proxy settings |
934b6582 | 337 | int Port = 0; |
492f957a | 338 | string Host; |
dd1fd92b | 339 | if (Proxy.empty() == true || Proxy.Host.empty() == true) |
be4401bf | 340 | { |
92e889c8 AL |
341 | if (ServerName.Port != 0) |
342 | Port = ServerName.Port; | |
be4401bf AL |
343 | Host = ServerName.Host; |
344 | } | |
345 | else | |
346 | { | |
92e889c8 AL |
347 | if (Proxy.Port != 0) |
348 | Port = Proxy.Port; | |
be4401bf AL |
349 | Host = Proxy.Host; |
350 | } | |
351 | ||
0837bd25 | 352 | // Connect to the remote server |
9505213b | 353 | if (Connect(Host,Port,"http",80,ServerFd,TimeOut,Owner) == false) |
0837bd25 | 354 | return false; |
3000ccea | 355 | |
be4401bf AL |
356 | return true; |
357 | } | |
358 | /*}}}*/ | |
359 | // ServerState::Close - Close a connection to the server /*{{{*/ | |
360 | // --------------------------------------------------------------------- | |
361 | /* */ | |
362 | bool ServerState::Close() | |
363 | { | |
364 | close(ServerFd); | |
365 | ServerFd = -1; | |
be4401bf AL |
366 | return true; |
367 | } | |
368 | /*}}}*/ | |
369 | // ServerState::RunHeaders - Get the headers before the data /*{{{*/ | |
370 | // --------------------------------------------------------------------- | |
92e889c8 AL |
371 | /* Returns 0 if things are OK, 1 if an IO error occursed and 2 if a header |
372 | parse error occured */ | |
373 | int ServerState::RunHeaders() | |
be4401bf AL |
374 | { |
375 | State = Header; | |
376 | ||
519c5591 | 377 | Owner->Status(_("Waiting for headers")); |
be4401bf AL |
378 | |
379 | Major = 0; | |
380 | Minor = 0; | |
381 | Result = 0; | |
382 | Size = 0; | |
383 | StartPos = 0; | |
92e889c8 AL |
384 | Encoding = Closes; |
385 | HaveContent = false; | |
be4401bf AL |
386 | time(&Date); |
387 | ||
388 | do | |
389 | { | |
390 | string Data; | |
391 | if (In.WriteTillEl(Data) == false) | |
392 | continue; | |
9d95e726 AL |
393 | |
394 | if (Debug == true) | |
395 | clog << Data; | |
be4401bf AL |
396 | |
397 | for (string::const_iterator I = Data.begin(); I < Data.end(); I++) | |
398 | { | |
399 | string::const_iterator J = I; | |
400 | for (; J != Data.end() && *J != '\n' && *J != '\r';J++); | |
42195eb2 | 401 | if (HeaderLine(string(I,J)) == false) |
92e889c8 | 402 | return 2; |
be4401bf AL |
403 | I = J; |
404 | } | |
e836f356 | 405 | |
b2e465d6 AL |
406 | // 100 Continue is a Nop... |
407 | if (Result == 100) | |
408 | continue; | |
409 | ||
e836f356 AL |
410 | // Tidy up the connection persistance state. |
411 | if (Encoding == Closes && HaveContent == true) | |
412 | Persistent = false; | |
413 | ||
92e889c8 | 414 | return 0; |
be4401bf AL |
415 | } |
416 | while (Owner->Go(false,this) == true); | |
e836f356 | 417 | |
92e889c8 | 418 | return 1; |
be4401bf AL |
419 | } |
420 | /*}}}*/ | |
421 | // ServerState::RunData - Transfer the data from the socket /*{{{*/ | |
422 | // --------------------------------------------------------------------- | |
423 | /* */ | |
424 | bool ServerState::RunData() | |
425 | { | |
426 | State = Data; | |
427 | ||
428 | // Chunked transfer encoding is fun.. | |
429 | if (Encoding == Chunked) | |
430 | { | |
431 | while (1) | |
432 | { | |
433 | // Grab the block size | |
434 | bool Last = true; | |
435 | string Data; | |
436 | In.Limit(-1); | |
437 | do | |
438 | { | |
439 | if (In.WriteTillEl(Data,true) == true) | |
440 | break; | |
441 | } | |
442 | while ((Last = Owner->Go(false,this)) == true); | |
443 | ||
444 | if (Last == false) | |
445 | return false; | |
446 | ||
447 | // See if we are done | |
448 | unsigned long Len = strtol(Data.c_str(),0,16); | |
449 | if (Len == 0) | |
450 | { | |
451 | In.Limit(-1); | |
452 | ||
453 | // We have to remove the entity trailer | |
454 | Last = true; | |
455 | do | |
456 | { | |
457 | if (In.WriteTillEl(Data,true) == true && Data.length() <= 2) | |
458 | break; | |
459 | } | |
460 | while ((Last = Owner->Go(false,this)) == true); | |
461 | if (Last == false) | |
462 | return false; | |
e1b96638 | 463 | return !_error->PendingError(); |
be4401bf AL |
464 | } |
465 | ||
466 | // Transfer the block | |
467 | In.Limit(Len); | |
468 | while (Owner->Go(true,this) == true) | |
469 | if (In.IsLimit() == true) | |
470 | break; | |
471 | ||
472 | // Error | |
473 | if (In.IsLimit() == false) | |
474 | return false; | |
475 | ||
476 | // The server sends an extra new line before the next block specifier.. | |
477 | In.Limit(-1); | |
478 | Last = true; | |
479 | do | |
480 | { | |
481 | if (In.WriteTillEl(Data,true) == true) | |
482 | break; | |
483 | } | |
484 | while ((Last = Owner->Go(false,this)) == true); | |
485 | if (Last == false) | |
486 | return false; | |
92e889c8 | 487 | } |
be4401bf AL |
488 | } |
489 | else | |
490 | { | |
491 | /* Closes encoding is used when the server did not specify a size, the | |
492 | loss of the connection means we are done */ | |
493 | if (Encoding == Closes) | |
494 | In.Limit(-1); | |
495 | else | |
496 | In.Limit(Size - StartPos); | |
497 | ||
498 | // Just transfer the whole block. | |
499 | do | |
500 | { | |
501 | if (In.IsLimit() == false) | |
502 | continue; | |
503 | ||
504 | In.Limit(-1); | |
e1b96638 | 505 | return !_error->PendingError(); |
be4401bf AL |
506 | } |
507 | while (Owner->Go(true,this) == true); | |
508 | } | |
509 | ||
e1b96638 | 510 | return Owner->Flush(this) && !_error->PendingError(); |
be4401bf AL |
511 | } |
512 | /*}}}*/ | |
513 | // ServerState::HeaderLine - Process a header line /*{{{*/ | |
514 | // --------------------------------------------------------------------- | |
515 | /* */ | |
516 | bool ServerState::HeaderLine(string Line) | |
517 | { | |
518 | if (Line.empty() == true) | |
519 | return true; | |
30456e14 | 520 | |
be4401bf AL |
521 | // The http server might be trying to do something evil. |
522 | if (Line.length() >= MAXLEN) | |
dc738e7a | 523 | return _error->Error(_("Got a single header line over %u chars"),MAXLEN); |
be4401bf AL |
524 | |
525 | string::size_type Pos = Line.find(' '); | |
526 | if (Pos == string::npos || Pos+1 > Line.length()) | |
c901051d AL |
527 | { |
528 | // Blah, some servers use "connection:closes", evil. | |
529 | Pos = Line.find(':'); | |
530 | if (Pos == string::npos || Pos + 2 > Line.length()) | |
dc738e7a | 531 | return _error->Error(_("Bad header line")); |
c901051d AL |
532 | Pos++; |
533 | } | |
be4401bf | 534 | |
c901051d AL |
535 | // Parse off any trailing spaces between the : and the next word. |
536 | string::size_type Pos2 = Pos; | |
537 | while (Pos2 < Line.length() && isspace(Line[Pos2]) != 0) | |
538 | Pos2++; | |
539 | ||
540 | string Tag = string(Line,0,Pos); | |
541 | string Val = string(Line,Pos2); | |
542 | ||
42195eb2 | 543 | if (stringcasecmp(Tag.c_str(),Tag.c_str()+4,"HTTP") == 0) |
be4401bf AL |
544 | { |
545 | // Evil servers return no version | |
546 | if (Line[4] == '/') | |
547 | { | |
548 | if (sscanf(Line.c_str(),"HTTP/%u.%u %u %[^\n]",&Major,&Minor, | |
549 | &Result,Code) != 4) | |
db0db9fe | 550 | return _error->Error(_("The HTTP server sent an invalid reply header")); |
be4401bf AL |
551 | } |
552 | else | |
553 | { | |
554 | Major = 0; | |
555 | Minor = 9; | |
556 | if (sscanf(Line.c_str(),"HTTP %u %[^\n]",&Result,Code) != 2) | |
db0db9fe | 557 | return _error->Error(_("The HTTP server sent an invalid reply header")); |
be4401bf | 558 | } |
e836f356 AL |
559 | |
560 | /* Check the HTTP response header to get the default persistance | |
561 | state. */ | |
562 | if (Major < 1) | |
563 | Persistent = false; | |
564 | else | |
565 | { | |
566 | if (Major == 1 && Minor <= 0) | |
567 | Persistent = false; | |
568 | else | |
569 | Persistent = true; | |
570 | } | |
b2e465d6 | 571 | |
be4401bf AL |
572 | return true; |
573 | } | |
574 | ||
92e889c8 | 575 | if (stringcasecmp(Tag,"Content-Length:") == 0) |
be4401bf AL |
576 | { |
577 | if (Encoding == Closes) | |
578 | Encoding = Stream; | |
92e889c8 | 579 | HaveContent = true; |
be4401bf AL |
580 | |
581 | // The length is already set from the Content-Range header | |
582 | if (StartPos != 0) | |
583 | return true; | |
584 | ||
585 | if (sscanf(Val.c_str(),"%lu",&Size) != 1) | |
db0db9fe | 586 | return _error->Error(_("The HTTP server sent an invalid Content-Length header")); |
be4401bf AL |
587 | return true; |
588 | } | |
589 | ||
92e889c8 AL |
590 | if (stringcasecmp(Tag,"Content-Type:") == 0) |
591 | { | |
592 | HaveContent = true; | |
593 | return true; | |
594 | } | |
595 | ||
596 | if (stringcasecmp(Tag,"Content-Range:") == 0) | |
be4401bf | 597 | { |
92e889c8 AL |
598 | HaveContent = true; |
599 | ||
be4401bf | 600 | if (sscanf(Val.c_str(),"bytes %lu-%*u/%lu",&StartPos,&Size) != 2) |
db0db9fe | 601 | return _error->Error(_("The HTTP server sent an invalid Content-Range header")); |
be4401bf | 602 | if ((unsigned)StartPos > Size) |
db0db9fe | 603 | return _error->Error(_("This HTTP server has broken range support")); |
be4401bf AL |
604 | return true; |
605 | } | |
606 | ||
92e889c8 | 607 | if (stringcasecmp(Tag,"Transfer-Encoding:") == 0) |
be4401bf | 608 | { |
92e889c8 AL |
609 | HaveContent = true; |
610 | if (stringcasecmp(Val,"chunked") == 0) | |
e836f356 | 611 | Encoding = Chunked; |
be4401bf AL |
612 | return true; |
613 | } | |
614 | ||
e836f356 AL |
615 | if (stringcasecmp(Tag,"Connection:") == 0) |
616 | { | |
617 | if (stringcasecmp(Val,"close") == 0) | |
618 | Persistent = false; | |
619 | if (stringcasecmp(Val,"keep-alive") == 0) | |
620 | Persistent = true; | |
621 | return true; | |
622 | } | |
623 | ||
92e889c8 | 624 | if (stringcasecmp(Tag,"Last-Modified:") == 0) |
be4401bf AL |
625 | { |
626 | if (StrToTime(Val,Date) == false) | |
dc738e7a | 627 | return _error->Error(_("Unknown date format")); |
be4401bf AL |
628 | return true; |
629 | } | |
630 | ||
631 | return true; | |
632 | } | |
633 | /*}}}*/ | |
634 | ||
635 | // HttpMethod::SendReq - Send the HTTP request /*{{{*/ | |
636 | // --------------------------------------------------------------------- | |
637 | /* This places the http request in the outbound buffer */ | |
638 | void HttpMethod::SendReq(FetchItem *Itm,CircleBuf &Out) | |
639 | { | |
640 | URI Uri = Itm->Uri; | |
c1a22377 | 641 | |
be4401bf | 642 | // The HTTP server expects a hostname with a trailing :port |
c1a22377 | 643 | char Buf[1000]; |
be4401bf AL |
644 | string ProperHost = Uri.Host; |
645 | if (Uri.Port != 0) | |
646 | { | |
647 | sprintf(Buf,":%u",Uri.Port); | |
648 | ProperHost += Buf; | |
649 | } | |
650 | ||
c1a22377 AL |
651 | // Just in case. |
652 | if (Itm->Uri.length() >= sizeof(Buf)) | |
653 | abort(); | |
654 | ||
492f957a AL |
655 | /* Build the request. We include a keep-alive header only for non-proxy |
656 | requests. This is to tweak old http/1.0 servers that do support keep-alive | |
657 | but not HTTP/1.1 automatic keep-alive. Doing this with a proxy server | |
658 | will glitch HTTP/1.0 proxies because they do not filter it out and | |
659 | pass it on, HTTP/1.1 says the connection should default to keep alive | |
660 | and we expect the proxy to do this */ | |
02b7ddb1 | 661 | if (Proxy.empty() == true || Proxy.Host.empty()) |
be4401bf | 662 | sprintf(Buf,"GET %s HTTP/1.1\r\nHost: %s\r\nConnection: keep-alive\r\n", |
a4edf53b | 663 | QuoteString(Uri.Path,"~").c_str(),ProperHost.c_str()); |
be4401bf | 664 | else |
c1a22377 AL |
665 | { |
666 | /* Generate a cache control header if necessary. We place a max | |
667 | cache age on index files, optionally set a no-cache directive | |
668 | and a no-store directive for archives. */ | |
be4401bf AL |
669 | sprintf(Buf,"GET %s HTTP/1.1\r\nHost: %s\r\n", |
670 | Itm->Uri.c_str(),ProperHost.c_str()); | |
106e6740 MV |
671 | // only generate a cache control header if we actually want to |
672 | // use a cache | |
673 | if (_config->FindB("Acquire::http::No-Cache",false) == false) | |
c1a22377 AL |
674 | { |
675 | if (Itm->IndexFile == true) | |
676 | sprintf(Buf+strlen(Buf),"Cache-Control: max-age=%u\r\n", | |
bcbe61ae | 677 | _config->FindI("Acquire::http::Max-Age",0)); |
c1a22377 AL |
678 | else |
679 | { | |
680 | if (_config->FindB("Acquire::http::No-Store",false) == true) | |
681 | strcat(Buf,"Cache-Control: no-store\r\n"); | |
682 | } | |
683 | } | |
684 | } | |
106e6740 MV |
685 | // generate a no-cache header if needed |
686 | if (_config->FindB("Acquire::http::No-Cache",false) == true) | |
687 | strcat(Buf,"Cache-Control: no-cache\r\nPragma: no-cache\r\n"); | |
688 | ||
c1a22377 | 689 | |
be4401bf | 690 | string Req = Buf; |
492f957a | 691 | |
be4401bf AL |
692 | // Check for a partial file |
693 | struct stat SBuf; | |
694 | if (stat(Itm->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0) | |
695 | { | |
696 | // In this case we send an if-range query with a range header | |
1ae93c94 | 697 | sprintf(Buf,"Range: bytes=%li-\r\nIf-Range: %s\r\n",(long)SBuf.st_size - 1, |
be4401bf AL |
698 | TimeRFC1123(SBuf.st_mtime).c_str()); |
699 | Req += Buf; | |
700 | } | |
701 | else | |
702 | { | |
703 | if (Itm->LastModified != 0) | |
704 | { | |
705 | sprintf(Buf,"If-Modified-Since: %s\r\n",TimeRFC1123(Itm->LastModified).c_str()); | |
706 | Req += Buf; | |
707 | } | |
708 | } | |
709 | ||
8d64c395 AL |
710 | if (Proxy.User.empty() == false || Proxy.Password.empty() == false) |
711 | Req += string("Proxy-Authorization: Basic ") + | |
712 | Base64Encode(Proxy.User + ":" + Proxy.Password) + "\r\n"; | |
be4401bf | 713 | |
b2e465d6 AL |
714 | if (Uri.User.empty() == false || Uri.Password.empty() == false) |
715 | Req += string("Authorization: Basic ") + | |
716 | Base64Encode(Uri.User + ":" + Uri.Password) + "\r\n"; | |
717 | ||
59b46c41 | 718 | Req += "User-Agent: Debian APT-HTTP/1.3 ("VERSION")\r\n\r\n"; |
c98b1307 AL |
719 | |
720 | if (Debug == true) | |
721 | cerr << Req << endl; | |
c1a22377 | 722 | |
be4401bf AL |
723 | Out.Read(Req); |
724 | } | |
725 | /*}}}*/ | |
726 | // HttpMethod::Go - Run a single loop /*{{{*/ | |
727 | // --------------------------------------------------------------------- | |
728 | /* This runs the select loop over the server FDs, Output file FDs and | |
729 | stdin. */ | |
730 | bool HttpMethod::Go(bool ToFile,ServerState *Srv) | |
731 | { | |
732 | // Server has closed the connection | |
8195ae46 AL |
733 | if (Srv->ServerFd == -1 && (Srv->In.WriteSpace() == false || |
734 | ToFile == false)) | |
be4401bf AL |
735 | return false; |
736 | ||
d955fe80 | 737 | fd_set rfds,wfds; |
be4401bf AL |
738 | FD_ZERO(&rfds); |
739 | FD_ZERO(&wfds); | |
be4401bf | 740 | |
e836f356 AL |
741 | /* Add the server. We only send more requests if the connection will |
742 | be persisting */ | |
743 | if (Srv->Out.WriteSpace() == true && Srv->ServerFd != -1 | |
744 | && Srv->Persistent == true) | |
be4401bf | 745 | FD_SET(Srv->ServerFd,&wfds); |
e836f356 | 746 | if (Srv->In.ReadSpace() == true && Srv->ServerFd != -1) |
be4401bf AL |
747 | FD_SET(Srv->ServerFd,&rfds); |
748 | ||
749 | // Add the file | |
750 | int FileFD = -1; | |
751 | if (File != 0) | |
752 | FileFD = File->Fd(); | |
753 | ||
754 | if (Srv->In.WriteSpace() == true && ToFile == true && FileFD != -1) | |
755 | FD_SET(FileFD,&wfds); | |
756 | ||
757 | // Add stdin | |
758 | FD_SET(STDIN_FILENO,&rfds); | |
759 | ||
be4401bf AL |
760 | // Figure out the max fd |
761 | int MaxFd = FileFD; | |
762 | if (MaxFd < Srv->ServerFd) | |
763 | MaxFd = Srv->ServerFd; | |
8195ae46 | 764 | |
be4401bf AL |
765 | // Select |
766 | struct timeval tv; | |
3000ccea | 767 | tv.tv_sec = TimeOut; |
be4401bf AL |
768 | tv.tv_usec = 0; |
769 | int Res = 0; | |
d955fe80 | 770 | if ((Res = select(MaxFd+1,&rfds,&wfds,0,&tv)) < 0) |
c37b9502 AL |
771 | { |
772 | if (errno == EINTR) | |
773 | return true; | |
dc738e7a | 774 | return _error->Errno("select",_("Select failed")); |
c37b9502 | 775 | } |
be4401bf AL |
776 | |
777 | if (Res == 0) | |
778 | { | |
dc738e7a | 779 | _error->Error(_("Connection timed out")); |
be4401bf AL |
780 | return ServerDie(Srv); |
781 | } | |
782 | ||
be4401bf AL |
783 | // Handle server IO |
784 | if (Srv->ServerFd != -1 && FD_ISSET(Srv->ServerFd,&rfds)) | |
785 | { | |
786 | errno = 0; | |
787 | if (Srv->In.Read(Srv->ServerFd) == false) | |
788 | return ServerDie(Srv); | |
789 | } | |
790 | ||
791 | if (Srv->ServerFd != -1 && FD_ISSET(Srv->ServerFd,&wfds)) | |
792 | { | |
793 | errno = 0; | |
794 | if (Srv->Out.Write(Srv->ServerFd) == false) | |
795 | return ServerDie(Srv); | |
796 | } | |
797 | ||
798 | // Send data to the file | |
799 | if (FileFD != -1 && FD_ISSET(FileFD,&wfds)) | |
800 | { | |
801 | if (Srv->In.Write(FileFD) == false) | |
dc738e7a | 802 | return _error->Errno("write",_("Error writing to output file")); |
be4401bf AL |
803 | } |
804 | ||
805 | // Handle commands from APT | |
806 | if (FD_ISSET(STDIN_FILENO,&rfds)) | |
807 | { | |
6920216d | 808 | if (Run(true) != -1) |
be4401bf AL |
809 | exit(100); |
810 | } | |
811 | ||
812 | return true; | |
813 | } | |
814 | /*}}}*/ | |
815 | // HttpMethod::Flush - Dump the buffer into the file /*{{{*/ | |
816 | // --------------------------------------------------------------------- | |
817 | /* This takes the current input buffer from the Server FD and writes it | |
818 | into the file */ | |
819 | bool HttpMethod::Flush(ServerState *Srv) | |
820 | { | |
821 | if (File != 0) | |
822 | { | |
b57c8bb4 MV |
823 | // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking |
824 | // can't be set | |
825 | if (File->Name() != "/dev/null") | |
826 | SetNonBlock(File->Fd(),false); | |
be4401bf AL |
827 | if (Srv->In.WriteSpace() == false) |
828 | return true; | |
829 | ||
830 | while (Srv->In.WriteSpace() == true) | |
831 | { | |
832 | if (Srv->In.Write(File->Fd()) == false) | |
dc738e7a | 833 | return _error->Errno("write",_("Error writing to file")); |
92e889c8 AL |
834 | if (Srv->In.IsLimit() == true) |
835 | return true; | |
be4401bf AL |
836 | } |
837 | ||
838 | if (Srv->In.IsLimit() == true || Srv->Encoding == ServerState::Closes) | |
839 | return true; | |
840 | } | |
841 | return false; | |
842 | } | |
843 | /*}}}*/ | |
844 | // HttpMethod::ServerDie - The server has closed the connection. /*{{{*/ | |
845 | // --------------------------------------------------------------------- | |
846 | /* */ | |
847 | bool HttpMethod::ServerDie(ServerState *Srv) | |
848 | { | |
2b154e53 AL |
849 | unsigned int LErrno = errno; |
850 | ||
be4401bf AL |
851 | // Dump the buffer to the file |
852 | if (Srv->State == ServerState::Data) | |
853 | { | |
b57c8bb4 MV |
854 | // on GNU/kFreeBSD, apt dies on /dev/null because non-blocking |
855 | // can't be set | |
856 | if (File->Name() != "/dev/null") | |
857 | SetNonBlock(File->Fd(),false); | |
be4401bf AL |
858 | while (Srv->In.WriteSpace() == true) |
859 | { | |
860 | if (Srv->In.Write(File->Fd()) == false) | |
dc738e7a | 861 | return _error->Errno("write",_("Error writing to the file")); |
92e889c8 AL |
862 | |
863 | // Done | |
864 | if (Srv->In.IsLimit() == true) | |
865 | return true; | |
be4401bf AL |
866 | } |
867 | } | |
868 | ||
869 | // See if this is because the server finished the data stream | |
870 | if (Srv->In.IsLimit() == false && Srv->State != ServerState::Header && | |
871 | Srv->Encoding != ServerState::Closes) | |
872 | { | |
3d615484 | 873 | Srv->Close(); |
2b154e53 | 874 | if (LErrno == 0) |
db0db9fe | 875 | return _error->Error(_("Error reading from server. Remote end closed connection")); |
2b154e53 | 876 | errno = LErrno; |
dc738e7a | 877 | return _error->Errno("read",_("Error reading from server")); |
be4401bf AL |
878 | } |
879 | else | |
880 | { | |
881 | Srv->In.Limit(-1); | |
882 | ||
883 | // Nothing left in the buffer | |
884 | if (Srv->In.WriteSpace() == false) | |
885 | return false; | |
886 | ||
887 | // We may have got multiple responses back in one packet.. | |
888 | Srv->Close(); | |
889 | return true; | |
890 | } | |
891 | ||
892 | return false; | |
893 | } | |
894 | /*}}}*/ | |
895 | // HttpMethod::DealWithHeaders - Handle the retrieved header data /*{{{*/ | |
896 | // --------------------------------------------------------------------- | |
897 | /* We look at the header data we got back from the server and decide what | |
898 | to do. Returns | |
899 | 0 - File is open, | |
900 | 1 - IMS hit | |
92e889c8 | 901 | 3 - Unrecoverable error |
94235cfb AL |
902 | 4 - Error with error content page |
903 | 5 - Unrecoverable non-server error (close the connection) */ | |
be4401bf AL |
904 | int HttpMethod::DealWithHeaders(FetchResult &Res,ServerState *Srv) |
905 | { | |
906 | // Not Modified | |
907 | if (Srv->Result == 304) | |
908 | { | |
909 | unlink(Queue->DestFile.c_str()); | |
910 | Res.IMSHit = true; | |
911 | Res.LastModified = Queue->LastModified; | |
912 | return 1; | |
913 | } | |
914 | ||
915 | /* We have a reply we dont handle. This should indicate a perm server | |
916 | failure */ | |
917 | if (Srv->Result < 200 || Srv->Result >= 300) | |
918 | { | |
919 | _error->Error("%u %s",Srv->Result,Srv->Code); | |
92e889c8 AL |
920 | if (Srv->HaveContent == true) |
921 | return 4; | |
be4401bf AL |
922 | return 3; |
923 | } | |
924 | ||
925 | // This is some sort of 2xx 'data follows' reply | |
926 | Res.LastModified = Srv->Date; | |
927 | Res.Size = Srv->Size; | |
928 | ||
929 | // Open the file | |
930 | delete File; | |
931 | File = new FileFd(Queue->DestFile,FileFd::WriteAny); | |
932 | if (_error->PendingError() == true) | |
94235cfb | 933 | return 5; |
492f957a AL |
934 | |
935 | FailFile = Queue->DestFile; | |
30b30ec1 | 936 | FailFile.c_str(); // Make sure we dont do a malloc in the signal handler |
492f957a AL |
937 | FailFd = File->Fd(); |
938 | FailTime = Srv->Date; | |
939 | ||
be4401bf AL |
940 | // Set the expected size |
941 | if (Srv->StartPos >= 0) | |
942 | { | |
943 | Res.ResumePoint = Srv->StartPos; | |
944 | ftruncate(File->Fd(),Srv->StartPos); | |
945 | } | |
946 | ||
947 | // Set the start point | |
948 | lseek(File->Fd(),0,SEEK_END); | |
949 | ||
63b1700f AL |
950 | delete Srv->In.Hash; |
951 | Srv->In.Hash = new Hashes; | |
be4401bf | 952 | |
63b1700f | 953 | // Fill the Hash if the file is non-empty (resume) |
be4401bf AL |
954 | if (Srv->StartPos > 0) |
955 | { | |
956 | lseek(File->Fd(),0,SEEK_SET); | |
63b1700f | 957 | if (Srv->In.Hash->AddFD(File->Fd(),Srv->StartPos) == false) |
be4401bf | 958 | { |
dc738e7a | 959 | _error->Errno("read",_("Problem hashing file")); |
94235cfb | 960 | return 5; |
be4401bf AL |
961 | } |
962 | lseek(File->Fd(),0,SEEK_END); | |
963 | } | |
964 | ||
965 | SetNonBlock(File->Fd(),true); | |
966 | return 0; | |
967 | } | |
968 | /*}}}*/ | |
492f957a AL |
969 | // HttpMethod::SigTerm - Handle a fatal signal /*{{{*/ |
970 | // --------------------------------------------------------------------- | |
971 | /* This closes and timestamps the open file. This is neccessary to get | |
972 | resume behavoir on user abort */ | |
973 | void HttpMethod::SigTerm(int) | |
974 | { | |
975 | if (FailFd == -1) | |
ffe9323a | 976 | _exit(100); |
492f957a AL |
977 | close(FailFd); |
978 | ||
979 | // Timestamp | |
980 | struct utimbuf UBuf; | |
492f957a AL |
981 | UBuf.actime = FailTime; |
982 | UBuf.modtime = FailTime; | |
983 | utime(FailFile.c_str(),&UBuf); | |
984 | ||
ffe9323a | 985 | _exit(100); |
492f957a AL |
986 | } |
987 | /*}}}*/ | |
5cb5d8dc AL |
988 | // HttpMethod::Fetch - Fetch an item /*{{{*/ |
989 | // --------------------------------------------------------------------- | |
990 | /* This adds an item to the pipeline. We keep the pipeline at a fixed | |
991 | depth. */ | |
992 | bool HttpMethod::Fetch(FetchItem *) | |
993 | { | |
994 | if (Server == 0) | |
995 | return true; | |
3000ccea | 996 | |
5cb5d8dc AL |
997 | // Queue the requests |
998 | int Depth = -1; | |
f93d1355 AL |
999 | for (FetchItem *I = Queue; I != 0 && Depth < (signed)PipelineDepth; |
1000 | I = I->Next, Depth++) | |
5cb5d8dc | 1001 | { |
f93d1355 AL |
1002 | // If pipelining is disabled, we only queue 1 request |
1003 | if (Server->Pipeline == false && Depth >= 0) | |
1004 | break; | |
1005 | ||
5cb5d8dc AL |
1006 | // Make sure we stick with the same server |
1007 | if (Server->Comp(I->Uri) == false) | |
1008 | break; | |
5cb5d8dc | 1009 | if (QueueBack == I) |
5cb5d8dc | 1010 | { |
5cb5d8dc AL |
1011 | QueueBack = I->Next; |
1012 | SendReq(I,Server->Out); | |
1013 | continue; | |
f93d1355 | 1014 | } |
5cb5d8dc AL |
1015 | } |
1016 | ||
1017 | return true; | |
1018 | }; | |
1019 | /*}}}*/ | |
85f72a56 AL |
1020 | // HttpMethod::Configuration - Handle a configuration message /*{{{*/ |
1021 | // --------------------------------------------------------------------- | |
1022 | /* We stash the desired pipeline depth */ | |
1023 | bool HttpMethod::Configuration(string Message) | |
1024 | { | |
1025 | if (pkgAcqMethod::Configuration(Message) == false) | |
1026 | return false; | |
1027 | ||
30456e14 AL |
1028 | TimeOut = _config->FindI("Acquire::http::Timeout",TimeOut); |
1029 | PipelineDepth = _config->FindI("Acquire::http::Pipeline-Depth", | |
1030 | PipelineDepth); | |
c98b1307 | 1031 | Debug = _config->FindB("Debug::Acquire::http",false); |
3000ccea | 1032 | |
85f72a56 AL |
1033 | return true; |
1034 | } | |
1035 | /*}}}*/ | |
492f957a | 1036 | // HttpMethod::Loop - Main loop /*{{{*/ |
be4401bf AL |
1037 | // --------------------------------------------------------------------- |
1038 | /* */ | |
1039 | int HttpMethod::Loop() | |
1040 | { | |
492f957a AL |
1041 | signal(SIGTERM,SigTerm); |
1042 | signal(SIGINT,SigTerm); | |
1043 | ||
5cb5d8dc | 1044 | Server = 0; |
be4401bf | 1045 | |
92e889c8 | 1046 | int FailCounter = 0; |
be4401bf | 1047 | while (1) |
2b154e53 | 1048 | { |
be4401bf AL |
1049 | // We have no commands, wait for some to arrive |
1050 | if (Queue == 0) | |
1051 | { | |
1052 | if (WaitFd(STDIN_FILENO) == false) | |
1053 | return 0; | |
1054 | } | |
1055 | ||
6920216d AL |
1056 | /* Run messages, we can accept 0 (no message) if we didn't |
1057 | do a WaitFd above.. Otherwise the FD is closed. */ | |
1058 | int Result = Run(true); | |
1059 | if (Result != -1 && (Result != 0 || Queue == 0)) | |
be4401bf AL |
1060 | return 100; |
1061 | ||
1062 | if (Queue == 0) | |
1063 | continue; | |
1064 | ||
1065 | // Connect to the server | |
1066 | if (Server == 0 || Server->Comp(Queue->Uri) == false) | |
1067 | { | |
1068 | delete Server; | |
1069 | Server = new ServerState(Queue->Uri,this); | |
1070 | } | |
e836f356 AL |
1071 | /* If the server has explicitly said this is the last connection |
1072 | then we pre-emptively shut down the pipeline and tear down | |
1073 | the connection. This will speed up HTTP/1.0 servers a tad | |
1074 | since we don't have to wait for the close sequence to | |
1075 | complete */ | |
1076 | if (Server->Persistent == false) | |
1077 | Server->Close(); | |
1078 | ||
a7fb252c AL |
1079 | // Reset the pipeline |
1080 | if (Server->ServerFd == -1) | |
1081 | QueueBack = Queue; | |
1082 | ||
be4401bf AL |
1083 | // Connnect to the host |
1084 | if (Server->Open() == false) | |
1085 | { | |
43252d15 | 1086 | Fail(true); |
a1459f52 AL |
1087 | delete Server; |
1088 | Server = 0; | |
be4401bf AL |
1089 | continue; |
1090 | } | |
be4401bf | 1091 | |
5cb5d8dc AL |
1092 | // Fill the pipeline. |
1093 | Fetch(0); | |
1094 | ||
92e889c8 AL |
1095 | // Fetch the next URL header data from the server. |
1096 | switch (Server->RunHeaders()) | |
be4401bf | 1097 | { |
92e889c8 AL |
1098 | case 0: |
1099 | break; | |
1100 | ||
1101 | // The header data is bad | |
1102 | case 2: | |
1103 | { | |
db0db9fe | 1104 | _error->Error(_("Bad header data")); |
43252d15 | 1105 | Fail(true); |
b2e465d6 | 1106 | RotateDNS(); |
92e889c8 AL |
1107 | continue; |
1108 | } | |
1109 | ||
1110 | // The server closed a connection during the header get.. | |
1111 | default: | |
1112 | case 1: | |
1113 | { | |
1114 | FailCounter++; | |
3d615484 | 1115 | _error->Discard(); |
92e889c8 | 1116 | Server->Close(); |
f93d1355 AL |
1117 | Server->Pipeline = false; |
1118 | ||
2b154e53 AL |
1119 | if (FailCounter >= 2) |
1120 | { | |
dc738e7a | 1121 | Fail(_("Connection failed"),true); |
2b154e53 AL |
1122 | FailCounter = 0; |
1123 | } | |
1124 | ||
b2e465d6 | 1125 | RotateDNS(); |
92e889c8 AL |
1126 | continue; |
1127 | } | |
1128 | }; | |
5cb5d8dc | 1129 | |
be4401bf AL |
1130 | // Decide what to do. |
1131 | FetchResult Res; | |
bfd22fc0 | 1132 | Res.Filename = Queue->DestFile; |
be4401bf AL |
1133 | switch (DealWithHeaders(Res,Server)) |
1134 | { | |
1135 | // Ok, the file is Open | |
1136 | case 0: | |
1137 | { | |
1138 | URIStart(Res); | |
1139 | ||
1140 | // Run the data | |
492f957a AL |
1141 | bool Result = Server->RunData(); |
1142 | ||
b2e465d6 AL |
1143 | /* If the server is sending back sizeless responses then fill in |
1144 | the size now */ | |
1145 | if (Res.Size == 0) | |
1146 | Res.Size = File->Size(); | |
1147 | ||
492f957a AL |
1148 | // Close the file, destroy the FD object and timestamp it |
1149 | FailFd = -1; | |
1150 | delete File; | |
1151 | File = 0; | |
1152 | ||
1153 | // Timestamp | |
1154 | struct utimbuf UBuf; | |
1155 | time(&UBuf.actime); | |
1156 | UBuf.actime = Server->Date; | |
1157 | UBuf.modtime = Server->Date; | |
1158 | utime(Queue->DestFile.c_str(),&UBuf); | |
1159 | ||
1160 | // Send status to APT | |
1161 | if (Result == true) | |
92e889c8 | 1162 | { |
a7c835af | 1163 | Res.TakeHashes(*Server->In.Hash); |
92e889c8 AL |
1164 | URIDone(Res); |
1165 | } | |
492f957a | 1166 | else |
9a52beaa OS |
1167 | { |
1168 | if (Server->ServerFd == -1) | |
1169 | { | |
1170 | FailCounter++; | |
1171 | _error->Discard(); | |
1172 | Server->Close(); | |
e836f356 | 1173 | |
9a52beaa OS |
1174 | if (FailCounter >= 2) |
1175 | { | |
1176 | Fail(_("Connection failed"),true); | |
1177 | FailCounter = 0; | |
1178 | } | |
1179 | ||
1180 | QueueBack = Queue; | |
1181 | } | |
1182 | else | |
1183 | Fail(true); | |
1184 | } | |
1185 | ||
be4401bf AL |
1186 | break; |
1187 | } | |
1188 | ||
1189 | // IMS hit | |
1190 | case 1: | |
1191 | { | |
1192 | URIDone(Res); | |
1193 | break; | |
1194 | } | |
1195 | ||
1196 | // Hard server error, not found or something | |
1197 | case 3: | |
1198 | { | |
1199 | Fail(); | |
1200 | break; | |
1201 | } | |
94235cfb AL |
1202 | |
1203 | // Hard internal error, kill the connection and fail | |
1204 | case 5: | |
1205 | { | |
a305f593 AL |
1206 | delete File; |
1207 | File = 0; | |
1208 | ||
94235cfb | 1209 | Fail(); |
b2e465d6 | 1210 | RotateDNS(); |
94235cfb AL |
1211 | Server->Close(); |
1212 | break; | |
1213 | } | |
92e889c8 AL |
1214 | |
1215 | // We need to flush the data, the header is like a 404 w/ error text | |
1216 | case 4: | |
1217 | { | |
1218 | Fail(); | |
1219 | ||
1220 | // Send to content to dev/null | |
1221 | File = new FileFd("/dev/null",FileFd::WriteExists); | |
1222 | Server->RunData(); | |
1223 | delete File; | |
1224 | File = 0; | |
1225 | break; | |
1226 | } | |
be4401bf AL |
1227 | |
1228 | default: | |
dc738e7a | 1229 | Fail(_("Internal error")); |
be4401bf | 1230 | break; |
92e889c8 AL |
1231 | } |
1232 | ||
1233 | FailCounter = 0; | |
be4401bf AL |
1234 | } |
1235 | ||
1236 | return 0; | |
1237 | } | |
1238 | /*}}}*/ | |
1239 | ||
1240 | int main() | |
1241 | { | |
049c0171 | 1242 | setlocale(LC_ALL, ""); |
049c0171 | 1243 | |
be4401bf AL |
1244 | HttpMethod Mth; |
1245 | ||
1246 | return Mth.Loop(); | |
1247 | } | |
a305f593 AL |
1248 | |
1249 |