]>
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 | ||
6 | HTTP Aquire Method - This is the HTTP aquire method for APT. | |
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; | |
3000ccea AL |
58 | unsigned long PipelineDepth = 10; |
59 | unsigned long TimeOut = 120; | |
c98b1307 | 60 | bool Debug = false; |
492f957a | 61 | |
7c6e2dc7 MV |
62 | |
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 | ||
098d7904 | 718 | Req += "User-Agent: Ubuntu 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; | |
999 | bool Tail = false; | |
f93d1355 AL |
1000 | for (FetchItem *I = Queue; I != 0 && Depth < (signed)PipelineDepth; |
1001 | I = I->Next, Depth++) | |
5cb5d8dc | 1002 | { |
f93d1355 AL |
1003 | // If pipelining is disabled, we only queue 1 request |
1004 | if (Server->Pipeline == false && Depth >= 0) | |
1005 | break; | |
1006 | ||
5cb5d8dc AL |
1007 | // Make sure we stick with the same server |
1008 | if (Server->Comp(I->Uri) == false) | |
1009 | break; | |
5cb5d8dc AL |
1010 | if (QueueBack == I) |
1011 | Tail = true; | |
1012 | if (Tail == true) | |
1013 | { | |
5cb5d8dc AL |
1014 | QueueBack = I->Next; |
1015 | SendReq(I,Server->Out); | |
1016 | continue; | |
f93d1355 | 1017 | } |
5cb5d8dc AL |
1018 | } |
1019 | ||
1020 | return true; | |
1021 | }; | |
1022 | /*}}}*/ | |
85f72a56 AL |
1023 | // HttpMethod::Configuration - Handle a configuration message /*{{{*/ |
1024 | // --------------------------------------------------------------------- | |
1025 | /* We stash the desired pipeline depth */ | |
1026 | bool HttpMethod::Configuration(string Message) | |
1027 | { | |
1028 | if (pkgAcqMethod::Configuration(Message) == false) | |
1029 | return false; | |
1030 | ||
30456e14 AL |
1031 | TimeOut = _config->FindI("Acquire::http::Timeout",TimeOut); |
1032 | PipelineDepth = _config->FindI("Acquire::http::Pipeline-Depth", | |
1033 | PipelineDepth); | |
c98b1307 | 1034 | Debug = _config->FindB("Debug::Acquire::http",false); |
3000ccea | 1035 | |
85f72a56 AL |
1036 | return true; |
1037 | } | |
1038 | /*}}}*/ | |
492f957a | 1039 | // HttpMethod::Loop - Main loop /*{{{*/ |
be4401bf AL |
1040 | // --------------------------------------------------------------------- |
1041 | /* */ | |
1042 | int HttpMethod::Loop() | |
1043 | { | |
492f957a AL |
1044 | signal(SIGTERM,SigTerm); |
1045 | signal(SIGINT,SigTerm); | |
1046 | ||
5cb5d8dc | 1047 | Server = 0; |
be4401bf | 1048 | |
92e889c8 | 1049 | int FailCounter = 0; |
be4401bf | 1050 | while (1) |
2b154e53 | 1051 | { |
be4401bf AL |
1052 | // We have no commands, wait for some to arrive |
1053 | if (Queue == 0) | |
1054 | { | |
1055 | if (WaitFd(STDIN_FILENO) == false) | |
1056 | return 0; | |
1057 | } | |
1058 | ||
6920216d AL |
1059 | /* Run messages, we can accept 0 (no message) if we didn't |
1060 | do a WaitFd above.. Otherwise the FD is closed. */ | |
1061 | int Result = Run(true); | |
1062 | if (Result != -1 && (Result != 0 || Queue == 0)) | |
be4401bf AL |
1063 | return 100; |
1064 | ||
1065 | if (Queue == 0) | |
1066 | continue; | |
1067 | ||
1068 | // Connect to the server | |
1069 | if (Server == 0 || Server->Comp(Queue->Uri) == false) | |
1070 | { | |
1071 | delete Server; | |
1072 | Server = new ServerState(Queue->Uri,this); | |
1073 | } | |
e836f356 AL |
1074 | |
1075 | /* If the server has explicitly said this is the last connection | |
1076 | then we pre-emptively shut down the pipeline and tear down | |
1077 | the connection. This will speed up HTTP/1.0 servers a tad | |
1078 | since we don't have to wait for the close sequence to | |
1079 | complete */ | |
1080 | if (Server->Persistent == false) | |
1081 | Server->Close(); | |
1082 | ||
a7fb252c AL |
1083 | // Reset the pipeline |
1084 | if (Server->ServerFd == -1) | |
1085 | QueueBack = Queue; | |
1086 | ||
be4401bf AL |
1087 | // Connnect to the host |
1088 | if (Server->Open() == false) | |
1089 | { | |
43252d15 | 1090 | Fail(true); |
a1459f52 AL |
1091 | delete Server; |
1092 | Server = 0; | |
be4401bf AL |
1093 | continue; |
1094 | } | |
be4401bf | 1095 | |
5cb5d8dc AL |
1096 | // Fill the pipeline. |
1097 | Fetch(0); | |
1098 | ||
92e889c8 AL |
1099 | // Fetch the next URL header data from the server. |
1100 | switch (Server->RunHeaders()) | |
be4401bf | 1101 | { |
92e889c8 AL |
1102 | case 0: |
1103 | break; | |
1104 | ||
1105 | // The header data is bad | |
1106 | case 2: | |
1107 | { | |
db0db9fe | 1108 | _error->Error(_("Bad header data")); |
43252d15 | 1109 | Fail(true); |
b2e465d6 | 1110 | RotateDNS(); |
92e889c8 AL |
1111 | continue; |
1112 | } | |
1113 | ||
1114 | // The server closed a connection during the header get.. | |
1115 | default: | |
1116 | case 1: | |
1117 | { | |
1118 | FailCounter++; | |
3d615484 | 1119 | _error->Discard(); |
92e889c8 | 1120 | Server->Close(); |
f93d1355 AL |
1121 | Server->Pipeline = false; |
1122 | ||
2b154e53 AL |
1123 | if (FailCounter >= 2) |
1124 | { | |
dc738e7a | 1125 | Fail(_("Connection failed"),true); |
2b154e53 AL |
1126 | FailCounter = 0; |
1127 | } | |
1128 | ||
b2e465d6 | 1129 | RotateDNS(); |
92e889c8 AL |
1130 | continue; |
1131 | } | |
1132 | }; | |
5cb5d8dc | 1133 | |
be4401bf AL |
1134 | // Decide what to do. |
1135 | FetchResult Res; | |
bfd22fc0 | 1136 | Res.Filename = Queue->DestFile; |
be4401bf AL |
1137 | switch (DealWithHeaders(Res,Server)) |
1138 | { | |
1139 | // Ok, the file is Open | |
1140 | case 0: | |
1141 | { | |
1142 | URIStart(Res); | |
1143 | ||
1144 | // Run the data | |
492f957a AL |
1145 | bool Result = Server->RunData(); |
1146 | ||
b2e465d6 AL |
1147 | /* If the server is sending back sizeless responses then fill in |
1148 | the size now */ | |
1149 | if (Res.Size == 0) | |
1150 | Res.Size = File->Size(); | |
1151 | ||
492f957a AL |
1152 | // Close the file, destroy the FD object and timestamp it |
1153 | FailFd = -1; | |
1154 | delete File; | |
1155 | File = 0; | |
1156 | ||
1157 | // Timestamp | |
1158 | struct utimbuf UBuf; | |
1159 | time(&UBuf.actime); | |
1160 | UBuf.actime = Server->Date; | |
1161 | UBuf.modtime = Server->Date; | |
1162 | utime(Queue->DestFile.c_str(),&UBuf); | |
1163 | ||
1164 | // Send status to APT | |
1165 | if (Result == true) | |
92e889c8 | 1166 | { |
a7c835af | 1167 | Res.TakeHashes(*Server->In.Hash); |
92e889c8 AL |
1168 | URIDone(Res); |
1169 | } | |
492f957a | 1170 | else |
2b154e53 | 1171 | Fail(true); |
e836f356 | 1172 | |
be4401bf AL |
1173 | break; |
1174 | } | |
1175 | ||
1176 | // IMS hit | |
1177 | case 1: | |
1178 | { | |
1179 | URIDone(Res); | |
1180 | break; | |
1181 | } | |
1182 | ||
1183 | // Hard server error, not found or something | |
1184 | case 3: | |
1185 | { | |
1186 | Fail(); | |
1187 | break; | |
1188 | } | |
94235cfb AL |
1189 | |
1190 | // Hard internal error, kill the connection and fail | |
1191 | case 5: | |
1192 | { | |
a305f593 AL |
1193 | delete File; |
1194 | File = 0; | |
1195 | ||
94235cfb | 1196 | Fail(); |
b2e465d6 | 1197 | RotateDNS(); |
94235cfb AL |
1198 | Server->Close(); |
1199 | break; | |
1200 | } | |
92e889c8 AL |
1201 | |
1202 | // We need to flush the data, the header is like a 404 w/ error text | |
1203 | case 4: | |
1204 | { | |
1205 | Fail(); | |
1206 | ||
1207 | // Send to content to dev/null | |
1208 | File = new FileFd("/dev/null",FileFd::WriteExists); | |
1209 | Server->RunData(); | |
1210 | delete File; | |
1211 | File = 0; | |
1212 | break; | |
1213 | } | |
be4401bf AL |
1214 | |
1215 | default: | |
dc738e7a | 1216 | Fail(_("Internal error")); |
be4401bf | 1217 | break; |
92e889c8 AL |
1218 | } |
1219 | ||
1220 | FailCounter = 0; | |
be4401bf AL |
1221 | } |
1222 | ||
1223 | return 0; | |
1224 | } | |
1225 | /*}}}*/ | |
1226 | ||
1227 | int main() | |
1228 | { | |
049c0171 | 1229 | setlocale(LC_ALL, ""); |
049c0171 | 1230 | |
be4401bf AL |
1231 | HttpMethod Mth; |
1232 | ||
1233 | return Mth.Loop(); | |
1234 | } | |
a305f593 AL |
1235 | |
1236 |