]>
Commit | Line | Data |
---|---|---|
7330f4df DK |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
3 | /* ###################################################################### | |
4 | ||
5 | HTTP and HTTPS share a lot of common code and these classes are | |
6 | exactly the dumping ground for this common code | |
7 | ||
8 | ##################################################################### */ | |
9 | /*}}}*/ | |
10 | // Include Files /*{{{*/ | |
11 | #include <config.h> | |
12 | ||
7330f4df DK |
13 | #include <apt-pkg/configuration.h> |
14 | #include <apt-pkg/error.h> | |
453b82a3 DK |
15 | #include <apt-pkg/fileutl.h> |
16 | #include <apt-pkg/strutl.h> | |
7330f4df | 17 | |
453b82a3 DK |
18 | #include <ctype.h> |
19 | #include <signal.h> | |
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
7330f4df DK |
22 | #include <sys/stat.h> |
23 | #include <sys/time.h> | |
453b82a3 | 24 | #include <time.h> |
7330f4df | 25 | #include <unistd.h> |
7330f4df | 26 | #include <iostream> |
453b82a3 | 27 | #include <limits> |
7330f4df | 28 | #include <map> |
453b82a3 DK |
29 | #include <string> |
30 | #include <vector> | |
7330f4df | 31 | |
453b82a3 | 32 | #include "server.h" |
7330f4df DK |
33 | |
34 | #include <apti18n.h> | |
35 | /*}}}*/ | |
36 | using namespace std; | |
37 | ||
38 | string ServerMethod::FailFile; | |
39 | int ServerMethod::FailFd = -1; | |
40 | time_t ServerMethod::FailTime = 0; | |
41 | ||
42 | // ServerState::RunHeaders - Get the headers before the data /*{{{*/ | |
43 | // --------------------------------------------------------------------- | |
44 | /* Returns 0 if things are OK, 1 if an IO error occurred and 2 if a header | |
45 | parse error occurred */ | |
9622b211 MV |
46 | ServerState::RunHeadersResult ServerState::RunHeaders(FileFd * const File, |
47 | const std::string &Uri) | |
7330f4df DK |
48 | { |
49 | State = Header; | |
50 | ||
51 | Owner->Status(_("Waiting for headers")); | |
52 | ||
53 | Major = 0; | |
54 | Minor = 0; | |
55 | Result = 0; | |
6291f60e | 56 | TotalFileSize = 0; |
ed793a19 | 57 | JunkSize = 0; |
7330f4df DK |
58 | StartPos = 0; |
59 | Encoding = Closes; | |
60 | HaveContent = false; | |
61 | time(&Date); | |
62 | ||
63 | do | |
64 | { | |
65 | string Data; | |
66 | if (ReadHeaderLines(Data) == false) | |
67 | continue; | |
68 | ||
69 | if (Owner->Debug == true) | |
9622b211 | 70 | clog << "Answer for: " << Uri << endl << Data; |
7330f4df DK |
71 | |
72 | for (string::const_iterator I = Data.begin(); I < Data.end(); ++I) | |
73 | { | |
74 | string::const_iterator J = I; | |
75 | for (; J != Data.end() && *J != '\n' && *J != '\r'; ++J); | |
76 | if (HeaderLine(string(I,J)) == false) | |
77 | return RUN_HEADERS_PARSE_ERROR; | |
78 | I = J; | |
79 | } | |
80 | ||
81 | // 100 Continue is a Nop... | |
82 | if (Result == 100) | |
83 | continue; | |
84 | ||
1e3f4083 | 85 | // Tidy up the connection persistence state. |
7330f4df DK |
86 | if (Encoding == Closes && HaveContent == true) |
87 | Persistent = false; | |
88 | ||
89 | return RUN_HEADERS_OK; | |
90 | } | |
91 | while (LoadNextResponse(false, File) == true); | |
92 | ||
93 | return RUN_HEADERS_IO_ERROR; | |
94 | } | |
95 | /*}}}*/ | |
96 | // ServerState::HeaderLine - Process a header line /*{{{*/ | |
97 | // --------------------------------------------------------------------- | |
98 | /* */ | |
99 | bool ServerState::HeaderLine(string Line) | |
100 | { | |
101 | if (Line.empty() == true) | |
102 | return true; | |
103 | ||
104 | string::size_type Pos = Line.find(' '); | |
105 | if (Pos == string::npos || Pos+1 > Line.length()) | |
106 | { | |
107 | // Blah, some servers use "connection:closes", evil. | |
108 | Pos = Line.find(':'); | |
109 | if (Pos == string::npos || Pos + 2 > Line.length()) | |
110 | return _error->Error(_("Bad header line")); | |
111 | Pos++; | |
112 | } | |
113 | ||
114 | // Parse off any trailing spaces between the : and the next word. | |
115 | string::size_type Pos2 = Pos; | |
74dedb4a | 116 | while (Pos2 < Line.length() && isspace_ascii(Line[Pos2]) != 0) |
7330f4df | 117 | Pos2++; |
d3e8fbb3 | 118 | |
7330f4df DK |
119 | string Tag = string(Line,0,Pos); |
120 | string Val = string(Line,Pos2); | |
d3e8fbb3 | 121 | |
7330f4df DK |
122 | if (stringcasecmp(Tag.c_str(),Tag.c_str()+4,"HTTP") == 0) |
123 | { | |
124 | // Evil servers return no version | |
125 | if (Line[4] == '/') | |
126 | { | |
127 | int const elements = sscanf(Line.c_str(),"HTTP/%3u.%3u %3u%359[^\n]",&Major,&Minor,&Result,Code); | |
128 | if (elements == 3) | |
129 | { | |
130 | Code[0] = '\0'; | |
0c2dc43d | 131 | if (Owner != NULL && Owner->Debug == true) |
b58e2c7c | 132 | clog << "HTTP server doesn't give Reason-Phrase for " << std::to_string(Result) << std::endl; |
7330f4df DK |
133 | } |
134 | else if (elements != 4) | |
135 | return _error->Error(_("The HTTP server sent an invalid reply header")); | |
136 | } | |
137 | else | |
138 | { | |
139 | Major = 0; | |
140 | Minor = 9; | |
141 | if (sscanf(Line.c_str(),"HTTP %3u%359[^\n]",&Result,Code) != 2) | |
142 | return _error->Error(_("The HTTP server sent an invalid reply header")); | |
143 | } | |
144 | ||
1e3f4083 | 145 | /* Check the HTTP response header to get the default persistence |
7330f4df DK |
146 | state. */ |
147 | if (Major < 1) | |
148 | Persistent = false; | |
149 | else | |
150 | { | |
151 | if (Major == 1 && Minor == 0) | |
b6d88f39 | 152 | { |
7330f4df | 153 | Persistent = false; |
b6d88f39 | 154 | } |
7330f4df | 155 | else |
b6d88f39 | 156 | { |
7330f4df | 157 | Persistent = true; |
b6d88f39 JAK |
158 | if (PipelineAllowed) |
159 | Pipeline = true; | |
160 | } | |
7330f4df DK |
161 | } |
162 | ||
163 | return true; | |
d3e8fbb3 DK |
164 | } |
165 | ||
7330f4df DK |
166 | if (stringcasecmp(Tag,"Content-Length:") == 0) |
167 | { | |
168 | if (Encoding == Closes) | |
169 | Encoding = Stream; | |
170 | HaveContent = true; | |
d3e8fbb3 | 171 | |
ceafe8a6 | 172 | unsigned long long * DownloadSizePtr = &DownloadSize; |
ed793a19 | 173 | if (Result == 416) |
ceafe8a6 | 174 | DownloadSizePtr = &JunkSize; |
7330f4df | 175 | |
ceafe8a6 MV |
176 | *DownloadSizePtr = strtoull(Val.c_str(), NULL, 10); |
177 | if (*DownloadSizePtr >= std::numeric_limits<unsigned long long>::max()) | |
7330f4df | 178 | return _error->Errno("HeaderLine", _("The HTTP server sent an invalid Content-Length header")); |
ceafe8a6 | 179 | else if (*DownloadSizePtr == 0) |
7330f4df | 180 | HaveContent = false; |
ceafe8a6 MV |
181 | |
182 | // On partial content (206) the Content-Length less than the real | |
183 | // size, so do not set it here but leave that to the Content-Range | |
184 | // header instead | |
6291f60e MV |
185 | if(Result != 206 && TotalFileSize == 0) |
186 | TotalFileSize = DownloadSize; | |
ceafe8a6 | 187 | |
7330f4df DK |
188 | return true; |
189 | } | |
190 | ||
191 | if (stringcasecmp(Tag,"Content-Type:") == 0) | |
192 | { | |
193 | HaveContent = true; | |
194 | return true; | |
195 | } | |
d3e8fbb3 | 196 | |
7330f4df DK |
197 | if (stringcasecmp(Tag,"Content-Range:") == 0) |
198 | { | |
199 | HaveContent = true; | |
200 | ||
201 | // §14.16 says 'byte-range-resp-spec' should be a '*' in case of 416 | |
6291f60e | 202 | if (Result == 416 && sscanf(Val.c_str(), "bytes */%llu",&TotalFileSize) == 1) |
ed793a19 | 203 | ; // we got the expected filesize which is all we wanted |
6291f60e | 204 | else if (sscanf(Val.c_str(),"bytes %llu-%*u/%llu",&StartPos,&TotalFileSize) != 2) |
7330f4df | 205 | return _error->Error(_("The HTTP server sent an invalid Content-Range header")); |
6291f60e | 206 | if ((unsigned long long)StartPos > TotalFileSize) |
7330f4df | 207 | return _error->Error(_("This HTTP server has broken range support")); |
ceafe8a6 MV |
208 | |
209 | // figure out what we will download | |
6291f60e | 210 | DownloadSize = TotalFileSize - StartPos; |
7330f4df DK |
211 | return true; |
212 | } | |
d3e8fbb3 | 213 | |
7330f4df DK |
214 | if (stringcasecmp(Tag,"Transfer-Encoding:") == 0) |
215 | { | |
216 | HaveContent = true; | |
217 | if (stringcasecmp(Val,"chunked") == 0) | |
d3e8fbb3 | 218 | Encoding = Chunked; |
7330f4df DK |
219 | return true; |
220 | } | |
221 | ||
222 | if (stringcasecmp(Tag,"Connection:") == 0) | |
223 | { | |
224 | if (stringcasecmp(Val,"close") == 0) | |
225 | Persistent = false; | |
226 | if (stringcasecmp(Val,"keep-alive") == 0) | |
227 | Persistent = true; | |
228 | return true; | |
229 | } | |
d3e8fbb3 | 230 | |
7330f4df DK |
231 | if (stringcasecmp(Tag,"Last-Modified:") == 0) |
232 | { | |
233 | if (RFC1123StrToTime(Val.c_str(), Date) == false) | |
234 | return _error->Error(_("Unknown date format")); | |
235 | return true; | |
236 | } | |
237 | ||
238 | if (stringcasecmp(Tag,"Location:") == 0) | |
239 | { | |
240 | Location = Val; | |
241 | return true; | |
242 | } | |
243 | ||
244 | return true; | |
245 | } | |
246 | /*}}}*/ | |
247 | // ServerState::ServerState - Constructor /*{{{*/ | |
2651f1c0 DK |
248 | ServerState::ServerState(URI Srv, ServerMethod *Owner) : |
249 | DownloadSize(0), ServerName(Srv), TimeOut(120), Owner(Owner) | |
7330f4df DK |
250 | { |
251 | Reset(); | |
252 | } | |
253 | /*}}}*/ | |
34faa8f7 DK |
254 | bool ServerState::AddPartialFileToHashes(FileFd &File) /*{{{*/ |
255 | { | |
256 | File.Truncate(StartPos); | |
257 | return GetHashes()->AddFD(File, StartPos); | |
258 | } | |
259 | /*}}}*/ | |
7330f4df | 260 | |
7330f4df DK |
261 | // ServerMethod::DealWithHeaders - Handle the retrieved header data /*{{{*/ |
262 | // --------------------------------------------------------------------- | |
263 | /* We look at the header data we got back from the server and decide what | |
264 | to do. Returns DealWithHeadersResult (see http.h for details). | |
265 | */ | |
266 | ServerMethod::DealWithHeadersResult | |
267 | ServerMethod::DealWithHeaders(FetchResult &Res) | |
268 | { | |
269 | // Not Modified | |
270 | if (Server->Result == 304) | |
271 | { | |
ce1f3a2c | 272 | RemoveFile("server", Queue->DestFile); |
7330f4df DK |
273 | Res.IMSHit = true; |
274 | Res.LastModified = Queue->LastModified; | |
275 | return IMS_HIT; | |
276 | } | |
dcbb364f | 277 | |
7330f4df DK |
278 | /* Redirect |
279 | * | |
280 | * Note that it is only OK for us to treat all redirection the same | |
281 | * because we *always* use GET, not other HTTP methods. There are | |
282 | * three redirection codes for which it is not appropriate that we | |
283 | * redirect. Pass on those codes so the error handling kicks in. | |
284 | */ | |
285 | if (AllowRedirect | |
286 | && (Server->Result > 300 && Server->Result < 400) | |
287 | && (Server->Result != 300 // Multiple Choices | |
288 | && Server->Result != 304 // Not Modified | |
289 | && Server->Result != 306)) // (Not part of HTTP/1.1, reserved) | |
290 | { | |
291 | if (Server->Location.empty() == true); | |
292 | else if (Server->Location[0] == '/' && Queue->Uri.empty() == false) | |
293 | { | |
294 | URI Uri = Queue->Uri; | |
295 | if (Uri.Host.empty() == false) | |
296 | NextURI = URI::SiteOnly(Uri); | |
297 | else | |
298 | NextURI.clear(); | |
299 | NextURI.append(DeQuoteString(Server->Location)); | |
300 | return TRY_AGAIN_OR_REDIRECT; | |
301 | } | |
302 | else | |
303 | { | |
9082a1fc DK |
304 | NextURI = DeQuoteString(Server->Location); |
305 | URI tmpURI = NextURI; | |
306 | URI Uri = Queue->Uri; | |
307 | // same protocol redirects are okay | |
308 | if (tmpURI.Access == Uri.Access) | |
309 | return TRY_AGAIN_OR_REDIRECT; | |
310 | // as well as http to https | |
311 | else if (Uri.Access == "http" && tmpURI.Access == "https") | |
312 | return TRY_AGAIN_OR_REDIRECT; | |
7330f4df DK |
313 | } |
314 | /* else pass through for error message */ | |
315 | } | |
316 | // retry after an invalid range response without partial data | |
317 | else if (Server->Result == 416) | |
318 | { | |
319 | struct stat SBuf; | |
320 | if (stat(Queue->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0) | |
321 | { | |
dcbb364f DK |
322 | bool partialHit = false; |
323 | if (Queue->ExpectedHashes.usable() == true) | |
324 | { | |
325 | Hashes resultHashes(Queue->ExpectedHashes); | |
326 | FileFd file(Queue->DestFile, FileFd::ReadOnly); | |
4fc6b757 | 327 | Server->TotalFileSize = file.FileSize(); |
dcbb364f DK |
328 | Server->Date = file.ModificationTime(); |
329 | resultHashes.AddFD(file); | |
330 | HashStringList const hashList = resultHashes.GetHashStringList(); | |
331 | partialHit = (Queue->ExpectedHashes == hashList); | |
332 | } | |
4fc6b757 | 333 | else if ((unsigned long long)SBuf.st_size == Server->TotalFileSize) |
dcbb364f DK |
334 | partialHit = true; |
335 | if (partialHit == true) | |
7330f4df DK |
336 | { |
337 | // the file is completely downloaded, but was not moved | |
ed793a19 DK |
338 | if (Server->HaveContent == true) |
339 | { | |
340 | // Send to error page to dev/null | |
341 | FileFd DevNull("/dev/null",FileFd::WriteExists); | |
342 | Server->RunData(&DevNull); | |
343 | } | |
344 | Server->HaveContent = false; | |
6291f60e | 345 | Server->StartPos = Server->TotalFileSize; |
7330f4df | 346 | Server->Result = 200; |
7330f4df | 347 | } |
ce1f3a2c | 348 | else if (RemoveFile("server", Queue->DestFile)) |
7330f4df DK |
349 | { |
350 | NextURI = Queue->Uri; | |
351 | return TRY_AGAIN_OR_REDIRECT; | |
352 | } | |
353 | } | |
354 | } | |
355 | ||
3a8776a3 | 356 | /* We have a reply we don't handle. This should indicate a perm server |
7330f4df DK |
357 | failure */ |
358 | if (Server->Result < 200 || Server->Result >= 300) | |
359 | { | |
84361def DK |
360 | std::string err; |
361 | strprintf(err, "HttpError%u", Server->Result); | |
7330f4df | 362 | SetFailReason(err); |
84361def | 363 | _error->Error("%u %s", Server->Result, Server->Code); |
7330f4df DK |
364 | if (Server->HaveContent == true) |
365 | return ERROR_WITH_CONTENT_PAGE; | |
366 | return ERROR_UNRECOVERABLE; | |
367 | } | |
368 | ||
369 | // This is some sort of 2xx 'data follows' reply | |
370 | Res.LastModified = Server->Date; | |
6291f60e | 371 | Res.Size = Server->TotalFileSize; |
7330f4df DK |
372 | |
373 | // Open the file | |
374 | delete File; | |
375 | File = new FileFd(Queue->DestFile,FileFd::WriteAny); | |
376 | if (_error->PendingError() == true) | |
377 | return ERROR_NOT_FROM_SERVER; | |
378 | ||
379 | FailFile = Queue->DestFile; | |
3a8776a3 | 380 | FailFile.c_str(); // Make sure we don't do a malloc in the signal handler |
7330f4df DK |
381 | FailFd = File->Fd(); |
382 | FailTime = Server->Date; | |
383 | ||
34faa8f7 | 384 | if (Server->InitHashes(Queue->ExpectedHashes) == false || Server->AddPartialFileToHashes(*File) == false) |
7330f4df DK |
385 | { |
386 | _error->Errno("read",_("Problem hashing file")); | |
387 | return ERROR_NOT_FROM_SERVER; | |
388 | } | |
389 | if (Server->StartPos > 0) | |
390 | Res.ResumePoint = Server->StartPos; | |
391 | ||
392 | SetNonBlock(File->Fd(),true); | |
393 | return FILE_IS_OPEN; | |
394 | } | |
395 | /*}}}*/ | |
396 | // ServerMethod::SigTerm - Handle a fatal signal /*{{{*/ | |
397 | // --------------------------------------------------------------------- | |
1e3f4083 | 398 | /* This closes and timestamps the open file. This is necessary to get |
7330f4df DK |
399 | resume behavoir on user abort */ |
400 | void ServerMethod::SigTerm(int) | |
401 | { | |
402 | if (FailFd == -1) | |
403 | _exit(100); | |
9ce3cfc9 | 404 | |
246bbb61 | 405 | struct timeval times[2]; |
9ce3cfc9 DK |
406 | times[0].tv_sec = FailTime; |
407 | times[1].tv_sec = FailTime; | |
246bbb61 DK |
408 | times[0].tv_usec = times[1].tv_usec = 0; |
409 | utimes(FailFile.c_str(), times); | |
7330f4df | 410 | close(FailFd); |
9ce3cfc9 | 411 | |
7330f4df DK |
412 | _exit(100); |
413 | } | |
414 | /*}}}*/ | |
415 | // ServerMethod::Fetch - Fetch an item /*{{{*/ | |
416 | // --------------------------------------------------------------------- | |
417 | /* This adds an item to the pipeline. We keep the pipeline at a fixed | |
418 | depth. */ | |
419 | bool ServerMethod::Fetch(FetchItem *) | |
420 | { | |
742f67ea | 421 | if (Server == nullptr || QueueBack == nullptr) |
7330f4df DK |
422 | return true; |
423 | ||
742f67ea DK |
424 | // If pipelining is disabled, we only queue 1 request |
425 | auto const AllowedDepth = Server->Pipeline ? PipelineDepth : 0; | |
426 | // how deep is our pipeline currently? | |
427 | decltype(PipelineDepth) CurrentDepth = 0; | |
428 | for (FetchItem const *I = Queue; I != QueueBack; I = I->Next) | |
429 | ++CurrentDepth; | |
430 | ||
431 | do { | |
7330f4df | 432 | // Make sure we stick with the same server |
742f67ea DK |
433 | if (Server->Comp(QueueBack->Uri) == false) |
434 | break; | |
435 | ||
436 | bool const UsableHashes = QueueBack->ExpectedHashes.usable(); | |
437 | // if we have no hashes, do at most one such request | |
438 | // as we can't fixup pipeling misbehaviors otherwise | |
439 | if (CurrentDepth != 0 && UsableHashes == false) | |
7330f4df | 440 | break; |
742f67ea DK |
441 | |
442 | if (UsableHashes && FileExists(QueueBack->DestFile)) | |
7330f4df | 443 | { |
742f67ea DK |
444 | FileFd partial(QueueBack->DestFile, FileFd::ReadOnly); |
445 | Hashes wehave(QueueBack->ExpectedHashes); | |
446 | if (QueueBack->ExpectedHashes.FileSize() == partial.FileSize()) | |
447 | { | |
448 | if (wehave.AddFD(partial) && | |
449 | wehave.GetHashStringList() == QueueBack->ExpectedHashes) | |
450 | { | |
451 | FetchResult Res; | |
452 | Res.Filename = QueueBack->DestFile; | |
453 | Res.ResumePoint = QueueBack->ExpectedHashes.FileSize(); | |
454 | URIStart(Res); | |
455 | // move item to the start of the queue as URIDone will | |
456 | // always dequeued the first item in the queue | |
457 | if (Queue != QueueBack) | |
458 | { | |
459 | FetchItem *Prev = Queue; | |
460 | for (; Prev->Next != QueueBack; Prev = Prev->Next) | |
461 | /* look for the previous queue item */; | |
462 | Prev->Next = QueueBack->Next; | |
463 | QueueBack->Next = Queue; | |
464 | Queue = QueueBack; | |
465 | QueueBack = Prev->Next; | |
466 | } | |
467 | Res.TakeHashes(wehave); | |
468 | URIDone(Res); | |
469 | continue; | |
470 | } | |
471 | else | |
472 | RemoveFile("Fetch-Partial", QueueBack->DestFile); | |
473 | } | |
7330f4df | 474 | } |
742f67ea DK |
475 | auto const Tmp = QueueBack; |
476 | QueueBack = QueueBack->Next; | |
477 | SendReq(Tmp); | |
478 | ++CurrentDepth; | |
479 | } while (CurrentDepth <= AllowedDepth && QueueBack != nullptr); | |
480 | ||
7330f4df | 481 | return true; |
d3e8fbb3 | 482 | } |
7330f4df DK |
483 | /*}}}*/ |
484 | // ServerMethod::Loop - Main loop /*{{{*/ | |
485 | int ServerMethod::Loop() | |
486 | { | |
487 | typedef vector<string> StringVector; | |
488 | typedef vector<string>::iterator StringVectorIterator; | |
489 | map<string, StringVector> Redirected; | |
490 | ||
491 | signal(SIGTERM,SigTerm); | |
492 | signal(SIGINT,SigTerm); | |
493 | ||
494 | Server = 0; | |
495 | ||
496 | int FailCounter = 0; | |
497 | while (1) | |
498 | { | |
499 | // We have no commands, wait for some to arrive | |
500 | if (Queue == 0) | |
501 | { | |
502 | if (WaitFd(STDIN_FILENO) == false) | |
503 | return 0; | |
504 | } | |
505 | ||
506 | /* Run messages, we can accept 0 (no message) if we didn't | |
507 | do a WaitFd above.. Otherwise the FD is closed. */ | |
508 | int Result = Run(true); | |
509 | if (Result != -1 && (Result != 0 || Queue == 0)) | |
510 | { | |
511 | if(FailReason.empty() == false || | |
512 | _config->FindB("Acquire::http::DependOnSTDIN", true) == true) | |
513 | return 100; | |
514 | else | |
515 | return 0; | |
516 | } | |
517 | ||
518 | if (Queue == 0) | |
519 | continue; | |
520 | ||
521 | // Connect to the server | |
522 | if (Server == 0 || Server->Comp(Queue->Uri) == false) | |
7330f4df | 523 | Server = CreateServerState(Queue->Uri); |
830a1b8c | 524 | |
7330f4df DK |
525 | /* If the server has explicitly said this is the last connection |
526 | then we pre-emptively shut down the pipeline and tear down | |
527 | the connection. This will speed up HTTP/1.0 servers a tad | |
528 | since we don't have to wait for the close sequence to | |
529 | complete */ | |
530 | if (Server->Persistent == false) | |
531 | Server->Close(); | |
532 | ||
533 | // Reset the pipeline | |
534 | if (Server->IsOpen() == false) | |
535 | QueueBack = Queue; | |
536 | ||
537 | // Connnect to the host | |
538 | if (Server->Open() == false) | |
539 | { | |
540 | Fail(true); | |
830a1b8c | 541 | Server = nullptr; |
7330f4df DK |
542 | continue; |
543 | } | |
544 | ||
545 | // Fill the pipeline. | |
546 | Fetch(0); | |
547 | ||
548 | // Fetch the next URL header data from the server. | |
9622b211 | 549 | switch (Server->RunHeaders(File, Queue->Uri)) |
7330f4df DK |
550 | { |
551 | case ServerState::RUN_HEADERS_OK: | |
552 | break; | |
553 | ||
554 | // The header data is bad | |
555 | case ServerState::RUN_HEADERS_PARSE_ERROR: | |
556 | { | |
557 | _error->Error(_("Bad header data")); | |
558 | Fail(true); | |
cc0a4c82 | 559 | Server->Close(); |
7330f4df DK |
560 | RotateDNS(); |
561 | continue; | |
562 | } | |
563 | ||
564 | // The server closed a connection during the header get.. | |
565 | default: | |
566 | case ServerState::RUN_HEADERS_IO_ERROR: | |
567 | { | |
568 | FailCounter++; | |
569 | _error->Discard(); | |
570 | Server->Close(); | |
571 | Server->Pipeline = false; | |
b6d88f39 | 572 | Server->PipelineAllowed = false; |
7330f4df DK |
573 | |
574 | if (FailCounter >= 2) | |
575 | { | |
576 | Fail(_("Connection failed"),true); | |
577 | FailCounter = 0; | |
578 | } | |
579 | ||
580 | RotateDNS(); | |
581 | continue; | |
582 | } | |
583 | }; | |
584 | ||
585 | // Decide what to do. | |
586 | FetchResult Res; | |
587 | Res.Filename = Queue->DestFile; | |
588 | switch (DealWithHeaders(Res)) | |
589 | { | |
590 | // Ok, the file is Open | |
591 | case FILE_IS_OPEN: | |
592 | { | |
593 | URIStart(Res); | |
594 | ||
595 | // Run the data | |
596 | bool Result = true; | |
dcd5856b MV |
597 | |
598 | // ensure we don't fetch too much | |
f2b47ba2 MV |
599 | // we could do "Server->MaximumSize = Queue->MaximumSize" here |
600 | // but that would break the clever pipeline messup detection | |
601 | // so instead we use the size of the biggest item in the queue | |
602 | Server->MaximumSize = FindMaximumObjectSizeInQueue(); | |
dcd5856b | 603 | |
7330f4df DK |
604 | if (Server->HaveContent) |
605 | Result = Server->RunData(File); | |
606 | ||
607 | /* If the server is sending back sizeless responses then fill in | |
608 | the size now */ | |
609 | if (Res.Size == 0) | |
610 | Res.Size = File->Size(); | |
611 | ||
612 | // Close the file, destroy the FD object and timestamp it | |
613 | FailFd = -1; | |
614 | delete File; | |
615 | File = 0; | |
616 | ||
617 | // Timestamp | |
246bbb61 | 618 | struct timeval times[2]; |
9ce3cfc9 | 619 | times[0].tv_sec = times[1].tv_sec = Server->Date; |
246bbb61 DK |
620 | times[0].tv_usec = times[1].tv_usec = 0; |
621 | utimes(Queue->DestFile.c_str(), times); | |
7330f4df DK |
622 | |
623 | // Send status to APT | |
624 | if (Result == true) | |
625 | { | |
895417ef DK |
626 | Hashes * const resultHashes = Server->GetHashes(); |
627 | HashStringList const hashList = resultHashes->GetHashStringList(); | |
628 | if (PipelineDepth != 0 && Queue->ExpectedHashes.usable() == true && Queue->ExpectedHashes != hashList) | |
629 | { | |
630 | // we did not get the expected hash… mhhh: | |
631 | // could it be that server/proxy messed up pipelining? | |
632 | FetchItem * BeforeI = Queue; | |
633 | for (FetchItem *I = Queue->Next; I != 0 && I != QueueBack; I = I->Next) | |
634 | { | |
635 | if (I->ExpectedHashes.usable() == true && I->ExpectedHashes == hashList) | |
636 | { | |
637 | // yes, he did! Disable pipelining and rewrite queue | |
638 | if (Server->Pipeline == true) | |
639 | { | |
b9c20219 | 640 | Warning(_("Automatically disabled %s due to incorrect response from server/proxy. (man 5 apt.conf)"), "Acquire::http::Pipeline-Depth"); |
895417ef | 641 | Server->Pipeline = false; |
b6d88f39 | 642 | Server->PipelineAllowed = false; |
895417ef DK |
643 | // we keep the PipelineDepth value so that the rest of the queue can be fixed up as well |
644 | } | |
645 | Rename(Res.Filename, I->DestFile); | |
646 | Res.Filename = I->DestFile; | |
647 | BeforeI->Next = I->Next; | |
648 | I->Next = Queue; | |
649 | Queue = I; | |
650 | break; | |
651 | } | |
652 | BeforeI = I; | |
653 | } | |
654 | } | |
655 | Res.TakeHashes(*resultHashes); | |
7330f4df DK |
656 | URIDone(Res); |
657 | } | |
658 | else | |
659 | { | |
660 | if (Server->IsOpen() == false) | |
661 | { | |
662 | FailCounter++; | |
663 | _error->Discard(); | |
664 | Server->Close(); | |
665 | ||
666 | if (FailCounter >= 2) | |
667 | { | |
668 | Fail(_("Connection failed"),true); | |
669 | FailCounter = 0; | |
670 | } | |
671 | ||
672 | QueueBack = Queue; | |
673 | } | |
674 | else | |
a2d40703 MV |
675 | { |
676 | Server->Close(); | |
7330f4df | 677 | Fail(true); |
a2d40703 | 678 | } |
7330f4df DK |
679 | } |
680 | break; | |
681 | } | |
682 | ||
683 | // IMS hit | |
684 | case IMS_HIT: | |
685 | { | |
686 | URIDone(Res); | |
687 | break; | |
688 | } | |
689 | ||
690 | // Hard server error, not found or something | |
691 | case ERROR_UNRECOVERABLE: | |
692 | { | |
693 | Fail(); | |
694 | break; | |
695 | } | |
696 | ||
697 | // Hard internal error, kill the connection and fail | |
698 | case ERROR_NOT_FROM_SERVER: | |
699 | { | |
700 | delete File; | |
701 | File = 0; | |
702 | ||
703 | Fail(); | |
704 | RotateDNS(); | |
705 | Server->Close(); | |
706 | break; | |
707 | } | |
708 | ||
709 | // We need to flush the data, the header is like a 404 w/ error text | |
710 | case ERROR_WITH_CONTENT_PAGE: | |
711 | { | |
712 | Fail(); | |
713 | ||
714 | // Send to content to dev/null | |
715 | File = new FileFd("/dev/null",FileFd::WriteExists); | |
716 | Server->RunData(File); | |
717 | delete File; | |
718 | File = 0; | |
719 | break; | |
720 | } | |
721 | ||
722 | // Try again with a new URL | |
723 | case TRY_AGAIN_OR_REDIRECT: | |
724 | { | |
725 | // Clear rest of response if there is content | |
726 | if (Server->HaveContent) | |
727 | { | |
728 | File = new FileFd("/dev/null",FileFd::WriteExists); | |
729 | Server->RunData(File); | |
730 | delete File; | |
731 | File = 0; | |
732 | } | |
733 | ||
734 | /* Detect redirect loops. No more redirects are allowed | |
735 | after the same URI is seen twice in a queue item. */ | |
736 | StringVector &R = Redirected[Queue->DestFile]; | |
737 | bool StopRedirects = false; | |
738 | if (R.empty() == true) | |
739 | R.push_back(Queue->Uri); | |
740 | else if (R[0] == "STOP" || R.size() > 10) | |
741 | StopRedirects = true; | |
742 | else | |
743 | { | |
744 | for (StringVectorIterator I = R.begin(); I != R.end(); ++I) | |
745 | if (Queue->Uri == *I) | |
746 | { | |
747 | R[0] = "STOP"; | |
748 | break; | |
749 | } | |
750 | ||
751 | R.push_back(Queue->Uri); | |
752 | } | |
753 | ||
754 | if (StopRedirects == false) | |
755 | Redirect(NextURI); | |
756 | else | |
757 | Fail(); | |
758 | ||
759 | break; | |
760 | } | |
761 | ||
762 | default: | |
763 | Fail(_("Internal error")); | |
764 | break; | |
765 | } | |
766 | ||
767 | FailCounter = 0; | |
768 | } | |
769 | ||
770 | return 0; | |
f2b47ba2 MV |
771 | } |
772 | /*}}}*/ | |
830a1b8c | 773 | unsigned long long ServerMethod::FindMaximumObjectSizeInQueue() const /*{{{*/ |
f2b47ba2 MV |
774 | { |
775 | unsigned long long MaxSizeInQueue = 0; | |
8bb043fc | 776 | for (FetchItem *I = Queue; I != 0 && I != QueueBack; I = I->Next) |
f2b47ba2 MV |
777 | MaxSizeInQueue = std::max(MaxSizeInQueue, I->MaximumSize); |
778 | return MaxSizeInQueue; | |
7330f4df DK |
779 | } |
780 | /*}}}*/ | |
23e64f6d DK |
781 | ServerMethod::ServerMethod(char const * const Binary, char const * const Ver,unsigned long const Flags) :/*{{{*/ |
782 | aptMethod(Binary, Ver, Flags), Server(nullptr), File(NULL), PipelineDepth(10), | |
830a1b8c DK |
783 | AllowRedirect(false), Debug(false) |
784 | { | |
785 | } | |
786 | /*}}}*/ |