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